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

Commit 3595a4e

Browse files
authored
Start logging popular search terms (#386)
1 parent 96e1bab commit 3595a4e

5 files changed

Lines changed: 64 additions & 18 deletions

File tree

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
"mongoose-paginate": "5.0.3",
4646
"ms": "2.1.2",
4747
"pino": "6.0.0",
48-
"pino-elasticsearch": "4.4.0"
48+
"pino-elasticsearch": "4.4.0",
49+
"slugify": "1.4.0"
4950
},
5051
"engines": {
5152
"node": "10.17.0",

src/app/routes/v1/fills.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const moment = require('moment');
33
const mongoose = require('mongoose');
44
const Router = require('koa-router');
55

6+
const { logSearch } = require('../../../search');
67
const Fill = require('../../../model/fill');
78
const getRelayerLookupId = require('../../../relayers/get-relayer-lookup-id');
89
const InvalidParameterError = require('../../errors/invalid-parameter-error');
@@ -138,23 +139,26 @@ const createRouter = () => {
138139
);
139140
}
140141

141-
const { docs, pages, total } = await searchFills(
142-
{
143-
address,
144-
bridgeAddress,
145-
bridged,
146-
dateFrom,
147-
dateTo,
148-
protocolVersion,
149-
query,
150-
relayerId: relayerLookupId,
151-
status: reverseMapStatus(status),
152-
token,
153-
valueFrom,
154-
valueTo,
155-
},
156-
{ limit, page },
157-
);
142+
const [{ docs, pages, total }] = await Promise.all([
143+
searchFills(
144+
{
145+
address,
146+
bridgeAddress,
147+
bridged,
148+
dateFrom,
149+
dateTo,
150+
protocolVersion,
151+
query,
152+
relayerId: relayerLookupId,
153+
status: reverseMapStatus(status),
154+
token,
155+
valueFrom,
156+
valueTo,
157+
},
158+
{ limit, page },
159+
),
160+
query !== undefined ? logSearch(query, new Date()) : Promise.resolve(),
161+
]);
158162

159163
response.body = {
160164
fills: transformFills(docs),

src/search/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const logSearch = require('./log-search');
2+
3+
module.exports = { logSearch };

src/search/log-search.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const slugify = require('slugify');
2+
3+
const elasticsearch = require('../util/elasticsearch');
4+
5+
const logSearch = async (terms, date) => {
6+
const year = date.getUTCFullYear();
7+
const month = date.getUTCMonth();
8+
const day = date.getUTCDate();
9+
const hour = date.getUTCHours();
10+
const logDate = new Date();
11+
12+
logDate.setTime(Date.UTC(year, month, day, hour));
13+
14+
const slug = slugify(terms, { replacement: '_', lower: true, strict: true });
15+
16+
await elasticsearch.getClient().update({
17+
id: `${year}_${month + 1}_${day}_${hour}_${slug}`,
18+
index: 'search_terms',
19+
body: {
20+
script: {
21+
lang: 'painless',
22+
source: 'ctx._source.hits += 1',
23+
},
24+
upsert: {
25+
date: logDate,
26+
hits: 1,
27+
terms,
28+
},
29+
},
30+
});
31+
};
32+
33+
module.exports = logSearch;

0 commit comments

Comments
 (0)