Commit 385dbb67 authored by Vytor Calixto's avatar Vytor Calixto 👾

Basic gropuing

parent 2f88f61c
Pipeline #7576 failed with stage
in 7 minutes and 5 seconds
// This function returns the intersection of two arrays
function intersect(a, b) {
let t;
if (b.length > a.length) {
t = b; b = a; a = t;
}
return a.filter((e) => b.indexOf(e) !== -1);
}
// This function returns the difference between two arrays
function diff(a, b) {
return a.filter(function(i) {return b.indexOf(i) < 0;});
}
// This function returns true if two arrays are equal, false otherwise
function equals(a, b) {
// if the other array is a falsy value, return
if (!b)
return false;
// compare lengths - can save a lot of time
if (a.length != b.length)
return false;
for (var i = 0, l=a.length; i < l; i++) {
// Check if we have nested arrays
if (a[i] instanceof Array && b[i] instanceof Array) {
// recurse into the nested arrays
if (!a[i].equals(b[i]))
return false;
}
else if (a[i] != b[i]) {
// Warning - two different object instances will never be equal: {x:20} != {x:20}
return false;
}
}
return true;
}
module.exports = {
intersect: (a, b) => {
return intersect(a, b);
},
diff: (a, b) => {
return diff(a,b);
},
equals: (a, b) => {
return equals(a, b);
}
};
......@@ -16,14 +16,7 @@ const libs = `${process.cwd()}/libs`;
const log = require(`${libs}/log`)(module);
// This function returns the intersection of two arrays
function intersect(a, b) {
let t;
if (b.length > a.length) {
t = b; b = a; a = t;
}
return a.filter((e) => b.indexOf(e) !== -1);
}
const intersect = require(`${libs}/middlewares/arrayFunctions`).intersect;
function parseParams(queryParam, arr) {
return (req, res, next) => {
......
......@@ -14,7 +14,7 @@ const response = require(`${libs}/middlewares/response`);
const parseParams = require(`${libs}/middlewares/parseParams`);
// **Temporary** solution to add where clauses that are common to all requests
const af = require(`${libs}/middlewares/arrayFunctions`);
// Complete range of the enrollments dataset.
......@@ -49,7 +49,7 @@ enrollmentApp.get('/adm_dependency', (req, res, next) => {
}, query, response('adm_dependency'));
// Parse the filters and dimensions parameter in the query
enrollmentApp.use('/', parseParams('filter', [
enrollmentApp.use('/', parseParams('filters', [
'min_year',
'max_year',
'adm_dependency',
......@@ -68,40 +68,40 @@ enrollmentApp.use('/', parseParams('filter', [
'city',
'school'
]), (req, res, next) => {
log.debug(req.filter);
log.debug(req.filters);
log.debug(req.dims);
// Do the joins
if(typeof req.filter.adm_dependency !== 'undefined'
if(typeof req.filters.adm_dependency !== 'undefined'
|| typeof req.dims.adm_dependency !== 'undefined') {
req.sql.join('dependencia_adms', null, 'fk_dependencia_adm_id=dependencia_adms.pk_dependencia_adm_id');
}
if(typeof req.filter.education_level !== 'undefined'
if(typeof req.filters.education_level !== 'undefined'
|| typeof req.dims.education_level !== 'undefined') {
req.sql.join('etapa_ensino', null, 'fk_etapa_ensino_id=etapa_ensino.pk_etapa_ensino_id');
}
if(typeof req.filter.region !== 'undefined'
if(typeof req.filters.region !== 'undefined'
|| typeof req.dims.region !== 'undefined') {
req.sql.join('municipios', null, 'fk_municipio_id=municipios.pk_municipio_id')
.join('estados', null, 'municipios.fk_estado_id=estados.pk_estado_id')
.join('regioes', null, 'estados.fk_regiao_id=regioes.pk_regiao_id');
}
if((typeof req.filter.state !== 'undefined'
if((typeof req.filters.state !== 'undefined'
|| typeof req.dims.state !== 'undefined')
&& (typeof req.filter.region === 'undefined'
&& (typeof req.filters.region === 'undefined'
&& typeof req.dims.region === 'undefined')) {
req.sql.join('municipios', null, 'fk_municipio_id=municipios.pk_municipio_id')
.join('estados', null, 'municipios.fk_estado_id=estados.pk_estado_id');
}
if((typeof req.filter.city !== 'undefined'
if((typeof req.filters.city !== 'undefined'
|| typeof req.dims.city !== 'undefined')
&& (typeof req.filter.state === 'undefined'
&& (typeof req.filters.state === 'undefined'
&& typeof req.dims.state === 'undefined')
&& (typeof req.filter.region === 'undefined'
&& (typeof req.filters.region === 'undefined'
&& typeof req.dims.region === 'undefined')) {
req.sql.join('municipios', null, 'fk_municipio_id=municipios.pk_municipio_id');
}
......@@ -163,45 +163,105 @@ enrollmentApp.use('/', parseParams('filter', [
// Filter (add where)
if (typeof req.filter.min_year !== 'undefined') {
req.sql.where('turmas.ano_censo>=?', parseInt(req.filter.min_year, 10));
if (typeof req.filters.min_year !== 'undefined') {
req.sql.where('turmas.ano_censo>=?', parseInt(req.filters.min_year, 10));
}
if (typeof req.filter.max_year !== 'undefined') {
req.sql.where('turmas.ano_censo<=?', parseInt(req.filter.max_year, 10));
if (typeof req.filters.max_year !== 'undefined') {
req.sql.where('turmas.ano_censo<=?', parseInt(req.filters.max_year, 10));
}
if (typeof req.filter.adm_dependency !== 'undefined') {
req.sql.where('pk_dependencia_adm_id=?', parseInt(req.filter.adm_dependency, 10));
if (typeof req.filters.adm_dependency !== 'undefined') {
req.sql.where('pk_dependencia_adm_id=?', parseInt(req.filters.adm_dependency, 10));
}
if (typeof req.filter.location !== 'undefined') {
req.sql.where('turmas.id_localizacao=?', parseInt(req.filter.location, 10));
if (typeof req.filters.location !== 'undefined') {
req.sql.where('turmas.id_localizacao=?', parseInt(req.filters.location, 10));
}
if (typeof req.filter.education_level !== 'undefined') {
req.sql.where('pk_etapa_ensino_id=?', parseInt(req.filter.education_level, 10));
if (typeof req.filters.education_level !== 'undefined') {
req.sql.where('pk_etapa_ensino_id=?', parseInt(req.filters.education_level, 10));
}
if (typeof req.filter.region !== 'undefined') {
req.sql.where('pk_regiao_id=?', parseInt(req.filter.region, 10));
if (typeof req.filters.region !== 'undefined') {
req.sql.where('pk_regiao_id=?', parseInt(req.filters.region, 10));
}
if (typeof req.filter.state !== 'undefined') {
req.sql.where('pk_estado_id=?', parseInt(req.filter.state, 10));
if (typeof req.filters.state !== 'undefined') {
req.sql.where('pk_estado_id=?', parseInt(req.filters.state, 10));
}
if (typeof req.filter.city !== 'undefined') {
req.sql.where('turmas.fk_municipio_id=?', parseInt(req.filter.city, 10));
if (typeof req.filters.city !== 'undefined') {
req.sql.where('turmas.fk_municipio_id=?', parseInt(req.filters.city, 10));
}
if (typeof req.filter.school !== 'undefined') {
req.sql.where('turmas.fk_escola_id=?', parseInt(req.filter.school, 10));
if (typeof req.filters.school !== 'undefined') {
req.sql.where('turmas.fk_escola_id=?', parseInt(req.filters.school, 10));
}
log.debug(req.sql.toParam());
next();
});
function objectGrouping(req, res, next) {
log.debug('REQ.DIMS!', req.dims);
log.debug('REQ.FILTERS!', req.filters);
// set of attributes that identify the object
let objIdentifiers = [];
// attribute that we want to group
let objVariable = '';
// historic series
if(typeof req.filters.min_year === 'undefined' && typeof req.filters.max_year === 'undefined') {
objVariable = 'year';
} else if(typeof req.dims.education_level !== 'undefined') {
objVariable = 'education_level';
} else if(typeof req.dims.adm_dependency !== 'undefined') {
objVariable = 'adm_dependency'
} else if(typeof req.dims.location !== 'undefined') {
objVariable = 'location'
}
// 'adm_dependency',
// 'location',
// 'education_level',
// 'region',
// 'state',
// 'city',
// 'school'
if(objVariable === '') next();
// get the difference of the arrays
objIdentifiers = af.diff(Object.keys(req.dims), [objVariable]);
log.debug('OBJIDENTIFIERS', objIdentifiers);
// add '_name' termination
objIdentifiers.forEach((o, i, a) => {
if(o != 'education_level') a[i] = o+'_name';
if(o == 'size') a.splice(i, i);
});
if(objVariable !== 'year' && objVariable !== 'education_level') {
objVariable += '_name';
}
let groupResult = {};
// Group the objects
req.result.forEach((o, i, a) => {
let oString = '';
for(let i=0; i < objIdentifiers.length; ++i) {
oString += o[objIdentifiers[i]];
}
if(typeof groupResult[oString] === 'undefined') {
groupResult[oString] = {};
for(let i=0; i < objIdentifiers.length; ++i) groupResult[oString][objIdentifiers[i]] = o[objIdentifiers[i]];
groupResult[oString][objVariable] = {};
}
groupResult[oString][objVariable][o[objVariable]] = o.total;
});
log.debug('OBJIDENTIFIERS', objIdentifiers);
log.debug('RESULT', groupResult);
req.result = {result: []};
for(let key in groupResult) {
log.debug(groupResult[key]);
req.result.result.push(groupResult[key]);
}
next();
}
enrollmentApp.get('/', (req, res, next) => {
req.sql.field('COALESCE(SUM(num_matriculas), 0)', 'total')
.field('turmas.ano_censo', 'year')
......@@ -222,6 +282,6 @@ enrollmentApp.get('/', (req, res, next) => {
}
});
next();
}, response('enrollment'));
}, objectGrouping, response('enrollment'));
module.exports = enrollmentApp;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment