Skip to content

Commit

Permalink
Merge branch 'reporting' into SDA#53
Browse files Browse the repository at this point in the history
  • Loading branch information
jortilles authored Jun 10, 2024
2 parents 5a8e9ed + 6739447 commit 5dddb8b
Show file tree
Hide file tree
Showing 25 changed files with 329 additions and 108 deletions.
4 changes: 4 additions & 0 deletions eda/eda_api/config/eda_api_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
//podem modificar el valor null de la bbdd per a que ens otorgui un altre valor de lectura en pantalla
null_value: ''
}
142 changes: 115 additions & 27 deletions eda/eda_api/lib/module/dashboard/dashboard.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { CachedQueryService } from '../../services/cache-service/cached-query.se
import { QueryOptions } from 'mongoose'
import ServerLogService from '../../services/server-log/server-log.service'
const cache_config = require('../../../config/cache.config')

const eda_api_config = require('../../../config/eda_api_config');
export class DashboardController {
static async getDashboards(req: Request, res: Response, next: NextFunction) {
try {
Expand Down Expand Up @@ -693,13 +693,11 @@ export class DashboardController {
mylabels.push(req.body.query.fields[c].column_name)
}
}

myQuery.simple = req.body.query.simple;
myQuery.queryLimit = req.body.query.queryLimit;
myQuery.joinType = req.body.query.joinType ? req.body.query.joinType : 'inner';



if (myQuery.fields.length == 0) {
console.log('you cannot see any data');
return res.status(200).json([['noDataAllowed'], [[]]]);
Expand All @@ -723,7 +721,65 @@ export class DashboardController {
}
}
}

let nullFilter = {};
const filters = myQuery.filters;


filters.forEach(a => {
a.filter_elements.forEach(b => {
if( b.value1){
if (
( b.value1.includes('null') || b.value1.includes('1900-01-01') )
&& b.value1.length > 1 /** Si tengo varios elementos */
&& ( a.filter_type == '=' || a.filter_type == 'in' || a.filter_type == 'like' || a.filter_type == 'between')
) {
nullFilter = {
filter_id: 'is_null',
filter_table: a.filter_table,
filter_column: a.filter_column ,
filter_type: 'is_null',
filter_elements: [{value1:['null']}],
filter_column_type: a.filter_column_type,
isGlobal: true,
applyToAll: false
}
b.value1 = b.value1.filter(c => c != 'null')
filters.push(nullFilter);
}else if ( ( b.value1.includes('null') || b.value1.includes('1900-01-01') )
&& b.value1.length > 1 /** Si tengo varios elementos */
&& ( a.filter_type == '!=' || a.filter_type == 'not_in' || a.filter_type == 'not_like' )
) {
nullFilter = {
filter_id: 'not_null',
filter_table: a.filter_table,
filter_column: a.filter_column ,
filter_type: 'not_null',
filter_elements: [{value1:['null']}],
filter_column_type: a.filter_column_type,
isGlobal: true,
applyToAll: false
}
b.value1 = b.value1.filter(c => c != 'null')
filters.push(nullFilter);
} else if (
( b.value1.includes('null') || b.value1.includes('1900-01-01') )
&& b.value1.length == 1
&& ( a.filter_type == '=' || a.filter_type == 'in' || a.filter_type == 'like' || a.filter_type == 'between')
){
a.filter_type='is_null';
} else if (
( b.value1.includes('null') || b.value1.includes('1900-01-01') )
&& b.value1.length == 1
&& ( a.filter_type == '!=' || a.filter_type == 'not_in' || a.filter_type == 'not_like')
){
a.filter_type='not_null';
}
}
})
})

myQuery.filters = filters;
const query = await connection.getQueryBuilded(
myQuery,
dataModelObject,
Expand Down Expand Up @@ -769,7 +825,8 @@ export class DashboardController {
}
})
}
const results = []

let results = []

// Normalize data here i also transform oracle numbers who come as strings to real numbers
for (let i = 0, n = getResults.length; i < n; i++) {
Expand All @@ -783,22 +840,22 @@ export class DashboardController {
if (numerics[ind] == 'true') {
const res = parseFloat(r[i])
if (isNaN(res)) {
return null
return eda_api_config.null_value;
} else {
return res
}
} else {
//això es per evitar els null trec els nulls i els canvio per '' dels lavels
if (r[i] == null == null) {
return ''
if (r[i] === null) {
return eda_api_config.null_value;
} else {
return r[i]
return r[i];
}
}
} else {
// trec els nulls i els canvio per '' dels lavels
// trec els nulls i els canvio per eda_api_config.null_value dels lavels
if (numerics[ind] != 'true' && r[i] == null) {
return ''
return eda_api_config.null_value;
} else {
return r[i];
}
Expand All @@ -807,11 +864,11 @@ export class DashboardController {
}

})
results.push(output)

results.push(output)
}
// las etiquetas son el nombre técnico...
const output = [mylabels, results]

