Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,23 @@ schedule:
```

## 完整参数说明
| Configuration | Instruction | Default | Required |
|-----------------|---------------------|---------|----------|
| id | 任务的唯一标识 | | Yes |
| url | 需要轮询的数据文件地址 | | Yes |
| options | fetch的options | | No |
| every | 每隔几分钟执行一次,默认每次执行 | 1 | No |
| random | 执行时间是否加入随机Delay | No | No |
| enable | 是否启用 | Yes | No |
| record | 是否保存 | Yes | No |
| filters | 文件差异比较时,需要忽略的属性 | | No |
| condition | 文件差异比较时,需满足的额外条件 | | No |
| format | 保存时是否格式化 | No | No |
| notify | 是否启用通知 | No | No |
| notifyCondition | 通知的额外条件 | | No |
| errorCondition | 异常条件,满足时会记录以及发送消息通知 | | No |
| Configuration | Instruction | Default | Required |
|-----------------|---------------------------|---------|----------|
| id | 任务的唯一标识 | | Yes |
| url | 需要轮询的数据文件地址 | | Yes |
| options | fetch的options | | No |
| every | 每隔几分钟执行一次,默认每次执行 | 1 | No |
| random | 执行时间(秒)是否加入随机Delay | False | No |
| randomMin | 是否在随机分钟时执行,需配合perDay使用 | | No |
| perDay | 每天随机运行的总次数,需配合randomMin使用 | | No |
| enable | 是否启用 | True | No |
| record | 是否保存 | True | No |
| filters | 文件差异比较时,需要忽略的属性 | | No |
| condition | 文件差异比较时,需满足的额外条件 | | No |
| format | 保存时是否格式化 | False | No |
| notify | 是否启用通知 | False | No |
| notifyCondition | 通知的额外条件 | | No |
| errorCondition | 异常条件,满足时会记录以及发送消息通知 | | No |

## Actions参数说明
| Repository secrets | Instruction | Required |
Expand Down
34 changes: 18 additions & 16 deletions README_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,24 @@ For example, Linux's cron:
}
```

## Full Parameter Description
| Configuration | Instruction | Default | Required |
|-----------------|----------------------------------------------------------|---------|----------|
| id | Unique identifier for the task | | Yes |
| url | URL of the data file to poll | | Yes |
| options | Fetch options | | No |
| every | Execution interval in minutes (default: every execution) | 1 | No |
| random | Whether to add random delay to execution time | No | No |
| enable | Whether to enable the task | Yes | No |
| record | Whether to save the data | Yes | No |
| filters | Properties to ignore during file comparison | | No |
| condition | Additional conditions for file comparison | | No |
| format | Whether to format the saved data | No | No |
| notify | Whether to enable notifications | No | No |
| notifyCondition | Additional conditions for notifications | | No |
| errorCondition | Error conditions that trigger logging and notifications | | No |
# Complete Parameter Description
| Configuration | Instruction | Default | Required |
|-----------------|----------------------------------------------------------------|---------|----------|
| id | Unique identifier for the task | | Yes |
| url | URL of the data file to poll | | Yes |
| options | Fetch options | | No |
| every | Execution interval in minutes (default: every execution) | 1 | No |
| random | Whether to add random delay (in seconds) to execution time | False | No |
| randomMin | Whether to execute at random minutes (requires perDay) | | No |
| perDay | Total number of random executions per day (requires randomMin) | | No |
| enable | Whether to enable the task | True | No |
| record | Whether to save the data | True | No |
| filters | Properties to ignore during file comparison | | No |
| condition | Additional conditions for file comparison | | No |
| format | Whether to format the saved data | False | No |
| notify | Whether to enable notifications | False | No |
| notifyCondition | Additional conditions for notifications | | No |
| errorCondition | Error conditions that trigger logging and notifications | | No |

