Skip to content

Commit

Permalink
New Version
Browse files Browse the repository at this point in the history
Release new version
  • Loading branch information
alesancor1 committed Oct 28, 2021
2 parents c19e52d + a456dec commit 37efba4
Show file tree
Hide file tree
Showing 15 changed files with 398 additions and 161 deletions.
100 changes: 50 additions & 50 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,55 +43,55 @@ jobs:
# github-token: ${{ secrets.GITHUB_TOKEN }}
# env:
# CI: true
e2eTest:
name: (develop) E2E - Checkout infrastructure, install mocha and run tests (docker-compose up, mocha tests and docker-compose down)
needs: buildTest
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x]
steps:
- uses: actions/checkout@v2
with:
repository: governify/bluejay-infrastructure
ref: refs/heads/assets
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: npm ci
- name: Add host.docker.internal association to 172.17.0.1
run: sudo echo "172.17.0.1 host.docker.internal" | sudo tee -a /etc/hosts
- name: Run tests
run: npm run test
env:
CI: true
e2eMain:
name: (main) E2E - Checkout infrastructure, install mocha and run tests (docker-compose up, mocha tests and docker-compose down)
if: ${{ github.ref == 'refs/heads/main' }}
needs: test
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x]
steps:
- uses: actions/checkout@v2
with:
repository: governify/bluejay-infrastructure
ref: refs/heads/assets
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: npm ci
- name: Add host.docker.internal association to 172.17.0.1
run: sudo echo "172.17.0.1 host.docker.internal" | sudo tee -a /etc/hosts
- name: Run tests
run: npm run test
env:
CI: true
## e2eTest:
## name: (develop) E2E - Checkout infrastructure, install mocha and run tests (docker-compose up, mocha tests and docker-compose down)
## needs: buildTest
## runs-on: ubuntu-latest
## strategy:
## matrix:
## node-version: [14.x]
## steps:
## - uses: actions/checkout@v2
## with:
## repository: governify/bluejay-infrastructure
## ref: refs/heads/develop
## - name: Use Node.js ${{ matrix.node-version }}
## uses: actions/setup-node@v1
## with:
## node-version: ${{ matrix.node-version }}
## - name: Install dependencies
## run: npm ci
## - name: Add host.docker.internal association to 172.17.0.1
## run: sudo echo "172.17.0.1 host.docker.internal" | sudo tee -a /etc/hosts
## - name: Run tests
## run: npm run test
## env:
## CI: true
## e2eMain:
## name: (main) E2E - Checkout infrastructure, install mocha and run tests (docker-compose up, mocha tests and docker-compose down)
## if: ${{ github.ref == 'refs/heads/main' }}
## needs: test
## runs-on: ubuntu-latest
## strategy:
## matrix:
## node-version: [14.x]
## steps:
## - uses: actions/checkout@v2
## with:
## repository: governify/bluejay-infrastructure
## ref: refs/heads/develop
## - name: Use Node.js ${{ matrix.node-version }}
## uses: actions/setup-node@v1
## with:
## node-version: ${{ matrix.node-version }}
## - name: Install dependencies
## run: npm ci
## - name: Add host.docker.internal association to 172.17.0.1
## run: sudo echo "172.17.0.1 host.docker.internal" | sudo tee -a /etc/hosts
## - name: Run tests
## run: npm run test
## env:
## CI: true
buildTest:
name: Build and push to dockerhub using develop tag
if: ${{ github.ref == 'refs/heads/develop' && github.event_name == 'push' }}
Expand All @@ -118,7 +118,7 @@ jobs:
buildProdAndRelease:
name: Release, build and push to dockerhub using tag version
if: ${{ github.ref == 'refs/heads/main' && github.event_name == 'push'}}
needs: e2eMain
## needs: e2eMain
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand Down
8 changes: 4 additions & 4 deletions config/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use strict';

var jsyaml = require('js-yaml');
const jsyaml = require('js-yaml');
const fs = require('fs');
const path = require('path');
const mustache = require('mustache');
mustache.escape = function (text) { return text; };

