Project

General

Profile

« Previous | Next » 

Revision 61425

[Monitor Dashboard|Trunk]

Add importAndSave indicators method

View differences:

modules/uoa-monitor-portal/trunk/monitor_dashboard/src/app/utils/indicator-utils.ts
547 547
      this.parameterizeDefaultQuery(chart, indicatorPath, stakeholder);
548 548
      this.extractStakeHolders(chart, indicatorPath, stakeholder);
549 549
      indicatorPath.chartObject = JSON.stringify(chart);
550
      indicatorPath.jsonPath = ["data","0","0","0"];
550
      if(!jsonPath || jsonPath.length == 0 ) {
551
        indicatorPath.jsonPath = ["data", "0", "0", "0"];
552
      }
551 553
      // this.addResultFilters(chart, indicatorPath);
552 554
    }else {
553 555
      for( let service of sourceServices){
modules/uoa-monitor-portal/trunk/monitor_dashboard/src/app/topic/indicators.component.html
1 1
<div *ngIf="stakeholder && canEdit">
2 2
  <div class="uk-text-right uk-width-expand">
3
    <button class="uk-margin-bottom uk-margin-right uk-button uk-button-primary" (click)="export_indicators()">
3
    <a class="uk-margin-bottom uk-margin-right  " uk-icon="icon:download" title="Export indicators (JSON file)"
4
       (click)="export_indicators()">
4 5
      <span class="uk-text-small">Export</span>
5
    </button>
6
    <div uk-form-custom class="js-upload">
6
    </a>
7
    <div uk-form-custom class="js-upload uk-margin-top">
7 8
      <input id="exampleInputFile" type="file" class="uk-width-medium" (change)="fileChangeEvent($event)"/>
8
      <button class="uk-button uk-button-primary uk-margin-bottom">Import (JSON file) </button>
9
      <a class=" uk-margin-bottom" uk-icon="icon:upload" title="Import indicators (JSON file)"> Import</a>
9 10
    </div>
10 11
  </div>
11 12

  
modules/uoa-monitor-portal/trunk/monitor_dashboard/src/app/topic/indicators.component.ts
566 566
          this.numberIndicatorPaths.at(index).get('result').setValue(null);
567 567
          if (this.numberIndicatorPaths.at(index).get('url').valid) {
568 568
            let indicatorPath: IndicatorPath = this.indicatorUtils.generateIndicatorByNumberUrl(this.statisticsService.getNumberSource(value), value, this.stakeholder, this.numberIndicatorPaths.at(index).get('jsonPath').value, this.statisticsService.numberSources.get(this.statisticsService.getNumberSource(value)));
569
            if ((indicatorPath.chartObject && Object.keys(indicatorPath.parameters).indexOf("index_id") == -1 && Object.keys(indicatorPath.parameters).indexOf("index_name") == -1 && Object.keys(indicatorPath.parameters).indexOf("index_shortName") == -1)
570
              || (!indicatorPath.chartObject && indicatorPath.url.indexOf("index_id") == -1 && indicatorPath.url.indexOf("index_name") == -1 && (indicatorPath.url).indexOf("index_shortName") == -1)) {
569
            if (!this.isStakeholderParametersValid(indicatorPath)) {
571 570
              // default profile
572 571
              if (this.stakeholder.defaultId == null) {
573 572
                this.urlParameterizedMessage = "This indicator couldn't be generated properly. Stakeholders based on this profile may not inherit the data correctly."
......
636 635
      this.urlSubscriptions.push(this.chartIndicatorPaths.at(index).get('url').valueChanges.subscribe(value => {
637 636
        if (this.chartIndicatorPaths.at(index).get('url').valid) {
638 637
          let indicatorPath: IndicatorPath = this.indicatorUtils.generateIndicatorByChartUrl(this.statisticsService.getChartSource(value), value, this.chartIndicatorPaths.at(index).get('type').value, this.stakeholder);
639
          if (indicatorPath.chartObject && Object.keys(indicatorPath.parameters).indexOf("index_id") == -1 && Object.keys(indicatorPath.parameters).indexOf("index_name") == -1 && Object.keys(indicatorPath.parameters).indexOf("index_shortName") == -1) {
638
          if (!this.isStakeholderParametersValid(indicatorPath)) {
640 639
            // default profile
641 640
            if (this.stakeholder.defaultId == null) {
642 641
              this.urlParameterizedMessage = "This chart couldn't be generated properly. Stakeholders based on this profile may not inherit the data correctly."
......
663 662
      }));
664 663
    }
665 664
  }
666
  
665
  private  isStakeholderParametersValid(indicatorPath:IndicatorPath) {
666
    return !((indicatorPath.chartObject && Object.keys(indicatorPath.parameters).indexOf("index_id") == -1 && Object.keys(indicatorPath.parameters).indexOf("index_name") == -1 && Object.keys(indicatorPath.parameters).indexOf("index_shortName") == -1)
667
    || (!indicatorPath.chartObject && indicatorPath.url.indexOf("index_id") == -1 && indicatorPath.url.indexOf("index_name") == -1 && (indicatorPath.url).indexOf("index_shortName") == -1));
668

  
669
  }
667 670
  private getJsonPathAsFormArray(indicatorPath: IndicatorPath): FormArray {
668 671
    let jsonPath = this.fb.array([]);
669 672
    if (indicatorPath.jsonPath) {
......
755 758
    }
756 759
    this.editNumberModal.open();
757 760
  }
758
  
761

  
759 762
  public editChartIndicatorOpen(section: Section, id = null) {
760 763
    this.urlParameterizedMessage = null;
761 764
    this.urlSubscriptions.forEach(value => {
......
892 895
      this.editing = false;
893 896
    }));
894 897
  }
895
  
898
  saveIndicators(indicators:Indicator[]) {
899
    this.editing = true;
900
    let path = [
901
      this.stakeholder._id,
902
      this.stakeholder.topics[this.topicIndex]._id,
903
      this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex]._id,
904
      this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.subcategoryIndex]._id
905
    ];
906
    this.subscriptions.push(this.stakeholderService.saveBulkElements(this.properties.monitorServiceAPIURL, indicators, path).subscribe(stakeholder => {
907
      this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.subcategoryIndex].charts = stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.subcategoryIndex].charts;
908
      this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.subcategoryIndex].numbers = stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.subcategoryIndex].numbers;
909
      this.buildSections();
910
      this.filterCharts();
911
      this.filterNumbers();
912
      this.editing = false;
913
      UIkit.notification("Success! Indicated were imported", {
914
        status: 'success',
915
        timeout: 6000,
916
        pos: 'bottom-right'
917
      });
918
    }, error => {
919
      this.chartIndicatorFb = null;
920
      UIkit.notification(error.error.message, {
921
        status: 'danger',
922
        timeout: 6000,
923
        pos: 'bottom-right'
924
      });
925
      this.editing = false;
926
    }));
927

  
928

  
929
  }
896 930
  reorderIndicators(sectionId: string, type: IndicatorType, reorder: Reorder) {
897 931
    this.editing = true;
898 932
    let path = [
......
1190 1224
    //new schema
1191 1225
    this.showCheckForSchemaEnhancements = this.isAdministrator && url && !this.properties.useOldStatisticsSchema && this.indicatorUtils.checkForSchemaEnhancements(url);
1192 1226
  }
1227
 importIndicatorsAndSave( charts:any[]){
1193 1228

  
1229
    let indicators:Indicator[] = [];
1230
   // name description additionalDescription, height, width, visibility
1231
   let noValidParams = 0;
1232
   let duplicates = 0;
1233
    for(let chart of charts) {
1234
       let exists = false;
1235
      let indicatorPath;
1236
      if (chart.type == "chart") {
1237
        indicatorPath = this.indicatorUtils.generateIndicatorByChartUrl(this.statisticsService.getChartSource(chart.url), chart.url, chart.type, this.stakeholder);
1238
        for (let section of this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.subcategoryIndex].charts) {
1239
          for (let chart of section.indicators) {
1240
            if (JSON.stringify(chart.indicatorPaths[0].chartObject) == JSON.stringify(indicatorPath.chartObject)) {
1241
              duplicates++;
1242
              exists = true;
1243
            }
1244
          }
1245

  
1246
        }
1247
      }else if (chart.type == "number") {
1248
        indicatorPath = this.indicatorUtils.generateIndicatorByNumberUrl(this.statisticsService.getNumberSource(chart.url), chart.url, this.stakeholder,
1249
          chart.jsonPath, this.statisticsService.numberSources.get(this.statisticsService.getNumberSource(chart.url)));
1250
        for (let section of this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.subcategoryIndex].numbers) {
1251
          for (let chart of section.indicators) {
1252
            if (JSON.stringify(chart.indicatorPaths[0].chartObject) == JSON.stringify(indicatorPath.chartObject)) {
1253
              duplicates++;
1254
              exists = true;
1255
            }
1256
          }
1257

  
1258
        }
1259
      }
1260
      if (!this.isStakeholderParametersValid(indicatorPath)) {
1261
        noValidParams++;
1262
      }
1263
      if (!exists) {
1264
        let i: Indicator = new Indicator(chart.name, chart.description, chart.additionalDescription, chart.type, chart.width, chart.height, chart.visibility, [indicatorPath]);
1265
        indicators.push(i);
1266
      }
1267

  
1268
    }
1269
    if(duplicates > 0 ){
1270
      UIkit.notification(duplicates + " urls already exist and will not be imported!", {
1271
        status: 'warning',
1272
        timeout: 6000,
1273
        pos: 'bottom-right'
1274
      });
1275
    }
1276
    if(noValidParams > 0 ){
1277
      let noValidMesseage = "Some indicators couldn't be generated properly. Please make sure chart data is for the current stakeholder.";
1278
      if (this.stakeholder.defaultId == null) {
1279
        noValidMesseage = "Some indicators couldn't be generated properly. Stakeholders based on this profile may not inherit the data correctly.";
1280
      }
1281
      UIkit.notification(noValidMesseage, {
1282
        status: 'danger',
1283
        timeout: 6000,
1284
        pos: 'bottom-right'
1285
      });
1286
    }else if(indicators.length > 0){
1287
      console.log(indicators);
1288
      this.saveIndicators(indicators)
1289
    }
1290
    if(indicators.length == 0){
1291
      UIkit.notification(  " No urls imported!", {
1292
        status: 'warning',
1293
        timeout: 6000,
1294
        pos: 'bottom-right'
1295
      });
1296
    }
1297
 }
