/* * Copyright (C) 2016 Centro de Computacao Cientifica e Software Livre * Departamento de Informatica - Universidade Federal do Parana * * This file is part of blend. * * blend is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * blend is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with blend. If not, see . */ import { RelationType, DataType } from "../common/types"; import { EnumType } from "./enumType"; /** Parameters used to create a Dimension object. */ export interface DimensionOptions { /** Dimension name. */ name: string; /** Dimension data type. */ dataType: DataType; /** Parent dimension, in case this dimension is a sub dimenion. */ parent?: Dimension; /** Relationship with the parent. */ relation?: RelationType; /** Breif description of what this dimension represents. */ description?: string; /* Enumerable type name, used if data type is enumerable type. */ enumType?: string; } /** * Parameters used to define dimension object in the configuration file. * Also the string description of a dimension. */ export interface DimensionStrOptions { /** Dimension name. */ name: string; /** Dimension data type. */ dataType: string; /** Parent dimension, in case this dimension is a sub dimenion. */ parent?: string; /** Relationship with the parent. */ relation?: string; /** Breif description of what this dimension represents. */ description?: string; } /** * One attribute of database that can be read. * A dimension defines a aspect or characteristic of the data. * Used in a query as a desired information and is separated from * the metrics because this two types of attributes behave differently * in the query. While dimensions are grouped, metrics are aggregated. */ export class Dimension { /** Dimension name. */ public readonly name: string; /** Dimenion data type. */ public readonly dataType: DataType; /** Parent dimension, in case this dimension is a sub dimenion. */ public readonly parent: Dimension; /** Relationship with the parent. */ public readonly relation: RelationType; /** Breif description of what this dimension represents. */ public readonly description: string; /* Enumerable type name, used if data type is enumerable type. */ public readonly enumType: string; /** * Creates a dimension. * @param options - Parameters required to create a dimension. */ constructor(options: DimensionOptions) { this.name = options.name; this.dataType = options.dataType; this.relation = (options.relation) ? options.relation : RelationType.NONE; this.parent = (options.parent) ? options.parent : null; this.description = (options.description) ? options.description : ""; this.enumType = (options.enumType) ? (options.enumType) : ""; } /** * Creates a object with the same options used to create this * dimension as strings. Used to inform the API users. */ public strOptions(): DimensionStrOptions { if (this.relation === RelationType.NONE) { return { name: this.name, dataType: (this.dataType !== DataType.NONE) ? EnumType.stringfyDataType(this.dataType) : this.enumType , description: this.description }; } else { return { name: this.name, dataType: (this.dataType !== DataType.NONE) ? EnumType.stringfyDataType(this.dataType) : this.enumType , parent: this.parent.name, relation: Dimension.stringifyRelationType(this.relation), description: this.description }; } } /** * Parse a string to enum(Relation Type). * @param r - Relation in string format. */ public static parseRelationType(r: string): RelationType { switch (r) { case "day": return RelationType.DAY; case "month": return RelationType.MONTH; case "year": return RelationType.YEAR; default: return RelationType.NONE; } } /** * Parse an enum(Relation Type) to string. * @param r - Relation to be stringified. */ public static stringifyRelationType(r: RelationType): string { switch (r) { case RelationType.DAY: return "day"; case RelationType.MONTH: return "month"; case RelationType.YEAR: return "year"; default: return ""; } } }