Issue #18: Add subdimensions reading to configParser

Signed-off-by: Lucas Fernandes de Oliveira's avatarLucas Fernandes de Oliveira <lfo14@inf.ufpr.br>
parent 54c2a9ff
Pipeline #10996 passed with stage
in 1 minute and 47 seconds
......@@ -20,7 +20,24 @@
import { expect } from "chai";
import { ConfigParser, ViewParsingOptions } from "./configParser";
import { ConfigParser, ViewParsingOptions, DimensionStrOptions } from "./configParser";
import { Dimension } from "../core/dimension";
import { RelationType } from "../common/types";
function strToRelationType (str: string): RelationType {
switch (str) {
case "day":
return RelationType.DAY;
case "month":
return RelationType.MONTH;
case "year":
return RelationType.YEAR;
case "dayofweek":
return RelationType.DAYOFWEEK;
default:
return RelationType.NONE;
}
}
describe("configParser utility library", () => {
let metMap = new Map();
......@@ -46,7 +63,7 @@ describe("configParser utility library", () => {
expect(error).to.be.true;
});
it("should throw expection for inexistent metric", () => {
it("should throw expection for inexistent dimension", () => {
let opts: ViewParsingOptions = {
alias: "Test",
data: "test",
......@@ -67,4 +84,75 @@ describe("configParser utility library", () => {
expect(error).to.be.true;
});
it("should throw expection for inexistent parent for subdimension", () => {
let opts: DimensionStrOptions = {
name: "dim:-1",
dataType: "integer",
parent: "dim:0",
relation: "none"
};
let dims: Dimension[] = [];
let error: boolean = false;
try {
ConfigParser.parseDimOpts(opts, dims);
}
catch (e) {
error = true;
expect(e.message).to.be
.equal("[Parsing error] Parent for subdimension " + opts.name + " not found");
}
expect(error).to.be.true;
});
it("should parse subdimentions relations correctly", () => {
let opts: DimensionStrOptions[] = [
{
name: "dim:day",
dataType: "integer",
parent: "dim:0",
relation: "day"
},
{
name: "dim:month",
dataType: "integer",
parent: "dim:0",
relation: "month"
},
{
name: "dim:year",
dataType: "integer",
parent: "dim:0",
relation: "year"
},
{
name: "dim:dayofweek",
dataType: "integer",
parent: "dim:0",
relation: "dayofweek"
},
{
name: "dim:none",
dataType: "integer",
parent: "dim:0",
relation: "none"
},
];
let dims: Dimension[] = [
new Dimension({name: "dim:a", dataType: "date"}),
new Dimension({name: "dim:0", dataType: "date"})
];
for (let i = 0; i < opts.length; ++i) {
let parsed = ConfigParser.parseDimOpts(opts[i], dims);
expect(parsed.name).to.be.equal(opts[i].name);
expect(parsed.dataType).to.be.equal(opts[i].dataType);
expect(parsed.parent).to.be.equal(dims[1]);
expect(parsed.relation).to.be.equal(strToRelationType(opts[i].relation));
}
});
});
......@@ -39,7 +39,7 @@ interface MetricStrOptions {
dataType: string;
}
interface DimensionStrOptions {
export interface DimensionStrOptions {
name: string;
dataType: string;
parent?: string;
......@@ -100,7 +100,7 @@ export class ConfigParser {
}
for (let i = 0; i < dimensionsOpts.length; ++i) {
let dim = new Dimension(this.parseDimOpts(dimensionsOpts[i]));
let dim = new Dimension(this.parseDimOpts(dimensionsOpts[i], parsed.dimensions));
parsed.dimensions.push(dim);
dimMap.set(dim.name, dim);
}
......@@ -120,8 +120,8 @@ export class ConfigParser {
}
public static parseViewOpt(opts: ViewParsingOptions,
metMap: Map<string, Metric>,
dimMap: Map<string, Dimension>): ViewOptions {
metMap: Map<string, Metric>,
dimMap: Map<string, Dimension>): ViewOptions {
let viewOpt: ViewOptions = {
metrics: [],
......@@ -152,9 +152,20 @@ export class ConfigParser {
return viewOpt;
}
private static parseDimOpts (opts: DimensionStrOptions): DimensionOptions {
public static parseDimOpts (opts: DimensionStrOptions, dims: Dimension[]): DimensionOptions {
if (opts.parent || opts.relation) {
throw new Error("Sub dimensions not implemented in config parser !!!");
for (let i = 0; i < dims.length; ++i) {
if (dims[i].name === opts.parent) {
return {
name: opts.name,
dataType: opts.dataType,
parent: dims[i],
relation: this.strToRelationType(opts.relation)
};
}
}
throw new Error("[Parsing error] Parent for subdimension " + opts.name + " not found");
}
return {
name: opts.name,
......@@ -184,4 +195,19 @@ export class ConfigParser {
return AggregationType.NONE;
}
}
private static strToRelationType (str: string): RelationType {
switch (str) {
case "day":
return RelationType.DAY;
case "month":
return RelationType.MONTH;
case "year":
return RelationType.YEAR;
case "dayofweek":
return RelationType.DAYOFWEEK;
default:
return RelationType.NONE;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment