Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: reduce memory usage #711

Closed
wants to merge 8 commits into from
Closed
6 changes: 3 additions & 3 deletions src/graphql/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export function checkLimits(args: any = {}, type) {
}

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

space.id = id;
space.private = space.private || false;
Expand Down Expand Up @@ -88,7 +88,7 @@ export function formatSpace({ id, settings, verified, flagged }) {

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

// always return parent and children in child node format
// will be overwritten if other fields than id are requested
Expand Down
37 changes: 20 additions & 17 deletions src/graphql/operations/ranking.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { checkLimits, formatSpace, handleRelatedSpaces, PublicError } from '../helpers';
import log from '../../helpers/log';
import db from '../../helpers/mysql';
import { rankedSpaces } from '../../helpers/spaces';
import { spaces as globalSpaces, rankedSpaceIds } from '../../helpers/spaces';
import { capture } from '@snapshot-labs/snapshot-sentry';

export default async function (_parent, args, context, info) {
Expand All @@ -18,22 +18,25 @@ export default async function (_parent, args, context, info) {
const searchStr = search.toLowerCase();
let searchCategory = category.toLowerCase();
if (searchCategory === 'all') searchCategory = '';

let filteredSpaces = rankedSpaces.filter((space: any) => {
const filteredBySearch =
space.id.includes(searchStr) || space.name?.toLowerCase().includes(searchStr);
const filteredByNetwork = network ? space.networks.includes(network) : true;
if (filteredBySearch && filteredByNetwork) {
// count categories, should not consider searchCategory for counting
space.categories.forEach(category => {
metrics.categories[category] = (metrics.categories[category] || 0) + 1;
});
}

// filter by category if where.category is defined
const filteredByCategory = searchCategory ? space.categories.includes(searchCategory) : true;
return filteredBySearch && filteredByCategory && filteredByNetwork;
});
let filteredSpaces = rankedSpaceIds
.map(id => globalSpaces[id])
.filter((space: any) => {
const filteredBySearch =
space.id.includes(searchStr) || space.name?.toLowerCase().includes(searchStr);
const filteredByNetwork = network ? space.networks.includes(network) : true;
if (filteredBySearch && filteredByNetwork) {
// count categories, should not consider searchCategory for counting
space.categories.forEach(category => {
metrics.categories[category] = (metrics.categories[category] || 0) + 1;
});
}

// filter by category if where.category is defined
const filteredByCategory = searchCategory
? space.categories.includes(searchCategory)
: true;
return filteredBySearch && filteredByCategory && filteredByNetwork;
});

metrics.total = filteredSpaces.length;
filteredSpaces = Array.from(filteredSpaces.slice(skip, skip + first), (space: any) => space.id);
Expand Down
32 changes: 14 additions & 18 deletions src/helpers/spaces.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 let spaces = {};
export const spacesMetadata = {};
export let rankedSpaces: any = [];
export let rankedSpaceIds: string[] = [];
const spaceProposals = {};
const spaceVotes = {};
const spaceFollowers = {};
Expand Down Expand Up @@ -43,29 +43,24 @@ function getPopularity(

function mapSpaces() {
Object.entries(spaces).forEach(([id, space]: any) => {
const verified = space.verified || false;
const flagged = space.flagged || false;
const networks = uniq(
const strategies = uniq(space.strategies?.map(strategy => strategy.name) || []);
space.categories = space.categories ?? [];
space.networks = uniq(
(space.strategies || [])
.map(strategy => strategy?.network || space.network)
.concat(space.network)
);
const strategies = uniq(space.strategies?.map(strategy => strategy.name) || []);
const popularity = getPopularity(id, {
verified,
networks,
verified: space.verified,
networks: space.networks,
strategies
});

spacesMetadata[id] = {
id,
name: space.name,
verified,
flagged,
flagged: space.flagged,
popularity,
private: space.private ?? false,
categories: space.categories ?? [],
networks,
counts: {
activeProposals: spaceProposals[id]?.active || 0,
proposalsCount: spaceProposals[id]?.count || 0,
Expand All @@ -78,13 +73,13 @@ function mapSpaces() {
};
});

rankedSpaces = Object.values(spacesMetadata)
rankedSpaceIds = Object.values(spacesMetadata)
.filter((space: any) => !space.private && !space.flagged)
.sort((a: any, b: any) => b.popularity - a.popularity);

rankedSpaces.forEach((space: any, i: number) => {
spacesMetadata[space.id].rank = i + 1;
});
.sort((a: any, b: any) => b.popularity - a.popularity)
.map((space: any, i: number) => {
space.rank = i + 1;
return space.id;
});
}

async function loadSpaces() {
Expand All @@ -95,6 +90,7 @@ async function loadSpaces() {
s.map(ensSpace => [
ensSpace.id,
{
id: ensSpace.id,
...JSON.parse(ensSpace.settings),
flagged: ensSpace.flagged === 1,
verified: ensSpace.verified === 1
Expand Down