summaryrefslogtreecommitdiffstats
path: root/ecomp-sdk/epsdk-app-overlay/src/main/webapp/ngapp/src/app/report-run/run/run-report/run-stacked-column-report/run-stacked-column-report.component.ts
blob: ac6001c740ae15a5c9ef58598d100f08c8f4c1ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import { ChangeDetectorRef, Component, Input, OnInit } from '@angular/core';
import { ChartOptions, ChartType, ChartDataSets } from 'chart.js';
import { Label } from 'ng2-charts';
import { RunService } from '../../run.service';
import {environment} from '../../../../../../src/environments/environment'
import { NgbModal, NgbModalOptions } from '@ng-bootstrap/ng-bootstrap';
import { ErrorModalComponent } from 'src/app/modals/error-modal/error-modal.component';
import { HttpCacheService } from 'src/app/shared/services/cache.service';
import { Router } from '@angular/router';
import { ChartWizard } from 'src/app/pages/analytics/Report_List/Report/chart-wizard/chart-wizard.service';

@Component({
  selector: 'app-run-stacked-column-report',
  templateUrl: './run-stacked-column-report.component.html',
  styleUrls: ['./run-stacked-column-report.component.scss']
})
export class RunStackedColumnReportComponent implements OnInit {

  @Input('reportId') inputReportId: string;
  @Input('reportMode') reportMode: string;
  @Input('reportType') reportType: string;
  @Input('chartType') chartType: string;
  @Input('queryString') queryString: string;
  @Input('groupSelectValue') groupSelectValue: string;

  showSpinner: boolean;
  chartRunUrl: string;
  environment:any;
  modalOptions: NgbModalOptions;
  isChartAvailable = false;
  displayedColumnsArr: string[];
  hideChart = false;
  displayOptions: {}[];
  chartJson: any;
  xAxisDataColumn: any;
  yAxisDataColumn: any;
  countCoulumn: any;

  constructor(private _runService: RunService,
    private changeDetectorRefs: ChangeDetectorRef,
    public ngbModal: NgbModal,
    private httpCacheService: HttpCacheService,
    private _router: Router,
    private chartService: ChartWizard
    
  ){
    this.environment = environment;
    this.modalOptions = {
      backdrop: 'static',
      keyboard: false,
      size: 'lg'
    };
  }
  
  public barChartOptions: ChartOptions = {
    responsive: true,
    scales: {
      yAxes: [
       {
        display: true,
        scaleLabel: {
         display: true,
         labelString: "Counts",
        },
       },
      ],
      xAxes: [
       {
        scaleLabel: {
         display: true,
         labelString: "Months",
        },
       },
      ],
     },
  };

  public barChartType: ChartType = 'bar';
  public barChartLegend = true;
  public barChartPlugins = [];
  
  public barChartLabels: Label[] = [];
  public barChartData: ChartDataSets[] =  [{ data: [], label: '', stack: 'a' }];
  // public barChartData: ChartDataSets[] = [
  //     { data: [65, 59, 80, 81, 56, 55, 40], label: 'ECOMP Portal', stack: 'a' },
  //     { data: [28, 48, 40, 19, 86, 27, 90], label: 'VID', stack: 'a' },
  //     { data: [28, 48, 40, 19, 86, 27, 90], label: 'SGNGC', stack: 'a' },
  //     { data: [28, 48, 40, 19, 86, 27, 90], label: 'SDNGP', stack: 'a' },
  //     { data: [28, 48, 40, 19, 86, 27, 90], label: 'ALTS DATA Browser', stack: 'a' },
  //     { data: [50, 48, 100, 19, 86, 27, 90], label: 'ALTS UI', stack: 'a' }   
  // ];

  ngOnInit() {
    console.log("inputReportId iniside stacked Column Chart>>>> ",this.inputReportId);
    console.log("reportMode iniside stacked Column Chart>>>> ",this.reportMode);
    console.log("reportType iniside stacked Column Chart>>>> ",this.reportType);
    console.log("barChartData initial>>>> ",this.barChartData);
    this.chartService.getReportTypeData(this.inputReportId).subscribe(
      (respone) => {
          this.displayOptions = respone['displayOptions'];
          for (let dpOption = 0 ; dpOption < this.displayOptions.length ; dpOption++) {
              if (this.displayOptions[dpOption]['name'] === 'HideChart') {
                  this.hideChart = this.displayOptions[dpOption]['selected'];
              }
          }
    });

    if (!this.hideChart) {
      this.chartService.getChartData(this.inputReportId).subscribe(
          (response) => {
              this.chartJson = response;
              this.xAxisDataColumn = this.chartJson['domainAxis'];
              this.yAxisDataColumn = this.chartJson['categoryAxis'];
              this.countCoulumn = this.chartJson['primaryAxisLabel'];
              console.log("chartJson xAxisDataColumn::",this.chartJson['domainAxis']);
              console.log("chartJson yAxisDataColumn::",this.chartJson['categoryAxis']);
              console.log("chartJson primaryAxisLabel::",this.chartJson['primaryAxisLabel']);
              
      });
    }

    this.getSqlQueryResponse();
  }

