Skip to content

Commit

Permalink
fix statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
tombertrand committed Jan 14, 2024
1 parent 9a4b012 commit 4b82647
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 112 deletions.
4 changes: 2 additions & 2 deletions server/api/representative.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { nodeCache } = require("../client/cache");
const { REPRESENTATIVE, EXPIRE_1W } = require("../constants");
const { REPRESENTATIVE, EXPIRE_7D } = require("../constants");
const { rpc } = require("../rpc");
const { Sentry } = require("../sentry");

Expand Down Expand Up @@ -51,7 +51,7 @@ const setRepresentatives = async ({ metrics, peers }) => {
Sentry.captureException(err);
}

nodeCache.set(REPRESENTATIVE, representatives, EXPIRE_1W);
nodeCache.set(REPRESENTATIVE, representatives, EXPIRE_7D);
};

module.exports = {
Expand Down
6 changes: 3 additions & 3 deletions server/client/mongo/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const {
EXPIRE_1M,
EXPIRE_48H,
EXPIRE_7D,
EXPIRE_14D,
// EXPIRE_14D,
TOTAL_CONFIRMATIONS_COLLECTION,
TOTAL_VOLUME_COLLECTION,
LARGE_TRANSACTIONS,
Expand Down Expand Up @@ -42,15 +42,15 @@ async function createIndexes() {
if (!indexExists3) {
database
.collection(TOTAL_CONFIRMATIONS_COLLECTION)
.createIndex({ createdAt: 1 }, { expireAfterSeconds: EXPIRE_14D, ...extraOptions });
.createIndex({ createdAt: 1 }, { expireAfterSeconds: EXPIRE_1M, ...extraOptions });
console.log("Index TOTAL_CONFIRMATIONS_COLLECTION createdAt created successfully");
}

const indexExists4 = await database.collection(TOTAL_VOLUME_COLLECTION).indexExists("createdAt");
if (!indexExists4) {
database
.collection(TOTAL_VOLUME_COLLECTION)
.createIndex({ createdAt: 1 }, { expireAfterSeconds: EXPIRE_14D, ...extraOptions });
.createIndex({ createdAt: 1 }, { expireAfterSeconds: EXPIRE_1M, ...extraOptions });
console.log("Index TOTAL_VOLUME_COLLECTION createdAt created successfully");
}

Expand Down
4 changes: 2 additions & 2 deletions server/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ const EXPIRE_1Y = EXPIRE_24H * 365;
const EXPIRE_5Y = EXPIRE_24H * 365 * 5;
const TOTAL_CONFIRMATIONS_COLLECTION = "TOTAL_CONFIRMATIONS";
const TOTAL_CONFIRMATIONS_24H = "TOTAL_CONFIRMATIONS_24H";
const TOTAL_CONFIRMATIONS_48H = "TOTAL_CONFIRMATIONS_48H";
const TOTAL_CONFIRMATIONS_7D = "TOTAL_CONFIRMATIONS_7D";
const TOTAL_CONFIRMATIONS_14D = "TOTAL_CONFIRMATIONS_14D";
const TOTAL_CONFIRMATIONS_48H = "TOTAL_CONFIRMATIONS_48H";
const TOTAL_VOLUME_COLLECTION = "TOTAL_VOLUME";
const TOTAL_VOLUME_24H = "TOTAL_VOLUME_24H";
const TOTAL_VOLUME_7D = "TOTAL_VOLUME_7D";
Expand Down Expand Up @@ -85,9 +85,9 @@ module.exports = {
EXPIRE_5Y,
TOTAL_CONFIRMATIONS_COLLECTION,
TOTAL_CONFIRMATIONS_24H,
TOTAL_CONFIRMATIONS_48H,
TOTAL_CONFIRMATIONS_7D,
TOTAL_CONFIRMATIONS_14D,
TOTAL_CONFIRMATIONS_48H,
TOTAL_VOLUME_COLLECTION,
TOTAL_VOLUME_24H,
TOTAL_VOLUME_7D,
Expand Down
12 changes: 6 additions & 6 deletions server/cron/distribution.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const { client: redisClient } = require("../client/redis");
const { Sentry } = require("../sentry");
const {
EXPIRE_24H,
EXPIRE_1W,
EXPIRE_7D,
DISTRIBUTION,
DELEGATORS,
DORMANT_FUNDS,
Expand Down Expand Up @@ -260,8 +260,8 @@ const doDistributionCron = async () => {

console.log(`Distribution cron finished in ${(new Date() - startTime) / 1000}s`);

nodeCache.set(DISTRIBUTION, distribution, EXPIRE_1W);
nodeCache.set(DORMANT_FUNDS, dormantFunds, EXPIRE_1W);
nodeCache.set(DISTRIBUTION, distribution, EXPIRE_7D);
nodeCache.set(DORMANT_FUNDS, dormantFunds, EXPIRE_7D);
nodeCache.set(KNOWN_EXCHANGES, knownExchanges, EXPIRE_24H);

// @NOTE manual add for now
Expand Down Expand Up @@ -324,14 +324,14 @@ const getDistributionData = () => {
distribution = fs.existsSync(DISTRIBUTION_PATH)
? JSON.parse(fs.readFileSync(DISTRIBUTION_PATH, "utf8"))
: [];
nodeCache.set(DISTRIBUTION, distribution, EXPIRE_1W);
nodeCache.set(DISTRIBUTION, distribution, EXPIRE_7D);
}

if (!dormantFunds) {
dormantFunds = fs.existsSync(DORMANT_FUNDS_PATH)
? JSON.parse(fs.readFileSync(DORMANT_FUNDS_PATH, "utf8"))
: {};
nodeCache.set(DORMANT_FUNDS, dormantFunds, EXPIRE_1W);
nodeCache.set(DORMANT_FUNDS, dormantFunds, EXPIRE_7D);
}

if (!knownExchanges) {
Expand All @@ -343,7 +343,7 @@ const getDistributionData = () => {

if (!status) {
status = fs.existsSync(STATUS_PATH) ? JSON.parse(fs.readFileSync(STATUS_PATH, "utf8")) : {};
nodeCache.set(STATUS, status, EXPIRE_1W);
nodeCache.set(STATUS, status, EXPIRE_7D);
}

return {
Expand Down
31 changes: 15 additions & 16 deletions server/cron/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@ const { Sentry } = require("../sentry");
const {
EXPIRE_1M,
EXPIRE_24H,
EXPIRE_1W,
EXPIRE_48H,
EXPIRE_7D,
EXPIRE_14D,
EXPIRE_48H,
TOTAL_CONFIRMATIONS_COLLECTION,
TOTAL_CONFIRMATIONS_24H,
TOTAL_CONFIRMATIONS_7D,
TOTAL_CONFIRMATIONS_48H,
TOTAL_CONFIRMATIONS_7D,
TOTAL_CONFIRMATIONS_14D,
TOTAL_VOLUME_COLLECTION,
TOTAL_VOLUME_24H,
TOTAL_VOLUME_7D,
TOTAL_VOLUME_48H,
TOTAL_VOLUME_7D,
TOTAL_VOLUME_14D,
CONFIRMATIONS_PER_SECOND,
} = require("../constants");
Expand Down Expand Up @@ -85,22 +84,22 @@ cron.schedule("*/10 * * * * *", async () => {
.then(([{ totalConfirmations24h = 0 } = {}]) => {
nodeCache.set(TOTAL_CONFIRMATIONS_24H, totalConfirmations24h);
});
// conf 7d

database
.collection(TOTAL_CONFIRMATIONS_COLLECTION)
.aggregate([
{
$match: {
createdAt: {
$gte: new Date(Date.now() - EXPIRE_1W * 1000),
$gte: new Date(Date.now() - EXPIRE_48H * 1000),
},
},
},
{ $group: { _id: null, totalConfirmations7d: { $sum: "$value" } } },
{ $group: { _id: null, totalConfirmations48h: { $sum: "$value" } } },
])
.toArray()
.then(([{ totalConfirmations7d = 0 } = {}]) => {
nodeCache.set(TOTAL_CONFIRMATIONS_7D, totalConfirmations7d);
.then(([{ totalConfirmations48h = 0 } = {}]) => {
nodeCache.set(TOTAL_CONFIRMATIONS_48H, totalConfirmations48h);
});

database
Expand All @@ -109,15 +108,15 @@ cron.schedule("*/10 * * * * *", async () => {
{
$match: {
createdAt: {
$lt: new Date(Date.now() + EXPIRE_48H * 1000),
$gte: new Date(Date.now() - EXPIRE_14D * 1000),
},
},
},
{ $group: { _id: null, totalConfirmations48h: { $sum: "$value" } } },
{ $group: { _id: null, totalConfirmations7d: { $sum: "$value" } } },
])
.toArray()
.then(([{ totalConfirmations48h = 0 } = {}]) => {
nodeCache.set(TOTAL_CONFIRMATIONS_48H, totalConfirmations48h);
.then(([{ totalConfirmations7d = 0 } = {}]) => {
nodeCache.set(TOTAL_CONFIRMATIONS_7D, totalConfirmations7d);
});

//conf 1w
Expand All @@ -144,7 +143,7 @@ cron.schedule("*/10 * * * * *", async () => {
{
$match: {
createdAt: {
$lt: new Date(Date.now() + EXPIRE_24H * 1000),
$gte: new Date(Date.now() - EXPIRE_24H * 1000),
},
},
},
Expand Down Expand Up @@ -178,7 +177,7 @@ cron.schedule("*/10 * * * * *", async () => {
{
$match: {
createdAt: {
$lt: new Date(Date.now() +TOTAL_VOLUME_14D * 1000),
$lt: new Date(Date.now() + TOTAL_VOLUME_14D * 1000),
},
},
},
Expand All @@ -195,7 +194,7 @@ cron.schedule("*/10 * * * * *", async () => {
{
$match: {
createdAt: {
$lt: new Date(Date.now() + EXPIRE_48H * 1000),
$gte: new Date(Date.now() - EXPIRE_48H * 1000),
},
},
},
Expand Down
13 changes: 6 additions & 7 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ const path = require("path");
const { nodeCache } = require("./client/cache");
const {
TOTAL_CONFIRMATIONS_24H,
TOTAL_CONFIRMATIONS_48H,
TOTAL_CONFIRMATIONS_7D,
TOTAL_CONFIRMATIONS_14D,
TOTAL_VOLUME_24H,
TOTAL_VOLUME_7D,
TOTAL_CONFIRMATIONS_48H,
TOTAL_VOLUME_48H,
TOTAL_VOLUME_7D,
TOTAL_VOLUME_14D,
CONFIRMATIONS_PER_SECOND,
NANOTICKER_STATS,
Expand Down Expand Up @@ -150,12 +150,12 @@ app.get("/api/developer-fund/transactions", async (req, res) => {

app.get("/api/market-statistics", async (req, res) => {
const cachedConfirmations24h = nodeCache.get(TOTAL_CONFIRMATIONS_24H);
const cachedConfirmations48h = nodeCache.get(TOTAL_CONFIRMATIONS_48H);
const cachedConfirmations7d = nodeCache.get(TOTAL_CONFIRMATIONS_7D);
const cachedConfirmations14d = nodeCache.get(TOTAL_CONFIRMATIONS_14D);
const cachedVolume24h = nodeCache.get(TOTAL_VOLUME_24H);
const cachedVolume7d = nodeCache.get(TOTAL_VOLUME_7D);
const cachedConfirmations48h = nodeCache.get(TOTAL_CONFIRMATIONS_48H);
const cachedVolume48h = nodeCache.get(TOTAL_VOLUME_48H);
const cachedVolume7d = nodeCache.get(TOTAL_VOLUME_7D);
const cachedVolume14d = nodeCache.get(TOTAL_VOLUME_14D);
const nanotpsStats = nodeCache.get(NANOTPS_STATS);
const nanoSpeedStats = nodeCache.get(NANOSPEED_STATS);
Expand All @@ -171,15 +171,14 @@ app.get("/api/market-statistics", async (req, res) => {

cryptocurrency: req.query.cryptocurrency,
});

res.send({
[TOTAL_CONFIRMATIONS_24H]: cachedConfirmations24h,
[TOTAL_CONFIRMATIONS_48H]: cachedConfirmations48h,
[TOTAL_CONFIRMATIONS_7D]: cachedConfirmations7d,
[TOTAL_CONFIRMATIONS_14D]: cachedConfirmations14d,
[TOTAL_VOLUME_24H]: cachedVolume24h,
[TOTAL_VOLUME_7D]: cachedVolume7d,
[TOTAL_CONFIRMATIONS_48H]: cachedConfirmations48h,
[TOTAL_VOLUME_48H]: cachedVolume48h,
[TOTAL_VOLUME_7D]: cachedVolume7d,
[TOTAL_VOLUME_14D]: cachedVolume14d,
[BITCOIN_TOTAL_TRANSACTION_FEES_24H]: btcTransactionFees24h,
[BITCOIN_TOTAL_TRANSACTION_FEES_7D]: btcTransactionFees7d,
Expand Down
12 changes: 6 additions & 6 deletions src/api/contexts/MarketStatistics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export const TOTAL_CONFIRMATIONS_24H = "TOTAL_CONFIRMATIONS_24H";
export const TOTAL_CONFIRMATIONS_7D = "TOTAL_CONFIRMATIONS_7D";
export const TOTAL_CONFIRMATIONS_14D = "TOTAL_CONFIRMATIONS_14D";
export const TOTAL_VOLUME_24H = "TOTAL_VOLUME_24H";
export const TOTAL_CONFIRMATIONS_48H = "TOTAL_CONFIRMATIONS_48H";
export const TOTAL_VOLUME_7D = "TOTAL_VOLUME_7D";
export const TOTAL_VOLUME_14D = "TOTAL_VOLUME_14D";
export const TOTAL_CONFIRMATIONS_48H = "TOTAL_CONFIRMATIONS_48H";
export const TOTAL_VOLUME_48H = "TOTAL_VOLUME_48H";
export const BITCOIN_TOTAL_TRANSACTION_FEES_24H = "BITCOIN_TOTAL_TRANSACTION_FEES_24H";
export const BITCOIN_TOTAL_TRANSACTION_FEES_7D = "BITCOIN_TOTAL_TRANSACTION_FEES_7D";
Expand All @@ -21,12 +21,12 @@ export const NANOSPEED_STATS = "NANOSPEED_STATS";

export interface Response {
[TOTAL_CONFIRMATIONS_24H]: number;
[TOTAL_CONFIRMATIONS_7D]: number;
[TOTAL_VOLUME_24H]: number;
[TOTAL_VOLUME_7D]: number;
[TOTAL_CONFIRMATIONS_48H]: number;
[TOTAL_CONFIRMATIONS_7D]: number;
[TOTAL_CONFIRMATIONS_14D]: number;
[TOTAL_VOLUME_24H]: number;
[TOTAL_VOLUME_48H]: number;
[TOTAL_VOLUME_7D]: number;
[TOTAL_VOLUME_14D]: number;
[BITCOIN_TOTAL_TRANSACTION_FEES_24H]: number;
[BITCOIN_TOTAL_TRANSACTION_FEES_7D]: number;
Expand Down Expand Up @@ -66,12 +66,12 @@ let pollMarketStatisticsTimeout: number | undefined;
export const MarketStatisticsContext = React.createContext<Context>({
marketStatistics: {
[TOTAL_CONFIRMATIONS_24H]: 0,
[TOTAL_CONFIRMATIONS_48H]: 0,
[TOTAL_CONFIRMATIONS_7D]: 0,
[TOTAL_CONFIRMATIONS_14D]: 0,
[TOTAL_VOLUME_24H]: 0,
[TOTAL_VOLUME_7D]: 0,
[TOTAL_CONFIRMATIONS_48H]: 0,
[TOTAL_VOLUME_48H]: 0,
[TOTAL_VOLUME_7D]: 0,
[TOTAL_VOLUME_14D]: 0,
[BITCOIN_TOTAL_TRANSACTION_FEES_24H]: 0,
[BITCOIN_TOTAL_TRANSACTION_FEES_7D]: 0,
Expand Down
3 changes: 2 additions & 1 deletion src/components/StatisticsChange/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ interface StatisticsChangeProps {
}

const StatisticsChange: React.FC<StatisticsChangeProps> = ({
value,
value: rawValue,
isPercent,
isNumber,
suffix,
}) => {
let value = rawValue;
const { theme } = React.useContext(PreferencesContext);
const color = (
value === 0
Expand Down
Loading

0 comments on commit 4b82647

Please sign in to comment.