From b1718db188566baa464a39d1913471d0447e717d Mon Sep 17 00:00:00 2001 From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com> Date: Tue, 8 Oct 2024 14:46:32 +0400 Subject: [PATCH 1/4] feat: add new options endpoint --- src/graphql/operations/index.ts | 2 ++ src/graphql/operations/options.ts | 13 +++++++++++++ src/graphql/schema.gql | 7 +++++++ src/helpers/schema.sql | 6 ++++++ 4 files changed, 28 insertions(+) create mode 100644 src/graphql/operations/options.ts diff --git a/src/graphql/operations/index.ts b/src/graphql/operations/index.ts index d9e7e250..1d79c9cb 100644 --- a/src/graphql/operations/index.ts +++ b/src/graphql/operations/index.ts @@ -3,6 +3,7 @@ import follows from './follows'; import leaderboards from './leaderboards'; import messages from './messages'; import networks from './networks'; +import options from './options'; import plugins from './plugins'; import proposal from './proposal'; import proposals from './proposals'; @@ -36,6 +37,7 @@ export default { subscriptions, skins, networks, + options, validations, plugins, strategies, diff --git a/src/graphql/operations/options.ts b/src/graphql/operations/options.ts new file mode 100644 index 00000000..aa75aaa3 --- /dev/null +++ b/src/graphql/operations/options.ts @@ -0,0 +1,13 @@ +import { capture } from '@snapshot-labs/snapshot-sentry'; +import log from '../../helpers/log'; +import db from '../../helpers/mysql'; + +export default async function (_, args) { + try { + return await db.queryAsync('SELECT s.* FROM options s'); + } catch (e: any) { + log.error(`[graphql] options, ${JSON.stringify(e)}`); + capture(e, { args }); + return Promise.reject(new Error('request failed')); + } +} diff --git a/src/graphql/schema.gql b/src/graphql/schema.gql index 83b79c65..060a2b68 100644 --- a/src/graphql/schema.gql +++ b/src/graphql/schema.gql @@ -110,6 +110,8 @@ type Query { orderBy: String orderDirection: OrderDirection ): [Leaderboard] + + options: [Option] } input SpaceWhere { @@ -640,3 +642,8 @@ type Leaderboard { votesCount: Int lastVote: Int } + +type Option { + name: String + value: String +} diff --git a/src/helpers/schema.sql b/src/helpers/schema.sql index 78a2eec2..4d4a052b 100644 --- a/src/helpers/schema.sql +++ b/src/helpers/schema.sql @@ -183,3 +183,9 @@ CREATE TABLE leaderboard ( INDEX proposal_count (proposal_count), INDEX last_vote (last_vote) ); + +CREATE TABLE options ( + name VARCHAR(100) NOT NULL, + value VARCHAR(100) NOT NULL, + PRIMARY KEY (name) +); From 00d8acd6f03b56f41baa436456e49ac9ae8fc00c Mon Sep 17 00:00:00 2001 From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com> Date: Tue, 8 Oct 2024 17:37:24 +0400 Subject: [PATCH 2/4] fix: cache result for 2 minutes --- src/graphql/operations/options.ts | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/graphql/operations/options.ts b/src/graphql/operations/options.ts index aa75aaa3..d3c71a3b 100644 --- a/src/graphql/operations/options.ts +++ b/src/graphql/operations/options.ts @@ -1,13 +1,32 @@ +import snapshot from '@snapshot-labs/snapshot.js'; import { capture } from '@snapshot-labs/snapshot-sentry'; import log from '../../helpers/log'; import db from '../../helpers/mysql'; -export default async function (_, args) { +const RUN_INTERVAL = 12e3; + +let options = []; + +export default async function () { + return options; +} + +async function loadOptions() { + options = await db.queryAsync('SELECT s.* FROM options s'); +} + +async function run() { try { - return await db.queryAsync('SELECT s.* FROM options s'); + log.info('[options] Start options refresh'); + await loadOptions(); + log.info(`[options] ${options.length} options reloaded`); + log.info('[options] End options refresh'); } catch (e: any) { - log.error(`[graphql] options, ${JSON.stringify(e)}`); - capture(e, { args }); - return Promise.reject(new Error('request failed')); + capture(e); + log.error(`[options] failed to refresh options, ${JSON.stringify(e)}`); } + await snapshot.utils.sleep(RUN_INTERVAL); + run(); } + +run(); From c3e48c9320404b9f3ee3c59d9a55e26d7ceb43e0 Mon Sep 17 00:00:00 2001 From: Chaitanya Date: Wed, 9 Oct 2024 11:54:33 +0530 Subject: [PATCH 3/4] Update src/graphql/operations/options.ts --- src/graphql/operations/options.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/graphql/operations/options.ts b/src/graphql/operations/options.ts index d3c71a3b..c89d4a24 100644 --- a/src/graphql/operations/options.ts +++ b/src/graphql/operations/options.ts @@ -1,5 +1,5 @@ -import snapshot from '@snapshot-labs/snapshot.js'; import { capture } from '@snapshot-labs/snapshot-sentry'; +import snapshot from '@snapshot-labs/snapshot.js'; import log from '../../helpers/log'; import db from '../../helpers/mysql'; From 06c36bffe8f3cc84527daa5de74e75e9f50319e2 Mon Sep 17 00:00:00 2001 From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com> Date: Wed, 9 Oct 2024 12:25:27 +0400 Subject: [PATCH 4/4] fix: fix wrong interval value --- src/graphql/operations/options.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/graphql/operations/options.ts b/src/graphql/operations/options.ts index c89d4a24..7bc787c9 100644 --- a/src/graphql/operations/options.ts +++ b/src/graphql/operations/options.ts @@ -3,7 +3,7 @@ import snapshot from '@snapshot-labs/snapshot.js'; import log from '../../helpers/log'; import db from '../../helpers/mysql'; -const RUN_INTERVAL = 12e3; +const RUN_INTERVAL = 120e3; let options = [];