configParser.ts 8.14 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) 2017 Centro de Computacao Cientifica e Software Livre
 * Departamento de Informatica - Universidade Federal do Parana
 *
 * This file is part of blendb.
 *
 * blendb 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.
 *
 * blendb 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 blendb.  If not, see <http://www.gnu.org/licenses/>.
 */

21 22
import { Metric, MetricOptions, MetricStrOptions } from "../core/metric";
import { Dimension, DimensionOptions, DimensionStrOptions } from "../core/dimension";
23
import { View, ViewOptions, LoadView } from "../core/view";
24
import { RelationType } from "../common/types";
25 26
import { Filter } from "../core/filter";
import { Clause } from "../core/clause";
27 28 29 30 31 32 33
import { PoolConfig } from "pg";
import * as fs from "fs";
import * as yaml from "js-yaml";

export interface ViewParsingOptions {
    alias: string;
    data: string;
34
    file?: string;
35
    origin?: boolean;
36 37
    dimensions: string[];
    metrics: string[];
38
    clauses?: string[];
39
    keys?: string[];
40 41 42 43
}

interface ConfigSchema {
    views: ViewParsingOptions[];
44 45
    metrics: MetricStrOptions[];
    dimensions: DimensionStrOptions[];
46 47 48 49 50 51 52 53
}

interface ConfigFile {
    connection: PoolConfig;
    struct: LoadStruct;
    schema: ConfigSchema;
}

54 55 56 57 58 59
interface BuildView {
    view: View;
    file: string;
    alias: string;
}

60 61 62 63 64 65 66 67 68 69 70 71
export interface LoadStruct{
    create: boolean;
    insert: boolean;
}

export interface ParsedConfig {
    connection: PoolConfig;
    views: View[];
    metrics: Metric[];
    dimensions: Dimension[];
    struct: LoadStruct;
    loadViews: LoadView[];
72
    buildViews: BuildView[];
73 74
}

75 76 77 78 79 80 81 82
interface DimensionMap {
    [key: string]: Dimension;
}

interface MetricMap {
    [key: string]: Metric;
}

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
export class ConfigParser {
    public static parse(configPath: string): ParsedConfig {
        let config: ConfigFile = yaml.safeLoad(fs.readFileSync(configPath, {
            encoding: "utf-8"
        }));

        let metricsOpts = config.schema.metrics;
        let viewsOpts = config.schema.views;
        let dimensionsOpts = config.schema.dimensions;
        let parsed: ParsedConfig = {
            connection: config.connection,
            views: [],
            metrics: [],
            dimensions: [],
            struct: config.struct,
98 99
            loadViews: [],
            buildViews: []
100 101
        };

102 103
        let metMap: MetricMap = {};
        let dimMap: DimensionMap = {};
104 105

        for (let i = 0; i < metricsOpts.length; ++i) {
106
            let met = new Metric(this.parseMetOpts(metricsOpts[i]));
107
            parsed.metrics.push(met);
108
            metMap[met.name] = met;
109 110 111
        }

        for (let i = 0; i < dimensionsOpts.length; ++i) {
112
            let dim = new Dimension(this.parseDimOpts(dimensionsOpts[i], parsed.dimensions));
113
            parsed.dimensions.push(dim);
114
            dimMap[dim.name] = dim;
115 116 117
        }

        for (let i = 0; i < viewsOpts.length; ++i) {
118 119 120
            if (!viewsOpts[i].metrics) {
                viewsOpts[i].metrics = [];
            }
121 122 123 124 125
            let viewOpts = ConfigParser.parseViewOpt(viewsOpts[i], metMap, dimMap);
            let view = new View(viewOpts);
            parsed.views.push(view);
            let loadView: LoadView = {view: view, data: viewsOpts[i].data};
            parsed.loadViews.push(loadView);
126 127 128 129 130 131
            const buildView: BuildView = {
                view: view,
                file: viewsOpts[i].file,
                alias: viewsOpts[i].alias
            };
            parsed.buildViews.push(buildView);
132 133 134 135 136 137
        }

        return parsed;
    }

