Skip to content

Commit

Permalink
feat: add option to run speedtest at cron interval
Browse files Browse the repository at this point in the history
fixes #889
  • Loading branch information
MauriceNino committed Jan 19, 2024
1 parent e555519 commit d05c39c
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 7 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ npm-debug.log
yarn-error.log
testem.log
/typings
/speedtest_result

# System Files
.DS_Store
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
10 changes: 10 additions & 0 deletions apps/docs/docs/config/widget-specific/network.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ At which interval the network speed-test should be rerun (in minutes).
- type: `number`
- default: `240` (every 4 hours)

## `DASHDOT_SPEED_TEST_INTERVAL_CRON`

At which interval the network speed-test should be rerun, passed as a cron string.
This setting overrides `DASHDOT_SPEED_TEST_INTERVAL` if set.

Example: `0 0 * * *` (every day at midnight)

- type: `string`
- default: `unset`

## `DASHDOT_NETWORK_WIDGET_GROW`

To adjust the relative size of the Network widget.
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export const CONFIG: Config = {
ram_poll_interval: numNull(penv('RAM_POLL_INTERVAL')) ?? 1000,

speed_test_interval: numNull(penv('SPEED_TEST_INTERVAL')) ?? 60 * 4,
speed_test_interval_cron: penv('SPEED_TEST_INTERVAL_CRON'),
network_widget_grow: numNull(penv('NETWORK_WIDGET_GROW')) ?? 6,
network_widget_min_width: numNull(penv('NETWORK_WIDGET_MIN_WIDTH')) ?? 500,
network_shown_datapoints: numNull(penv('NETWORK_SHOWN_DATAPOINTS')) ?? 20,
Expand Down
23 changes: 18 additions & 5 deletions apps/server/src/dynamic-info.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { interval, mergeMap, Observable, ReplaySubject } from 'rxjs';
import cron from 'node-cron';
import { interval, mergeMap, Observable, ReplaySubject, Subject } from 'rxjs';
import { inspect } from 'util';
import { CONFIG } from './config';
import getCpuInfo from './data/cpu';
Expand Down Expand Up @@ -84,13 +85,25 @@ export const getDynamicServerInfo = () => {
getGpuInfo.dynamic
);

const speedTestObs = CONFIG.widget_list.includes('network')
? interval(CONFIG.speed_test_interval * 60 * 1000).pipe(
let speedTestObs = new Observable();

if (CONFIG.widget_list.includes('network')) {
if (CONFIG.speed_test_interval_cron) {
const subject = new Subject();

cron.schedule(CONFIG.speed_test_interval_cron, async () => {
subject.next(await loadInfo('network', getNetworkInfo.speedTest, true));
});

speedTestObs = subject.asObservable();
} else {
speedTestObs = interval(CONFIG.speed_test_interval * 60 * 1000).pipe(
mergeMap(
async () => await loadInfo('network', getNetworkInfo.speedTest, true)
)
)
: new Observable();
);
}
}

return {
cpu: cpuObs,
Expand Down
17 changes: 17 additions & 0 deletions apps/server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import compression from 'compression';
import cors from 'cors';
import cronParser from 'cronstrue';
import express from 'express';
import { readFileSync } from 'fs';
import http from 'http';
import cron from 'node-cron';
import path from 'path';
import {
debounceTime,
Expand Down Expand Up @@ -181,6 +183,21 @@ server.listen(CONFIG.port, async () => {
if (CONFIG.widget_list.includes('network')) {
try {
console.log('Running speed-test (this may take a few minutes)...');

if (CONFIG.speed_test_interval_cron) {
if (cron.validate(CONFIG.speed_test_interval_cron)) {
console.log(
`Speed-test interval cron expression: ${
CONFIG.speed_test_interval_cron
} (${cronParser.toString(CONFIG.speed_test_interval_cron)})`
);
} else {
console.warn(
`Invalid cron expression: ${CONFIG.speed_test_interval_cron}`
);
}
}

await loadInfo('network', () => getNetworkInfo.speedTest(true), true);
} catch (e) {
console.warn(e);
Expand Down
1 change: 1 addition & 0 deletions libs/common/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export type Config = {

// Network Widget
speed_test_interval: number;
speed_test_interval_cron?: string;
network_widget_grow: number;
network_widget_min_width: number;
network_shown_datapoints: number;
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@
"compression": "1.7.4",
"core-js": "3.31.0",
"cors": "2.8.5",
"cronstrue": "2.47.0",
"docusaurus-plugin-image-zoom": "1.0.1",
"docusaurus-plugin-sass": "0.2.5",
"express": "4.18.2",
"framer-motion": "10.16.16",
"node-cron": "3.0.3",
"parse-domain": "7.0.1",
"prism-react-renderer": "^1",
"punycode": "2.3.1",
Expand Down Expand Up @@ -112,6 +114,7 @@
"@types/express": "4.17.21",
"@types/jest": "29.5.4",
"@types/node": "20.10.6",
"@types/node-cron": "3.0.11",
"@types/qs": "6.9.11",
"@types/react": "18.2.47",
"@types/react-color": "3.0.11",
Expand Down
5 changes: 5 additions & 0 deletions speedtest_result
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"unit": "byte",
"speedDown": 150000000,
"speedUp": 50000000
}
30 changes: 29 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9782,6 +9782,13 @@ __metadata:
languageName: node
linkType: hard

"@types/node-cron@npm:3.0.11":
version: 3.0.11
resolution: "@types/node-cron@npm:3.0.11"
checksum: a73f69bcca52a5f3b1671cfb00a8e4a1d150d0aef36a611564a2f94e66b6981bade577e267ceeeca6fcee241768902d55eb8cf3a81f9ef4ed767a23112fdb16d
languageName: node
linkType: hard

"@types/node@npm:*, @types/node@npm:>=10.0.0, @types/node@npm:>=12":
version: 17.0.38
resolution: "@types/node@npm:17.0.38"
Expand Down Expand Up @@ -13584,6 +13591,15 @@ __metadata:
languageName: node
linkType: hard

"cronstrue@npm:2.47.0":
version: 2.47.0
resolution: "cronstrue@npm:2.47.0"
bin:
cronstrue: bin/cli.js
checksum: a6752c8873fec30cabab4c5b92abde3a625cb5ef1797d9360f762b9e0bb9c0607a09744340faccdbf5b97eaeb5df0ebe9b5c424043fdba9b39799c82404076ec
languageName: node
linkType: hard

"cross-fetch@npm:^3.1.5":
version: 3.1.5
resolution: "cross-fetch@npm:3.1.5"
Expand Down Expand Up @@ -14202,6 +14218,7 @@ __metadata:
"@types/express": 4.17.21
"@types/jest": 29.5.4
"@types/node": 20.10.6
"@types/node-cron": 3.0.11
"@types/qs": 6.9.11
"@types/react": 18.2.47
"@types/react-color": 3.0.11
Expand All @@ -14227,6 +14244,7 @@ __metadata:
compression: 1.7.4
core-js: 3.31.0
cors: 2.8.5
cronstrue: 2.47.0
docusaurus-plugin-image-zoom: 1.0.1
docusaurus-plugin-sass: 0.2.5
dotenv: 16.3.1
Expand All @@ -14245,6 +14263,7 @@ __metadata:
jest: 29.5.0
jest-environment-node: 29.5.0
jsdom: 22.1.0
node-cron: 3.0.3
nx: 17.2.8
parse-domain: 7.0.1
prettier: 2.8.8
Expand Down Expand Up @@ -21420,6 +21439,15 @@ __metadata:
languageName: node
linkType: hard

"node-cron@npm:3.0.3":
version: 3.0.3
resolution: "node-cron@npm:3.0.3"
dependencies:
uuid: 8.3.2
checksum: 351c37491ebf717d0ae69cc941465de118e5c2ef5d48bc3f87c98556241b060f100402c8a618c7b86f9f626b44756b20d8b5385b70e52f80716f21e55db0f1c5
languageName: node
linkType: hard

"node-domexception@npm:^1.0.0":
version: 1.0.0
resolution: "node-domexception@npm:1.0.0"
Expand Down Expand Up @@ -29180,7 +29208,7 @@ __metadata:
languageName: node
linkType: hard

"uuid@npm:^8.3.2":
"uuid@npm:8.3.2, uuid@npm:^8.3.2":
version: 8.3.2
resolution: "uuid@npm:8.3.2"
bin:
Expand Down

0 comments on commit d05c39c

Please sign in to comment.