monitor-dashboard/src/app/utils/indicator-utils.ts

131 lines
5.0 KiB
TypeScript

import {ChartHelper, IndicatorPath} from "./entities/stakeholder";
export class IndicatorUtils {
chartTypes: Map<string, string> = new Map([
['pie', 'pie_chart'],
['table', 'table_chart'],
['line', 'show_chart'],
['column', 'bar_chart'],
['bar', 'bar_chart'],
['image', 'perm_media']
]);
isPublicIcon: Map<boolean, string> = new Map([
[true, 'public'],
[false, 'lock']
]);
isActiveIcon: string = 'brightness_1';
public getFullUrl(indicatorPath: IndicatorPath): string {
let replacedUrl = indicatorPath.chartObject;
if (indicatorPath.parameters) {
Object.entries(indicatorPath.parameters).forEach((key, value) => {
replacedUrl = replacedUrl.replace(ChartHelper.prefix + key + ChartHelper.suffix, value.toString());
});
}
return indicatorPath.url + encodeURIComponent(replacedUrl);
}
generateIndicatorByChartUrl(source: string, url: string, type: string = null): IndicatorPath {
let indicatorPath = new IndicatorPath("", source, "", "", []);
if (source === 'stats-tool') {
indicatorPath.url = url.split("json=")[0] + "json=";
indicatorPath.url = indicatorPath.url.split("/")[indicatorPath.url.split("/").length - 1];
indicatorPath.chartObject = decodeURIComponent(url.split("json=")[1]);
let chart = JSON.parse(indicatorPath.chartObject);
indicatorPath.type = this.extractType(chart, indicatorPath);
this.extractTitle(chart, indicatorPath);
this.extractXTitle(chart, indicatorPath);
this.extractYTitle(chart, indicatorPath);
this.extractFunder(chart, indicatorPath);
this.extractStartYear(chart, indicatorPath);
this.extractEndYear(chart, indicatorPath);
indicatorPath.chartObject = JSON.stringify(chart);
} else if (source === 'old') {
indicatorPath.url = url.split("data=")[0] + "data=";
indicatorPath.chartObject = decodeURIComponent(url.split("data=")[1]);
} else {
indicatorPath.url = url;
indicatorPath.type = type;
}
return indicatorPath;
}
private extractType(obj, indicatorPath: IndicatorPath): string {
let defaultTypes = ["column", "bar", "pie"];
let type = obj["chartDescription"]["queries"][0]["type"];
if (defaultTypes.indexOf(type) == -1) {
type = defaultTypes [0];
} else {
obj["chartDescription"]["queries"][0]["type"] = ChartHelper.prefix + "type" + ChartHelper.suffix;
indicatorPath.parameters.set("type", type);
}
return type;
}
private extractFunder(obj, indicatorPath: IndicatorPath) {
let funderName;
for (let filter of obj["chartDescription"]["queries"][0]["query"]["filters"]) {
if (filter["groupFilters"][0]["field"].indexOf(".funder") != -1) {
funderName = filter["groupFilters"][0]["values"][0];
filter["groupFilters"][0]["values"][0] = ChartHelper.prefix + "funder_name" + ChartHelper.suffix;
indicatorPath.parameters.set("funder_name", funderName);
}
}
}
private extractStartYear(obj, indicatorPath: IndicatorPath) {
let start_year;
for (let filter of obj["chartDescription"]["queries"][0]["query"]["filters"]) {
for (let gfilter of filter["groupFilters"]) {
if (gfilter["field"].indexOf(".year") != -1 && gfilter["type"].indexOf(">") != -1) {
start_year = gfilter["values"][0];
gfilter["values"][0] = ChartHelper.prefix + "start_year" + ChartHelper.suffix;
indicatorPath.parameters.set("start_year", start_year);
}
}
}
}
private extractEndYear(obj, indicatorPath: IndicatorPath) {
let end_year;
for (let filter of obj["chartDescription"]["queries"][0]["query"]["filters"]) {
for (let gfilter of filter["groupFilters"]) {
if (gfilter["field"].indexOf(".year") != -1 && gfilter["type"].indexOf("<") != -1) {
end_year = gfilter["values"][0];
gfilter["values"][0] = ChartHelper.prefix + "end_year" + ChartHelper.suffix;
indicatorPath.parameters.set("end_year", end_year);
}
}
}
}
private extractTitle(obj, indicatorPath: IndicatorPath) {
let title = obj["chartDescription"]["title"]["text"];
if (obj["chartDescription"]["title"]) {
obj["chartDescription"]["title"]["text"] = ChartHelper.prefix + "title" + ChartHelper.suffix;
indicatorPath.parameters.set("title", title);
}
}
private extractXTitle(obj, indicatorPath: IndicatorPath) {
let title = obj["chartDescription"]["xAxis"]["title"]["text"];
if (obj["chartDescription"]["xAxis"]["title"]) {
obj["chartDescription"]["xAxis"]["title"]["text"] = ChartHelper.prefix + "xAxisTitle" + ChartHelper.suffix;
indicatorPath.parameters.set("xAxisTitle", title);
}
}
private extractYTitle(obj, indicatorPath: IndicatorPath) {
let title = obj["chartDescription"]["yAxis"]["title"]["text"];
if (obj["chartDescription"]["yAxis"]["title"]) {
obj["chartDescription"]["yAxis"]["title"]["text"] = ChartHelper.prefix + "yAxisTitle" + ChartHelper.suffix;
indicatorPath.parameters.set("yAxisTitle", title);
}
}
}