    public static parseViewOpt(opts: ViewParsingOptions,
138 139
                               metMap: MetricMap,
                               dimMap: DimensionMap): ViewOptions {
140 141 142 143 144

        let viewOpt: ViewOptions = {
            metrics: [],
            dimensions: [],
            materialized: true,
145
            origin: opts.origin,
146
            childViews: [],
147 148
            clauses: [],
            keys: []
149 150
        };

151 152
        const keys = (opts.keys) ? opts.keys : [];

153
        for (let i = 0; i < opts.metrics.length; ++i) {
154 155
            if (metMap[opts.metrics[i]]) {
                viewOpt.metrics.push(metMap[opts.metrics[i]]);
156 157 158 159 160 161 162 163
            }

            else {
                throw new Error("[Parsing error] Non exist metric set to view " + opts.alias);
            }
        }

        for (let i = 0; i < opts.dimensions.length; ++i) {
164 165
            if (dimMap[opts.dimensions[i]]) {
                viewOpt.dimensions.push(dimMap[opts.dimensions[i]]);
166 167 168 169 170 171
            }

            else {
                throw new Error("[Parsing error] Non exist dimension set to view " + opts.alias);
            }
        }
172

173 174
        for (let i = 0; i < keys.length; ++i) {
            if (dimMap[keys[i]]) {
175
                viewOpt.keys.push(dimMap[opts.keys[i]]);
176 177 178 179 180 181 182
            }

            else {
                throw new Error("[Parsing error] Non exist key set to view " + opts.alias);
            }
        }

183 184 185 186 187 188 189 190 191 192 193 194 195
        if (opts.clauses) {
            for (let i = 0; i < opts.clauses.length; ++i) {
                const clause = this.parseClause(opts.clauses[i], metMap, dimMap);
                if (clause) {
                    viewOpt.clauses.push(clause);
                }
                else {
                    throw new Error("[Parsing error] Bad formed clause: on view \"" +
                                    opts.alias + "\" the clause \"" + opts.clauses[i] +
                                    "\" could not be created");
                }
            }
        }
196 197
        return viewOpt;
    }
198

199
    public static parseDimOpts (opts: DimensionStrOptions, dims: Dimension[]): DimensionOptions {
200
        if (opts.parent || opts.relation) {
201 202 203 204 205
            for (let i = 0; i < dims.length; ++i) {
                if (dims[i].name === opts.parent) {
                    return {
                        name: opts.name,
                        dataType: opts.dataType,
206
                        description: opts.description,
207
                        parent: dims[i],
208
                        relation: Dimension.parseRelationType(opts.relation)
209 210 211 212 213
                    };
                }
            }

            throw new Error("[Parsing error] Parent for subdimension " + opts.name + "  not found");
214 215 216 217
        }
        return {
            name: opts.name,
            dataType: opts.dataType,
218
            description: opts.description,
219 220 221 222 223 224 225 226
            parent: null,
            relation: RelationType.NONE
        };
    }

    private static parseMetOpts (opts: MetricStrOptions): MetricOptions {
        return {
            name: opts.name,
227 228 229
            aggregation: Metric.parseAggrType(opts.aggregation),
            dataType : opts.dataType,
            description: opts.description
230 231 232
        };
    }

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
    private static parseClause (opts: string, metMap: MetricMap, dimMap: DimensionMap): Clause {
        const strFilters = opts.split(",");
        const filters: Filter[] = strFilters.map((item) => {
            return this.parseFilter(item, metMap, dimMap);
        });

        for (let i = 0; i < filters.length; ++i) {
            if (!filters[i] || !filters[i].isValid) {
                return null;
            }
        }

        return new Clause ({filters: filters});
    }

    private static parseFilter (opts: string, metMap: MetricMap, dimMap: DimensionMap): Filter {
        const strFilter = Filter.segment(opts);
        if (!strFilter) {
            return null;
        }
        let target: Metric|Dimension = dimMap[strFilter.target];
        if (!target) {
            target = metMap[strFilter.target];
            if (!target) {
                return null;
            }
        }

        const operator = Filter.parseOperator(strFilter.operator);

        return new Filter({
            target: target,
            operator: operator,
            value: strFilter.value
        });
    }

270
}