mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-20 21:23:08 +03:00
Merge branch 'fix/crontab-enh' into chore/all-my-stuffs
# Conflicts: # package.json # pnpm-lock.yaml
This commit is contained in:
commit
501678202b
7 files changed
+285
-10
No files matched your search
@@ -117,6 +117,9 @@
|
||||
"convert": "^5.4.1",
|
||||
"country-code-lookup": "^0.1.0",
|
||||
"crc": "^4.3.2",
|
||||
"countries-and-timezones": "^3.6.0",
|
||||
"country-code-lookup": "^0.1.0",
|
||||
"cron-parser": "^4.9.0",
|
||||
"cron-validator": "^1.3.1",
|
||||
"cronstrue": "^2.26.0",
|
||||
"crypto-js": "^4.1.1",
|
||||
@@ -138,6 +141,7 @@
|
||||
"emojilib": "^3.0.10",
|
||||
"fast_array_intersect": "^1.1.0",
|
||||
"exif-be-gone": "^1.5.1",
|
||||
"event-cron-parser": "^1.0.34",
|
||||
"figlet": "^1.7.0",
|
||||
"figue": "^1.2.0",
|
||||
"flatten-anything": "^4.0.1",
|
||||
|
||||
Generated
+25
@@ -197,6 +197,9 @@ dependencies:
|
||||
crc:
|
||||
specifier: ^4.3.2
|
||||
version: 4.3.2
|
||||
cron-parser:
|
||||
specifier: ^4.9.0
|
||||
version: 4.9.0
|
||||
cron-validator:
|
||||
specifier: ^1.3.1
|
||||
version: 1.3.1
|
||||
@@ -251,6 +254,9 @@ dependencies:
|
||||
emojilib:
|
||||
specifier: ^3.0.10
|
||||
version: 3.0.10
|
||||
event-cron-parser:
|
||||
specifier: ^1.0.34
|
||||
version: 1.0.34
|
||||
exif-be-gone:
|
||||
specifier: ^1.5.1
|
||||
version: 1.5.1
|
||||
@@ -7621,6 +7627,13 @@ packages:
|
||||
'@egjs/list-differ': 1.0.1
|
||||
dev: false
|
||||
|
||||
/cron-parser@4.9.0:
|
||||
resolution: {integrity: sha512-p0SaNjrHOnQeR8/VnfGbmg9te2kfyYSQ7Sc/j/6DtPL3JQvKxmjO9TSjNFpujqV3vEYYBvNNvXSxzyksBWAx1Q==}
|
||||
engines: {node: '>=12.0.0'}
|
||||
dependencies:
|
||||
luxon: 3.5.0
|
||||
dev: false
|
||||
|
||||
/cron-validator@1.3.1:
|
||||
resolution: {integrity: sha512-C1HsxuPCY/5opR55G5/WNzyEGDWFVG+6GLrA+fW/sCTcP6A6NTjUP2AK7B8n2PyFs90kDG2qzwm8LMheADku6A==}
|
||||
dev: false
|
||||
@@ -8872,6 +8885,12 @@ packages:
|
||||
resolution: {integrity: sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==}
|
||||
engines: {node: '>=0.10.0'}
|
||||
|
||||
/event-cron-parser@1.0.34:
|
||||
resolution: {integrity: sha512-ytqZmMrNfSvzHWriiHdoNOpYKFr4d2fDoC4Rgq0F8lEA37abCWkYhSsqslC/kngWwnTGq7L0Q9VlMreKe6EJbQ==}
|
||||
dependencies:
|
||||
number-to-words: 1.2.4
|
||||
dev: false
|
||||
|
||||
/event-stream@3.3.4:
|
||||
resolution: {integrity: sha512-QHpkERcGsR0T7Qm3HNJSyXKEEj8AHNxkY3PK8TS2KJvQ7NiSHe3DDpwVKKtoYprL/AreyzFBeIkBIWChAqn60g==}
|
||||
dependencies:
|
||||
@@ -10658,6 +10677,7 @@ packages:
|
||||
/jsesc@2.5.2:
|
||||
resolution: {integrity: sha512-OYu7XEzjkCQ3C5Ps3QIZsQfNpqoJyZZA99wd9aWd05NCtC5pWOkShK2mkL6HXQR6/Cy2lbNdPlZBpuQHXE63gA==}
|
||||
engines: {node: '>=4'}
|
||||
hasBin: true
|
||||
|
||||
/jsesc@3.0.2:
|
||||
resolution: {integrity: sha512-xKqzzWXDttJuOcawBt4KnKHHIf5oQ/Cxax+0PWFG+DFDgHNAdi+TXECADI+RYiFUMmx8792xsMbbgXj4CwnP4g==}
|
||||
@@ -11731,6 +11751,10 @@ packages:
|
||||
dependencies:
|
||||
boolbase: 1.0.0
|
||||
|
||||
/number-to-words@1.2.4:
|
||||
resolution: {integrity: sha512-/fYevVkXRcyBiZDg6yzZbm0RuaD6i0qRfn8yr+6D0KgBMOndFPxuW10qCHpzs50nN8qKuv78k8MuotZhcVX6Pw==}
|
||||
dev: false
|
||||
|
||||
/nwsapi@2.2.7:
|
||||
resolution: {integrity: sha512-ub5E4+FBPKwAZx0UwIQOjYWGHTEq5sPqHQNRN8Z9e4A7u3Tj1weLJsL59yH9vmvqEtBHaOmT6cYQKIZOxp35FQ==}
|
||||
dev: true
|
||||
@@ -15886,6 +15910,7 @@ packages:
|
||||
|
||||
/yamljs@0.3.0:
|
||||
resolution: {integrity: sha512-C/FsVVhht4iPQYXOInoxUM/1ELSf9EsgKH34FofQOp6hwCPrW4vG4w5++TED3xRUo8gD7l0P1J1dLlDYzODsTQ==}
|
||||
hasBin: true
|
||||
dependencies:
|
||||
argparse: 1.0.10
|
||||
glob: 7.2.3
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { getCronType, getLastExecutionTimes, isCronValid } from './crontab-generator.service';
|
||||
|
||||
describe('crontab-generator', () => {
|
||||
describe('isCronValid', () => {
|
||||
it('should return true for all valid formats', () => {
|
||||
// standard format
|
||||
expect(isCronValid('0 0 * * 1-5')).toBe(true);
|
||||
expect(isCronValid('23 0-20/2 * * *')).toBe(true);
|
||||
|
||||
// AWS formats
|
||||
expect(isCronValid('0 11-22 ? * MON-FRI *')).toBe(true);
|
||||
expect(isCronValid('0 0 ? * 1 *')).toBe(true);
|
||||
});
|
||||
it('should check standard format', () => {
|
||||
// standard format
|
||||
expect(isCronValid('0 0 * * 1-5', 'standard')).toBe(true);
|
||||
expect(isCronValid('23 0-20/2 * * *', 'standard')).toBe(true);
|
||||
|
||||
// AWS format
|
||||
expect(isCronValid('0 11-22 ? * MON-FRI *', 'standard')).toBe(false);
|
||||
expect(isCronValid('0 0 ? * 1 *', 'standard')).toBe(false);
|
||||
});
|
||||
it('should check aws format', () => {
|
||||
// standard format
|
||||
expect(isCronValid('0 0 * * 1-5', 'aws')).toBe(false);
|
||||
expect(isCronValid('23 0-20/2 * * *', 'aws')).toBe(false);
|
||||
|
||||
// AWS format
|
||||
expect(isCronValid('0 11-22 ? * MON-FRI *', 'aws')).toBe(true);
|
||||
expect(isCronValid('0 0 ? * 1 *', 'aws')).toBe(true);
|
||||
});
|
||||
it('should return false for all invalid formats', () => {
|
||||
expect(isCronValid('aert')).toBe(false);
|
||||
expect(isCronValid('40 *')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getCronType', () => {
|
||||
it('should return right type', () => {
|
||||
expect(getCronType('0 0 * * 1-5')).toBe('standard');
|
||||
expect(getCronType('23 0-20/2 * * *')).toBe('standard');
|
||||
|
||||
// AWS formats
|
||||
expect(getCronType('0 11-22 ? * MON-FRI *')).toBe('aws');
|
||||
expect(getCronType('0 0 ? * 1 *')).toBe('aws');
|
||||
|
||||
expect(getCronType('aert')).toBe(false);
|
||||
expect(getCronType('40 *')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getLastExecutionTimes', () => {
|
||||
it('should return next valid datetimes', () => {
|
||||
expect(getLastExecutionTimes('0 0 * * 1-5')).toHaveLength(5);
|
||||
expect(getLastExecutionTimes('23 0-20/2 * * *')).toHaveLength(5);
|
||||
|
||||
// AWS formats
|
||||
expect(getLastExecutionTimes('0 11-22 ? * MON-FRI *')).toHaveLength(5);
|
||||
expect(getLastExecutionTimes('0 0 ? * 1 *')).toHaveLength(5);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,47 @@
|
||||
import { parseExpression } from 'cron-parser';
|
||||
import EventCronParser from 'event-cron-parser';
|
||||
|
||||
export type CronType = 'standard' | 'aws';
|
||||
|
||||
export function getLastExecutionTimes(cronExpression: string, tz: string | undefined = undefined, count: number = 5) {
|
||||
if (getCronType(cronExpression) === 'standard') {
|
||||
const interval = parseExpression(cronExpression, { tz });
|
||||
const times = [];
|
||||
for (let i = 0; i < count; i++) {
|
||||
times.push(interval.next().toJSON());
|
||||
}
|
||||
return times;
|
||||
}
|
||||
if (getCronType(cronExpression) === 'aws') {
|
||||
const parsed = new EventCronParser(cronExpression);
|
||||
const times = [];
|
||||
for (let i = 0; i < count; i++) {
|
||||
times.push(JSON.stringify(parsed.next()));
|
||||
}
|
||||
return times;
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
export function isCronValid(cronExpression: string, cronType: CronType | 'any' = 'any') {
|
||||
const expressionCronType = getCronType(cronExpression);
|
||||
return cronType === 'any' ? !!expressionCronType : expressionCronType === cronType;
|
||||
}
|
||||
|
||||
export function getCronType(cronExpression: string) {
|
||||
try {
|
||||
parseExpression(cronExpression);
|
||||
return 'standard';
|
||||
}
|
||||
catch (_) {
|
||||
try {
|
||||
const parsed = new EventCronParser(cronExpression);
|
||||
parsed.validate();
|
||||
return 'aws';
|
||||
}
|
||||
catch (_) {
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -1,11 +1,10 @@
|
||||
<script setup lang="ts">
|
||||
import cronstrue from 'cronstrue';
|
||||
import { isValidCron } from 'cron-validator';
|
||||
import ctz from 'countries-and-timezones';
|
||||
import getTimezoneOffset from 'get-timezone-offset';
|
||||
import { type CronType, getLastExecutionTimes, isCronValid } from './crontab-generator.service';
|
||||
import { useStyleStore } from '@/stores/style.store';
|
||||
|
||||
function isCronValid(v: string) {
|
||||
return isValidCron(v, { allowBlankDay: true, alias: true, seconds: true });
|
||||
}
|
||||
import { useQueryParamOrStorage } from '@/composable/queryParams';
|
||||
|
||||
const styleStore = useStyleStore();
|
||||
|
||||
@@ -15,9 +14,25 @@ const cronstrueConfig = reactive({
|
||||
dayOfWeekStartIndexZero: true,
|
||||
use24HourTimeFormat: true,
|
||||
throwExceptionOnParseError: true,
|
||||
monthStartIndexZero: false,
|
||||
tzOffset: (new Date()).getTimezoneOffset() / 60,
|
||||
});
|
||||
|
||||
const helpers = [
|
||||
// getTimezoneOffset(tz.name, now) / 60
|
||||
const browserTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
||||
const allTimezones = Object.values(ctz.getAllTimezones()).map((tz) => {
|
||||
const timezoneUTCDSTOffset = tz.utcOffset === tz.dstOffset ? tz.utcOffsetStr : `${tz.utcOffsetStr}/${tz.dstOffsetStr}`;
|
||||
return {
|
||||
value: tz.name,
|
||||
label: `${tz.name === browserTimezone ? 'Browser TZ - ' : ''}${tz.name} (${timezoneUTCDSTOffset})`,
|
||||
};
|
||||
});
|
||||
const currentTimezone = useQueryParamOrStorage({ name: 'tz', storageName: 'crongen:tz', defaultValue: browserTimezone });
|
||||
watchEffect(() => {
|
||||
cronstrueConfig.tzOffset = -getTimezoneOffset(currentTimezone.value, new Date()) / 60;
|
||||
});
|
||||
|
||||
const commonHelpers = [
|
||||
{
|
||||
symbol: '*',
|
||||
meaning: 'Any value',
|
||||
@@ -42,6 +57,10 @@ const helpers = [
|
||||
example: '*/10 * * *',
|
||||
equivalent: 'Every 10 minutes',
|
||||
},
|
||||
];
|
||||
|
||||
const standardHelpers = [
|
||||
...commonHelpers,
|
||||
{
|
||||
symbol: '@yearly',
|
||||
meaning: 'Once every year at midnight of 1 January',
|
||||
@@ -92,6 +111,59 @@ const helpers = [
|
||||
},
|
||||
];
|
||||
|
||||
const awsHelpers = [
|
||||
...commonHelpers,
|
||||
{
|
||||
symbol: '?',
|
||||
meaning: 'One or another. In the Day-of-month field you could enter 7, and if you didn\'t care what day of the week the seventh was, you could enter ? in the Day-of-week field',
|
||||
example: '9 * 7,9,11 5 ? 2021',
|
||||
equivalent: 'At 9 minutes past the hour, every hour, on day 7, 9, and 11 of the month, only in May, only in 2021',
|
||||
},
|
||||
{
|
||||
symbol: 'L',
|
||||
meaning: 'The L wildcard in the Day-of-month or Day-of-week fields specifies the last day of the month or week.',
|
||||
example: '9 * L 5 ? 2019,2020',
|
||||
equivalent: 'At 9 minutes past the hour, every hour, on the last day of the month, only in May, only in 2019 and 2020',
|
||||
},
|
||||
{
|
||||
symbol: 'W',
|
||||
meaning: 'The W wildcard in the Day-of-month field specifies a weekday. In the Day-of-month field, 3W specifies the day closest to the third weekday of the month.',
|
||||
example: '19 4 3W 9 ? 2019,2020',
|
||||
equivalent: 'At 04:19 AM, on the weekday nearest day 3 of the month, only in September, only in 2019 and 2020',
|
||||
},
|
||||
{
|
||||
symbol: '#',
|
||||
meaning: 'The # wildcard in the Day-of-week field specifies the nieth weekday of the month. 3#5 specifies the fifth Wednesday of the month',
|
||||
example: '9 8-20 ? 12 3#5 2019,2020',
|
||||
equivalent: 'At 9 minutes past the hour, between 08:00 AM and 08:59 PM, on the fifth Wednesday of the month, only in December, only in 2019 and 2020',
|
||||
},
|
||||
];
|
||||
|
||||
const defaultAWSCronExpression = '0 0 ? * 1 *';
|
||||
const defaultStandardCronExpression = '40 * * * *';
|
||||
const cronType = ref<CronType>('standard');
|
||||
watch(cronType,
|
||||
(newCronType) => {
|
||||
if (newCronType === 'aws') {
|
||||
if (!cron.value || cron.value === defaultStandardCronExpression) {
|
||||
cron.value = defaultAWSCronExpression;
|
||||
}
|
||||
}
|
||||
else if (newCronType === 'standard') {
|
||||
if (!cron.value || cron.value === defaultAWSCronExpression) {
|
||||
cron.value = defaultStandardCronExpression;
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
const getHelpers = computed(() => {
|
||||
if (cronType.value === 'aws') {
|
||||
return awsHelpers;
|
||||
}
|
||||
return standardHelpers;
|
||||
});
|
||||
|
||||
const cronString = computed(() => {
|
||||
if (isCronValid(cron.value)) {
|
||||
return cronstrue.toString(cron.value, cronstrueConfig);
|
||||
@@ -101,10 +173,24 @@ const cronString = computed(() => {
|
||||
|
||||
const cronValidationRules = [
|
||||
{
|
||||
validator: (value: string) => isCronValid(value),
|
||||
validator: (value: string) => isCronValid(value, cronType.value),
|
||||
message: 'This cron is invalid',
|
||||
},
|
||||
];
|
||||
|
||||
const executionTimesString = computed(() => {
|
||||
if (isCronValid(cron.value)) {
|
||||
try {
|
||||
const lastExecutionTimes = getLastExecutionTimes(cron.value, currentTimezone.value);
|
||||
const executionTimesString = lastExecutionTimes.join('\n');
|
||||
return `Next 5 execution times:\n${executionTimesString}`;
|
||||
}
|
||||
catch (e: any) {
|
||||
return e.toString();
|
||||
}
|
||||
}
|
||||
return ' ';
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -119,10 +205,27 @@ const cronValidationRules = [
|
||||
/>
|
||||
</div>
|
||||
|
||||
<n-radio-group v-model:value="cronType" name="radiogroup" mb-2 flex justify-center>
|
||||
<n-space>
|
||||
<n-radio
|
||||
value="standard"
|
||||
label="Unix standard"
|
||||
/>
|
||||
<n-radio
|
||||
value="aws"
|
||||
label="AWS"
|
||||
/>
|
||||
</n-space>
|
||||
</n-radio-group>
|
||||
|
||||
<div class="cron-string">
|
||||
{{ cronString }}
|
||||
</div>
|
||||
|
||||
<div class="cron-execution-string">
|
||||
{{ executionTimesString }}
|
||||
</div>
|
||||
|
||||
<n-divider />
|
||||
|
||||
<div flex justify-center>
|
||||
@@ -136,11 +239,21 @@ const cronValidationRules = [
|
||||
<n-form-item label="Days start at 0">
|
||||
<n-switch v-model:value="cronstrueConfig.dayOfWeekStartIndexZero" />
|
||||
</n-form-item>
|
||||
<n-form-item label="Months start at 0">
|
||||
<n-switch v-model:value="cronstrueConfig.monthStartIndexZero" />
|
||||
</n-form-item>
|
||||
<c-select
|
||||
v-model:value="currentTimezone"
|
||||
searchable
|
||||
label="Timezone:"
|
||||
:options="allTimezones"
|
||||
/>
|
||||
</n-form>
|
||||
</div>
|
||||
</c-card>
|
||||
<c-card>
|
||||
<pre>
|
||||
<pre v-if="cronType === 'standard'">
|
||||
-- Standard CRON Syntax --
|
||||
┌──────────── [optional] seconds (0 - 59)
|
||||
| ┌────────── minute (0 - 59)
|
||||
| | ┌──────── hour (0 - 23)
|
||||
@@ -150,8 +263,19 @@ const cronValidationRules = [
|
||||
| | | | | |
|
||||
* * * * * * command</pre>
|
||||
|
||||
<pre v-if="cronType === 'aws'">
|
||||
-- AWS CRON Syntax --
|
||||
┌──────────── minute (0 - 59)
|
||||
| ┌────────── hour (0 - 23)
|
||||
| | ┌──────── day of month (1 - 31) OR ? OR L OR W
|
||||
| | | ┌────── month (1 - 12) OR jan,feb,mar,apr ...
|
||||
| | | | ┌──── day of week (0 - 6, sunday=0) OR sun,mon OR L ...
|
||||
| | | | | ┌── year
|
||||
| | | | | |
|
||||
* * * * * *</pre>
|
||||
|
||||
<div v-if="styleStore.isSmallScreen">
|
||||
<c-card v-for="{ symbol, meaning, example, equivalent } in helpers" :key="symbol" mb-3 important:border-none>
|
||||
<c-card v-for="{ symbol, meaning, example, equivalent } in getHelpers" :key="symbol" mb-3 important:border-none>
|
||||
<div>
|
||||
Symbol: <strong>{{ symbol }}</strong>
|
||||
</div>
|
||||
@@ -168,7 +292,7 @@ const cronValidationRules = [
|
||||
</c-card>
|
||||
</div>
|
||||
|
||||
<c-table v-else :data="helpers" />
|
||||
<c-table v-else :data="getHelpers" />
|
||||
</c-card>
|
||||
</template>
|
||||
|
||||
@@ -191,4 +315,12 @@ pre {
|
||||
overflow: auto;
|
||||
padding: 10px 0;
|
||||
}
|
||||
|
||||
.cron-execution-string{
|
||||
text-align: center;
|
||||
font-size: 14px;
|
||||
opacity: 0.8;
|
||||
margin: 5px 0 15px;
|
||||
white-space: pre-wrap;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,3 @@
|
||||
declare module "get-timezone-offset" {
|
||||
export default function(timeZoneName: string, date: Date);
|
||||
}
|
||||
@@ -20,6 +20,7 @@ export const tool = defineTool({
|
||||
'day',
|
||||
'minute',
|
||||
'second',
|
||||
'aws',
|
||||
],
|
||||
component: () => import('./crontab-generator.vue'),
|
||||
icon: Alarm,
|
||||
|
||||
Reference in new issue
Block a user