1298

  
1194 1299
  public export_indicators() {
1195 1300
    console.debug("Export indicators");
1196 1301

  
......
1202 1307
        indicator.indicatorPaths.forEach(indicatorPath => {
1203 1308
          console.debug("export number: ", this.statisticsService.getNumberUrl(indicatorPath.source, this.indicatorUtils.getFullUrl(this.stakeholder, indicatorPath)));
1204 1309
          indicators[index] = {
1205
            "type": indicator.type, "name": indicator.name,
1310
            "type": indicator.type, "name": indicator.name, "jsonPath":indicatorPath.jsonPath,
1206 1311
            "description": indicator.description, "additionalDescription": indicator.additionalDescription,
1207 1312
            "visibility": indicator.visibility, "width": indicator.width, "height": indicator.height,
1208 1313
            "url": this.statisticsService.getNumberUrl(indicatorPath.source, this.indicatorUtils.getFullUrl(this.stakeholder, indicatorPath))};
......
1263 1368
    // this.loading.open();
1264 1369

  
1265 1370
    this.makeFileRequest(this.properties.utilsService + '/upload?type=json', [], this.filesToUpload).then(async (result: string) => {
1266
      // const rows = (result as any).split('\n');  // I have used space, you can use any thing.
1267
      let invalid_rows = 0;
1268 1371

  
1269
      // let chartSection = null;//this.createSection(-1, "chart");
1270
      // let numberSection = null;//this.createSection(-1, "number");
1271
      // await this.newSectionReady;
1272

  
1273 1372
      let json_result = JSON.parse(result);
1274
      for (let i = 0; i < (json_result.length); i++) {
1275
        if (json_result[i] && json_result[i] != null && json_result[i] != "") {
1276
          this.processIndicatorEntry(json_result[i]);
1277
        } else {
1278
          invalid_rows++;
1279
        }
1280
      }
1373
      this.importIndicatorsAndSave(json_result);
1281 1374
      this.endOfFetching();
1282 1375
    }, (error) => {
1283 1376
      this.enableUpload = true;
......
1311 1404
    this.enableUpload = true;
1312 1405
  }
1313 1406

  
1314
  processIndicatorEntry(indicator) {
1315
    if(indicator.type == "chart") {
1316
      console.debug("TODO Create chart for url: "+indicator.url);
1317
      //this.editChartIndicatorOpen(chartSection, null, false, indicator.url);
1318
    } else if(indicator.type == "number") {
1319
      console.debug("TODO Create number for url: "+indicator.url);
1320
      //this.editNumberIndicatorOpen(numberSection);
1321
    }
1322
    console.debug(indicator);
1323
    // this.saveIndicator(false);
1324
  }
1325 1407
}

Also available in: Unified diff