Skip to content

Commit

Permalink
feat: rename probe status check operation
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-yarmosh committed Feb 18, 2025
1 parent 69abbdc commit 5363f1a
Show file tree
Hide file tree
Showing 19 changed files with 54 additions and 43 deletions.
3 changes: 1 addition & 2 deletions .env.development.example
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ GEONAMES_USERNAME=
# App
CREDITS_PER_DOLLAR=2000
CREDITS_PER_ADOPTED_PROBE_DAY=150
ADOPTED_PROBES_CHECK_TIME_MAX_DEVIATION_MINS=5
# To trigger credits assignment probe need to be online >20 hours during the day. Current "Adopted probes status CRON" is "*/10 * * * *", which is 6 times an hour
# To trigger credits assignment probe need to be online >20 hours during the day. Current "Probes status CRON" is "*/10 * * * *", which is 6 times an hour
# So required value is 20 * 6 = 120
ADOPTED_PROBES_REQUIRED_ONLINE_TIMES=120
TARGET_NODE_VERSION=v20.13.0 # Same format as in gp_probes.nodeVersion
Expand Down
3 changes: 1 addition & 2 deletions .env.e2e.example
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ GEONAMES_USERNAME=
# App
CREDITS_PER_DOLLAR=2000
CREDITS_PER_ADOPTED_PROBE_DAY=150
ADOPTED_PROBES_CHECK_TIME_MAX_DEVIATION_MINS=5
# To trigger credits assignment probe need to be online >20 hours during the day. Current "Adopted probes status CRON" is "*/10 * * * *", which is 6 times an hour
# To trigger credits assignment probe need to be online >20 hours during the day. Current "Probes status CRON" is "*/10 * * * *", which is 6 times an hour
# So required value is 20 * 6 = 120
ADOPTED_PROBES_REQUIRED_ONLINE_TIMES=120
TARGET_NODE_VERSION=v20.13.0 # Same format as in gp_probes.nodeVersion
Expand Down
3 changes: 1 addition & 2 deletions .env.production.example
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ GEONAMES_USERNAME=
# App
CREDITS_PER_DOLLAR=2000
CREDITS_PER_ADOPTED_PROBE_DAY=150
ADOPTED_PROBES_CHECK_TIME_MAX_DEVIATION_MINS=5
# To trigger credits assignment probe need to be online >20 hours during the day. Current "Adopted probes status CRON" is "*/10 * * * *", which is 6 times an hour
# To trigger credits assignment probe need to be online >20 hours during the day. Current "Probes status CRON" is "*/10 * * * *", which is 6 times an hour
# So required value is 20 * 6 = 120
ADOPTED_PROBES_REQUIRED_ONLINE_TIMES=120
TARGET_NODE_VERSION=v20.13.0 # Same format as in gp_probes.nodeVersion
Expand Down
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ COPY src/extensions/interfaces/token/package.json src/extensions/interfaces/toke
COPY src/extensions/lib/package.json src/extensions/lib/
COPY src/extensions/modules/probes-adapter/package.json src/extensions/modules/probes-adapter/
COPY src/extensions/operations/adopted-probes-credits-cron-handler/package.json src/extensions/operations/adopted-probes-credits-cron-handler/
COPY src/extensions/operations/adopted-probes-status-cron-handler/package.json src/extensions/operations/adopted-probes-status-cron-handler/
COPY src/extensions/operations/probes-status-cron-handler/package.json src/extensions/operations/probes-status-cron-handler/
COPY src/extensions/operations/check-outdated-firmware-cron-handler/package.json src/extensions/operations/check-outdated-firmware-cron-handler/
COPY src/extensions/operations/gh-webhook-handler/package.json src/extensions/operations/gh-webhook-handler/
COPY src/extensions/operations/remove-banned-users-cron-handler/package.json src/extensions/operations/remove-banned-users-cron-handler/
Expand Down Expand Up @@ -86,8 +86,8 @@ COPY --from=builder /builder/src/extensions/modules/probes-adapter/dist/* /direc
COPY --from=builder /builder/src/extensions/modules/probes-adapter/package.json /directus/extensions/probes-adapter/
COPY --from=builder /builder/src/extensions/operations/adopted-probes-credits-cron-handler/dist/* /directus/extensions/adopted-probes-credits-cron-handler/dist/
COPY --from=builder /builder/src/extensions/operations/adopted-probes-credits-cron-handler/package.json /directus/extensions/adopted-probes-credits-cron-handler/
COPY --from=builder /builder/src/extensions/operations/adopted-probes-status-cron-handler/dist/* /directus/extensions/adopted-probes-status-cron-handler/dist/
COPY --from=builder /builder/src/extensions/operations/adopted-probes-status-cron-handler/package.json /directus/extensions/adopted-probes-status-cron-handler/
COPY --from=builder /builder/src/extensions/operations/probes-status-cron-handler/dist/* /directus/extensions/probes-status-cron-handler/dist/
COPY --from=builder /builder/src/extensions/operations/probes-status-cron-handler/package.json /directus/extensions/probes-status-cron-handler/
COPY --from=builder /builder/src/extensions/operations/check-outdated-firmware-cron-handler/dist/* /directus/extensions/check-outdated-firmware-cron-handler/dist/
COPY --from=builder /builder/src/extensions/operations/check-outdated-firmware-cron-handler/package.json /directus/extensions/check-outdated-firmware-cron-handler/
COPY --from=builder /builder/src/extensions/operations/gh-webhook-handler/dist/* /directus/extensions/gh-webhook-handler/dist/
Expand Down
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const DIRECTUS_URL = process.env.DIRECTUS_URL;
const ADMIN_ACCESS_TOKEN = process.env.ADMIN_ACCESS_TOKEN;
const FLOW_ID = '284f22f9-0233-44ee-a9e1-503e9a2e3830'; // Flow id needs to be a uuid, as Directus throws otherwise. This is a random value.

export const FLOW_ID = '284f22f9-0233-44ee-a9e1-503e9a2e3830'; // Flow id needs to be a uuid, as Directus throws otherwise. This is a random value.

async function createFlow () {
const URL = `${DIRECTUS_URL}/flows?access_token=${ADMIN_ACCESS_TOKEN}`;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { FLOW_ID } from './20231213GP-add-adopted-probes-status-cron-handler.js';

export async function up (knex) {
await knex('directus_operations').where({ type: 'adopted-probes-status-cron-handler' }).update({
name: 'Probes status CRON handler',
key: 'probes_status_cron_handler',
type: 'probes-status-cron-handler',
});

await knex('directus_flows').where({ id: FLOW_ID }).update({
name: 'Probes status CRON',
description: 'Checks online status of the probes',
});

console.log('Renamed probe status check operation');
}

export async function down () {
console.log('There is no down operation for that migration.');
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "adopted-probes-status-cron-handler",
"name": "probes-status-cron-handler",
"description": "Please enter a description for your extension",
"icon": "extension",
"version": "1.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { OperationContext } from '@directus/extensions';
import { getProbes, increaseOnlineTimes } from '../repositories/directus.js';

export const checkOnlineStatus = async (context: OperationContext) => {
const probes = await getProbes(context);
const onlineProbes = probes.filter(({ status }) => status === 'ready');

const updatedIds = await increaseOnlineTimes(onlineProbes, context);
return updatedIds;
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,13 @@ import _ from 'lodash';
import { checkOnlineStatus } from './actions/check-online-status.js';

export default defineOperationApi({
id: 'adopted-probes-status-cron-handler',
id: 'probes-status-cron-handler',
handler: async (_operationData, context: OperationContext) => {
const maxDeviation = parseFloat(context.env.ADOPTED_PROBES_CHECK_TIME_MAX_DEVIATION_MINS);

if (!maxDeviation) {
throw new Error('ADOPTED_PROBES_CHECK_TIME_MAX_DEVIATION_MINS was not provided');
}

const timeOffset = _.random(0, maxDeviation * 60 * 1000);
const timeOffset = _.random(0, 5 * 60 * 1000);
await setTimeout(timeOffset);

const onlineIds = await checkOnlineStatus(context);

return onlineIds.length ? `Online adopted probes ids: ${onlineIds.toString()}` : 'No adopted online probes';
return onlineIds.length ? `Online probes ids: ${onlineIds.toString()}` : 'No online probes';
},
});
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { defineOperationApp } from '@directus/extensions-sdk';

export default defineOperationApp({
id: 'adopted-probes-status-cron-handler',
name: 'Adopted probes status CRON handler',
id: 'probes-status-cron-handler',
name: 'Probes status CRON handler',
icon: 'schedule',
description: 'CRON job to check if adopted probes are online.',
description: 'CRON job to check if probes are online.',
overview: () => [],
options: [],
});
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import type { OperationContext } from '@directus/extensions';

export type AdoptedProbe = {
export type Probe = {
id: string;
status: string;
onlineTimesToday: number;
}

export const getAdoptedProbes = async ({ services, database, getSchema }: OperationContext) => {
export const getProbes = async ({ services, database, getSchema }: OperationContext) => {
const { ItemsService } = services;

const itemsService = new ItemsService('gp_probes', {
schema: await getSchema({ database }),
knex: database,
});

const result = await itemsService.readByQuery({}) as AdoptedProbe[];
const result = await itemsService.readByQuery({}) as Probe[];
return result;
};

export const increaseOnlineTimes = async (adoptedProbes: AdoptedProbe[], { services, database, getSchema }: OperationContext) => {
if (adoptedProbes.length === 0) {
export const increaseOnlineTimes = async (probes: Probe[], { services, database, getSchema }: OperationContext) => {
if (probes.length === 0) {
return [];
}

Expand All @@ -30,7 +30,7 @@ export const increaseOnlineTimes = async (adoptedProbes: AdoptedProbe[], { servi
knex: database,
});

const updatedIds = await itemsService.updateBatch(adoptedProbes.map(({ id, onlineTimesToday }) => ({
const updatedIds = await itemsService.updateBatch(probes.map(({ id, onlineTimesToday }) => ({
id,
onlineTimesToday: onlineTimesToday + 1,
})), { emitEvents: false }) as number[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import { expect } from 'chai';
import * as sinon from 'sinon';
import { checkOnlineStatus } from '../src/actions/check-online-status.js';

describe('Adopted probes status cron handler', () => {
describe('Probes status cron handler', () => {
const database = {} as OperationContext['database'];
const accountability = {} as OperationContext['accountability'];
const logger = console.log as unknown as OperationContext['logger'];
const getSchema = (() => Promise.resolve({})) as OperationContext['getSchema'];
const env = {
GLOBALPING_URL: 'https://api.globalping.io/v1',
GP_SYSTEM_KEY: 'system',
ADOPTED_PROBES_CHECK_TIME_MAX_DEVIATION_MINS: '5',
};

const data = {};
Expand Down Expand Up @@ -65,7 +64,7 @@ describe('Adopted probes status cron handler', () => {
expect(result).to.deep.equal([ 1 ]);
});

it('should not call "updateBatch" if adopted probe is not connected to gp', async () => {
it('should not call "updateBatch" if probe is not connected to gp', async () => {
readByQuery.resolves([{
id: '1',
status: 'offline',
Expand Down

0 comments on commit 5363f1a

Please sign in to comment.