export: add section information

import: sends a list of sections to save instead of a list of indicators
This commit is contained in:
argirok 2022-09-08 17:45:59 +03:00
parent 62dff971f3
commit a3b2d58dc9
1 changed files with 91 additions and 72 deletions

View File

@ -833,7 +833,7 @@ export class IndicatorsComponent implements OnInit, OnDestroy, OnChanges, AfterV
}));
}
saveIndicators(indicators: Indicator[]) {
saveIndicators(sections) {
this.editing = true;
let path = [
this.stakeholder._id,
@ -841,7 +841,7 @@ export class IndicatorsComponent implements OnInit, OnDestroy, OnChanges, AfterV
this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex]._id,
this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.index]._id
];
this.subscriptions.push(this.stakeholderService.saveBulkElements(this.properties.monitorServiceAPIURL, indicators, path).subscribe(stakeholder => {
this.subscriptions.push(this.stakeholderService.saveBulkElements(this.properties.monitorServiceAPIURL, sections, path).subscribe(stakeholder => {
this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.index].charts = stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.index].charts;
this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.index].numbers = stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.index].numbers;
this.setCharts();
@ -1216,70 +1216,81 @@ export class IndicatorsComponent implements OnInit, OnDestroy, OnChanges, AfterV
this.showCheckForSchemaEnhancements = this.isAdministrator && url && !this.properties.useOldStatisticsSchema && this.indicatorUtils.checkForSchemaEnhancements(url);
}
importIndicatorsAndSave(charts: any[]) {
let indicators: Indicator[] = [];
importIndicatorsAndSave(charts: any[]) {
let sectionsToSave: Section[] = [];
let countIndicators = 0;
// name description additionalDescription, height, width, visibility
let noValidParams = 0;
let duplicates = 0;
for (let chart of charts) {
let exists = false;
let indicatorPath;
// validate indicators' schema from file
let invalid_file_message = "";
if (!chart.type) {
invalid_file_message = "No indicator type is specified. Type should be chart or number.";
} else if (chart.type != "chart" && chart.type != "number") {
invalid_file_message = "Invalid indicator type. Type should be chart or number.";
} else if (chart.type == "number" && !chart.jsonPath) {
invalid_file_message = "No jsonPath is specified for number indicator."
} else if (!chart.url) {
invalid_file_message = "No indicator url is specified.";
}
if (invalid_file_message) {
UIkit.notification(invalid_file_message, {
status: 'danger',
timeout: 6000,
pos: 'bottom-right'
});
this.editing = false;
this.importLoading = false;
break;
}
if (chart.type == "chart") {
indicatorPath = this.indicatorUtils.generateIndicatorByChartUrl(this.statisticsService.getChartSource(chart.url), chart.url, chart.type, this.stakeholder);
for (let section of this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.index].charts) {
for (let chart of section.indicators) {
if (JSON.stringify(chart.indicatorPaths[0].chartObject) == JSON.stringify(indicatorPath.chartObject)) {
duplicates++;
exists = true;
}
}
for (let chart of charts) {
if(!chart['sectionIndex']){
chart['sectionIndex'] = chart['type'] == 'chart'?1:0;
}
} else if (chart.type == "number") {
indicatorPath = this.indicatorUtils.generateIndicatorByNumberUrl(this.statisticsService.getNumberSource(chart.url), chart.url, this.stakeholder,
chart.jsonPath, this.statisticsService.numberSources.get(this.statisticsService.getNumberSource(chart.url)));
for (let section of this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.index].numbers) {
for (let chart of section.indicators) {
if (JSON.stringify(chart.indicatorPaths[0].chartObject) == JSON.stringify(indicatorPath.chartObject)) {
duplicates++;
exists = true;
}
}
if(!sectionsToSave[chart['sectionIndex']]){
let sectionToSave = new Section(chart['sectionType']?chart['sectionType']:chart['type'], chart['sectionTitle']);
sectionToSave.indicators = [];
sectionsToSave[chart['sectionIndex']] = sectionToSave;
}
let exists = false;
let indicatorPath;
// validate indicators' schema from file
let invalid_file_message = "";
if (!chart.type) {
invalid_file_message = "No indicator type is specified. Type should be chart or number.";
} else if (chart.type != "chart" && chart.type != "number") {
invalid_file_message = "Invalid indicator type. Type should be chart or number.";
} else if (chart.type == "number" && !chart.jsonPath) {
invalid_file_message = "No jsonPath is specified for number indicator."
} else if (!chart.url) {
invalid_file_message = "No indicator url is specified.";
}
if (invalid_file_message) {
UIkit.notification(invalid_file_message, {
status: 'danger',
timeout: 6000,
pos: 'bottom-right'
});
this.editing = false;
this.importLoading = false;
break;
}
if (chart.type == "chart") {
indicatorPath = this.indicatorUtils.generateIndicatorByChartUrl(this.statisticsService.getChartSource(chart.url), chart.url, chart.type, this.stakeholder);
for (let section of this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.index].charts) {
for (let chart of section.indicators) {
if (JSON.stringify(chart.indicatorPaths[0].chartObject) == JSON.stringify(indicatorPath.chartObject)) {
duplicates++;
exists = true;
}
}
}
} else if (chart.type == "number") {
indicatorPath = this.indicatorUtils.generateIndicatorByNumberUrl(this.statisticsService.getNumberSource(chart.url), chart.url, this.stakeholder,
chart.jsonPath, this.statisticsService.numberSources.get(this.statisticsService.getNumberSource(chart.url)));
for (let section of this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.index].numbers) {
for (let chart of section.indicators) {
if (JSON.stringify(chart.indicatorPaths[0].chartObject) == JSON.stringify(indicatorPath.chartObject)) {
duplicates++;
exists = true;
}
}
}
}
if (!this.isStakeholderParametersValid(indicatorPath)) {
noValidParams++;
}
if (!exists) {
let i: Indicator = new Indicator(chart.name, chart.description, chart.additionalDescription, chart.type, chart.width, chart.height, "RESTRICTED", [indicatorPath]);
sectionsToSave[chart['sectionIndex']].indicators.push(i);
countIndicators ++ ;
}
}
if (!this.isStakeholderParametersValid(indicatorPath)) {
noValidParams++;
}
if (!exists) {
let i: Indicator = new Indicator(chart.name, chart.description, chart.additionalDescription, chart.type, chart.width, chart.height, "RESTRICTED", [indicatorPath]);
indicators.push(i);
}
}
if (duplicates > 0) {
UIkit.notification(duplicates + " urls already exist and will not be imported!", {
status: 'warning',
@ -1299,10 +1310,10 @@ export class IndicatorsComponent implements OnInit, OnDestroy, OnChanges, AfterV
});
this.editing = false;
this.importLoading = false;
} else if (indicators.length > 0) {
this.saveIndicators(indicators)
} else if (sectionsToSave.length > 0 && countIndicators >0) {
this.saveIndicators(sectionsToSave)
}
if (indicators.length == 0) {
if (sectionsToSave.length == 0 || countIndicators == 0) {
UIkit.notification(" No urls imported!", {
status: 'warning',
timeout: 6000,
@ -1317,34 +1328,42 @@ export class IndicatorsComponent implements OnInit, OnDestroy, OnChanges, AfterV
this.editing = true;
let indicators = [];
let index: number = 0;
let indexIndicator: number = 0;
this.numbers.forEach(section => {
section.indicators.forEach(indicator => {
indicator.indicatorPaths.forEach(indicatorPath => {
// console.debug("export number: ", this.statisticsService.getNumberUrl(indicatorPath.source, this.indicatorUtils.getFullUrl(this.stakeholder, indicatorPath)));
indicators[index] = {
indicators[indexIndicator] = {
"type": indicator.type, "name": indicator.name, "jsonPath": indicatorPath.jsonPath,
"description": indicator.description, "additionalDescription": indicator.additionalDescription,
"visibility": indicator.visibility, "width": indicator.width, "height": indicator.height,
"url": this.statisticsService.getNumberUrl(indicatorPath.source, this.indicatorUtils.getFullUrl(this.stakeholder, indicatorPath))
"url": this.statisticsService.getNumberUrl(indicatorPath.source, this.indicatorUtils.getFullUrl(this.stakeholder, indicatorPath)),
"sectionTitle" : section.title,
"sectionType": section.type,
"sectionIndex": index
};
index++;
indexIndicator++;
});
});
});
index ++ ;
});
this.charts.forEach(section => {
section.indicators.forEach(indicator => {
indicator.indicatorPaths.forEach(indicatorPath => {
// console.debug("export chart: " + this.getUrlByStakeHolder(indicatorPath));
indicators[index] = {
indicators[indexIndicator] = {
"type": indicator.type, "name": indicator.name,
"description": indicator.description, "additionalDescription": indicator.additionalDescription,
"visibility": indicator.visibility, "width": indicator.width, "height": indicator.height,
"url": this.getUrlByStakeHolder(indicatorPath)
"url": this.getUrlByStakeHolder(indicatorPath),
"sectionTitle" : section.title,
"sectionType": section.type,
"sectionIndex": index
};
index++;
indexIndicator++;
});
});
index ++ ;
});
let topic = this.stakeholder ? this.stakeholder.topics[this.topicIndex] : null;