  getSqlQueryResponse(){
    this._runService.getReportDataWithFormFields(this.queryString, this.inputReportId, this.groupSelectValue)
    .subscribe((response) => {
        console.log("response>>>>>inside stacked column",response);
        
        if(response && response.reportDataRows && response.reportDataRows.length >0){
          this.postFetchingReportDataFn(response);
        }

        if (response['errormessage']) {
            this.openErrorModel(response['errormessage']);
            this.showError();
            this.changeDetectorRefs.detectChanges();
        } else {
           // 
        }
    }, error => {
        this.openErrorModel('Error occurred while running report: ' + this.inputReportId);
        this.showError();
    });
  }

  postFetchingReportDataFn(response: any) {
    let app_usage_count_map  =  new Map();
    this.displayedColumnsArr = [];
    let rdr_cntr = 0;
    let monthArr = [];
    let counter = 0;
    while (response['reportDataRows'][counter]) {
      const reportDataRows = response['reportDataRows'][counter];
      let monthOfYear = '';
      monthOfYear = reportDataRows[this.xAxisDataColumn]['displayValue'];
      monthArr.push(Number(monthOfYear));
      counter++;
    }
    while (response['reportDataRows'][rdr_cntr]) {
        const reportDataRows = response['reportDataRows'][rdr_cntr];
        //console.log("reportDataRow ::",reportDataRows);
        let app_name = '';
        let usagescount = '';
        let monthOfYear = '';
        app_name = reportDataRows[this.yAxisDataColumn]['displayValue'];
        usagescount = reportDataRows[this.countCoulumn]['displayValue'];
        monthOfYear = reportDataRows[this.xAxisDataColumn]['displayValue'];
        monthArr.push(Number(monthOfYear));
        //console.log("rdr_cntr >>>>",rdr_cntr, "app_name >>>>",app_name, "usagescount >>>>",usagescount);
        if(app_usage_count_map.has(app_name)){
              let existingCountArr = app_usage_count_map.get(app_name);
              existingCountArr[Number(monthOfYear)-1] = Number(usagescount);
              app_usage_count_map.set(app_name, existingCountArr);
        }else{
              let countArray: Array<number> = [];
              for(let i=1; i<=Math.max(...monthArr); i++){
                countArray[i]=0;
              }
              countArray[Number(monthOfYear)-1] = Number(usagescount);
              app_usage_count_map.set(app_name, countArray);
        }
        
        rdr_cntr++;
    }
    /**
     * Setting max months display over x axis
     */
    let maxMonth = Math.max(...monthArr);
    for(let i=1; i <=maxMonth; i++ ){
      this.barChartLabels.push(String(i));
    }
    this.prepareChartDataModel(app_usage_count_map)
 }

 prepareChartDataModel(app_usage_count_map:any){
  console.log("app_usage_count_map",app_usage_count_map);
  if(app_usage_count_map){
    app_usage_count_map.forEach((value: Array<number>, key: string) => {
      var random_color = this.getRandomColor(); 
      const chartdata: ChartDataSets = <ChartDataSets><any>{
        "data": value,
        "label": key,
        "stack": 'a',
        "backgroundColor":random_color,
        "hoverBackgroundColor":random_color
      }
      this.barChartData.push(chartdata);
    });
    this.barChartData.shift();
    console.log("barChartData ::: ",this.barChartData);
  }
}

getRandomColor() {
  var letters = '0123456789ABCDEF';
  var color = '#';
  for (var i = 0; i < 6; i++) {
    color += letters[Math.floor(Math.random() * 16)];
  }
  return color
}

openErrorModelPopup(_message: string) {
  this.changeDetectorRefs.detectChanges();
  const modalInfoRef = this.ngbModal.open(ErrorModalComponent, this.modalOptions);
  modalInfoRef.componentInstance.message = _message;
  modalInfoRef.result.then((data) => {
      this.resetFormFieldValues(this.inputReportId);
  });
  return modalInfoRef;
}

openErrorModel(_message: string) { 
  this.changeDetectorRefs.detectChanges(); 
  const modalInfoRef = this.ngbModal.open(ErrorModalComponent, this.modalOptions);
  modalInfoRef.componentInstance.message = _message; 
  return modalInfoRef; 
}

showError() {
  this.showSpinner = false;
  this.isChartAvailable = false;
  this.changeDetectorRefs.detectChanges();
}

resetFormFieldValues(reportId) {
  this.httpCacheService.clearCache();
  this._router.navigateByUrl('v2/app/refresh', {skipLocationChange: true}).then(() =>
  this._router.navigate(['v2/run', reportId]));
}

}