data.ts 2.11 KB
Newer Older
Eduardo L. Buratti's avatar
Eduardo L. Buratti committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright (C) 2015 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
import * as express from "express";
22 23
import { Request } from "../types";
import { Query } from "../../common/query";
Eduardo L. Buratti's avatar
Eduardo L. Buratti committed
24

25
export class DataCtrl {
26
    public static read(req: Request, res: express.Response, next: express.NextFunction) {
Eduardo L. Buratti's avatar
Eduardo L. Buratti committed
27 28
        let metrics = req.query.metrics.split(",");
        let dimensions = req.query.dimensions.split(",");
29
        let view;
Eduardo L. Buratti's avatar
Eduardo L. Buratti committed
30

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
        try {
            let query: Query = { metrics: [], dimensions: [] };
            for (let i = 0; i < metrics.length; ++i) {
                query.metrics.push(req.engine.getMetricByName(metrics[i]));
            }

            for (let i = 0; i < metrics.length; ++i) {
                query.dimensions.push(req.engine.getDimensionByName(dimensions[i]));
            }
            view = req.engine.query(query);
        }
        catch (e) {
            res.status(500).json({ message: "Query execution failed: " +
                "Could not construct query with the paramters given." });
            return;
        }

        req.adapter.getDataFromView(view, (err: Error, result: any[]) => {
            if (err) {
                res.status(500).json({ message: "Query execution failed " +
                    "failed on execute query on database." });
                return;
            }

            res.status(200).json(result);
            return;
        });
Eduardo L. Buratti's avatar
Eduardo L. Buratti committed
58 59
    }
}