var configStringTemplate = fs.readFileSync(path.resolve(__dirname, './credentials/credentials.yaml'), 'utf8');
var configString = mustache.render(configStringTemplate, process.env, {}, ['$_[', ']']);
var credentials, config;
const configStringTemplate = fs.readFileSync(path.resolve(__dirname, './credentials/credentials.yaml'), 'utf8');
const configString = mustache.render(configStringTemplate, process.env, {}, ['$_[', ']']);
let credentials, config;
if (configString) {
credentials = jsyaml.safeLoad(configString);
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/apiv2computationsController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var varapiv2computationsController = require('./apiv2computationsControllerService');
const varapiv2computationsController = require('./apiv2computationsControllerService');

module.exports.addComputation = function addComputation (req, res, next) {
varapiv2computationsController.addComputation(req.swagger.params, res, next);
Expand Down
84 changes: 43 additions & 41 deletions controllers/apiv2computationsControllerService.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const crypto = require('crypto');

const computationCalculator = require('./computationCalculator');
const governify = require('governify-commons');
const logger = governify.getLogger().tag('computationService');

const computationsBD = {};

Expand All @@ -21,7 +23,7 @@ module.exports.addComputation = function addComputation (req, res, next) {
// Set the computations to the bd
computationsBD[computationId] = computations;
}).catch(err => {
console.log('error - addComputation.calculateComputations:', err.message);
logger.error('error - addComputation.calculateComputations:', err.message);
computationsBD[computationId] = err.message;
});

Expand All @@ -33,15 +35,15 @@ module.exports.addComputation = function addComputation (req, res, next) {
computation: '/api/v2/computations/' + computationId
});
}).catch(err => {
console.log('error - addComputation.getPeriods:', err.message);
logger.error('error - addComputation.getPeriods:', err.message);
sendError(res, err);
});
}).catch(err => {
console.log('error - addComputation.validateInput:', err.message);
logger.error('error - addComputation.validateInput:', err.message);
sendError(res, err);
});
} catch (err) {
console.log('error - addComputation:', err.message);
logger.error('error - addComputation:', err.message);
sendError(res, err);
}
};
Expand All @@ -60,15 +62,15 @@ module.exports.getComputation = (computationId) => {
};

