configParser.ts 5.54 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * 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/>.
 */

import { Metric, MetricOptions } from "../core/metric";
import { Dimension, DimensionOptions } from "../core/dimension";
import { View, ViewOptions, LoadView } from "../core/view";
24
import { AggregationType, RelationType } from "../common/types";
25 26 27 28 29 30 31 32 33 34 35
import { PoolConfig } from "pg";
import * as fs from "fs";
import * as yaml from "js-yaml";

export interface ViewParsingOptions {
    alias: string;
    data: string;
    dimensions: string[];
    metrics: string[];
}

36 37 38 39 40 41 42 43 44 45 46 47 48
interface MetricStrOptions {
    name: string;
    aggregation: string;
    dataType: string;
}

interface DimensionStrOptions {
    name: string;
    dataType: string;
    parent?: string;
    relation?: string;
}

49 50
interface ConfigSchema {
    views: ViewParsingOptions[];
51 52
    metrics: MetricStrOptions[];
    dimensions: DimensionStrOptions[];
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
}

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

export interface LoadStruct{
    create: boolean;
    insert: boolean;
}

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

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,
            loadViews: []
        };

        let metMap: Map<string, Metric> = new Map();
        let dimMap: Map<string, Dimension> = new Map();

        for (let i = 0; i < metricsOpts.length; ++i) {
97
            let met = new Metric(this.parseMetOpts(metricsOpts[i]));
98 99 100 101 102
            parsed.metrics.push(met);
            metMap.set(met.name, met);
        }

        for (let i = 0; i < dimensionsOpts.length; ++i) {
103
            let dim = new Dimension(this.parseDimOpts(dimensionsOpts[i]));
104 105 106 107 108
            parsed.dimensions.push(dim);
            dimMap.set(dim.name, dim);
        }

        for (let i = 0; i < viewsOpts.length; ++i) {
109 110 111
            if (!viewsOpts[i].metrics) {
                viewsOpts[i].metrics = [];
            }
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
            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);
        }

        return parsed;
    }

    public static parseViewOpt(opts: ViewParsingOptions,
                                metMap: Map<string, Metric>,
                                dimMap: Map<string, Dimension>): ViewOptions {

        let viewOpt: ViewOptions = {
            metrics: [],
            dimensions: [],
            materialized: true,
            childViews: [],
        };

        for (let i = 0; i < opts.metrics.length; ++i) {
            if (metMap.has(opts.metrics[i])) {
                viewOpt.metrics.push(metMap.get(opts.metrics[i]));
            }

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

        for (let i = 0; i < opts.dimensions.length; ++i) {
            if (dimMap.has(opts.dimensions[i])) {
                viewOpt.dimensions.push(dimMap.get(opts.dimensions[i]));
            }

            else {
                throw new Error("[Parsing error] Non exist dimension set to view " + opts.alias);
            }
        }
        return viewOpt;
    }
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

    private static parseDimOpts (opts: DimensionStrOptions): DimensionOptions {
        if (opts.parent || opts.relation) {
            throw new Error("Sub dimensions not implemented in config parser !!!");
        }
        return {
            name: opts.name,
            dataType: opts.dataType,
            parent: null,
            relation: RelationType.NONE
        };
    }

    private static parseMetOpts (opts: MetricStrOptions): MetricOptions {
        return {
            name: opts.name,
            aggregation: this.strToAggregationType(opts.aggregation),
            dataType : opts.dataType
        };
    }

    private static strToAggregationType (str: string): AggregationType {
        switch (str) {
            case "sum":
                return AggregationType.SUM;
            case "avg":
                return AggregationType.AVG;
            case "count":
                return AggregationType.COUNT;
            default:
                return AggregationType.NONE;
        }
    }
187
}