Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
simcaq-node
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
simcaq
simcaq-node
Commits
22a79ace
Commit
22a79ace
authored
8 years ago
by
Vytor Calixto
Browse files
Options
Downloads
Patches
Plain Diff
Update enrollments routes. Using squel now.
All routes now use squel and the sql, query, response chain
parent
6694746e
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/libs/routes/enrollment.js
+75
-163
75 additions, 163 deletions
src/libs/routes/enrollment.js
with
75 additions
and
163 deletions
src/libs/routes/enrollment.js
+
75
−
163
View file @
22a79ace
...
@@ -12,7 +12,24 @@ const query = require(`${libs}/middlewares/query`);
...
@@ -12,7 +12,24 @@ const query = require(`${libs}/middlewares/query`);
const
response
=
require
(
`
${
libs
}
/middlewares/response`
);
const
response
=
require
(
`
${
libs
}
/middlewares/response`
);
const
dbQuery
=
require
(
'
../db/query_exec
'
);
// **Temporary** solution to add where clauses that are common to all requests
function
filter
(
req
,
q
)
{
if
(
typeof
req
.
census_year
!==
'
undefined
'
)
{
q
.
where
(
'
ano_censo=?
'
,
req
.
census_year
);
}
if
(
typeof
req
.
adm_dependency_id
!==
'
undefined
'
)
{
q
.
where
(
'
fk_dependencia_adm_id=?
'
,
req
.
adm_dependency_id
);
}
if
(
typeof
req
.
location_id
!==
'
undefined
'
)
{
q
.
where
(
'
id_localizacao=?
'
,
req
.
location_id
);
}
if
(
typeof
req
.
education_level_id
!==
'
undefined
'
)
{
q
.
where
(
'
fk_etapa_ensino_id=?
'
,
req
.
education_level_id
);
}
}
/**
/**
* Complete range of the enrollments dataset
* Complete range of the enrollments dataset
...
@@ -20,12 +37,26 @@ const dbQuery = require('../db/query_exec');
...
@@ -20,12 +37,26 @@ const dbQuery = require('../db/query_exec');
* Returns a tuple of start and ending years of the complete enrollments dataset.
* Returns a tuple of start and ending years of the complete enrollments dataset.
*/
*/
enrollmentApp
.
get
(
'
/year_range
'
,
(
req
,
res
,
next
)
=>
{
enrollmentApp
.
get
(
'
/year_range
'
,
(
req
,
res
,
next
)
=>
{
req
.
sql
=
squel
.
select
().
from
(
'
turmas
'
).
field
(
'
MIN(turmas.ano_censo)
'
,
'
start_year
'
).
field
(
'
MAX(turmas.ano_censo)
'
,
'
end_year
'
).
toParam
();
req
.
sql
=
squel
.
select
()
.
from
(
'
turmas
'
)
.
field
(
'
MIN(turmas.ano_censo)
'
,
'
start_year
'
)
.
field
(
'
MAX(turmas.ano_censo)
'
,
'
end_year
'
)
.
toParam
();
next
();
next
();
},
query
,
response
(
'
range
'
));
},
query
,
response
(
'
range
'
));
/**
* Returns all educational levels avaible
*
*/
enrollmentApp
.
get
(
'
/education_level
'
,
(
req
,
res
,
next
)
=>
{
enrollmentApp
.
get
(
'
/education_level
'
,
(
req
,
res
,
next
)
=>
{
req
.
sql
=
squel
.
select
().
from
(
'
etapa_ensino
'
).
field
(
'
pk_etapa_ensino_id
'
,
'
id
'
).
field
(
'
desc_etapa
'
,
'
name
'
).
toParam
();
req
.
sql
=
squel
.
select
()
.
from
(
'
etapa_ensino
'
)
.
field
(
'
pk_etapa_ensino_id
'
,
'
id
'
)
.
field
(
'
desc_etapa
'
,
'
name
'
)
.
toParam
();
next
();
next
();
},
query
,
response
(
'
education_level
'
));
},
query
,
response
(
'
education_level
'
));
...
@@ -70,45 +101,14 @@ enrollmentApp.use('/', (req, res, next) => {
...
@@ -70,45 +101,14 @@ enrollmentApp.use('/', (req, res, next) => {
const
params
=
req
.
query
;
const
params
=
req
.
query
;
if
(
typeof
params
.
aggregate
!==
'
undefined
'
&&
params
.
aggregate
===
'
region
'
)
{
if
(
typeof
params
.
aggregate
!==
'
undefined
'
&&
params
.
aggregate
===
'
region
'
)
{
log
.
debug
(
'
Using enrollments query for regions
'
);
log
.
debug
(
'
Using enrollments query for regions
'
);
req
.
sqlQuery
=
'
SELECT r.nome AS name, COALESCE(SUM(t.num_matriculas), 0) AS total
'
const
q
=
squel
.
select
().
from
(
'
mat_regioes
'
).
field
(
'
name
'
).
field
(
'
SUM(total)
'
,
'
total
'
);
+
'
FROM regioes AS r
'
+
'
INNER JOIN estados AS e ON r.pk_regiao_id = e.fk_regiao_id
'
+
'
INNER JOIN municipios AS m ON e.pk_estado_id = m.fk_estado_id
'
+
'
LEFT OUTER JOIN turmas AS t ON (
'
+
'
m.pk_municipio_id = t.fk_municipio_id
'
;
req
.
sqlQueryParams
=
[];
if
(
typeof
req
.
census_year
!==
'
undefined
'
)
{
req
.
sqlQuery
+=
'
AND
'
;
req
.
sqlQuery
+=
'
t.ano_censo = ?
'
;
req
.
sqlQueryParams
.
push
(
req
.
census_year
);
}
if
(
typeof
req
.
adm_dependency_id
!==
'
undefined
'
)
{
req
.
sqlQuery
+=
'
AND
'
;
req
.
sqlQuery
+=
'
t.fk_dependencia_adm_id = ?
'
;
req
.
sqlQueryParams
.
push
(
req
.
adm_dependency_id
);
}
if
(
typeof
req
.
location_id
!==
'
undefined
'
)
{
filter
(
q
);
req
.
sqlQuery
+=
'
AND
'
;
req
.
sqlQuery
+=
'
t.id_localizacao = ?
'
;
req
.
sqlQueryParams
.
push
(
req
.
location_id
);
}
if
(
typeof
req
.
education_level_id
!==
'
undefined
'
)
{
req
.
sqlQuery
+=
'
AND
'
;
req
.
sqlQuery
+=
'
t.fk_etapa_ensino_id = ?
'
;
req
.
sqlQueryParams
.
push
(
req
.
education_level_id
);
}
req
.
sqlQuery
+=
'
)
'
;
if
(
typeof
req
.
id
!==
'
undefined
'
)
{
if
(
typeof
req
.
id
!==
'
undefined
'
)
{
req
.
sqlQuery
+=
'
WHERE
'
;
q
.
where
(
'
pk_regiao_id=?
'
,
req
.
id
);
req
.
sqlQuery
+=
'
r.pk_regiao_id = ?
'
;
req
.
sqlQueryParams
.
push
(
req
.
id
);
}
}
req
.
sql
Query
+=
'
GROUP BY r.nome
'
;
req
.
sql
=
q
.
group
(
'
name
'
).
toParam
()
;
}
}
next
();
next
();
});
});
...
@@ -117,46 +117,14 @@ enrollmentApp.use('/', (req, res, next) => {
...
@@ -117,46 +117,14 @@ enrollmentApp.use('/', (req, res, next) => {
const
params
=
req
.
query
;
const
params
=
req
.
query
;
if
(
typeof
params
.
aggregate
!==
'
undefined
'
&&
params
.
aggregate
===
'
state
'
)
{
if
(
typeof
params
.
aggregate
!==
'
undefined
'
&&
params
.
aggregate
===
'
state
'
)
{
log
.
debug
(
'
Using enrollments query for states
'
);
log
.
debug
(
'
Using enrollments query for states
'
);
req
.
sqlQuery
=
'
SELECT e.nome AS name, COALESCE(SUM(t.num_matriculas), 0) as total
'
const
q
=
squel
.
select
().
from
(
'
mat_estados
'
).
field
(
'
name
'
).
field
(
'
SUM(total)
'
,
'
total
'
);
+
'
FROM estados AS e
'
+
'
INNER JOIN municipios AS m ON m.fk_estado_id = e.pk_estado_id
'
+
'
LEFT OUTER JOIN turmas AS t ON (
'
+
'
m.pk_municipio_id = t.fk_municipio_id
'
;
req
.
sqlQueryParams
=
[];
if
(
typeof
req
.
census_year
!==
'
undefined
'
)
{
req
.
sqlQuery
+=
'
AND
'
;
req
.
sqlQuery
+=
'
t.ano_censo = ?
'
;
req
.
sqlQueryParams
.
push
(
req
.
census_year
);
}
if
(
typeof
req
.
adm_dependency_id
!==
'
undefined
'
)
{
filter
(
req
,
q
);
req
.
sqlQuery
+=
'
AND
'
;
req
.
sqlQuery
+=
'
t.fk_dependencia_adm_id = ?
'
;
req
.
sqlQueryParams
.
push
(
req
.
adm_dependency_id
);
}
if
(
typeof
req
.
location_id
!==
'
undefined
'
)
{
req
.
sqlQuery
+=
'
AND
'
;
req
.
sqlQuery
+=
'
t.id_localizacao = ?
'
;
req
.
sqlQueryParams
.
push
(
req
.
location_id
);
}
if
(
typeof
req
.
education_level_id
!==
'
undefined
'
)
{
req
.
sqlQuery
+=
'
AND
'
;
req
.
sqlQuery
+=
'
t.fk_etapa_ensino_id = ?
'
;
req
.
sqlQueryParams
.
push
(
req
.
education_level_id
);
}
req
.
sqlQuery
+=
'
)
'
;
if
(
typeof
req
.
id
!==
'
undefined
'
)
{
if
(
typeof
req
.
id
!==
'
undefined
'
)
{
req
.
sqlQuery
+=
'
WHERE
'
;
q
.
where
(
'
pk_estado_id=?
'
,
req
.
id
);
req
.
sqlQuery
+=
'
e.pk_estado_id = ?
'
;
req
.
sqlQueryParams
.
push
(
req
.
id
);
}
}
req
.
sql
=
q
.
group
(
'
name
'
).
toParam
();
req
.
sqlQuery
+=
'
GROUP BY e.nome
'
;
}
}
next
();
next
();
});
});
...
@@ -165,45 +133,14 @@ enrollmentApp.use('/', (req, res, next) => {
...
@@ -165,45 +133,14 @@ enrollmentApp.use('/', (req, res, next) => {
const
params
=
req
.
query
;
const
params
=
req
.
query
;
if
(
typeof
params
.
aggregate
!==
'
undefined
'
&&
params
.
aggregate
===
'
city
'
)
{
if
(
typeof
params
.
aggregate
!==
'
undefined
'
&&
params
.
aggregate
===
'
city
'
)
{
log
.
debug
(
'
Using enrollments query for cities
'
);
log
.
debug
(
'
Using enrollments query for cities
'
);
req
.
sqlQuery
=
'
SELECT m.nome AS name, COALESCE(SUM(t.num_matriculas), 0) as total
'
const
q
=
squel
.
select
().
from
(
'
mat_estados
'
).
field
(
'
name
'
).
field
(
'
SUM(total)
'
,
'
total
'
);
+
'
FROM municipios AS m
'
+
'
LEFT OUTER JOIN turmas AS t ON (
'
+
'
m.pk_municipio_id = t.fk_municipio_id
'
;
req
.
sqlQueryParams
=
[];
if
(
typeof
req
.
census_year
!==
'
undefined
'
)
{
req
.
sqlQuery
+=
'
AND
'
;
req
.
sqlQuery
+=
'
t.ano_censo = ?
'
;
req
.
sqlQueryParams
.
push
(
req
.
census_year
);
}
if
(
typeof
req
.
adm_dependency_id
!==
'
undefined
'
)
{
filter
(
req
,
q
);
req
.
sqlQuery
+=
'
AND
'
;
req
.
sqlQuery
+=
'
t.fk_dependencia_adm_id = ?
'
;
req
.
sqlQueryParams
.
push
(
req
.
adm_dependency_id
);
}
if
(
typeof
req
.
location_id
!==
'
undefined
'
)
{
req
.
sqlQuery
+=
'
AND
'
;
req
.
sqlQuery
+=
'
t.id_localizacao = ?
'
;
req
.
sqlQueryParams
.
push
(
req
.
location_id
);
}
if
(
typeof
req
.
education_level_id
!==
'
undefined
'
)
{
req
.
sqlQuery
+=
'
AND
'
;
req
.
sqlQuery
+=
'
t.fk_etapa_ensino_id = ?
'
;
req
.
sqlQueryParams
.
push
(
req
.
education_level_id
);
}
req
.
sqlQuery
+=
'
)
'
;
if
(
typeof
req
.
id
!==
'
undefined
'
)
{
if
(
typeof
req
.
id
!==
'
undefined
'
)
{
req
.
sqlQuery
+=
'
WHERE
'
;
q
.
where
(
'
pk_municipio_id=?
'
,
req
.
id
);
req
.
sqlQuery
+=
'
m.pk_municipio_id = ?
'
;
req
.
sqlQueryParams
.
push
(
req
.
id
);
}
}
req
.
sql
=
q
.
group
(
'
name
'
).
toParam
();
req
.
sqlQuery
+=
'
GROUP BY m.nome
'
;
}
}
next
();
next
();
});
});
...
@@ -212,67 +149,42 @@ enrollmentApp.use('/', (req, res, next) => {
...
@@ -212,67 +149,42 @@ enrollmentApp.use('/', (req, res, next) => {
const
params
=
req
.
query
;
const
params
=
req
.
query
;
if
(
typeof
params
.
aggregate
===
'
undefined
'
)
{
if
(
typeof
params
.
aggregate
===
'
undefined
'
)
{
log
.
debug
(
'
Using enrollments query for the whole country
'
);
log
.
debug
(
'
Using enrollments query for the whole country
'
);
req
.
sqlQuery
=
'
SELECT
\'
Brasil
\'
AS name, COALESCE(SUM(t.num_matriculas),0) AS total
'
const
q
=
squel
.
select
().
from
(
'
turmas
'
).
field
(
"
'Brasil'
"
,
'
name
'
)
+
'
FROM turmas AS t
'
;
.
field
(
'
COALESCE(SUM(num_matriculas),0)
'
,
'
total
'
);
req
.
sqlQueryParams
=
[];
if
(
req
.
paramCnt
>
0
)
{
filter
(
req
,
q
);
req
.
sqlQuery
+=
'
WHERE
'
;
}
if
(
typeof
req
.
census_year
!==
'
undefined
'
)
{
req
.
sqlQuery
+=
'
t.ano_censo = ?
'
;
req
.
sqlQueryParams
.
push
(
req
.
census_year
);
}
if
(
typeof
req
.
adm_dependency_id
!==
'
undefined
'
)
{
req
.
sql
=
q
.
toParam
();
if
(
req
.
sqlQueryParams
.
length
>
0
)
{
req
.
sqlQuery
+=
'
AND
'
;
}
req
.
sqlQuery
+=
'
t.fk_dependencia_adm_id = ?
'
;
req
.
sqlQueryParams
.
push
(
req
.
adm_dependency_id
);
}
if
(
typeof
req
.
location_id
!==
'
undefined
'
)
{
if
(
req
.
sqlQueryParams
.
length
>
0
)
{
req
.
sqlQuery
+=
'
AND
'
;
}
req
.
sqlQuery
+=
'
t.id_localizacao = ?
'
;
req
.
sqlQueryParams
.
push
(
req
.
location_id
);
}
if
(
typeof
req
.
education_level_id
!==
'
undefined
'
)
{
if
(
req
.
sqlQueryParams
.
length
>
0
)
{
req
.
sqlQuery
+=
'
AND
'
;
}
req
.
sqlQuery
+=
'
t.fk_etapa_ensino_id = ?
'
;
req
.
sqlQueryParams
.
push
(
req
.
education_level_id
);
}
}
}
next
();
next
();
});
});
enrollmentApp
.
get
(
'
/
'
,
(
req
,
res
,
next
)
=>
{
enrollmentApp
.
get
(
'
/
'
,
(
req
,
res
,
next
)
=>
{
log
.
debug
(
`Request parameters:
${
req
}
`
);
log
.
debug
(
`Request parameters:
${
req
}
`
);
if
(
typeof
req
.
sqlQuery
===
'
undefined
'
)
{
next
();
// Should only happen if there is a bug in the chaining of the
},
query
,
response
(
'
enrollments
'
));
// '/enrollments' route, since when no +aggregate+ parameter is given,
// it defaults to use the query for the whole country.
// enrollmentApp.get('/', (req, res, next) => {
log
.
error
(
'
BUG -- No SQL query was found to be executed!
'
);
// log.debug(`Request parameters: ${req}`);
next
(
'
Internal error, request could not be satisfied at this moment. Please,
'
// if (typeof req.sqlQuery === 'undefined') {
+
'
try again later
'
);
// // Should only happen if there is a bug in the chaining of the
}
else
{
// // '/enrollments' route, since when no +aggregate+ parameter is given,
log
.
debug
(
'
SQL query: ${ req.sqlQuery }?
'
);
// // it defaults to use the query for the whole country.
log
.
debug
(
req
.
sqlQuery
);
// log.error('BUG -- No SQL query was found to be executed!');
dbQuery
(
req
.
sqlQuery
).
then
((
result
)
=>
{
// next('Internal error, request could not be satisfied at this moment. Please, '
req
.
result
=
result
;
// + 'try again later');
return
response
(
req
,
res
);
// } else {
},
(
error
)
=>
{
// log.debug('SQL query: ${ req.sqlQuery }?');
log
.
error
(
`[
${
req
.
originalUrl
}
] SQL query error:
${
error
}
`
);
// log.debug(req.sqlQuery);
next
(
'
Internal error, request could not be satisfied at this moment. Please,
'
// dbQuery(req.sqlQuery).then((result) => {
+
'
try again later
'
);
// req.result = result;
});
// return response(req, res);
}
// }, (error) => {
});
// log.error(`[${req.originalUrl}] SQL query error: ${error}`);
// next('Internal error, request could not be satisfied at this moment. Please, '
// + 'try again later');
// });
// }
// });
module
.
exports
=
enrollmentApp
;
module
.
exports
=
enrollmentApp
;
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment