diff --git a/packages/cozy-clisk/CHANGELOG.md b/packages/cozy-clisk/CHANGELOG.md index 39b358a03..7dcb3378f 100644 --- a/packages/cozy-clisk/CHANGELOG.md +++ b/packages/cozy-clisk/CHANGELOG.md @@ -3,6 +3,24 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +# [0.38.0](https://github.com/konnectors/libs/compare/cozy-clisk@0.37.0...cozy-clisk@0.38.0) (2024-07-26) + + +### Bug Fixes + +* Mock date of the day for shouldFullSync ([40d3391](https://github.com/konnectors/libs/commit/40d3391f81dcd2c1315834b030aa73da7ce1d13f)) +* Small renaming and corrections after review ([e0f0eee](https://github.com/konnectors/libs/commit/e0f0eee17ea9f1dce799f62f3db6ef49ec48caa2)) + + +### Features + +* Add shouldFullSync function ([7a573d4](https://github.com/konnectors/libs/commit/7a573d4e0e425fbee836d830fd10103ddbfc848c)) +* Add shouldFullSync UTs ([df6ba20](https://github.com/konnectors/libs/commit/df6ba20af6073ce5f0ff5e83178854c34c301d68)) + + + + + # [0.37.0](https://github.com/konnectors/libs/compare/cozy-clisk@0.36.1...cozy-clisk@0.37.0) (2024-06-17) diff --git a/packages/cozy-clisk/package.json b/packages/cozy-clisk/package.json index 27021b727..558ef3a25 100644 --- a/packages/cozy-clisk/package.json +++ b/packages/cozy-clisk/package.json @@ -1,6 +1,6 @@ { "name": "cozy-clisk", - "version": "0.37.0", + "version": "0.38.0", "description": "All the libs needed to run a cozy client connector", "repository": { "type": "git", diff --git a/packages/cozy-clisk/src/contentscript/ContentScript.js b/packages/cozy-clisk/src/contentscript/ContentScript.js index 1e659ec8e..eb12dc81d 100644 --- a/packages/cozy-clisk/src/contentscript/ContentScript.js +++ b/packages/cozy-clisk/src/contentscript/ContentScript.js @@ -100,7 +100,6 @@ export default class ContentScript { suffixFn: args => args?.[0] } ) - if (options.requestInterceptor) { this.requestInterceptor = options.requestInterceptor this.requestInterceptor.setLogger(this.log.bind(this)) @@ -923,6 +922,57 @@ export default class ContentScript { } } + /** + * Determine if the konnector must fetch all or parts of the data. + * + * @param {object} options - All the data already fetched by the connector in a previous execution. + * Useful to optimize connector execution by not fetching data we already have. + * @returns {Promise<object>} - Promise that resolves to an object with the following properties: + * @property {boolean} shouldFullSync - Indicates if a full synchronization is needed. + * @property {number|NaN} distanceInDays - The number of days since the last sync, or NaN if not applicable. + */ + + async shouldFullSync(options) { + this.onlyIn(PILOT_TYPE, 'shouldFullSync') + const { trigger, flags } = options + let forceFullSync = false + let flagFullSync = false + if (flags['clisk.force-full-sync'] === true) { + this.log('info', 'User forces full sync') + flagFullSync = true + } + const isFirstJob = + !trigger.current_state?.last_failure && + !trigger.current_state?.last_success + const isLastJobError = + !isFirstJob && + trigger.current_state?.last_failure > trigger.current_state?.last_success + const hasLastExecution = Boolean(trigger.current_state?.last_execution) + let distanceInDays = 0 + if (hasLastExecution) { + distanceInDays = getDateDistanceInDays( + trigger.current_state?.last_execution + ) + } + this.log('debug', `distanceInDays: ${distanceInDays}`) + if ( + flagFullSync || + !hasLastExecution || + isLastJobError || + distanceInDays >= 30 + ) { + this.log('info', '🐢️ Long execution') + this.log( + 'debug', + `isLastJobError: ${isLastJobError} | hasLastExecution: ${hasLastExecution}` + ) + forceFullSync = true + } else { + this.log('info', '🐇️ Quick execution') + } + return { forceFullSync, distanceInDays } + } + /** * Main function, fetches all connector data and save it to the cozy * @@ -951,3 +1001,10 @@ function sendPageMessage(message) { log.error('No window.ReactNativeWebView.postMessage available') } } + +function getDateDistanceInDays(dateString) { + const distanceMs = Date.now() - new Date(dateString).getTime() + const days = 1000 * 60 * 60 * 24 + + return Math.floor(distanceMs / days) +} diff --git a/packages/cozy-clisk/src/contentscript/ContentScript.spec.js b/packages/cozy-clisk/src/contentscript/ContentScript.spec.js index 19c92d61b..4aa1c0fea 100644 --- a/packages/cozy-clisk/src/contentscript/ContentScript.spec.js +++ b/packages/cozy-clisk/src/contentscript/ContentScript.spec.js @@ -168,7 +168,6 @@ describe('ContentScript', () => { ) }) }) - describe('runInWorkerUntilTrue', () => { const contentScript = new ContentScript() contentScript.contentScriptType = PILOT_TYPE @@ -200,4 +199,97 @@ describe('ContentScript', () => { ).rejects.toThrow('Timeout error') }) }) + describe('shouldFullSync', () => { + const fixedDate = new Date('2024-07-24T14:55:57.83761233+02:00').getTime() + beforeAll(() => { + jest.spyOn(Date, 'now').mockImplementation(() => fixedDate) + }) + afterAll(() => { + jest.restoreAllMocks() + }) + it('should force full sync if forceFullSync flag is activated', async () => { + const contentScript = new ContentScript() + contentScript.contentScriptType = PILOT_TYPE + + const options = { + flags: { + 'clisk.force-full-sync': true + }, + trigger: { + current_state: { + last_execution: '2024-07-24T14:55:57.83761233+02:00', + last_failure: '2024-07-23T14:55:57.83761233+02:00', + last_success: '2024-07-24T14:55:57.83761233+02:00' + } + } + } + const result = await contentScript.shouldFullSync(options) + expect(result).toEqual({ + forceFullSync: true, + distanceInDays: 0 + }) + }) + it('should force full sync if it is the first execution', async () => { + const contentScript = new ContentScript() + contentScript.contentScriptType = PILOT_TYPE + + const options = { + flags: { + 'clisk.force-full-sync': false + }, + trigger: { + current_state: {} + } + } + const result = await contentScript.shouldFullSync(options) + expect(result).toEqual({ + forceFullSync: true, + distanceInDays: 0 + }) + }) + it('should force full sync if the last execution failed', async () => { + const contentScript = new ContentScript() + contentScript.contentScriptType = PILOT_TYPE + + const options = { + flags: { + 'clisk.force-full-sync': false + }, + trigger: { + current_state: { + last_execution: '2024-07-24T14:55:57.83761233+02:00', + last_failure: '2024-07-24T14:55:57.83761233+02:00', + last_success: '2024-07-23T14:55:57.83761233+02:00' + } + } + } + const result = await contentScript.shouldFullSync(options) + expect(result).toEqual({ + forceFullSync: true, + distanceInDays: 0 + }) + }) + it('should not force full sync in nominal case', async () => { + const contentScript = new ContentScript() + contentScript.contentScriptType = PILOT_TYPE + + const options = { + flags: { + 'clisk.force-full-sync': false + }, + trigger: { + current_state: { + last_execution: '2024-07-24T14:55:57.83761233+02:00', + last_failure: '2024-07-23T14:55:57.83761233+02:00', + last_success: '2024-07-24T14:55:57.83761233+02:00' + } + } + } + const result = await contentScript.shouldFullSync(options) + expect(result).toEqual({ + forceFullSync: false, + distanceInDays: 0 + }) + }) + }) }) diff --git a/packages/cozy-jobs-cli/CHANGELOG.md b/packages/cozy-jobs-cli/CHANGELOG.md index c7bc8bbc1..f4cd00d04 100644 --- a/packages/cozy-jobs-cli/CHANGELOG.md +++ b/packages/cozy-jobs-cli/CHANGELOG.md @@ -3,6 +3,14 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## [2.4.4](https://github.com/cozy/cozy-konnector-libs/compare/cozy-jobs-cli@2.4.3...cozy-jobs-cli@2.4.4) (2024-09-17) + +**Note:** Version bump only for package cozy-jobs-cli + + + + + ## [2.4.3](https://github.com/cozy/cozy-konnector-libs/compare/cozy-jobs-cli@2.4.2...cozy-jobs-cli@2.4.3) (2024-07-18) **Note:** Version bump only for package cozy-jobs-cli diff --git a/packages/cozy-jobs-cli/package.json b/packages/cozy-jobs-cli/package.json index 0c8c9ffa3..1b3c067f7 100644 --- a/packages/cozy-jobs-cli/package.json +++ b/packages/cozy-jobs-cli/package.json @@ -1,6 +1,6 @@ { "name": "cozy-jobs-cli", - "version": "2.4.3", + "version": "2.4.4", "description": "Manage cozy jobs", "main": "index.js", "repository": { @@ -29,7 +29,7 @@ "cozy-client": "45.14.1", "cozy-device-helper": "^2.1.0", "cozy-flags": "^2.8.7", - "cozy-konnector-libs": "^5.12.0", + "cozy-konnector-libs": "^5.12.1", "cozy-logger": "1.9.0", "node-fetch": "2.7.0", "open": "8.4.0", diff --git a/packages/cozy-konnector-libs/CHANGELOG.md b/packages/cozy-konnector-libs/CHANGELOG.md index a4ff6f028..1a484823b 100644 --- a/packages/cozy-konnector-libs/CHANGELOG.md +++ b/packages/cozy-konnector-libs/CHANGELOG.md @@ -3,6 +3,17 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## [5.12.1](https://github.com/cozy/cozy-konnector-libs/compare/cozy-konnector-libs@5.12.0...cozy-konnector-libs@5.12.1) (2024-09-17) + + +### Bug Fixes + +* Explicitely use node-fetch 2.7.0 ([443e524](https://github.com/cozy/cozy-konnector-libs/commit/443e52478c4facc0e288f564d2acbc66435abaa2)) + + + + + # [5.12.0](https://github.com/cozy/cozy-konnector-libs/compare/cozy-konnector-libs@5.11.0...cozy-konnector-libs@5.12.0) (2024-07-18) diff --git a/packages/cozy-konnector-libs/package.json b/packages/cozy-konnector-libs/package.json index 7bf2fb28b..720e6fb3f 100644 --- a/packages/cozy-konnector-libs/package.json +++ b/packages/cozy-konnector-libs/package.json @@ -1,6 +1,6 @@ { "name": "cozy-konnector-libs", - "version": "5.12.0", + "version": "5.12.1", "description": "All the libs needed by a cozy v3 konnector", "main": "dist/index.js", "repository": { @@ -38,7 +38,7 @@ "lodash-id": "^0.14.0", "lowdb": "^1.0.0", "mime-types": "^2.1.31", - "node-fetch": "^2.6.1", + "node-fetch": "^2.7.0", "raven": "^2.6.4", "raw-body": "^2.4.1", "request": "^2.88.2", diff --git a/yarn.lock b/yarn.lock index cbf2016df..ecbe857e2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9088,7 +9088,7 @@ nock@^12.0.3: lodash "^4.17.13" propagate "^2.0.0" -node-fetch@2.7.0, node-fetch@^2.0.0, node-fetch@^2.6.1, node-fetch@^2.6.12, node-fetch@^2.6.7: +node-fetch@2.7.0, node-fetch@^2.0.0, node-fetch@^2.6.1, node-fetch@^2.6.12, node-fetch@^2.6.7, node-fetch@^2.7.0: version "2.7.0" resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.7.0.tgz#d0f0fa6e3e2dc1d27efcd8ad99d550bda94d187d" integrity sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A== @@ -11310,7 +11310,7 @@ string-length@^4.0.1: char-regex "^1.0.2" strip-ansi "^6.0.0" -"string-width-cjs@npm:string-width@^4.2.0", "string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3: +"string-width-cjs@npm:string-width@^4.2.0": version "4.2.3" resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010" integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g== @@ -11328,6 +11328,15 @@ string-width@^1.0.1: is-fullwidth-code-point "^1.0.0" strip-ansi "^3.0.0" +"string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3: + version "4.2.3" + resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010" + integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g== + dependencies: + emoji-regex "^8.0.0" + is-fullwidth-code-point "^3.0.0" + strip-ansi "^6.0.1" + string-width@^5.0.1, string-width@^5.1.2: version "5.1.2" resolved "https://registry.yarnpkg.com/string-width/-/string-width-5.1.2.tgz#14f8daec6d81e7221d2a357e668cab73bdbca794" @@ -11403,7 +11412,7 @@ stringify-clone@^1.0.0: resolved "https://registry.yarnpkg.com/stringify-clone/-/stringify-clone-1.1.1.tgz#309a235fb4ecfccd7d388dbe18ba904facaf433b" integrity sha512-LIFpvBnQJF3ZGoV770s3feH+wRVCMRSisI8fl1E57WfgKOZKUMaC1r4eJXybwGgXZ/iTTJoK/tsOku1GLPyyxQ== -"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1: +"strip-ansi-cjs@npm:strip-ansi@^6.0.1": version "6.0.1" resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9" integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A== @@ -11417,6 +11426,13 @@ strip-ansi@^3.0.0, strip-ansi@^3.0.1: dependencies: ansi-regex "^2.0.0" +strip-ansi@^6.0.0, strip-ansi@^6.0.1: + version "6.0.1" + resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9" + integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A== + dependencies: + ansi-regex "^5.0.1" + strip-ansi@^7.0.1: version "7.1.0" resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-7.1.0.tgz#d5b6568ca689d8561370b0707685d22434faff45" @@ -12613,7 +12629,16 @@ wordwrapjs@^3.0.0: reduce-flatten "^1.0.1" typical "^2.6.1" -"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0: +"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0": + version "7.0.0" + resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43" + integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q== + dependencies: + ansi-styles "^4.0.0" + string-width "^4.1.0" + strip-ansi "^6.0.0" + +wrap-ansi@^7.0.0: version "7.0.0" resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43" integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==