Skip to content

Commit

Permalink
feat(tools): adding a cron for checking test.graph (#2671)
Browse files Browse the repository at this point in the history
  • Loading branch information
sstraatemans authored Nov 21, 2024
1 parent a2010ad commit 39a43a7
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .changeset/kind-schools-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
9 changes: 9 additions & 0 deletions packages/apps/tools/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ const config = {
},
],
},
{
source: '/api/graph',
headers: [
{
key: 'Cache-Control',
value: 'no-store, max-age=0',
},
],
},
];
},
};
Expand Down
8 changes: 8 additions & 0 deletions packages/apps/tools/src/app/api/graph/route.route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { runJob } from '@/scripts/graphCron';

export async function GET(request: Request) {
await runJob();
return new Response(`Graph cron job`);
}

export const revalidate = 0;
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ export const tokenId = process.env.SLACK_TOKEN;
export const faucetAccount = 'c:Ecwy85aCW3eogZUnIQxknH8tG8uXHM5QiC__jeI0nWA';
export const MINBALANCE = 1000;

//graph
export const MAXBLOCKHEIGHT_DIFFERENCE = 100;

export interface IChainAccount {
balance: number;
chainId: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { creatLowChainsString, lowFaucetChains, runJob } from '..';
import type { IAccount, IChainAccount } from '../constants';
import type { IAccount, IChainAccount } from '../../constants';

describe('faucetCron Utils', () => {
describe('creatLowChainsString', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IAccount } from '../constants';
import type { IAccount } from '../../constants';
import { sendErrorMessage, sendMessage } from '../messages';

const mocks = vi.hoisted(() => {
Expand Down
4 changes: 2 additions & 2 deletions packages/apps/tools/src/scripts/faucetCron/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { IAccount, IChainAccount } from './constants';
import { MINBALANCE, faucetAccount } from './constants';
import type { IAccount, IChainAccount } from './../constants';
import { MINBALANCE, faucetAccount } from './../constants';
import { sendErrorMessage, sendMessage } from './messages';

export const lowFaucetChains = (
Expand Down
4 changes: 2 additions & 2 deletions packages/apps/tools/src/scripts/faucetCron/messages.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { creatLowChainsString, lowFaucetChains } from '.';
import type { IAccount } from './constants';
import { MINBALANCE, channelId, faucetAccount, tokenId } from './constants';
import type { IAccount } from './../constants';
import { MINBALANCE, channelId, faucetAccount, tokenId } from './../constants';

export const sendMessage = async (data: IAccount): Promise<void> => {
const lowChains = lowFaucetChains(
Expand Down
98 changes: 98 additions & 0 deletions packages/apps/tools/src/scripts/graphCron/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { MAXBLOCKHEIGHT_DIFFERENCE } from '../constants';
import { sendErrorMessage } from './messages';

interface IEnvProps {
env: 'testnet04' | 'mainnet01';
chainweb: string;
graphql: string;
}

const countHeightOnChainweb = async (props: IEnvProps): Promise<number> => {
try {
const result = await fetch(props.chainweb);
const data: any = await result.json();
return Object.entries(data.hashes)
.map(([key, val]) => val)
.reduce((acc: number, val: any) => {
return acc + val.height;
}, 0);
} catch (e) {
return parseInt('no Number');
}
};

const countHeightOnGraph = async (props: IEnvProps): Promise<number> => {
const result = await fetch(props.graphql, {
method: 'POST',
headers: {
accept:
'application/graphql-response+json, application/json, multipart/mixed',
'cache-control': 'no-cache',
'content-type': 'application/json',
pragma: 'no-cache',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'cross-site',
},
body: JSON.stringify({
query: `query graphBlockHeight {
lastBlockHeight
}`,
variables: {},
extensions: {},
}),
});

const data = (await result.json()) as any;
return data.data.lastBlockHeight * 20;
};

export const runJobPerEnvironment = async (props: IEnvProps) => {
try {
const totalHeightOnChainWeb = await countHeightOnChainweb(props);
const totalHeightOnGraph = await countHeightOnGraph(props);

if (Number.isNaN(totalHeightOnChainWeb)) {
await sendErrorMessage({
title: `${props.env} chainweb.com fail`,
msg: `We were unable to retrieve the blockheights from chainweb. \n There seems to be an issue with ChainWeb (${props.chainweb})`,
});
return;
}
if (Number.isNaN(totalHeightOnGraph)) {
await sendErrorMessage({
title: `${props.env} graph fail`,
msg: `We were unable to retrieve the blockheights from the test graph. \n There seems to be an issue with test graph (${props.graphql})`,
});
return;
}

if (
Math.abs(totalHeightOnChainWeb - totalHeightOnGraph) >
MAXBLOCKHEIGHT_DIFFERENCE
) {
await sendErrorMessage({
title: `${props.env} graph issue`,
msg: `There is a large difference in the last blockheight between ${props.env} chainweb and graph. \n difference: ${Math.abs(totalHeightOnChainWeb - totalHeightOnGraph)}`,
});
}
} catch (e) {
await sendErrorMessage({
title: `There was a general issue with the ${props.env} graph cron job`,
msg: ``,
});
}
};

export const runJob = async () => {
await runJobPerEnvironment({
env: 'testnet04',
chainweb: 'https://ap1.testnet.chainweb.com/chainweb/0.0/testnet04/cut',
graphql: 'https://graph.testnet.kadena.network/graphql',
});

await runJobPerEnvironment({
env: 'mainnet01',
chainweb: 'https://api.chainweb.com/chainweb/0.0/mainnet01/cut ',
graphql: 'https://graph.kadena.network/graphql',
});
};
36 changes: 36 additions & 0 deletions packages/apps/tools/src/scripts/graphCron/messages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { channelId, tokenId } from './../constants';

export const sendErrorMessage = async ({
title,
msg,
}: {
title: string;
msg: string;
}): Promise<void> => {
await fetch('https://slack.com/api/chat.postMessage', {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8',
Authorization: `Bearer ${tokenId}`,
},
body: JSON.stringify({
channel: `${channelId}`,
blocks: JSON.stringify([
{
type: 'header',
text: {
type: 'plain_text',
text: title ? title : `Error in Graph check! 🚨`,
},
},
{
type: 'section',
text: {
type: 'mrkdwn',
text: `${msg}`,
},
},
]),
}),
});
};
4 changes: 4 additions & 0 deletions packages/apps/tools/vercel.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
{
"path": "/api/faucet",
"schedule": "15 3,15 * * *"
},
{
"path": "/api/graph",
"schedule": "15 4,16 * * *"
}
]
}

0 comments on commit 39a43a7

Please sign in to comment.