if (output[1].length < cache_config.MAX_STORED_ROWS && cacheEnabled) {
CachedQueryService.storeQuery(req.body.model_id, query, output)
}
Expand All @@ -828,6 +885,8 @@ export class DashboardController {
} Panel:${req.body.dashboard.panel_id} DONE\n`
)



return res.status(200).json(output)

/**
Expand All @@ -847,7 +906,7 @@ export class DashboardController {
'\x1b[32m%s\x1b[0m',
`Date: ${formatDate(new Date())} Dashboard:${req.body.dashboard.dashboard_id
} Panel:${req.body.dashboard.panel_id} DONE\n`
)
)
return res.status(200).json(cachedQuery.cachedQuery.response)
}
} catch (err) {
Expand Down Expand Up @@ -954,45 +1013,74 @@ export class DashboardController {
const output = Object.keys(r).map(i => r[i])
resultsRollback.push([...output])
const tmpArray = []

output.forEach((val, index) => {

if (DashboardController.isNotNumeric(val)) {
tmpArray.push('NaN')
tmpArray.push('NaN');
if(val===null ){
output[index] = eda_api_config.null_value; // los valores nulos les canvio per un espai en blanc pero que si no tinc problemes
resultsRollback[i][index] = eda_api_config.null_value; // los valores nulos les canvio per un espai en blanc pero que si no tinc problemes
}
} else {
tmpArray.push('int')
output[index] = parseFloat(val)
if(val !== null){
output[index] = parseFloat(val);
}else{
output[index] = eda_api_config.null_value;
resultsRollback[i][index] = eda_api_config.null_value;
//output[index] = null;
}

}
})
oracleDataTypes.push(tmpArray)
results.push(output)
} else {
const output = Object.keys(r).map(i => r[i])
const output = Object.keys(r).map(i => r[i]);
output.forEach((val, index) => {
if(val===null ){
output[index] = eda_api_config.null_value;// los valores nulos les canvio per un espai en blanc pero que si no tinc problemes
resultsRollback[i][index] = eda_api_config.null_value; // los valores nulos les canvio per un espai en blanc pero que si no tinc problemes
}
})
results.push(output)
resultsRollback.push(output)
}
}




/** si tinc resultats de oracle evaluo la matriu de tipus de numero per verure si tinc enters i textos barrejats.
* miro cada valor amb el seguent per baix de la matriu. */
if (oracleDataTypes.length > 1) {
for (var i = 0; i < oracleDataTypes.length - 1; i++) {
var e = oracleDataTypes[i]
for (var j = 0; j < e.length; j++) {
if (oracleDataTypes[i][j] != oracleDataTypes[i + 1][j]) {
oracleEval = false
if(oracleDataTypes[j][0]=='int' ){
if ( oracleDataTypes[i][j] != oracleDataTypes[i + 1][j]) {
oracleEval = false
}
}
}
}
}

/** si tinc numeros barrejats. Poso el rollback */
if (oracleEval !== true) {
results = resultsRollback
}else{
// pongo a nulo los numeros nulos
for (var i = 0; i < results.length; i++) {
var e = results[i]
for (var j = 0; j < e.length; j++) {
if(oracleDataTypes[j][0]=='int' ){
if ( results[i][j] == eda_api_config.null_value ) {
results[i][j] = null;
}
}
}
}
}
const output = [labels, results]

if (output[1].length < cache_config.MAX_STORED_ROWS && cacheEnabled) {
CachedQueryService.storeQuery(req.body.model_id, query, output)
}
Expand Down Expand Up @@ -1050,10 +1138,10 @@ export class DashboardController {
if( user.role.includes('135792467811111111111110') ){
return true;
}
if(user._id== '135792467811111111111112'){
console.log('Anonymous access');
return true;
}
/*SDA CUSTOM*/if(user._id== '135792467811111111111112'){
/*SDA CUSTOM*/ console.log('Anonymous access');
/*SDA CUSTOM*/ return true;
/*SDA CUSTOM*/}



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export class MySqlBuilderService extends QueryBuilderService {
myQuery += this.getFilters(filters);



// GroupBy
if (grouping.length > 0) {
myQuery += '\ngroup by ' + grouping.join(', ');
Expand Down Expand Up @@ -89,7 +88,7 @@ export class MySqlBuilderService extends QueryBuilderService {
return myQuery;
};

public getFilters(filters): any {
public getFilters(filters): any {
if (this.permissions.length > 0) {
this.permissions.forEach(permission => { filters.push(permission); });
}
Expand All @@ -99,33 +98,21 @@ export class MySqlBuilderService extends QueryBuilderService {
filters = filters.filter(f => !equalfilters.toRemove.includes(f.filter_id));
let filtersString = `\nwhere 1 = 1 `;

filters.forEach(f => {

filters.forEach(f => {
const column = this.findColumn(f.filter_table, f.filter_column);
const colname = this.getFilterColname(column);
if (f.filter_type === 'not_null') {
if (f.filter_type === 'not_null' || f.filter_type === 'not_null_nor_empty' || f.filter_type === 'null_or_empty') {
filtersString += '\nand ' + this.filterToString(f);
} else {
/* Control de nulos... se genera la consutla de forma diferente */
let nullValueIndex = f.filter_elements[0].value1.indexOf(null);
if (nullValueIndex != - 1) {
if (f.filter_elements[0].value1.length === 1) {
/* puedo haber escogido un nulo en la igualdad */
if (f.filter_type == '=') {
filtersString += `\nand ${colname} is null `;
} else {
filtersString += `\nand ${colname} is not null `;
}
} else {
if (f.filter_type == '=') {
filtersString += `\nand (${this.filterToString(f)} or ${colname} is null) `;
} else {
filtersString += `\nand (${this.filterToString(f)} or ${colname} is not null) `;
}
if ( f.filter_type == 'is_null' && f.filter_elements[0].value1.length === 1 && filters.length >1 ) {// Si tengo varios filtors es filtro por X o es nulo.
filtersString += `\nor ${colname} is null `;
} if ( f.filter_type == 'is_null' && f.filter_elements[0].value1.length === 1 && filters.length ==1 ) { // si soolo tengo el filtro de nulo es un and poqque digo 1=1 y es nulo.
filtersString += `\nand ${colname} is null `;
} else {
filtersString += `\nand (${this.filterToString(f)} ) `;
}
} else {
filtersString += '\nand ' + this.filterToString(f);
}
}
});

Expand Down Expand Up @@ -208,7 +195,6 @@ export class MySqlBuilderService extends QueryBuilderService {
const joinString = [];
const targetTableJoin = [];

console.log('joinTree', joinTree);

for (const join of joinTree) {

Expand All @@ -225,8 +211,7 @@ export class MySqlBuilderService extends QueryBuilderService {
// Si la join no existe ya, se añade
if (!joinExists.has(`${sourceJoin}=${targetJoin}`)) {
joinExists.add(`${sourceJoin}=${targetJoin}`);
console.log('sourceJoin', sourceJoin);
console.log('targetJoin', targetJoin);


let aliasSource;
if (sourceJoin.split('.')[0] == targetJoin.split('.')[0]) {
Expand Down Expand Up @@ -255,6 +240,7 @@ export class MySqlBuilderService extends QueryBuilderService {

joinType = valueListJoins.includes(targetTable) ? 'LEFT' : joinType;


if (aliasTargetTable) {
targetJoin = `\`${aliasTargetTable}\`.\`${targetColumn}\``;
joinStr = `${joinType} JOIN \`${targetTable}\` \`${aliasTargetTable}\` ON ${sourceJoin} = ${targetJoin} `;
Expand Down Expand Up @@ -429,14 +415,13 @@ export class MySqlBuilderService extends QueryBuilderService {
* @returns filter to string.
*/
public filterToString(filterObject: any): any {

const column = this.findColumn(filterObject.filter_table, filterObject.filter_column);
const colType = filterObject.filter_column_type;
if (!column.hasOwnProperty('minimumFractionDigits')) {
column.minimumFractionDigits = 0;
}
const colname=this.getFilterColname(column);
let colType = column.column_type;


switch (this.setFilterType(filterObject.filter_type)) {
case 0:
if (filterObject.filter_type === '!=') { filterObject.filter_type = '<>' }
Expand All @@ -456,6 +441,12 @@ export class MySqlBuilderService extends QueryBuilderService {
${this.processFilter(filterObject.filter_elements[0].value1, colType)} and ${this.processFilterEndRange(filterObject.filter_elements[1].value2, colType)}`;
case 3:
return `${colname} is not null`;
case 4:
return `${colname} is null`;
case 5:
return `${colname} is not null and ${colname} != ''`;
case 6:
return `${colname} is null or ${colname} = ''`;
}
}

Expand Down Expand Up @@ -587,7 +578,7 @@ public getHavingColname(column: any){

public processFilter(filter: any, columnType: string) {
filter = filter.map(elem => {
if (elem === null || elem === undefined) return 'ihatenulos';
if (elem === null || elem === undefined || elem === 'null') return 'null'; //aqui poner 'null'
else return elem;
});

Expand Down
Loading

0 comments on commit 5dddb8b

Please sign in to comment.