## Actions Parameter Description
| Repository Secrets | Instruction | Required |
Expand Down
7 changes: 7 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
"id": "simple_get",
"url": "https://www.baidu.com"
},
{
"id": "10_times_daily_in_random_time",
"url": "https://dummyjson.com/test",
"random": true,
"randomMin": true,
"perDay": 10
},
{
"id": "simple_get",
"url": "https://dummyjson.com/test",
Expand Down
9 changes: 5 additions & 4 deletions detect.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'fs';
import os from 'os';
import {areJsonEqual, executeWithDelay, getCurrentDateTimeStringPath, readDataFile, writeDataFile} from "./utils.js";
import {areJsonEqual, executeWithDelay, generateDailyMinutes, getCurrentDateTimeStringPath, readDataFile, writeDataFile} from "./utils.js";

const args = process.argv.slice(2);
const DATA_ROOT = args.length > 0 ? (!args[0].endsWith('/') ? args[0] + '/' : args[0]) : './';
Expand All @@ -11,10 +11,11 @@ const data = [];

const main = async (isRandom) => {
console.log(`main(random:${!!isRandom}) start`);
const minutes = new Date().getHours() * 60 + new Date().getMinutes();
const date = new Date();
const minutesOfDay = date.getHours() * 60 + date.getMinutes();
const configs = readDataFile(`${DATA_ROOT}config.json`);
for (const {id, url, options, format, filters, condition, notify, notifyCondition, errorCondition, every = 1, random, record = true, enable} of configs) {
if (enable === false || minutes % every !== 0 || !!isRandom !== !!random) {
for (const {id, url, options, format, filters, condition, notify, notifyCondition, errorCondition, every, random, record = true, randomMin, perDay, enable} of configs) {
if (enable === false || !!isRandom !== !!random || (every && minutesOfDay % every !== 0) || (randomMin && perDay && !generateDailyMinutes(date, perDay).includes(minutesOfDay))) {
console.log(`monitor id: ${id} skipped`);
continue;
}
Expand Down
6 changes: 5 additions & 1 deletion notify.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ async function main() {
method: 'POST',
body: Object.entries(data).map(([key, value]) => `${key}=${value}`).join('&')
};
await fetch(`${notifyServer}/webapi/entry.cgi?api=SYNO.Chat.External&method=incoming&version=2`, options);
try {
await fetch(`${notifyServer}/webapi/entry.cgi?api=SYNO.Chat.External&method=incoming&version=2`, options);
} catch (error) {
console.error('Fetch request failed:', error.message);
}
} else {
if (!notifyServer) console.log('notify_server required');
if (!notifyToken) console.log('notify_token required');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "monitor",
"version": "1.3.0",
"version": "1.4.0",
"description": "Monitor data file and record changed versions",
"type": "module",
"scripts": {
Expand Down
44 changes: 44 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,47 @@ export const executeWithDelay = (fn, ...args) => {
}, delay);
});
}

export const generateDailyMinutes = (date, count = 10) => {
// 参数验证和规范化
if (count < 1) count = 1;
if (count > 1440) count = 1440;

// 将日期转换为一致的字符串格式(YYYY-MM-DD)
const dateStr = typeof date === 'string' ? date : date.toISOString().slice(0, 10);
const normalizedDate = new Date(dateStr + 'T00:00:00Z'); // 使用UTC时间确保一致性

// 创建确定性的种子(使用日期字符串的哈希值)
let seed = 0;
for (let i = 0; i < dateStr.length; i++) {
seed = ((seed << 5) - seed) + dateStr.charCodeAt(i);
seed = seed & seed; // 转换为32位整数
}
seed = Math.abs(seed);

// 使用确定性随机数生成器
const minutes = new Set();
let attempts = 0;
const maxAttempts = count * 10; // 防止无限循环

while (minutes.size < count && attempts < maxAttempts) {
// 简单的LCG算法,确保确定性
seed = (seed * 1664525 + 1013904223) % 2147483647;

// 生成0-1439之间的分钟数
const minute = Math.abs(seed) % 1440;
minutes.add(minute);

attempts++;
}

// 如果因为重复而无法生成足够数量,补充剩余的数字
if (minutes.size < count) {
for (let i = 0; i < 1440 && minutes.size < count; i++) {
minutes.add(i);
}
}

// 转换为数组并排序
return Array.from(minutes).sort((a, b) => a - b).slice(0, count);
}