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

Commit

Permalink
Start logging popular search terms (#386)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbovis authored May 5, 2020
1 parent 96e1bab commit 3595a4e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 18 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
"mongoose-paginate": "5.0.3",
"ms": "2.1.2",
"pino": "6.0.0",
"pino-elasticsearch": "4.4.0"
"pino-elasticsearch": "4.4.0",
"slugify": "1.4.0"
},
"engines": {
"node": "10.17.0",
Expand Down
38 changes: 21 additions & 17 deletions src/app/routes/v1/fills.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const moment = require('moment');
const mongoose = require('mongoose');
const Router = require('koa-router');

const { logSearch } = require('../../../search');
const Fill = require('../../../model/fill');
const getRelayerLookupId = require('../../../relayers/get-relayer-lookup-id');
const InvalidParameterError = require('../../errors/invalid-parameter-error');
Expand Down Expand Up @@ -138,23 +139,26 @@ const createRouter = () => {
);
}

const { docs, pages, total } = await searchFills(
{
address,
bridgeAddress,
bridged,
dateFrom,
dateTo,
protocolVersion,
query,
relayerId: relayerLookupId,
status: reverseMapStatus(status),
token,
valueFrom,
valueTo,
},
{ limit, page },
);
const [{ docs, pages, total }] = await Promise.all([
searchFills(
{
address,
bridgeAddress,
bridged,
dateFrom,
dateTo,
protocolVersion,
query,
relayerId: relayerLookupId,
status: reverseMapStatus(status),
token,
valueFrom,
valueTo,
},
{ limit, page },
),
query !== undefined ? logSearch(query, new Date()) : Promise.resolve(),
]);

response.body = {
fills: transformFills(docs),
Expand Down
3 changes: 3 additions & 0 deletions src/search/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const logSearch = require('./log-search');

module.exports = { logSearch };
33 changes: 33 additions & 0 deletions src/search/log-search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const slugify = require('slugify');

const elasticsearch = require('../util/elasticsearch');

const logSearch = async (terms, date) => {
const year = date.getUTCFullYear();
const month = date.getUTCMonth();
const day = date.getUTCDate();
const hour = date.getUTCHours();
const logDate = new Date();

logDate.setTime(Date.UTC(year, month, day, hour));

const slug = slugify(terms, { replacement: '_', lower: true, strict: true });

await elasticsearch.getClient().update({
id: `${year}_${month + 1}_${day}_${hour}_${slug}`,
index: 'search_terms',
body: {
script: {
lang: 'painless',
source: 'ctx._source.hits += 1',
},
upsert: {
date: logDate,
hits: 1,
terms,
},
},
});
};

module.exports = logSearch;

0 comments on commit 3595a4e

Please sign in to comment.