Skip to content

Commit

Permalink
Hooked up getCharts
Browse files Browse the repository at this point in the history
  • Loading branch information
heythisischris committed Jul 1, 2024
1 parent 7fde0fd commit f55f44a
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions src/routes/getCharts.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
import { jwtClaims } from '#src/app';
import { db } from '#src/utils';

export const getCharts = async ({ request, reply }) => {
await db.connect();
const report = (await db.query(`SELECT "id", "name", "filters" FROM "reports" WHERE "id" = $1`, [request.query.reportId])).rows?.[0]?.filters;
// const nodeIds = (await db.query(`SELECT "id" FROM "enodes" WHERE "user_id"=$1`, [jwtClaims.sub])).rows;
const report = (await db.query(`SELECT "id", "name", "filters" FROM "reports" WHERE "id" = $1`, [request.query.reportId])).rows?.[0];

const types = ['properties', 'urls', 'messages', 'nodes', 'tags'];
const filters = Object.fromEntries(types.map(obj => [obj, []]));
for (const type of types) {
filters[type].push(...report.filters.filter(obj => obj.type === type).map(obj => obj.value));
}

const urlIds = (await db.query({
text: `SELECT "id" FROM "urls" WHERE "id" = ANY($1::uuid[]) OR "property_id" = ANY($2::uuid[])`,
values: [filters.urls, filters.properties],
})).rows.map(obj => obj.id);

const enodeIds = (await db.query({
text: `SELECT id FROM enodes WHERE url_id = ANY($1::uuid[])`,
values: [urlIds],
})).rows.map(obj => obj.id);

const enodeUpdates = (await db.query({
text: `SELECT created_at as date, equalified FROM enode_updates WHERE enode_id = ANY($1::uuid[]) ORDER BY created_at desc`,
values: [enodeIds],
})).rows;

await db.clean();
return { report };
return {
status: 'success',
result: enodeUpdates,
total: enodeUpdates.length,
};
}

0 comments on commit f55f44a

Please sign in to comment.