From 7e3abddf2060e8877553a036bba6cd4cb91d2b7f Mon Sep 17 00:00:00 2001 From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com> Date: Wed, 18 Oct 2023 15:41:28 +0900 Subject: [PATCH] fix: get `validation` data from DB directly --- src/graphql/operations/validations.ts | 29 +++++++++++++++++---------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/graphql/operations/validations.ts b/src/graphql/operations/validations.ts index 24bca011..ca92bbc8 100644 --- a/src/graphql/operations/validations.ts +++ b/src/graphql/operations/validations.ts @@ -1,13 +1,20 @@ -import { spaces } from '../../helpers/spaces'; +import { capture } from '@snapshot-labs/snapshot-sentry'; +import db from '../../helpers/mysql'; -export default function () { - const validations = {}; - Object.values(spaces).forEach((space: any) => { - if (space.validation) - validations[space.validation.name] = (validations[space.validation.name] || 0) + 1; - }); - return Object.entries(validations).map(validation => ({ - id: validation[0], - spacesCount: validation[1] - })); +export default async function () { + const query = ` + SELECT + COALESCE(JSON_UNQUOTE(JSON_EXTRACT(settings, '$.validation.name')), '') AS validation, + COUNT(id) as spacesCount + FROM spaces + GROUP BY validation + ORDER BY spacesCount DESC + `; + + try { + return (await db.queryAsync(query)).map(v => ({ ...v, id: v.validation })); + } catch (e: any) { + capture(e); + return Promise.reject(new Error('request failed')); + } }