Skip to content

Commit

Permalink
feat: probe adoption expiration (#36)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Kolárik <[email protected]>
  • Loading branch information
alexey-yarmosh and MartinKolarik authored Oct 29, 2024
1 parent 4d6fdd1 commit 6d86359
Show file tree
Hide file tree
Showing 21 changed files with 6,658 additions and 112 deletions.
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ COPY src/extensions/operations/adopted-probes-credits-cron-handler/package.json
COPY src/extensions/operations/adopted-probes-status-cron-handler/package.json src/extensions/operations/adopted-probes-status-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/
COPY src/extensions/operations/remove-expired-adoptions-cron-handler/package.json src/extensions/operations/remove-expired-adoptions-cron-handler/
COPY src/extensions/operations/sponsors-cron-handler/package.json src/extensions/operations/sponsors-cron-handler/
# END: EXTENSIONS-BUILD-BLOCK

RUN pnpm install
COPY src src
RUN pnpm -r build

FROM directus/directus:10.12.1
FROM directus/directus:11.1.1

# Update via `npm run docker:ls:update`
# START: EXTENSIONS-RUN-BLOCK
Expand Down Expand Up @@ -80,6 +81,8 @@ COPY --from=builder /builder/src/extensions/operations/gh-webhook-handler/dist/*
COPY --from=builder /builder/src/extensions/operations/gh-webhook-handler/package.json /directus/extensions/gh-webhook-handler/
COPY --from=builder /builder/src/extensions/operations/remove-banned-users-cron-handler/dist/* /directus/extensions/remove-banned-users-cron-handler/dist/
COPY --from=builder /builder/src/extensions/operations/remove-banned-users-cron-handler/package.json /directus/extensions/remove-banned-users-cron-handler/
COPY --from=builder /builder/src/extensions/operations/remove-expired-adoptions-cron-handler/dist/* /directus/extensions/remove-expired-adoptions-cron-handler/dist/
COPY --from=builder /builder/src/extensions/operations/remove-expired-adoptions-cron-handler/package.json /directus/extensions/remove-expired-adoptions-cron-handler/
COPY --from=builder /builder/src/extensions/operations/sponsors-cron-handler/dist/* /directus/extensions/sponsors-cron-handler/dist/
COPY --from=builder /builder/src/extensions/operations/sponsors-cron-handler/package.json /directus/extensions/sponsors-cron-handler/
# END: EXTENSIONS-RUN-BLOCK
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
"init": "./scripts/init.sh",
"init:dev": "./scripts/init.sh --dev",
"docker:ls:update": "node ./scripts/docker-ls.js",
"migrate": "rm -rf ./extensions/migrations/* && mkdir -p ./extensions/migrations/ && cp -rp ./src/extensions/migrations/* ./extensions/migrations/ && cp -p ./src/extensions/migration-utils.js ./extensions/ && dotenv -- npx directus@10.12.1 database migrate:latest",
"migrate": "rm -rf ./extensions/migrations/* && mkdir -p ./extensions/migrations/ && cp -rp ./src/extensions/migrations/* ./extensions/migrations/ && cp -p ./src/extensions/migration-utils.js ./extensions/ && dotenv -- npx directus@11.1.1 database migrate:latest",
"seed": "NODE_ENV=development dotenv -- knex seed:run",
"schema:apply": "npx directus@10.12.1 schema apply --yes snapshots/collections-schema.yml",
"schema:snapshot": "npx directus@10.12.1 schema snapshot --yes snapshots/collections-schema.yml",
"schema:apply": "npx directus@11.1.1 schema apply --yes snapshots/collections-schema.yml",
"schema:snapshot": "npx directus@11.1.1 schema snapshot --yes snapshots/collections-schema.yml",
"prepare": "husky install || echo 'Failed to install husky'; rm -f .eslintcache",
"build:in-sequence": "grep -E 'AS builder-[0-9]+' Dockerfile | awk -F 'AS ' '{print $2}' | xargs -r -P 1 -I STAGE docker build --target STAGE ."
},
Expand Down
1,014 changes: 909 additions & 105 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion snapshots/collections-schema.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
version: 1
directus: 10.12.1
directus: 11.1.1
vendor: mysql
collections:
- collection: gp_adopted_probes
Expand Down
71 changes: 71 additions & 0 deletions src/extensions/migration-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const DIRECTUS_URL = process.env.DIRECTUS_URL;
const ADMIN_ACCESS_TOKEN = process.env.ADMIN_ACCESS_TOKEN;
const USER_ROLE_NAME = 'User';

// MANAGE USER PERMISSIONS

async function getUserRoleId () {
const URL = `${DIRECTUS_URL}/roles?filter[name][_eq]=${USER_ROLE_NAME}&access_token=${ADMIN_ACCESS_TOKEN}`;
const response = await fetch(URL).then((response) => {
Expand Down Expand Up @@ -68,3 +70,72 @@ export async function updatePermissions (readPermissions, fieldsToAdd, fieldsToR
});
return response.data;
}

// ADD FLOWS

export async function createFlow (flowId, config) {
const URL = `${DIRECTUS_URL}/flows?access_token=${ADMIN_ACCESS_TOKEN}`;
const response = await fetch(URL, {
method: 'POST',
body: JSON.stringify({
id: flowId,
status: 'active',
accountability: 'all',
...config,
}),
headers: {
'Content-Type': 'application/json',
},
}).then((response) => {
if (!response.ok) {
throw new Error(`Fetch request failed. Status: ${response.status}`);
}

return response.json();
});
return response.data;
}

export async function createOperation (flowId, config) {
const URL = `${DIRECTUS_URL}/operations?access_token=${ADMIN_ACCESS_TOKEN}`;
const response = await fetch(URL, {
method: 'POST',
body: JSON.stringify({
flow: flowId,
position_x: 19,
position_y: 1,
options: {},
...config,
}),
headers: {
'Content-Type': 'application/json',
},
}).then((response) => {
if (!response.ok) {
throw new Error(`Fetch request failed. Status: ${response.status}`);
}

return response.json();
});
return response.data;
}

export async function assignOperationToFlow (flowId, operationId) {
const URL = `${DIRECTUS_URL}/flows/${flowId}?access_token=${ADMIN_ACCESS_TOKEN}`;
const response = await fetch(URL, {
method: 'PATCH',
body: JSON.stringify({
operation: operationId,
}),
headers: {
'Content-Type': 'application/json',
},
}).then((response) => {
if (!response.ok) {
throw new Error(`Fetch request failed. Status: ${response.status}`);
}

return response.json();
});
return response.data;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { randomUUID } from 'node:crypto';
import { createFlow, createOperation, assignOperationToFlow } from '../migration-utils.js';

export async function up () {
const flowId = randomUUID(); // Flow id needs to be a uuid, as Directus throws otherwise. This is a random value.

await createFlow(flowId, {
name: 'Expired probes CRON',
description: 'Removes expired probe adoptions from directus',
trigger: 'schedule',
options: {
cron: '0 0 * * *',
},
});

const operation = await createOperation(flowId, {
name: 'Remove expired adoptions CRON handler',
key: 'remove_expired_adoptions_cron_handler',
type: 'remove-expired-adoptions-cron-handler',
});

await assignOperationToFlow(flowId, operation.id);
console.log('Expired probes CRON handler added');
}

export async function down () {
console.log('There is no down operation for that migration.');
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { OperationContext } from '@directus/extensions';

type AdoptedProbe = {
id: number;
id: string;
name: string | null;
ip: string;
onlineTimesToday: number;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { OperationContext } from '@directus/extensions';

export type AdoptedProbe = {
id: number;
id: string;
status: string;
onlineTimesToday: number;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
node_modules
dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extension": [
"ts"
],
"node-option": [
"experimental-specifier-resolution=node",
"loader=ts-node/esm",
"no-experimental-fetch"
],
"spec": "test/**/*.ts"
}
Loading

0 comments on commit 6d86359

Please sign in to comment.