From 2313dac04d3b0584a93245643a173959d8a7e1a0 Mon Sep 17 00:00:00 2001 From: Wan <495709+wa0x6e@users.noreply.github.com> Date: Wed, 9 Oct 2024 17:26:37 +0900 Subject: [PATCH] feat: add new options endpoint (#940) * feat: add new options endpoint * fix: cache result for 2 minutes * Update src/graphql/operations/options.ts * fix: fix wrong interval value --------- Co-authored-by: Chaitanya --- src/graphql/operations/index.ts | 2 ++ src/graphql/operations/options.ts | 32 +++++++++++++++++++++++++++++++ src/graphql/schema.gql | 7 +++++++ src/helpers/schema.sql | 6 ++++++ 4 files changed, 47 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..7bc787c9 --- /dev/null +++ b/src/graphql/operations/options.ts @@ -0,0 +1,32 @@ +import { capture } from '@snapshot-labs/snapshot-sentry'; +import snapshot from '@snapshot-labs/snapshot.js'; +import log from '../../helpers/log'; +import db from '../../helpers/mysql'; + +const RUN_INTERVAL = 120e3; + +let options = []; + +export default async function () { + return options; +} + +async function loadOptions() { + options = await db.queryAsync('SELECT s.* FROM options s'); +} + +async function run() { + try { + log.info('[options] Start options refresh'); + await loadOptions(); + log.info(`[options] ${options.length} options reloaded`); + log.info('[options] End options refresh'); + } catch (e: any) { + capture(e); + log.error(`[options] failed to refresh options, ${JSON.stringify(e)}`); + } + await snapshot.utils.sleep(RUN_INTERVAL); + run(); +} + +run(); diff --git a/src/graphql/schema.gql b/src/graphql/schema.gql index afc6f2c1..dd6ee209 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 { @@ -641,3 +643,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 eced9c00..d863e088 100644 --- a/src/helpers/schema.sql +++ b/src/helpers/schema.sql @@ -184,3 +184,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) +);