From 484e0df3529016d2be7406368014987419d84849 Mon Sep 17 00:00:00 2001 From: Gilson Filho Date: Thu, 19 Oct 2023 22:34:37 -0300 Subject: [PATCH] refactor(scheduler): Update status domain service in chunks --- scheduler/src/service.ts | 30 +++++++++++--------- shared/chunk/jest.config.js | 5 ++++ shared/chunk/package.json | 19 +++++++++++++ shared/chunk/src/index.test.ts | 51 ++++++++++++++++++++++++++++++++++ shared/chunk/src/index.ts | 11 ++++++++ shared/chunk/tsconfig.json | 15 ++++++++++ yarn.lock | 8 ++++++ 7 files changed, 126 insertions(+), 13 deletions(-) create mode 100644 shared/chunk/jest.config.js create mode 100644 shared/chunk/package.json create mode 100644 shared/chunk/src/index.test.ts create mode 100644 shared/chunk/src/index.ts create mode 100644 shared/chunk/tsconfig.json diff --git a/scheduler/src/service.ts b/scheduler/src/service.ts index 2b1a0db..114437e 100644 --- a/scheduler/src/service.ts +++ b/scheduler/src/service.ts @@ -1,5 +1,6 @@ import { createClient } from "@checkstatusgovbr/database-ts"; import { DatabaseClient } from "@checkstatusgovbr/database-ts/src/client"; +import chunk from "@checkstatusgovbr/chunk"; import AvailabilityDomains from "./availability"; import pino from "pino"; import { Domain } from "domain"; @@ -39,24 +40,27 @@ export default class AvailabilityDomainsService { async pingDomains() { const domains = await this.fetchDomains(); + const chunkedDomains = chunk(domains, 50); - let results: DomainData[] = []; - for (const data of domains) { - logger.info(`Pinging ${data.domain}`); - const isAvailable = await this.pingClient.isAvailable(data.domain); + for (const chunk of chunkedDomains) { + let results: DomainData[] = []; + for (const data of chunk) { + logger.info(`Pinging ${data.domain}`); + const isAvailable = await this.pingClient.isAvailable(data.domain); - logger.info( - `Domain ${data.domain} is ${ - isAvailable ? "available" : "not available" - }` - ); - results.push({ ...data, available: isAvailable }); - } + logger.info( + `Domain ${data.domain} is ${ + isAvailable ? "available" : "not available" + }` + ); + results.push({ ...data, available: isAvailable }); + } - await this.updateDomainsStatus(results); + await this.updateDomainsStatus(results); + } } - public async updateDomainsStatus(domains: DomainData[]) { + private async updateDomainsStatus(domains: DomainData[]) { const updateValues = domains .map((data) => `('${data.domain}', ${data.available}, NOW())`) .join(", "); diff --git a/shared/chunk/jest.config.js b/shared/chunk/jest.config.js new file mode 100644 index 0000000..3745fc2 --- /dev/null +++ b/shared/chunk/jest.config.js @@ -0,0 +1,5 @@ +/** @type {import('ts-jest').JestConfigWithTsJest} */ +module.exports = { + preset: 'ts-jest', + testEnvironment: 'node', +}; diff --git a/shared/chunk/package.json b/shared/chunk/package.json new file mode 100644 index 0000000..aa2eb7b --- /dev/null +++ b/shared/chunk/package.json @@ -0,0 +1,19 @@ +{ + "name": "@checkstatusgovbr/chunk", + "version": "1.0.0", + "main": "src/index.ts", + "license": "MIT", + "scripts": { + "build": "tsc", + "test": "jest" + }, + "dependencies": { + "ts-node": "^10.9.1", + "typescript": "^5.2.2" + }, + "devDependencies": { + "@types/jest": "^29.5.6", + "jest": "^29.7.0", + "ts-jest": "^29.1.1" + } +} diff --git a/shared/chunk/src/index.test.ts b/shared/chunk/src/index.test.ts new file mode 100644 index 0000000..ee38a89 --- /dev/null +++ b/shared/chunk/src/index.test.ts @@ -0,0 +1,51 @@ +import chunk from "."; + +describe("chunk", () => { + it("should return an empty array when given an empty array", () => { + const input: number[] = []; + const result = chunk(input); + expect(result).toEqual([]); + }); + + it("should return an array with one chunk when given an array with one element", () => { + const input = [1]; + const result = chunk(input); + expect(result).toEqual([[1]]); + }); + + it("should return an array with two chunks when given an array with two elements", () => { + const input = [1, 2]; + const result = chunk(input); + expect(result).toEqual([[1, 2]]); + }); + + it("should return an array with two chunks when given an array with three elements", () => { + const input = [1, 2, 3]; + const result = chunk(input); + expect(result).toEqual([[1, 2], [3]]); + }); + + it("should return an array with three chunks when given an array with four elements", () => { + const input = [1, 2, 3, 4]; + const result = chunk(input); + expect(result).toEqual([ + [1, 2], + [3, 4], + ]); + }); + + it("should return an array with three chunks when given an array with five elements and a chunk size of 2", () => { + const input = [1, 2, 3, 4, 5]; + const result = chunk(input, 2); + expect(result).toEqual([[1, 2], [3, 4], [5]]); + }); + + it("should return an array with two chunks when given an array with five elements and a chunk size of 3", () => { + const input = [1, 2, 3, 4, 5]; + const result = chunk(input, 3); + expect(result).toEqual([ + [1, 2, 3], + [4, 5], + ]); + }); +}); diff --git a/shared/chunk/src/index.ts b/shared/chunk/src/index.ts new file mode 100644 index 0000000..64c2085 --- /dev/null +++ b/shared/chunk/src/index.ts @@ -0,0 +1,11 @@ +const chunk = (collection: T[], size: number = 2): T[][] => { + const result: T[][] = []; + + for (let start = 0; start < collection.length; start += size) { + let end = start + size; + result.push(collection.slice(start, end)); + } + return result; +}; + +export default chunk; diff --git a/shared/chunk/tsconfig.json b/shared/chunk/tsconfig.json new file mode 100644 index 0000000..8b9510c --- /dev/null +++ b/shared/chunk/tsconfig.json @@ -0,0 +1,15 @@ +{ + "compilerOptions": { + "incremental": true, + "composite": true, + "target": "es2016", + "module": "commonjs", + "rootDir": "./src", + "outDir": "./dist", + "removeComments": true, + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "strict": true, + "skipLibCheck": true + } +} diff --git a/yarn.lock b/yarn.lock index c05ab69..f651bf4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1041,6 +1041,14 @@ expect "^29.0.0" pretty-format "^29.0.0" +"@types/jest@^29.5.6": + version "29.5.6" + resolved "https://registry.yarnpkg.com/@types/jest/-/jest-29.5.6.tgz#f4cf7ef1b5b0bfc1aa744e41b24d9cc52533130b" + integrity sha512-/t9NnzkOpXb4Nfvg17ieHE6EeSjDS2SGSpNYfoLbUAeL/EOueU/RSdOWFpfQTXBEM7BguYW1XQ0EbM+6RlIh6w== + dependencies: + expect "^29.0.0" + pretty-format "^29.0.0" + "@types/json5@^0.0.29": version "0.0.29" resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"