Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
"markdown-it": "^12.3.2",
"mongodb": "6.1.0",
"node-fetch": "^3.3.0",
"pluralize": "^8.0.0",
"socket.io": "^4.8.3"
"pluralize": "^8.0.0"
},
"devDependencies": {
"@types/koa-router": "^7.4.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ const {
getOutputTransferCollection,
getReferendaReferendumCollection,
getChildBountyCollection,
getSubsquareTreasurySpendCollection,
} = require("../../mongo");
const { setOverviewV2, getOverviewV2 } = require("./../store");
const { overviewRoomV2, OVERVIEW_FEED_INTERVAL } = require("../constants");
const util = require("util");
const { calcBestTipProposers } = require("./common/calcBestTipProposers");
const {
calcBestProposalBeneficiary,
Expand All @@ -20,26 +18,6 @@ const { getLatestSymbolPrice } = require("./common/getLatestSymbolPrice");
const { calcToBeAwarded } = require("./common/calcToBeAwarded");
const { calcOutput } = require("./common/calcOutput");
const { calcCount } = require("./common/calcCount");
const { getSubsquareTreasurySpendCollection } = require("../../mongo");

async function feedOverviewV2(io) {
try {
const oldStoreOverview = getOverviewV2();
const overview = await calcOverview();

if (util.isDeepStrictEqual(overview, oldStoreOverview)) {
return;
}

setOverviewV2(overview);

io.to(overviewRoomV2).emit("overview", overview);
} catch (e) {
console.error("feed overview error:", e);
} finally {
setTimeout(feedOverviewV2.bind(null, io), OVERVIEW_FEED_INTERVAL);
}
}

async function calcOverview() {
const proposalCol = await getProposalCollection();
Expand Down Expand Up @@ -161,6 +139,17 @@ async function calcOverview() {
};
}

module.exports = {
feedOverviewV2,
};
const CACHE_TTL_MS = 30 * 1000;
let cachedOverview = null;
let cacheTimestamp = 0;

async function getCachedOverview() {
const now = Date.now();
if (!cachedOverview || now - cacheTimestamp > CACHE_TTL_MS) {
cachedOverview = await calcOverview();
cacheTimestamp = now;
}
return cachedOverview;
}

module.exports = { calcOverview, getCachedOverview };
7 changes: 3 additions & 4 deletions packages/server/src/features/overview/overview.controller.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const { getOverviewV2 } = require("../../websocket/store");
const { getCachedOverview } = require("./calcOverview");

async function getOverview(ctx) {
ctx.body = {
...getOverviewV2(),
};
const overview = await getCachedOverview();
ctx.body = { ...overview };
}

module.exports = {
Expand Down
4 changes: 2 additions & 2 deletions packages/server/src/features/stats/stats.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const {
getInputWeeklyStatsCollection,
getOutputWeeklyStatsCollection,
} = require("../../mongo");
const { getOverview } = require("../../websocket/store");
const { getCachedOverview } = require("../overview/calcOverview");

class StatsController {
async getWeeklyStatsHistory(ctx) {
Expand Down Expand Up @@ -43,7 +43,7 @@ class StatsController {
}

async getTreasuryInOut(ctx) {
const overview = getOverview();
const overview = await getCachedOverview();
if (!overview) {
ctx.body = {};
return;
Expand Down
13 changes: 1 addition & 12 deletions packages/server/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ const Koa = require("koa");
const bodyParser = require("koa-bodyparser");
const logger = require("koa-logger");
const helmet = require("koa-helmet");
const http = require("http");
const cors = require("@koa/cors");
const { initDb } = require("./mongo");
const { initDb: initAdminDb } = require("./mongo-admin");
const { listenAndEmitInfo } = require("./websocket");

const app = new Koa();

Expand All @@ -30,22 +28,13 @@ app.use(async (ctx, next) => {
});

require("./routes")(app);
const server = http.createServer(app.callback());
const io = require("socket.io")(server, {
cors: {
origin: "*",
methods: ["GET", "POST"],
},
});

Promise.all([initDb(), initAdminDb()])
.then(async (db) => {
await listenAndEmitInfo(io);

app.context.db = db;
const port = process.env.PORT || 3213;

server.listen(port, () =>
app.listen(port, () =>
console.log(`✅ The server is running at http://127.0.0.1:${port}/`),
);
})
Expand Down
11 changes: 0 additions & 11 deletions packages/server/src/websocket/constants.js

This file was deleted.

39 changes: 0 additions & 39 deletions packages/server/src/websocket/index.js

This file was deleted.

4 changes: 0 additions & 4 deletions packages/server/src/websocket/overview/index.js

This file was deleted.

173 changes: 0 additions & 173 deletions packages/server/src/websocket/overview/v1.js

This file was deleted.

26 changes: 0 additions & 26 deletions packages/server/src/websocket/status.js

This file was deleted.

Loading
Loading