Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(scheduler): Update status domain service in chunks #67

Merged
merged 1 commit into from
Oct 20, 2023
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
30 changes: 17 additions & 13 deletions scheduler/src/service.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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(", ");
Expand Down
5 changes: 5 additions & 0 deletions shared/chunk/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
19 changes: 19 additions & 0 deletions shared/chunk/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
51 changes: 51 additions & 0 deletions shared/chunk/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -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<number>(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<number>(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<number>(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<number>(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<number>(input, 3);
expect(result).toEqual([
[1, 2, 3],
[4, 5],
]);
});
});
11 changes: 11 additions & 0 deletions shared/chunk/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const chunk = <T>(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;
15 changes: 15 additions & 0 deletions shared/chunk/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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
}
}
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down