const validateInput = (dsl) => {
console.log('dsl: ' + JSON.stringify(dsl));
logger.debug('dsl: ' + JSON.stringify(dsl));
return new Promise((resolve, reject) => {
try {
const initial = dsl.metric.window.initial;
const end = dsl.metric.window.end;

// ISO8601 Date validation
var iso8601 = /^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12][0-9])T(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(\\.[0-9]+)?(Z)?$/;
var iso8601Millis = /^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12][0-9])T(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(\\.[0-9]+)?\.([0-9][0-9][0-9])(Z)?$/;
const iso8601 = /^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12][0-9])T(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(\\.[0-9]+)?(Z)?$/;
const iso8601Millis = /^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12][0-9])T(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(\\.[0-9]+)?\.([0-9][0-9][0-9])(Z)?$/;

if (Date.parse(end) - Date.parse(initial) < 0) {
reject(new Error('End period date must be later than the initial.'));
Expand All @@ -90,45 +92,45 @@ const getPeriods = (dsl) => {
try {
const initial = dsl.metric.window.initial;
const end = dsl.metric.window.end;
const windowPeriod = dsl.metric.window.period;

// Translate period string to actual days and obtain number of periods
const periodLengths = {
daily: 1,
weekly: 7,
biweekly: 14,
monthly: 30,
bimonthly: 60,
annually: 365
};
const periodLength = periodLengths[windowPeriod];
if (periodLength === undefined) { reject(new Error('metric.window.period must be within these: daily, weekly, biweekly, monthly, bimonthly, annually.')); }
// const periodLengths = {
// daily: 1,
// weekly: 7,
// biweekly: 14,
// monthly: 30,
// bimonthly: 60,
// annually: 365
// };
// const periodLength = periodLengths[windowPeriod];
// if (periodLength === undefined) { reject(new Error('metric.window.period must be within these: daily, weekly, biweekly, monthly, bimonthly, annually.')); }

// Obtain periods
const periods = [];

let fromStr = initial;
let toDate;
let toStr;

let keepGoing = true;
while (keepGoing) {
// Set from after each iteration
if (toStr !== undefined) {
fromStr = toStr;
}

// Check if to is after end of periods
toDate = new Date(Date.parse(fromStr) + periodLength * 24 * 60 * 60 * 1000);
if (toDate >= new Date(Date.parse(end))) {
toDate = new Date(Date.parse(end));
keepGoing = false;
}
toStr = toDate.toISOString();

// Push into the array
periods.push({ from: fromStr, to: toStr });
}
// let fromStr = initial;
// let toDate;
// let toStr;
//
// let keepGoing = true;
// while (keepGoing) {
// // Set from after each iteration
// if (toStr !== undefined) {
// fromStr = toStr;
// }
//
// // Check if to is after end of periods
// toDate = new Date(Date.parse(fromStr) + periodLength * 24 * 60 * 60 * 1000);
// if (toDate >= new Date(Date.parse(end))) {
// toDate = new Date(Date.parse(end));
// keepGoing = false;
// }
// toStr = toDate.toISOString();
//
// // Push into the array
// periods.push({ from: fromStr, to: toStr });
// }
periods.push({ from: initial, to: end });

resolve(periods);
} catch (err) {
Expand Down Expand Up @@ -156,7 +158,7 @@ const calculateComputations = (dsl, periods) => {
value: rs.metric
});
});
console.log('RESULTS=>' + JSON.stringify(computations));
logger.debug('RESULTS=>' + JSON.stringify(computations));
resolve();
}).catch(err => {
reject(err);
Expand Down
2 changes: 1 addition & 1 deletion controllers/apiv2computationscomputationIdController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var varapiv2computationscomputationIdController = require('./apiv2computationscomputationIdControllerService');
const varapiv2computationscomputationIdController = require('./apiv2computationscomputationIdControllerService');

module.exports.findComputationById = function findComputationById (req, res, next) {
varapiv2computationscomputationIdController.findComputationById(req.swagger.params, res, next);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

const postControllerService = require('./apiv2computationsControllerService');

const governify = require('governify-commons');
const logger = governify.getLogger().tag('initialization');

module.exports.findComputationById = function findComputationById (req, res, next) {
try {
postControllerService.getComputation(req.computationId.value).then((computation) => {
Expand All @@ -17,11 +20,11 @@ module.exports.findComputationById = function findComputationById (req, res, nex
sendWithStatus(res, 500, 'Internal server error.');
}
}).catch(err => {
console.log('error - findComputationById.getComputation:', err.message);
logger.error('error - findComputationById.getComputation:', err.message);
sendWithStatus(res, 500, 'Internal server error: ' + err.message);
});
} catch (err) {
console.log('error - findComputationById:', err.message);
logger.error('error - findComputationById:', err.message);
sendWithStatus(res, 500, 'Internal server error: ' + err.message);
}
};
Expand Down
25 changes: 13 additions & 12 deletions controllers/computationCalculator/flavours/collectordynamic.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
/* eslint-disable no-async-promise-executor */

const governify = require('governify-commons');
const logger = governify.getLogger().tag('computerDynamic');
const config = require('../../../config');
const credentials = config.credentials();

async function applyStep (dsl, period, inputs, responseList) {
return new Promise(async function (resolve, reject) {
console.log('aplicando el 12312412512562');
logger.debug('Applying next step');
const url = dsl.config.url + inputs.request.endpoint;
var periodfromms = new Date(period.from).getTime();
const periodfromms = new Date(period.from).getTime();
// var periodtoms = new Date(period.to).getTime();
var realperiod = {};
console.log('DSL: ' + JSON.stringify(dsl));
let realperiod = {};
logger.debug('DSL: ' + JSON.stringify(dsl));
if (dsl.metric.scope.periodo_de_actividad === 'alta') {
realperiod = {
from: new Date(periodfromms + dsl.config.intervals.alta_actividad_fromOffset).toISOString(),
Expand All @@ -28,26 +29,26 @@ async function applyStep (dsl, period, inputs, responseList) {
}

const body = JSON.parse(JSON.stringify(inputs.request.body).replace(/>>>period.from<<</g, realperiod.from).replace(/>>>period.to<<</g, realperiod.to));
console.log('BODSY: ' + JSON.stringify(body));
logger.debug('BODY: ' + JSON.stringify(body));
const res = await governify.httpClient.request({
url: url,
method: 'POST',
headers: { Authorization: credentials.elk, 'Content-Type': 'application/json' },
json: body
}).then(response => { return response.data; }).catch(console.log);
console.log('REPSUESTA' + JSON.stringify(res));
}).then(response => { return response.data; }).catch(logger.info);
logger.debug('RESPONSE' + JSON.stringify(res));

var resultList = Object.getPropertyByString(res, 'aggregations.services.buckets');
var finalResult = [];
const resultList = Object.getPropertyByString(res, 'aggregations.services.buckets');
const finalResult = [];
resultList.forEach(rs => {
var newScope = Object.assign({}, dsl.metric.scope);
const newScope = Object.assign({}, dsl.metric.scope);
newScope.servicio = rs.key;
var metricResult = rs.avgresponsetime ? rs.avgresponsetime.value : rs.doc_count;
const metricResult = rs.avgresponsetime ? rs.avgresponsetime.value : rs.doc_count;
finalResult.push({ evidences: res.hits.hits, metric: metricResult != null ? metricResult : 0, scope: newScope });
});

// Add the result to the current result from previous steps.
var resultConcat = responseList.concat(finalResult);
const resultConcat = responseList.concat(finalResult);
resolve(resultConcat);
});
}
Expand Down
7 changes: 5 additions & 2 deletions controllers/computationCalculator/flavours/scopeGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
// }
// }

const governify = require('governify-commons');
const logger = governify.getLogger().tag('scopeGroup');

async function applyStep (dsl, period, inputs, responseList) {
return new Promise(async function (resolve, reject) {
// Create the inverted map with the assocations groups
var invertedMap = {};
const invertedMap = {};
Object.keys(inputs.groups).forEach(groupName => {
inputs.groups[groupName].forEach(servicio => {
invertedMap[servicio] = groupName;
Expand All @@ -23,7 +26,7 @@ async function applyStep (dsl, period, inputs, responseList) {

// Assign the grouped scope to each response
responseList.forEach(response => {
console.log(response);
logger.debug('Response:', response);
if (response.scope) {
response.scope[inputs.scopeGroupedIn] = invertedMap[response.scope[inputs.scopeGrouped]];
}
Expand Down
Loading

0 comments on commit 37efba4

Please sign in to comment.