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

Commit ac03ed5

Browse files
authored
Make index names configurable (#467)
1 parent 98c5d63 commit ac03ed5

File tree

6 files changed

+40
-12
lines changed

6 files changed

+40
-12
lines changed

src/consumers/index-fill-protocol-fee.js

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

4+
const { getModel } = require('../model');
45
const { JOB, QUEUE } = require('../constants');
6+
const createDocument = require('../index/fills/create-document');
57
const elasticsearch = require('../util/elasticsearch');
8+
const getIndexName = require('../index/get-index-name');
69

710
const indexFillProtocolFee = async (job, { logger }) => {
811
const { fillId, protocolFee } = job.data;
@@ -15,23 +18,27 @@ const indexFillProtocolFee = async (job, { logger }) => {
1518
throw new Error(`Invalid value: ${protocolFee}`);
1619
}
1720

18-
const exists = await elasticsearch
19-
.getClient()
20-
.exists({ id: fillId, index: 'fills', _source: false });
21-
const indexed = exists.body;
21+
const fill = await getModel('Fill')
22+
.findOne({ _id: fillId })
23+
.populate([
24+
{ path: 'takerMetadata', select: 'isContract' },
25+
{ path: 'transaction', select: 'from' },
26+
])
27+
.lean();
2228

23-
if (!indexed) {
24-
throw new Error(`Could not index protocol fee of fill: ${fillId}`);
29+
if (fill === null) {
30+
throw new Error(`Could not find fill: ${fillId}`);
2531
}
2632

2733
await elasticsearch.getClient().update({
2834
id: fillId,
29-
index: 'fills',
35+
index: getIndexName('fills'),
3036
body: {
3137
doc: {
3238
protocolFeeUSD: protocolFee,
3339
updatedAt: new Date(Date.now()).toISOString(),
3440
},
41+
upsert: createDocument(fill),
3542
},
3643
});
3744

src/consumers/index-fill-traders/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { JOB, QUEUE } = require('../../constants');
44
const { publishJob } = require('../../queues');
55
const elasticsearch = require('../../util/elasticsearch');
66
const getAddressMetadata = require('../../addresses/get-address-metadata');
7+
const getIndexName = require('../../index/get-index-name');
78
const getTransactionByHash = require('../../transactions/get-transaction-by-hash');
89

910
const indexFillTraders = async (job, { logger }) => {
@@ -94,7 +95,7 @@ const indexFillTraders = async (job, { logger }) => {
9495

9596
const result = await elasticsearch
9697
.getClient()
97-
.bulk({ body: `${requestBody}\n`, index: 'trader_fills' });
98+
.bulk({ body: `${requestBody}\n`, index: getIndexName('trader_fills') });
9899

99100
if (result.body.errors === true) {
100101
const errorMessage = _.get(

src/consumers/index-fill-value.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { JOB, QUEUE } = require('../constants');
55
const { getModel } = require('../model');
66
const createDocument = require('../index/fills/create-document');
77
const elasticsearch = require('../util/elasticsearch');
8+
const getIndexName = require('../index/get-index-name');
89
const relayerRegistry = require('../relayers/relayer-registry');
910

1011
const isOrderMatcher = relayerId => {
@@ -48,7 +49,7 @@ const indexFillValue = async (job, { logger }) => {
4849

4950
await elasticsearch.getClient().update({
5051
id: fillId,
51-
index: 'fills',
52+
index: getIndexName('fills'),
5253
body: {
5354
doc: {
5455
tradeVolume: calculateTradeVolume(value, relayerId),

src/consumers/index-fill.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ const mongoose = require('mongoose');
22

33
const { JOB, QUEUE } = require('../constants');
44
const { getModel } = require('../model');
5-
const fillsIndex = require('../index/fills');
65
const elasticsearch = require('../util/elasticsearch');
6+
const fillsIndex = require('../index/fills');
7+
const getIndexName = require('../index/get-index-name');
78

89
const indexFill = async (job, { logger }) => {
910
const { fillId } = job.data;
@@ -27,7 +28,7 @@ const indexFill = async (job, { logger }) => {
2728

2829
await elasticsearch.getClient().index({
2930
id: fill._id,
30-
index: 'fills',
31+
index: getIndexName('fills'),
3132
body: fillsIndex.createDocument(fill),
3233
});
3334

src/consumers/index-traded-tokens.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const _ = require('lodash');
22

33
const { JOB, QUEUE } = require('../constants');
44
const elasticsearch = require('../util/elasticsearch');
5+
const getIndexName = require('../index/get-index-name');
56

67
const consumer = async (job, { logger }) => {
78
const { attributions, date, fillId, relayerId, tradedTokens } = job.data;
@@ -40,7 +41,7 @@ const consumer = async (job, { logger }) => {
4041

4142
const result = await elasticsearch
4243
.getClient()
43-
.bulk({ body: `${body}\n`, index: 'traded_tokens' });
44+
.bulk({ body: `${body}\n`, index: getIndexName('traded_tokens') });
4445

4546
if (result.body.errors === true) {
4647
const errorMessage = _.get(

src/index/get-index-name.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const getIndexName = index => {
2+
if (index === 'fills') {
3+
return process.env.INDEX_NAME_FILLS || 'fills';
4+
}
5+
6+
if (index === 'traded_tokens') {
7+
return process.env.INDEX_NAME_TRADED_TOKENS || 'traded_tokens';
8+
}
9+
10+
if (index === 'trader_fills') {
11+
return process.env.INDEX_NAME_TRADER_FILLS || 'trader_fills';
12+
}
13+
14+
throw new Error(`Unsupported index: ${index}`);
15+
};
16+
17+
module.exports = getIndexName;

0 commit comments

Comments
 (0)