Skip to content
This repository was archived by the owner on Aug 12, 2023. It is now read-only.

Commit 7833b81

Browse files
authored
Pad data returned by all metrics endpoints (#344)
1 parent 333ad2d commit 7833b81

File tree

9 files changed

+189
-193
lines changed

9 files changed

+189
-193
lines changed

src/app/routes/v1/metrics.js

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ const { TIME_PERIOD } = require('../../../constants');
55
const checkTraderExists = require('../../../traders/check-trader-exists');
66
const determineGranularityForTimePeriod = require('../../../metrics/determine-granularity-for-time-period');
77
const getActiveTraderMetrics = require('../../../metrics/get-active-trader-metrics');
8-
const getDatesForMetrics = require('../../../util/get-dates-for-metrics');
9-
const getDatesForTimePeriod = require('../../../util/get-dates-for-time-period');
108
const getNetworkMetrics = require('../../../metrics/get-network-metrics');
119
const getProtocolMetrics = require('../../../metrics/get-protocol-metrics');
1210
const getRelayerLookupId = require('../../../relayers/get-relayer-lookup-id');
@@ -32,8 +30,8 @@ const createRouter = () => {
3230
request.query.granularity === undefined
3331
? determineGranularityForTimePeriod(period)
3432
: request.query.granularity;
35-
const { dateFrom, dateTo } = getDatesForMetrics(period, granularity);
36-
const metrics = await getNetworkMetrics(dateFrom, dateTo, granularity);
33+
34+
const metrics = await getNetworkMetrics(period, granularity);
3735

3836
response.body = metrics;
3937

@@ -51,11 +49,6 @@ const createRouter = () => {
5149
throw new MissingParameterError('token');
5250
}
5351

54-
const period = request.query.period || TIME_PERIOD.MONTH;
55-
56-
const { dateFrom, dateTo } = getDatesForTimePeriod(period);
57-
58-
const granularity = determineGranularityForTimePeriod(period);
5952
const token = await Token.findOne({ address: tokenAddress });
6053

6154
if (token === null) {
@@ -65,12 +58,10 @@ const createRouter = () => {
6558
);
6659
}
6760

68-
const metrics = await getTokenMetrics(
69-
token,
70-
dateFrom,
71-
dateTo,
72-
granularity,
73-
);
61+
const period = request.query.period || TIME_PERIOD.MONTH;
62+
const granularity = determineGranularityForTimePeriod(period);
63+
64+
const metrics = await getTokenMetrics(token, period, granularity);
7465

7566
response.body = metrics;
7667

@@ -104,13 +95,8 @@ const createRouter = () => {
10495
request.query.granularity === undefined
10596
? determineGranularityForTimePeriod(period)
10697
: request.query.granularity;
107-
const { dateFrom, dateTo } = getDatesForMetrics(period, granularity);
108-
const metrics = await getTraderMetrics(
109-
address,
110-
dateFrom,
111-
dateTo,
112-
granularity,
113-
);
98+
99+
const metrics = await getTraderMetrics(address, period, granularity);
114100

115101
response.body = metrics;
116102

@@ -143,11 +129,10 @@ const createRouter = () => {
143129
request.query.granularity === undefined
144130
? determineGranularityForTimePeriod(period)
145131
: request.query.granularity;
146-
const { dateFrom, dateTo } = getDatesForMetrics(period, granularity);
132+
147133
const metrics = await getRelayerMetrics(
148134
relayerLookupId,
149-
dateFrom,
150-
dateTo,
135+
period,
151136
granularity,
152137
);
153138

@@ -168,8 +153,7 @@ const createRouter = () => {
168153
? determineGranularityForTimePeriod(period)
169154
: request.query.granularity;
170155

171-
const { dateFrom, dateTo } = getDatesForMetrics(period, granularity);
172-
const metrics = await getProtocolMetrics(dateFrom, dateTo, granularity);
156+
const metrics = await getProtocolMetrics(period, granularity);
173157

174158
response.body = metrics;
175159

@@ -188,12 +172,7 @@ const createRouter = () => {
188172
? determineGranularityForTimePeriod(period)
189173
: request.query.granularity;
190174

191-
const { dateFrom, dateTo } = getDatesForMetrics(period, granularity);
192-
const metrics = await getActiveTraderMetrics(
193-
dateFrom,
194-
dateTo,
195-
granularity,
196-
);
175+
const metrics = await getActiveTraderMetrics(period, granularity);
197176

198177
response.body = metrics;
199178

src/metrics/determine-granularity-for-time-period.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ const determineGranularityForTimePeriod = timePeriod => {
77
return GRANULARITY.HOUR;
88
case TIME_PERIOD.MONTH:
99
case TIME_PERIOD.YEAR:
10-
case TIME_PERIOD.ALL:
1110
return GRANULARITY.DAY;
11+
case TIME_PERIOD.ALL:
12+
return GRANULARITY.WEEK;
1213
default:
1314
throw new Error(`Invalid time period: ${timePeriod}`);
1415
}

src/metrics/get-active-trader-metrics.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const { GRANULARITY } = require('../constants');
22
const elasticsearch = require('../util/elasticsearch');
3+
const getDatesForMetrics = require('../util/get-dates-for-metrics');
4+
const padMetrics = require('./pad-metrics');
35

46
const INDEX_MAPPINGS = {
57
[GRANULARITY.DAY]: 'active_trader_metrics_daily',
@@ -8,7 +10,9 @@ const INDEX_MAPPINGS = {
810
[GRANULARITY.WEEK]: 'active_trader_metrics_weekly',
911
};
1012

11-
const getActiveTraderMetrics = async (dateFrom, dateTo, granularity) => {
13+
const getActiveTraderMetrics = async (period, granularity) => {
14+
const { dateFrom, dateTo } = getDatesForMetrics(period, granularity);
15+
1216
const results = await elasticsearch.getClient().search({
1317
body: {
1418
query: {
@@ -24,12 +28,17 @@ const getActiveTraderMetrics = async (dateFrom, dateTo, granularity) => {
2428
size: 1000, // TODO: Determine this dynamically
2529
});
2630

27-
return results.body.hits.hits.map(x => ({
28-
date: new Date(x._source.date),
29-
makerCount: x._source.activeMakers,
30-
takerCount: x._source.activeTakers,
31-
traderCount: x._source.activeTraders,
32-
}));
31+
return padMetrics(
32+
results.body.hits.hits.map(x => ({
33+
date: new Date(x._source.date),
34+
makerCount: x._source.activeMakers,
35+
takerCount: x._source.activeTakers,
36+
traderCount: x._source.activeTraders,
37+
})),
38+
period,
39+
granularity,
40+
{ makerCount: 0, takerCount: 0, traderCount: 0 },
41+
);
3342
};
3443

3544
module.exports = getActiveTraderMetrics;

src/metrics/get-network-metrics.js

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,11 @@
1-
const { ETH_TOKEN_DECIMALS, GRANULARITY } = require('../constants');
1+
const { ETH_TOKEN_DECIMALS } = require('../constants');
22
const elasticsearch = require('../util/elasticsearch');
33
const formatTokenAmount = require('../tokens/format-token-amount');
4+
const getDatesForMetrics = require('../util/get-dates-for-metrics');
5+
const padMetrics = require('./pad-metrics');
46

5-
const getNetworkMetrics = async (dateFrom, dateTo, granularity) => {
6-
if (granularity === GRANULARITY.HOUR) {
7-
const results = await elasticsearch.getClient().search({
8-
body: {
9-
query: {
10-
range: {
11-
date: {
12-
gte: dateFrom,
13-
lte: dateTo,
14-
},
15-
},
16-
},
17-
},
18-
index: 'network_metrics_hourly',
19-
size: 200, // TODO: Determine this dynamically
20-
});
21-
22-
return results.body.hits.hits.map(x => ({
23-
date: new Date(x._source.date),
24-
fillCount: x._source.fillCount,
25-
fillVolume: x._source.fillVolume,
26-
protocolFees: {
27-
ETH: formatTokenAmount(x._source.protocolFeesETH, ETH_TOKEN_DECIMALS),
28-
USD: x._source.protocolFeesUSD,
29-
},
30-
tradeCount: x._source.tradeCount,
31-
tradeVolume: x._source.tradeVolume,
32-
}));
33-
}
7+
const getNetworkMetrics = async (period, granularity) => {
8+
const { dateFrom, dateTo } = getDatesForMetrics(period, granularity);
349

3510
const results = await elasticsearch.getClient().search({
3611
index: 'network_metrics_hourly',
@@ -75,17 +50,31 @@ const getNetworkMetrics = async (dateFrom, dateTo, granularity) => {
7550
},
7651
});
7752

78-
return results.body.aggregations.network_metrics_by_day.buckets.map(x => ({
79-
date: new Date(x.key_as_string),
80-
fillCount: x.fillCount.value,
81-
fillVolume: x.fillVolume.value,
82-
protocolFees: {
83-
ETH: formatTokenAmount(x.protocolFeesETH.value, ETH_TOKEN_DECIMALS),
84-
USD: x.protocolFeesUSD.value,
53+
return padMetrics(
54+
results.body.aggregations.network_metrics_by_day.buckets.map(x => ({
55+
date: new Date(x.key_as_string),
56+
fillCount: x.fillCount.value,
57+
fillVolume: x.fillVolume.value,
58+
protocolFees: {
59+
ETH: formatTokenAmount(x.protocolFeesETH.value, ETH_TOKEN_DECIMALS),
60+
USD: x.protocolFeesUSD.value,
61+
},
62+
tradeCount: x.tradeCount.value,
63+
tradeVolume: x.tradeVolume.value,
64+
})),
65+
period,
66+
granularity,
67+
{
68+
fillCount: 0,
69+
fillVolume: 0,
70+
protocolFees: {
71+
ETH: '0',
72+
USD: 0,
73+
},
74+
tradeCount: 0,
75+
tradeVolume: 0,
8576
},
86-
tradeCount: x.tradeCount.value,
87-
tradeVolume: x.tradeVolume.value,
88-
}));
77+
);
8978
};
9079

9180
module.exports = getNetworkMetrics;

src/metrics/get-protocol-metrics.js

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
const _ = require('lodash');
22

33
const elasticsearch = require('../util/elasticsearch');
4+
const getDatesForMetrics = require('../util/get-dates-for-metrics');
5+
const padMetrics = require('./pad-metrics');
6+
7+
const getProtocolMetrics = async (period, granularity) => {
8+
const { dateFrom, dateTo } = getDatesForMetrics(period, granularity);
49

5-
const getProtocolMetrics = async (dateFrom, dateTo, granularity) => {
610
const results = await elasticsearch.getClient().search({
711
index: 'protocol_metrics_hourly',
812
body: {
@@ -53,33 +57,38 @@ const getProtocolMetrics = async (dateFrom, dateTo, granularity) => {
5357
},
5458
});
5559

56-
return _(results.body.aggregations.stats_by_protocol.buckets)
57-
.map(x => {
58-
const protocolVersion = x.key;
59-
const stats = x.stats_by_date.buckets.map(y => ({
60-
protocolVersion,
61-
date: y.key_as_string,
62-
fillCount: y.fillCount.value,
63-
fillVolume: y.fillVolume.value,
64-
tradeCount: y.tradeCount.value,
65-
tradeVolume: y.tradeVolume.value,
66-
}));
60+
return padMetrics(
61+
_(results.body.aggregations.stats_by_protocol.buckets)
62+
.map(x => {
63+
const protocolVersion = x.key;
64+
const stats = x.stats_by_date.buckets.map(y => ({
65+
protocolVersion,
66+
date: y.key_as_string,
67+
fillCount: y.fillCount.value,
68+
fillVolume: y.fillVolume.value,
69+
tradeCount: y.tradeCount.value,
70+
tradeVolume: y.tradeVolume.value,
71+
}));
6772

68-
return stats;
69-
})
70-
.flatten()
71-
.groupBy('date')
72-
.map((stats, date) => ({
73-
date: new Date(date).toISOString(),
74-
stats: stats.map(stat => ({
75-
fillCount: stat.fillCount,
76-
fillVolume: stat.fillVolume,
77-
protocolVersion: stat.protocolVersion,
78-
tradeCount: stat.tradeCount,
79-
tradeVolume: stat.tradeVolume,
80-
})),
81-
}))
82-
.sortBy('date');
73+
return stats;
74+
})
75+
.flatten()
76+
.groupBy('date')
77+
.map((stats, date) => ({
78+
date: new Date(date).toISOString(),
79+
stats: stats.map(stat => ({
80+
fillCount: stat.fillCount,
81+
fillVolume: stat.fillVolume,
82+
protocolVersion: stat.protocolVersion,
83+
tradeCount: stat.tradeCount,
84+
tradeVolume: stat.tradeVolume,
85+
})),
86+
}))
87+
.sortBy('date'),
88+
period,
89+
granularity,
90+
{ stats: [] },
91+
);
8392
};
8493

8594
module.exports = getProtocolMetrics;

0 commit comments

Comments
 (0)