Skip to content

Commit

Permalink
fix(metrics): increased metrics performance
Browse files Browse the repository at this point in the history
  • Loading branch information
serge1peshcoff committed Sep 21, 2019
1 parent 1582712 commit 6f78319
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 59 deletions.
32 changes: 0 additions & 32 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,38 +43,6 @@ exports.getPermissions = (user, corePermissions) => {
};
};

// A helper to count objects by a set of fields.
exports.countByFields = (array, keys = []) => {
const reduceFunc = (acc, val) => {
// finding element with such keys values
const accElement = acc.find((elementInAcc) => {
for (const key of keys) {
if (elementInAcc[key] !== val[key]) {
return false;
}
}

return true;
});

// if found, increment value, if not, adding another element
// to result array.
if (accElement) {
accElement.value += 1;
} else {
const elt = { value: 1 };
for (const key of keys) {
elt[key] = val[key];
}
acc.push(elt);
}

return acc;
};

return array.reduce(reduceFunc, []);
};

// A helper to add data to gauge Prometheus metric.
exports.addGaugeData = (gauge, array) => {
// reset gauge...
Expand Down
60 changes: 33 additions & 27 deletions lib/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ const {

const {
Integration,
Code,
Category
} = require('../models');
const helpers = require('./helpers');
const { sequelize } = require('./sequelize');

const gaugesList = {
categoriesTotal: new Gauge({
Expand All @@ -32,38 +32,44 @@ const gaugesList = {
};

exports.getMetrics = async (req, res) => {
let [
const [
integrations,
codes,
categories
categories,
partners
] = await Promise.all([
Integration.findAll(),
Code.findAll({ include: [Integration] }),
Category.findAll()
Integration.findAll({
attributes: [
[sequelize.fn('COUNT', 'id'), 'value']
],
raw: true
}),
sequelize.query(
'SELECT COUNT(codes.id) AS value, (codes.claimed_by IS NOT NULL) AS claimed, integrations.name AS integration_name '
+ 'FROM codes, integrations '
+ 'WHERE codes.integration_id = integrations.id '
+ 'GROUP BY claimed, integration_name',
{ type: sequelize.QueryTypes.SELECT }
),
Category.findAll({
attributes: [
[sequelize.fn('COUNT', 'id'), 'value']
],
raw: true
}),
sequelize.query(
'SELECT categories.name AS category_name, COUNT(*) AS value '
+ 'FROM categories, jsonb_array_elements(categories.discounts) AS partners '
+ 'GROUP BY categories.id, categories.name',
{ type: sequelize.QueryTypes.SELECT }
)
]);

const partners = categories
.map((category) => category.toJSON())
.map((category) => {
for (const discount of category.discounts) {
discount.category_name = category.name;
}

return category.discounts;
}).flat();

categories = categories.map((category) => category.toJSON());
integrations = integrations.map((integration) => integration.toJSON());
codes = codes.map((code) => Object.assign(code.toJSON(), {
integration_name: code.integration.name,
claimed: code.claimed_by !== null
}));

// setting gauges with real data
helpers.addGaugeData(gaugesList.categoriesTotal, helpers.countByFields(categories));
helpers.addGaugeData(gaugesList.partnersTotal, helpers.countByFields(partners, ['category_name']));
helpers.addGaugeData(gaugesList.integrationsTotal, helpers.countByFields(integrations));
helpers.addGaugeData(gaugesList.codesTotal, helpers.countByFields(codes, ['integration_name', 'claimed']));
helpers.addGaugeData(gaugesList.categoriesTotal, categories);
helpers.addGaugeData(gaugesList.partnersTotal, partners);
helpers.addGaugeData(gaugesList.integrationsTotal, integrations);
helpers.addGaugeData(gaugesList.codesTotal, codes);

res.set('Content-Type', register.contentType);
res.end(register.metrics());
Expand Down
2 changes: 2 additions & 0 deletions lib/sequelize.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require('pg').defaults.parseInt8 = true; // to return count() as int, not string

const Sequelize = require('sequelize');

const logger = require('./logger');
Expand Down

0 comments on commit 6f78319

Please sign in to comment.