Skip to content

Commit

Permalink
whatif
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Alloza committed Jan 4, 2024
1 parent 8b2f51d commit d3f2a5e
Show file tree
Hide file tree
Showing 16 changed files with 298 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,13 @@ export class MySqlBuilderService extends QueryBuilderService {
this.queryTODO.fields.forEach(el => {
el.order !== 0 && el.table_id !== origin && !dest.includes(el.table_id) ? dest.push(el.table_id) : false;

if (!el.hasOwnProperty('minimumFractionDigits')) {
el.minimumFractionDigits = 0;
}
const table_column = `\`${el.table_id}\`.\`${el.column_name}\``;

let whatIfExpression = '';
if (el.whatif_column) whatIfExpression = `${el.whatif.operator} ${el.whatif.value}`;

el.minimumFractionDigits = el.minimumFractionDigits || 0;

// Aqui se manejan las columnas calculadas
if (el.computed_column === 'computed') {
if(el.column_type=='text'){
Expand Down Expand Up @@ -228,72 +232,72 @@ export class MySqlBuilderService extends QueryBuilderService {
} else {
if (el.aggregation_type !== 'none') {
if (el.aggregation_type === 'count_distinct') {
columns.push(`cast( count( distinct \`${el.table_id}\`.\`${el.column_name}\`) as decimal(32,${el.minimumFractionDigits||0}) ) as \`${el.display_name}\``);
columns.push(`cast(count(distinct ${table_column}) as decimal(32,${el.minimumFractionDigits})) ${whatIfExpression} as \`${el.display_name}\``);
} else {
columns.push(`cast(${el.aggregation_type}(\`${el.table_id}\`.\`${el.column_name}\`) as decimal(32,${el.minimumFractionDigits||0}) ) as \`${el.display_name}\``);
columns.push(`cast(${el.aggregation_type}(${table_column}) as decimal(32,${el.minimumFractionDigits})) ${whatIfExpression} as \`${el.display_name}\``);
}
} else {
if (el.column_type === 'numeric') {
columns.push(`cast(\`${el.table_id}\`.\`${el.column_name}\` as decimal(32,${el.minimumFractionDigits})) as \`${el.display_name}\``);
columns.push(`cast(${table_column} as decimal(32,${el.minimumFractionDigits})) ${whatIfExpression} as \`${el.display_name}\``);
} else if (el.column_type === 'date') {
if (el.format) {
if (_.isEqual(el.format, 'year')) {
columns.push(`DATE_FORMAT(\`${el.table_id}\`.\`${el.column_name}\`, '%Y') as \`${el.display_name}\``);
columns.push(`DATE_FORMAT(${table_column}, '%Y') as \`${el.display_name}\``);
} else if (_.isEqual(el.format, 'quarter')) {
columns.push( `concat( concat( year(\`${el.table_id}\`.\`${el.column_name}\`),'-Q' ), quarter(\`${el.table_id}\`.\`${el.column_name}\`) ) as \`${el.display_name}\`` );
columns.push( `concat( concat( year(${table_column}),'-Q' ), quarter(${table_column}) ) as \`${el.display_name}\`` );
} else if (_.isEqual(el.format, 'month')) {
columns.push(`DATE_FORMAT(\`${el.table_id}\`.\`${el.column_name}\`, '%Y-%m') as \`${el.display_name}\``);
columns.push(`DATE_FORMAT(${table_column}, '%Y-%m') as \`${el.display_name}\``);
} else if (_.isEqual(el.format, 'week')) {
columns.push(`DATE_FORMAT(\`${el.table_id}\`.\`${el.column_name}\`, '%x-%v') as \`${el.display_name}\``);
columns.push(`DATE_FORMAT(${table_column}, '%x-%v') as \`${el.display_name}\``);
} else if (_.isEqual(el.format, 'day')) {
columns.push(`DATE_FORMAT(\`${el.table_id}\`.\`${el.column_name}\`, '%Y-%m-%d') as \`${el.display_name}\``);
columns.push(`DATE_FORMAT(${table_column}, '%Y-%m-%d') as \`${el.display_name}\``);
} else if (_.isEqual(el.format, 'week_day')) {
columns.push(`WEEKDAY(\`${el.table_id}\`.\`${el.column_name}\`) + 1 as \`${el.display_name}\``);
columns.push(`WEEKDAY(${table_column}) + 1 as \`${el.display_name}\``);
}else if (_.isEqual(el.format, 'day_hour')) {
columns.push(`DATE_FORMAT(\`${el.table_id}\`.\`${el.column_name}\`, '%Y-%m-%d %H') as \`${el.display_name}\``);
columns.push(`DATE_FORMAT(${table_column}, '%Y-%m-%d %H') as \`${el.display_name}\``);
}else if (_.isEqual(el.format, 'day_hour_minute')) {
columns.push(`DATE_FORMAT(\`${el.table_id}\`.\`${el.column_name}\`, '%Y-%m-%d %H:%i') as \`${el.display_name}\``);
columns.push(`DATE_FORMAT(${table_column}, '%Y-%m-%d %H:%i') as \`${el.display_name}\``);
}else if (_.isEqual(el.format, 'timestamp')) {
columns.push(`DATE_FORMAT(\`${el.table_id}\`.\`${el.column_name}\`, '%Y-%m-%d %H:%i:%s') as \`${el.display_name}\``);
columns.push(`DATE_FORMAT(${table_column}, '%Y-%m-%d %H:%i:%s') as \`${el.display_name}\``);
} else {
columns.push(`DATE_FORMAT(\`${el.table_id}\`.\`${el.column_name}\`, '%Y-%m-%d') as \`${el.display_name}\``);
columns.push(`DATE_FORMAT(${table_column}, '%Y-%m-%d') as \`${el.display_name}\``);
}
} else {
columns.push(`DATE_FORMAT(\`${el.table_id}\`.\`${el.column_name}\`, '%Y-%m-%d') as \`${el.display_name}\``);
columns.push(`DATE_FORMAT(${table_column}, '%Y-%m-%d') as \`${el.display_name}\``);
}
} else {

columns.push(`\`${el.table_id}\`.\`${el.column_name}\` as \`${el.display_name}\``);
columns.push(`${table_column} as \`${el.display_name}\``);

}

// GROUP BY
if (el.format) {
if (_.isEqual(el.format, 'year')) {
grouping.push(`DATE_FORMAT(\`${el.table_id}\`.\`${el.column_name}\`, '%Y')`);
grouping.push(`DATE_FORMAT(${table_column}, '%Y')`);
} else if (_.isEqual(el.format, 'quarter')) {
grouping.push( `concat( concat( year(\`${el.table_id}\`.\`${el.column_name}\`),'-Q' ), quarter(\`${el.table_id}\`.\`${el.column_name}\`) ) ` );
grouping.push( `concat( concat( year(${table_column}),'-Q' ), quarter(${table_column}) ) ` );
} else if (_.isEqual(el.format, 'month')) {
grouping.push(`DATE_FORMAT(\`${el.table_id}\`.\`${el.column_name}\`, '%Y-%m')`);
grouping.push(`DATE_FORMAT(${table_column}, '%Y-%m')`);
} else if (_.isEqual(el.format, 'week')) {
grouping.push(`DATE_FORMAT(\`${el.table_id}\`.\`${el.column_name}\`, '%x-%v')`);
grouping.push(`DATE_FORMAT(${table_column}, '%x-%v')`);
} else if (_.isEqual(el.format, 'week_day')) {
grouping.push(`WEEKDAY(\`${el.table_id}\`.\`${el.column_name}\`) + 1`);
grouping.push(`WEEKDAY(${table_column}) + 1`);
} else if (_.isEqual(el.format, 'day')) {
grouping.push(`DATE_FORMAT(\`${el.table_id}\`.\`${el.column_name}\`, '%Y-%m-%d')`);
grouping.push(`DATE_FORMAT(${table_column}, '%Y-%m-%d')`);
}else if (_.isEqual(el.format, 'day_hour')) {
columns.push(`DATE_FORMAT(\`${el.table_id}\`.\`${el.column_name}\`, '%Y-%m-%d %H') `);
columns.push(`DATE_FORMAT(${table_column}, '%Y-%m-%d %H') `);
}else if (_.isEqual(el.format, 'day_hour_minute')) {
grouping.push(`DATE_FORMAT(\`${el.table_id}\`.\`${el.column_name}\`, '%Y-%m-%d %H:%i') `);
grouping.push(`DATE_FORMAT(${table_column}, '%Y-%m-%d %H:%i') `);
}else if (_.isEqual(el.format, 'timestamp')) {
grouping.push(`DATE_FORMAT(\`${el.table_id}\`.\`${el.column_name}\`, '%Y-%m-%d %H:%i:%s')`);
grouping.push(`DATE_FORMAT(${table_column}, '%Y-%m-%d %H:%i:%s')`);
} else {
grouping.push(`\`${el.table_id}\`.\`${el.column_name}\``);
grouping.push(`${table_column}`);
}
} else {
// Si es una única columna numérica no se agrega.
if( this.queryTODO.fields.length > 1 || el.column_type != 'numeric' ){
grouping.push(`\`${el.table_id}\`.\`${el.column_name}\``);
grouping.push(`${table_column}`);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions eda/eda_app/src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ <h6 [innerHTML]="message.detail"></h6>

<div [ngClass]="{'lock-on': displaySpinner, 'lock-off': !displaySpinner}"></div>
<p-progressSpinner strokeWidth="5" *ngIf="displaySpinner" class="spinner-load"></p-progressSpinner>

<router-outlet></router-outlet>
3 changes: 2 additions & 1 deletion eda/eda_app/src/app/module/components/component.index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ export { Knob } from './eda-knob/primengKnob/knob';
export { FunnelDialog } from './eda-panels/eda-blank-panel/funnel-dialog/funnel-dialog.component';
export { BubblechartDialog } from './eda-panels/eda-blank-panel/bubblechart-dialog/bubblechart-dialog.component';
export { EdaSunburstComponent } from './eda-sunburst/eda-sunburst.component';
export { SunburstDialogComponent } from './eda-panels/eda-blank-panel/sunbrust-dialog/sunburst-dialog.component';
export { SunburstDialogComponent } from './eda-panels/eda-blank-panel/sunbrust-dialog/sunburst-dialog.component';
export { WhatIfDialogComponent } from './eda-panels/eda-blank-panel/whatif-dialog/whatif-dialog.component';
10 changes: 6 additions & 4 deletions eda/eda_app/src/app/module/components/components.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ import {
BubblechartDialog,
CumSumAlertDialogComponent,
EdaSunburstComponent,
SunburstDialogComponent

SunburstDialogComponent,
WhatIfDialogComponent
} from './component.index';
import { SafeUrlPipe } from './eda-panels/eda-title-panel/urlSanitizer.pipe';
import { SafeHtmlPipe } from './eda-panels/eda-title-panel/htmlSanitizer.pipe';
Expand Down Expand Up @@ -96,7 +96,8 @@ import { SafeHtmlPipe } from './eda-panels/eda-title-panel/htmlSanitizer.pipe';
EdaBubblechartComponent,
BubblechartDialog,
EdaSunburstComponent,
SunburstDialogComponent
SunburstDialogComponent,
WhatIfDialogComponent
],
exports: [
EdaBlankPanelComponent,
Expand Down Expand Up @@ -125,7 +126,8 @@ import { SafeHtmlPipe } from './eda-panels/eda-title-panel/htmlSanitizer.pipe';
FunnelDialog,
BubblechartDialog,
EdaSunburstComponent,
SunburstDialogComponent
SunburstDialogComponent,
WhatIfDialogComponent
],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,29 +166,28 @@ <h4 class="mt-4" i18n="@@filtrosH4">
</div>

<div class="grid">
<div class="col-4 md:col-12 infoFiltres">
<div class="col-3 md:col-12 infoFiltres">
<span i18n="@@topQueryH4">
Mostrar Top: &nbsp;
<p-inputNumber [(ngModel)]="queryLimit" (onInput)="onTopChange()"></p-inputNumber>
</span>


</div>
<div class="col-6 md:col-12 flex infoJoin">
<div class="col-3 md:col-12 flex infoJoin">
<span i18n="@@joinTypeH4">
Tipo de intersección: &nbsp;
</span>
<p-selectButton [options]="joinTypeOptions" [(ngModel)]="joinType" (onChange)="onJoinTypeChange()" optionLabel="icon" optionValue="joinType" >
<ng-template let-item>
<i [class]="item.icon"></i>

</ng-template>
</p-selectButton>



<p-selectButton [options]="joinTypeOptions" [(ngModel)]="joinType" (onChange)="onJoinTypeChange()" optionLabel="icon" optionValue="joinType" >
<ng-template let-item>
<i [class]="item.icon"></i>

</ng-template>
</p-selectButton>
</div>

<div class="col-3 col-offset-3 md:col-12 infoFiltres d-flex justify-content-end">
<button pButton (click)="onWhatIfDialog()" icon="pi pi-question-circle" class="p-button-info p-button-outlined button-whatif ml-2" label="What IF"></button>
</div>

<div class="col-12 md:col-12 info-query" *ngIf="currentQuery.length !== 0">
Expand All @@ -215,7 +214,8 @@ <h6 i18n="@@yourQuerySQL">Mi consulta SQL</h6>

<p-progressSpinner *ngIf="display_v.minispinnerSQL"
[style]="{width: '4vw', height: '4vh'}" strokeWidth="5" class=""
fill="#EEEEEE" animationDuration=".5s"></p-progressSpinner>
fill="#EEEEEE" animationDuration=".5s">
</p-progressSpinner>

</div>

Expand Down Expand Up @@ -284,7 +284,8 @@ <h5 i18n="@@resumenFiltrosInformeH6">
<h6 i18n="@@sqlExpresionH6">Expresión SQL</h6>
<hr>
<textarea id="query-area" class="sqlInput" rows="12" cols="100" pInputTextarea
autoResize="autoResize" [(ngModel)]="currentSQLQuery"></textarea>
autoResize="autoResize" [(ngModel)]="currentSQLQuery">
</textarea>
<div class="col-6">
<h6 i18n="@@originTable">Tabla origen</h6>
<hr>
Expand Down Expand Up @@ -532,3 +533,5 @@ <h6 i18n="@@indicaciones">Indicaciones</h6>
<sunburst-dialog *ngIf="sunburstController" [controller]="sunburstController"> </sunburst-dialog>

<eda-context-menu [inject]="contextMenu"></eda-context-menu>

<app-whatif-dialog *ngIf="display_v.whatIf_dialog" [(currentQuery)]="currentQuery" (close)="onCloseWhatIfDialog()"></app-whatif-dialog>
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export class EdaBlankPanelComponent implements OnInit {
saved_panel: false, // saved panel
btnSave: false, // button guardar
aggreg_dialog: false, // aggregation dialog
whatIf_dialog: false,
calendar: false, // calendars inputs
between: false, // between inputs
filterValue: true,
Expand Down Expand Up @@ -274,7 +275,6 @@ export class EdaBlankPanelComponent implements OnInit {
this.tables = _.cloneDeep(tables.allTables);
this.tablesToShow = _.cloneDeep(tables.tablesToShow);
this.sqlOriginTables = _.cloneDeep(tables.sqlOriginTables);

}

/**
Expand Down Expand Up @@ -309,35 +309,60 @@ export class EdaBlankPanelComponent implements OnInit {
* @param panelContent panel content to build configuration .
*/

buildGlobalconfiguration(panelContent: any) {
public buildGlobalconfiguration(panelContent: any) {
if (!panelContent.query.query.modeSQL) {
try{
const queryTables = [...new Set(panelContent.query.query.fields.map((field) => field.table_id))];
const queryTables = [...new Set(panelContent.query.query.fields.map((field: any) => field.table_id))];

for (const idTable of queryTables) {
const table = this.tables.find(t => t.table_name === idTable);
// Init columns from table
PanelInteractionUtils.loadColumns(this, table);
panelContent.query.query.fields.forEach((el) => {
const column = this.columns.find(c => c.table_id === el.table_id && c.column_name === el.column_name && c.display_name.default === el.display_name);
if (column) PanelInteractionUtils.moveItem(this, column);
else {
if(el.table_id === idTable ){
let duplicatedColumn = _.cloneDeep( this.currentQuery.find(c => c.table_id === el.table_id && c.column_name === el.column_name ) ); // es una columna duplicada que es a la consulta

for (const contentColumn of panelContent.query.query.fields) {
const column = this.columns.find(c =>
c.table_id === contentColumn.table_id &&
c.column_name === contentColumn.column_name &&
c.display_name.default === contentColumn.display_name
);

if (column) {
column.whatif_column = contentColumn.whatif_column || false;
column.whatif = contentColumn.whatif || {};
PanelInteractionUtils.moveItem(this, column);
} else {
if(contentColumn.table_id === idTable) {
let duplicatedColumn = _.cloneDeep(
this.currentQuery.find(c =>
c.table_id === contentColumn.table_id &&
c.column_name === contentColumn.column_name
)
);

if(!duplicatedColumn){
duplicatedColumn = _.cloneDeep( this.columns.find(c => c.table_id === el.table_id && c.column_name === el.column_name ) ); // es una columna duplicada sense original a la consulta
duplicatedColumn = _.cloneDeep(
this.columns.find(c =>
c.table_id === contentColumn.table_id &&
c.column_name === contentColumn.column_name
)
);
}
if(duplicatedColumn){
duplicatedColumn.display_name.default = el.display_name;

PanelInteractionUtils.handleAggregationType4DuplicatedColumns( this, duplicatedColumn);
this.currentQuery.push(duplicatedColumn); // Moc la columna directament perque es una duplicada.... o no....
if(duplicatedColumn){
duplicatedColumn.display_name.default = contentColumn.display_name;
duplicatedColumn.whatif_column = contentColumn.whatif_column || false;
duplicatedColumn.whatif = contentColumn.whatif || {};
PanelInteractionUtils.handleAggregationType4DuplicatedColumns(this, duplicatedColumn);
// Moc la columna directament perque es una duplicada.... o no....
this.currentQuery.push(duplicatedColumn);
}
}

}
});
}
}

}
this.columns = this.columns.filter((c) => !c.isdeleted);
}catch(e){
} catch(e) {
console.error('Error loading columns to define query in blank panel compoment........ Do you have deleted any column?????');
console.error(e);
}
Expand All @@ -355,8 +380,6 @@ export class EdaBlankPanelComponent implements OnInit {

this.display_v.saved_panel = true;
this.display_v.minispinner = false;


}


Expand Down Expand Up @@ -1010,4 +1033,13 @@ export class EdaBlankPanelComponent implements OnInit {
public getNiceTableName( table ){
return this.tables.find( t => t.table_name === table).display_name.default;
}

public onWhatIfDialog(): void {
this.display_v.whatIf_dialog = true;
}

public onCloseWhatIfDialog(): void {
this.display_v.whatIf_dialog = false;
}

}
Loading

0 comments on commit d3f2a5e

Please sign in to comment.