Skip to content

Commit 0d8e7ac

Browse files
author
doubleface
committed
feat: Add getCronFromFrequency to manifest models
This feature is imported from harvest and allows to get the cron string from a given frequency like 'daily'. This cron string will configure the trigger associated to the konnector
1 parent b15f653 commit 0d8e7ac

File tree

8 files changed

+460
-8
lines changed

8 files changed

+460
-8
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
[cozy-client](../README.md) / [manifest](../modules/manifest.md) / FrequencyOptions
2+
3+
# Interface: FrequencyOptions<>
4+
5+
[manifest](../modules/manifest.md).FrequencyOptions
6+
7+
frequency options object
8+
9+
## Properties
10+
11+
### dayOfMonth
12+
13+
**dayOfMonth**: `number`
14+
15+
day of the month
16+
17+
*Defined in*
18+
19+
[packages/cozy-client/src/models/manifest.js:329](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/manifest.js#L329)
20+
21+
***
22+
23+
### dayOfWeek
24+
25+
**dayOfWeek**: `number`
26+
27+
day of the week
28+
29+
*Defined in*
30+
31+
[packages/cozy-client/src/models/manifest.js:330](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/manifest.js#L330)
32+
33+
***
34+
35+
### hours
36+
37+
**hours**: `number`
38+
39+
hours
40+
41+
*Defined in*
42+
43+
[packages/cozy-client/src/models/manifest.js:331](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/manifest.js#L331)
44+
45+
***
46+
47+
### minutes
48+
49+
**minutes**: `number`
50+
51+
minutes
52+
53+
*Defined in*
54+
55+
[packages/cozy-client/src/models/manifest.js:332](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/manifest.js#L332)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
[cozy-client](../README.md) / [manifest](../modules/manifest.md) / randomDayTimeResult
2+
3+
# Interface: randomDayTimeResult<>
4+
5+
[manifest](../modules/manifest.md).randomDayTimeResult
6+
7+
## Properties
8+
9+
### hours
10+
11+
**hours**: `number`
12+
13+
hours
14+
15+
*Defined in*
16+
17+
[packages/cozy-client/src/models/manifest.js:323](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/manifest.js#L323)
18+
19+
***
20+
21+
### minutes
22+
23+
**minutes**: `number`
24+
25+
minutes
26+
27+
*Defined in*
28+
29+
[packages/cozy-client/src/models/manifest.js:324](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/manifest.js#L324)

docs/api/cozy-client/modules/manifest.md

Lines changed: 90 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
# Namespace: manifest
44

5+
## Interfaces
6+
7+
* [FrequencyOptions](../interfaces/manifest.FrequencyOptions.md)
8+
* [randomDayTimeResult](../interfaces/manifest.randomDayTimeResult.md)
9+
510
## Variables
611

712
### ROLE_IDENTIFIER
@@ -22,7 +27,7 @@ Legacy login fields declared by some konnectors
2227

2328
*Defined in*
2429

25-
[packages/cozy-client/src/models/manifest.js:11](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/manifest.js#L11)
30+
[packages/cozy-client/src/models/manifest.js:21](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/manifest.js#L21)
2631

2732
## Functions
2833

@@ -42,7 +47,59 @@ Legacy login fields declared by some konnectors
4247

4348
*Defined in*
4449

45-
[packages/cozy-client/src/models/manifest.js:63](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/manifest.js#L63)
50+
[packages/cozy-client/src/models/manifest.js:73](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/manifest.js#L73)
51+
52+
***
53+
54+
### getCronFromFrequency
55+
56+
**getCronFromFrequency**(`frequency`, `options?`): `string`
57+
58+
Build a cron string for given frequency with given options
59+
See https://docs.cozy.io/en/cozy-stack/jobs/#cron-syntax
60+
61+
*Parameters*
62+
63+
| Name | Type | Description |
64+
| :------ | :------ | :------ |
65+
| `frequency` | `"hourly"` | `"daily"` | `"weekly"` | `"monthly"` | Frequency |
66+
| `options` | [`FrequencyOptions`](../interfaces/manifest.FrequencyOptions.md) | - |
67+
68+
*Returns*
69+
70+
`string`
71+
72+
* The cron definition for trigger
73+
74+
*Defined in*
75+
76+
[packages/cozy-client/src/models/manifest.js:280](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/manifest.js#L280)
77+
78+
***
79+
80+
### getCronFromKonnector
81+
82+
**getCronFromKonnector**(`konnector`, `startDate?`, `randomDayTimeFn?`): `string`
83+
84+
Build a cron string for given konnector and from a given start date
85+
86+
*Parameters*
87+
88+
| Name | Type | Default value | Description |
89+
| :------ | :------ | :------ | :------ |
90+
| `konnector` | `IOCozyKonnector` | `undefined` | io.cozy.konnectors object |
91+
| `startDate` | `Date` | `undefined` | start date |
92+
| `randomDayTimeFn` | `Function` | `randomDayTime` | - |
93+
94+
*Returns*
95+
96+
`string`
97+
98+
* The cron definition for trigger
99+
100+
*Defined in*
101+
102+
[packages/cozy-client/src/models/manifest.js:307](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/manifest.js#L307)
46103

47104
***
48105

@@ -66,7 +123,7 @@ The key for the identifier field, example 'login'
66123

67124
*Defined in*
68125

69-
[packages/cozy-client/src/models/manifest.js:161](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/manifest.js#L161)
126+
[packages/cozy-client/src/models/manifest.js:171](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/manifest.js#L171)
70127

71128
***
72129

@@ -86,7 +143,33 @@ The key for the identifier field, example 'login'
86143

87144
*Defined in*
88145

89-
[packages/cozy-client/src/models/manifest.js:67](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/manifest.js#L67)
146+
[packages/cozy-client/src/models/manifest.js:77](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/manifest.js#L77)
147+
148+
***
149+
150+
### randomDayTime
151+
152+
**randomDayTime**(`start?`, `end?`, `randomize?`): [`randomDayTimeResult`](../interfaces/manifest.randomDayTimeResult.md)
153+
154+
Returns an hour of the day between two hours given in parameters
155+
156+
*Parameters*
157+
158+
| Name | Type | Default value | Description |
159+
| :------ | :------ | :------ | :------ |
160+
| `start` | `number` | `0` | minimal start hour |
161+
| `end` | `number` | `1` | maximal end hour |
162+
| `randomize` | `Function` | `undefined` | The function used to generate random values |
163+
164+
*Returns*
165+
166+
[`randomDayTimeResult`](../interfaces/manifest.randomDayTimeResult.md)
167+
168+
Object containing two atributes : hours and minutes
169+
170+
*Defined in*
171+
172+
[packages/cozy-client/src/models/manifest.js:248](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/manifest.js#L248)
90173

91174
***
92175

@@ -108,7 +191,7 @@ Normalize app manifest, retro-compatibility for old manifests
108191

109192
*Defined in*
110193

111-
[packages/cozy-client/src/models/manifest.js:77](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/manifest.js#L77)
194+
[packages/cozy-client/src/models/manifest.js:87](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/manifest.js#L87)
112195

113196
***
114197

@@ -132,7 +215,7 @@ sanitized categories
132215

133216
*Defined in*
134217

135-
[packages/cozy-client/src/models/manifest.js:56](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/manifest.js#L56)
218+
[packages/cozy-client/src/models/manifest.js:66](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/manifest.js#L66)
136219

137220
***
138221

@@ -156,4 +239,4 @@ Ensures that fields has at least one field with the role 'identifier'
156239

157240
*Defined in*
158241

159-
[packages/cozy-client/src/models/manifest.js:130](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/manifest.js#L130)
242+
[packages/cozy-client/src/models/manifest.js:140](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/manifest.js#L140)

packages/cozy-client/src/models/manifest.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ import findKey from 'lodash/findKey'
55

66
export const ROLE_IDENTIFIER = 'identifier'
77

8+
const DAILY = 'daily'
9+
const HOURLY = 'hourly'
10+
const MONTHLY = 'monthly'
11+
const WEEKLY = 'weekly'
12+
const VALID_FREQUENCIES = [DAILY, HOURLY, MONTHLY, WEEKLY]
13+
14+
const DEFAULT_FREQUENCY = WEEKLY
15+
// By default konnectors are run at random hour between 12:00PM and 05:00AM.
16+
const DEFAULT_TIME_INTERVAL = [0, 5]
17+
818
/**
919
* Legacy login fields declared by some konnectors
1020
*/
@@ -226,3 +236,98 @@ const sanitizeFields = _flow([
226236
sanitizeRequired,
227237
sanitizeEncrypted
228238
])
239+
240+
/**
241+
* Returns an hour of the day between two hours given in parameters
242+
*
243+
* @param {number} start minimal start hour
244+
* @param {number} end maximal end hour
245+
* @param {function} randomize The function used to generate random values
246+
* @returns {randomDayTimeResult} Object containing two atributes : hours and minutes
247+
*/
248+
export const randomDayTime = (
249+
start = 0,
250+
end = 1,
251+
randomize = (min, max) => Math.random() * (max - min) + min
252+
) => {
253+
if (typeof start !== 'number')
254+
throw new Error('Parameter start must be a number')
255+
if (typeof end !== 'number') throw new Error('Parameter end must be a number')
256+
257+
if (typeof randomize !== 'function')
258+
throw new Error('Parameter randomize must be a function')
259+
260+
if (start < 0 || end > 24) throw new Error('interval must be inside [0, 24]')
261+
262+
const r = randomize(start, end)
263+
const hours = Math.floor(r)
264+
const minutes = Math.floor((r - hours) * 60)
265+
266+
if (hours < 0 || hours > 23)
267+
throw new Error('randomize function returns invalid hour value')
268+
269+
return { hours, minutes }
270+
}
271+
272+
/**
273+
* Build a cron string for given frequency with given options
274+
* See https://docs.cozy.io/en/cozy-stack/jobs/#cron-syntax
275+
*
276+
* @param {'hourly'|'daily'|'weekly'|'monthly'} frequency Frequency
277+
* @param {FrequencyOptions} [options] - FrequencyOptions object
278+
* @returns {String} - The cron definition for trigger
279+
*/
280+
export const getCronFromFrequency = (frequency, options = {}) => {
281+
const { dayOfMonth = 1, dayOfWeek = 1, hours = 0, minutes = 0 } = options
282+
const sanitizedFrequency = VALID_FREQUENCIES.includes(frequency)
283+
? frequency
284+
: DEFAULT_FREQUENCY
285+
286+
switch (sanitizedFrequency) {
287+
case DAILY:
288+
return `0 ${minutes} ${hours} * * *`
289+
case HOURLY:
290+
return `0 ${minutes} * * * *`
291+
case MONTHLY:
292+
return `0 ${minutes} ${hours} ${dayOfMonth} * *`
293+
default:
294+
// also WEEKLY
295+
return `0 ${minutes} ${hours} * * ${dayOfWeek}`
296+
}
297+
}
298+
299+
/**
300+
* Build a cron string for given konnector and from a given start date
301+
*
302+
* @param {import('../types').IOCozyKonnector} konnector - io.cozy.konnectors object
303+
* @param {Date} startDate - start date
304+
* @param {function} [randomDayTimeFn] - function generating random hours and minutes
305+
* @returns {String} - The cron definition for trigger
306+
*/
307+
export const getCronFromKonnector = (
308+
konnector,
309+
startDate = new Date(),
310+
randomDayTimeFn = randomDayTime
311+
) =>
312+
getCronFromFrequency(konnector.frequency, {
313+
...randomDayTimeFn.apply(
314+
null,
315+
konnector.time_interval || DEFAULT_TIME_INTERVAL
316+
),
317+
dayOfWeek: startDate.getDay(),
318+
dayOfMonth: startDate.getDate()
319+
})
320+
321+
/**
322+
* @typedef {object} randomDayTimeResult
323+
* @property {number} hours - hours
324+
* @property {number} minutes - minutes
325+
*/
326+
327+
/**
328+
* @typedef {object} FrequencyOptions - frequency options object
329+
* @property {Number} [dayOfMonth] - day of the month
330+
* @property {Number} [dayOfWeek] - day of the week
331+
* @property {Number} [hours] - hours
332+
* @property {Number} [minutes] - minutes
333+
*/

0 commit comments

Comments
 (0)