dimension.ts 5.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * 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 <http://www.gnu.org/licenses/>.
 */

21 22
import { RelationType, DataType } from "../common/types";
import { EnumType } from "./enumType";
23

24
/** Parameters used to create a Dimension object. */
25
export interface DimensionOptions {
26
    /**  Dimension name. */
27
    name: string;
28
    /** Dimension data type. */
29
    dataType: DataType;
30
    /** Parent dimension, in case this dimension is a sub dimenion. */
31
    parent?: Dimension;
32
    /** Relationship with the parent. */
33
    relation?: RelationType;
34
    /** Breif description of what this dimension represents. */
35
    description?: string;
36
    /* Enumerable type name, used if data type is enumerable type. */
37
    enumType?: string;
38 39
}

40 41 42 43
/**
 * Parameters used to define dimension object in the configuration file.
 * Also the string description of a dimension.
 */
44
export interface DimensionStrOptions {
45
    /**  Dimension name. */
46
    name: string;
47
    /** Dimension data type. */
48
    dataType: string;
49
    /** Parent dimension, in case this dimension is a sub dimenion. */
50
    parent?: string;
51
    /** Relationship with the parent. */
52
    relation?: string;
53
    /** Breif description of what this dimension represents. */
54
    description?: string;
55 56
}

57 58 59 60 61 62 63
/**
 * 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.
 */
64
export class Dimension {
65
    /**  Dimension name. */
66
    public readonly name: string;
67
    /** Dimenion data type. */
68
    public readonly dataType: DataType;
69
    /** Parent dimension, in case this dimension is a sub dimenion. */
70
    public readonly parent: Dimension;
71
    /** Relationship with the parent. */
72
    public readonly relation: RelationType;
73
    /** Breif description of what this dimension represents. */
74
    public readonly description: string;
75
    /* Enumerable type name, used if data type is enumerable type. */
76
    public readonly enumType: string;
77

78 79 80 81
    /**
     * Creates a dimension.
     * @param options - Parameters required to create a dimension.
     */
82 83
    constructor(options: DimensionOptions) {
        this.name = options.name;
84
        this.dataType = options.dataType;
85
        this.relation = (options.relation) ? options.relation : RelationType.NONE;
86
        this.parent = (options.parent) ? options.parent : null;
87
        this.description = (options.description) ? options.description : "";
88
        this.enumType = (options.enumType) ? (options.enumType) : "";
89 90
    }

91 92 93 94
    /**
     * Creates a object with the same options used to create this
     * dimension as strings. Used to inform the API users.
     */
95 96 97 98
    public strOptions(): DimensionStrOptions {
        if (this.relation === RelationType.NONE) {
            return {
                name: this.name,
99
                dataType: (this.dataType !== DataType.NONE) ? EnumType.stringfyDataType(this.dataType) : this.enumType ,
100 101 102 103 104 105
                description: this.description
            };
        }
        else {
            return {
                name: this.name,
106
                dataType: (this.dataType !== DataType.NONE) ? EnumType.stringfyDataType(this.dataType) : this.enumType ,
107 108 109 110 111 112 113
                parent: this.parent.name,
                relation: Dimension.stringifyRelationType(this.relation),
                description: this.description
            };
        }
    }

114 115 116 117
    /**
     * Parse a string to enum(Relation Type).
     * @param r - Relation in string format.
     */
118 119 120 121 122 123 124 125 126 127 128 129 130
    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;
        }
    }

131 132 133 134
    /**
     * Parse an enum(Relation Type) to string.
     * @param r - Relation to be stringified.
     */
135 136 137 138 139 140 141 142 143 144 145
    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 "";
        }
146 147
    }
}