Skip to content

Commit

Permalink
feat: add hibernated column to Space (#700)
Browse files Browse the repository at this point in the history
* feat: add hibernating column to Space

* feat: return space's `updated_at` column

* fix: return the space `created_at` field

* fix: rename column `hibernating` to `hibernated`

* fix: update schema

* chore: remove `created_at` filter

* fix: do not return `created_at`

* fix: add the `hibernated` property in query and cache

* fix: improve spaces per status metrics

---------

Co-authored-by: ChaituVR <[email protected]>
  • Loading branch information
wa0x6e and ChaituVR authored Nov 8, 2023
1 parent caf6034 commit 00a29db
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 22 deletions.
15 changes: 10 additions & 5 deletions src/graphql/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function checkLimits(args: any = {}, type) {
return true;
}

export function formatSpace({ id, settings, verified, flagged }) {
export function formatSpace({ id, settings, verified, flagged, hibernated }) {
const spaceMetadata = spacesMetadata[id] || {};
const space = { ...jsonParse(settings, {}), ...spaceMetadata.counts };

Expand Down Expand Up @@ -88,6 +88,7 @@ export function formatSpace({ id, settings, verified, flagged }) {

space.verified = verified ?? null;
space.flagged = flagged ?? null;
space.hibernated = hibernated ?? null;
space.rank = spaceMetadata?.rank ?? null;

// always return parent and children in child node format
Expand Down Expand Up @@ -280,7 +281,8 @@ export function formatProposal(proposal) {
id: proposal.space,
settings: proposal.settings,
verified: proposal.spaceVerified,
flagged: proposal.spaceFlagged
flagged: proposal.spaceFlagged,
hibernated: proposal.spaceHibernated
});
const networkStr = network === 'testnet' ? 'demo.' : '';
proposal.link = `https://${networkStr}snapshot.org/#/${proposal.space.id}/proposal/${proposal.id}`;
Expand All @@ -302,7 +304,8 @@ export function formatVote(vote) {
id: vote.space,
settings: vote.settings,
verified: vote.spaceVerified,
flagged: vote.spaceFlagged
flagged: vote.spaceFlagged,
hibernated: vote.spaceHibernated
});
return vote;
}
Expand All @@ -312,7 +315,8 @@ export function formatFollow(follow) {
id: follow.space,
settings: follow.settings,
verified: follow.spaceVerified,
flagged: follow.spaceFlagged
flagged: follow.spaceFlagged,
hibernated: follow.spaceHibernated
});
return follow;
}
Expand All @@ -322,7 +326,8 @@ export function formatSubscription(subscription) {
id: subscription.space,
settings: subscription.settings,
verified: subscription.spaceVerified,
flagged: subscription.spaceFlagged
flagged: subscription.spaceFlagged,
hibernated: subscription.spaceHibernated
});
return subscription;
}
2 changes: 1 addition & 1 deletion src/graphql/operations/follows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default async function (parent, args) {
if (!['ASC', 'DESC'].includes(orderDirection)) orderDirection = 'DESC';

const query = `
SELECT f.*, spaces.settings, spaces.flagged as spaceFlagged, spaces.verified as spaceVerified FROM follows f
SELECT f.*, spaces.settings, spaces.flagged as spaceFlagged, spaces.verified as spaceVerified, spaces.hibernated as spaceHibernated FROM follows f
INNER JOIN spaces ON spaces.id = f.space
WHERE spaces.settings IS NOT NULL ${queryStr}
ORDER BY ${orderBy} ${orderDirection} LIMIT ?, ?
Expand Down
2 changes: 1 addition & 1 deletion src/graphql/operations/proposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { capture } from '@snapshot-labs/snapshot-sentry';

export default async function (parent, { id }) {
const query = `
SELECT p.*, spaces.settings, spaces.flagged as spaceFlagged, spaces.verified as spaceVerified FROM proposals p
SELECT p.*, spaces.settings, spaces.flagged as spaceFlagged, spaces.verified as spaceVerified, spaces.hibernated as spaceHibernated FROM proposals p
INNER JOIN spaces ON spaces.id = p.space
WHERE p.id = ? AND spaces.settings IS NOT NULL
LIMIT 1
Expand Down
2 changes: 1 addition & 1 deletion src/graphql/operations/proposals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export default async function (parent, args) {
if (!['ASC', 'DESC'].includes(orderDirection)) orderDirection = 'DESC';

const query = `
SELECT p.*, spaces.settings, spaces.flagged as spaceFlagged, spaces.verified as spaceVerified FROM proposals p
SELECT p.*, spaces.settings, spaces.flagged as spaceFlagged, spaces.verified as spaceVerified, spaces.hibernated as spaceHibernated FROM proposals p
INNER JOIN spaces ON spaces.id = p.space
WHERE spaces.settings IS NOT NULL ${queryStr} ${searchSql}
ORDER BY ${orderBy} ${orderDirection}, p.id ASC LIMIT ?, ?
Expand Down
2 changes: 1 addition & 1 deletion src/graphql/operations/subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default async function (parent, args) {
let subscriptions: any[] = [];

const query = `
SELECT s.*, spaces.settings, spaces.flagged as spaceFlagged, spaces.verified as spaceVerified FROM subscriptions s
SELECT s.*, spaces.settings, spaces.flagged as spaceFlagged, spaces.verified as spaceVerified, spaces.hibernated as spaceHibernated FROM subscriptions s
INNER JOIN spaces ON spaces.id = s.space
WHERE spaces.settings IS NOT NULL ${queryStr}
ORDER BY ${orderBy} ${orderDirection} LIMIT ?, ?
Expand Down
4 changes: 2 additions & 2 deletions src/graphql/operations/vote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { capture } from '@snapshot-labs/snapshot-sentry';
export default async function (parent, { id }, context, info) {
const requestedFields = info ? graphqlFields(info) : {};
const query = `
SELECT v.*, spaces.settings, spaces.flagged as spaceFlagged, spaces.verified as spaceVerified FROM votes v
SELECT v.*, spaces.settings, spaces.flagged as spaceFlagged, spaces.verified as spaceVerified, spaces.hibernated as spaceHibernated FROM votes v
INNER JOIN spaces ON spaces.id = v.space
WHERE v.id = ? AND spaces.settings IS NOT NULL
LIMIT 1
Expand All @@ -18,7 +18,7 @@ export default async function (parent, { id }, context, info) {
if (requestedFields.proposal && result?.proposal) {
const proposalId = result.proposal;
const query = `
SELECT p.*, spaces.settings, spaces.flagged as spaceFlagged, spaces.verified as spaceVerified FROM proposals p
SELECT p.*, spaces.settings, spaces.flagged as spaceFlagged, spaces.verified as spaceVerified, spaces.hibernated as spaceHibernated FROM proposals p
INNER JOIN spaces ON spaces.id = p.space
WHERE spaces.settings IS NOT NULL AND p.id = ?
`;
Expand Down
2 changes: 1 addition & 1 deletion src/graphql/operations/votes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ async function query(parent, args, context?, info?) {
if (requestedFields.proposal && votes.length > 0) {
const proposalIds = votes.map(vote => vote.proposal);
const query = `
SELECT p.*, spaces.settings, spaces.flagged as spaceFlagged, spaces.verified as spaceVerified FROM proposals p
SELECT p.*, spaces.settings, spaces.flagged as spaceFlagged, spaces.verified as spaceVerified, spaces.hibernated as spaceHibernated FROM proposals p
INNER JOIN spaces ON spaces.id = p.space
WHERE spaces.settings IS NOT NULL AND p.id IN (?)
`;
Expand Down
1 change: 1 addition & 0 deletions src/graphql/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ type Space {
template: String
verified: Boolean
flagged: Boolean
hibernated: Boolean
rank: Float
created: Int!
}
Expand Down
14 changes: 6 additions & 8 deletions src/helpers/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,12 @@ new client.Gauge({
help: 'Number of spaces per status',
labelNames: ['status'],
async collect() {
this.set(
{ status: 'verified' },
Object.values(spacesMetadata).filter((s: any) => s.verified).length
);
this.set(
{ status: 'flagged' },
(await db.queryAsync('SELECT COUNT(id) as count FROM spaces WHERE flagged = 1'))[0].count
);
['verified', 'flagged', 'hibernated'].forEach(async status => {
this.set(
{ status },
(await db.queryAsync(`SELECT COUNT(id) as count FROM spaces WHERE ${status} = 1`))[0].count
);
});
}
});

Expand Down
2 changes: 2 additions & 0 deletions src/helpers/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ CREATE TABLE spaces (
verified INT NOT NULL DEFAULT '0',
deleted INT NOT NULL DEFAULT '0',
flagged INT NOT NULL DEFAULT '0',
hibernated INT NOT NULL DEFAULT '0',
created BIGINT NOT NULL,
updated BIGINT NOT NULL,
PRIMARY KEY (id),
INDEX name (name),
INDEX verified (verified),
INDEX flagged (flagged),
INDEX hibernated (hibernated),
INDEX deleted (deleted),
INDEX created (created),
INDEX updated (updated)
Expand Down
7 changes: 5 additions & 2 deletions src/helpers/spaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function mapSpaces() {
Object.entries(spaces).forEach(([id, space]: any) => {
const verified = space.verified || false;
const flagged = space.flagged || false;
const hibernated = space.hibernated || false;
const networks = uniq(
(space.strategies || [])
.map(strategy => strategy?.network || space.network)
Expand All @@ -62,6 +63,7 @@ function mapSpaces() {
name: space.name,
verified,
flagged,
hibernated,
popularity,
private: space.private ?? false,
categories: space.categories ?? [],
Expand Down Expand Up @@ -89,15 +91,16 @@ function mapSpaces() {

async function loadSpaces() {
const query =
'SELECT id, settings, flagged, verified FROM spaces WHERE deleted = 0 ORDER BY id ASC';
'SELECT id, settings, flagged, verified, hibernated FROM spaces WHERE deleted = 0 ORDER BY id ASC';
const s = await db.queryAsync(query);
spaces = Object.fromEntries(
s.map(ensSpace => [
ensSpace.id,
{
...JSON.parse(ensSpace.settings),
flagged: ensSpace.flagged === 1,
verified: ensSpace.verified === 1
verified: ensSpace.verified === 1,
hibernated: ensSpace.hibernated === 1
}
])
);
Expand Down

0 comments on commit 00a29db

Please sign in to comment.