data.ts 3.89 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) 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";
24

25 26 27 28
/**
 * Constroller responsable for data part from the API. In other
 * words, controller responsable for reading data in BlenDB.
 */
29
export class DataCtrl {
30 31 32 33 34 35 36
    /**
     * Route that validates a query and returns the query data.
     * @param req - Object with request information
     * @param res - Object used to create and send the response
     * @param next - Call next middleware or controller. Not used but required
     * by typescript definition of route.
     */
37
    public static read(req: Request, res: express.Response, next: express.NextFunction) {
38 39
        let metrics = req.query.metrics.split(",").filter((item: string) => item !== "");
        let dimensions = req.query.dimensions.split(",").filter((item: string) => item !== "");
40
        let clauses = [];
41
        let sort: string[] = [];
42 43 44
        if (req.query.filters) {
            clauses = req.query.filters.split(";").filter((item: string) => item !== "");
        }
45 46 47
        if (req.query.sort) {
            sort = req.query.sort.split(",").filter((item: string) => item !== "");
        }
48
        let view;
49

50
        try {
51
            let query: Query = { metrics: [], dimensions: [], clauses: [], sort: [] };
52 53 54 55
            for (let i = 0; i < metrics.length; ++i) {
                query.metrics.push(req.engine.getMetricByName(metrics[i]));
            }

56
            for (let i = 0; i < dimensions.length; ++i) {
57 58
                query.dimensions.push(req.engine.getDimensionByName(dimensions[i]));
            }
59 60 61 62

            for (let i = 0; i < clauses.length; ++i) {
                query.clauses.push(req.engine.parseClause(clauses[i]));
            }
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

            for (let i = 0; i < sort.length; ++i) {
                const m = query.metrics.find((item) => item.name === sort[i]);
                if (!m) {
                    const d = query.dimensions.find((item) => item.name === sort[i]);
                    if (!d) {
                        throw new Error(
                            "The item '" +  sort[i] +
                            "' is not present in neither metrics nor dimensions list");
                    }
                    else {
                        query.sort.push(d);
                    }
                }

                else {
                    query.sort.push(m);
                }

            }
83 84 85
            view = req.engine.query(query);
        }
        catch (e) {
86 87 88 89 90
            res.status(500).json({
                message: "Query execution failed: " +
                "Could not construct query with the paramters given.",
                error: e.message
             });
91 92 93 94 95
            return;
        }

        req.adapter.getDataFromView(view, (err: Error, result: any[]) => {
            if (err) {
96 97 98 99 100
                res.status(500).json({
                    message: "Query execution failed " +
                    "failed on execute query on database.",
                    error: err
                 });
101 102 103 104 105 106
                return;
            }

            res.status(200).json(result);
            return;
        });
107 108
    }
}