scenario.ts 13.3 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
import { ConfigParser } from "../src/util/configParser";
22 23 24
import { Metric } from "../src/core/metric";
import { Dimension } from "../src/core/dimension";
import { View } from "../src/core/view";
25 26
import { Filter, FilterOperator } from "../src/core/filter";
import { Clause } from "../src/core/clause";
27
import { AggregationType, RelationType , DataType} from "../src/common/types";
28
import { ViewHandler } from "../src/util/viewHandler";
29
import { EngineScenario, AdapterScenario, DataCtrlScenario } from "../src/util/scenarioHandler";
30
import { Query, QueryOpts } from "../src/common/query";
31

32 33 34 35 36 37 38 39 40
let configPath;

if(process.env.BLENDB_SCHEMA_FILE){
    configPath = process.env.BLENDB_SCHEMA_FILE;
}
else{
    throw new Error("BLENDB_SCHEMA_FILE wasn't informed");
}

41
const config = ConfigParser.parse(configPath);
42

43 44 45 46
const mets : {[key: string]: Metric} = {};
for (let i in config.metrics) {
    let met = config.metrics[i];
    mets[met.name] = met;
47
}
48

49 50 51 52 53
const dims : {[key:string]: Dimension} = {};
for (let i in config.dimensions) {
    let dim = config.dimensions[i];
    dims[dim.name] = dim;
}
54

55 56 57 58 59
const views : {[key:string]: View} = {};
for (let i in config.buildViews){
    let view = config.buildViews[i];
    views[view.alias] = view.view;
}
60

61 62 63 64 65 66 67 68
const wrongMet = new Metric({
    name: "met:this:is:just:a:test",
    aggregation: AggregationType.COUNT,
    dataType: DataType.INTEGER
});

const wrongDim = new Dimension({ name: "dim:this:is:just:a:test", dataType: DataType.INTEGER });

69
/**
70
 * Create new filters to use in clause and test
71 72
 * the clauses
 */
73
const filters: { [key: string]: Filter } = {
74 75 76 77
    "equal" : new Filter({
        target : dims["dim:product:validity"],
        operator : FilterOperator.EQUAL,
        value: "2018-05-10"
78
    }),
79 80 81 82
    "lower" : new Filter({
        target : dims["dim:product:validity"],
        operator : FilterOperator.LOWER,
        value: "2018-05-10"
83
    }),
84 85 86 87
    "greater" : new Filter({
        target : dims["dim:product:validity"],
        operator : FilterOperator.GREATER,
        value: "2018-05-10"
88
    }),
89 90 91 92
    "greaterq" : new Filter({
        target : dims["dim:product:validity"],
        operator : FilterOperator.GREATEREQ,
        value: "2018-06-10"
93
    }),
94 95 96 97
    "lowerq" : new Filter({
        target : dims["dim:product:validity"],
        operator : FilterOperator.LOWEREQ,
        value: "2018-06-10"
98
    }),
99 100
    "noteq" : new Filter({
        target: dims["dim:product:validity"],
101
        operator: FilterOperator.NOTEQUAL,
102
        value: "2018-08-23"
103
    }),
104 105 106 107
    "clientRegistered" : new Filter({
        target: dims["dim:sell:registered"],
        operator: FilterOperator.EQUAL,
        value: "false"
108
    }),
109 110 111 112
    "averageBought": new Filter({
        target: mets["met:sell:avg:quantity"],
        operator: FilterOperator.GREATEREQ,
        value: "10"
113
    }),
114 115
    "equalFilterView": new Filter({
        target: dims["dim:seller:status"],
116
        operator: FilterOperator.EQUAL,
117
        value: "active"
118 119 120 121 122
    }),
    "noneFilter": new Filter({
        target: dims["dim:seller:status"],
        operator: FilterOperator.NONE,
        value: "active"
123
    })
124
}
125

126 127 128
const subdims : {[key:string]: Dimension} = {
    "subdims_day" : new Dimension({
        name: "subdims_day",
129
        dataType: DataType.INTEGER,
130
        parent: dims["dim:sell:datein"],
131 132
        relation: RelationType.DAY
    }),
133 134
    "subdims_month" : new Dimension({
        name: "subdims_month",
135
        dataType: DataType.INTEGER,
136
        parent: dims["dim:sell:datein"],
137 138
        relation: RelationType.MONTH
    }),
139 140
    "subdims_year" : new Dimension({
        name: "subdims_year",
141
        dataType: DataType.INTEGER,
142 143
        parent: dims["dim:sell:datein"],
        relation: RelationType.YEAR
144
    }),
145 146
    "subdims_none" : new Dimension({
        name: "subdims_none",
147
        dataType: DataType.INTEGER,
148 149
        parent: null,
        relation: RelationType.NONE
150
    })
151
}
152

153
const clauses: { [key: string]: Clause }  = {
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
    "lastDay":
    new Clause({filters: [filters["equal"]]}),
    "undefined":
    new Clause({filters: [filters["noteq"]]}),
    "unexpired":
    new Clause({filters: [filters["greater"]]}),
    "expired":
    new Clause({filters: [filters["lower"]]}),
    "unexpiredLastDay":
    new Clause({filters: [filters["greaterq"]]}),
    "expiredlastDay":
    new Clause({filters: [filters["lowerq"]]}),
    "clientRegistered":
    new Clause({filters: [filters["clientRegistered"]]}),
    "averageBought":
    new Clause({filters: [filters["averageBought"]]}),
    "expiredAndAverage":
    // or between filteres => (A) || (B)
    new Clause ({filters: [filters["lower"],filters["averageBought"]]}),
    "equalClauseView":
174 175 176
    new Clause ({filters: [filters["equalFilterView"]]}),
    "emptyClause":
    new Clause ({filters: []})
177 178 179
}


180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 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 270 271 272
let qOpts : {[key: string]: QueryOpts} = {
    "JoinWithAncestors":
    {
        metrics: [mets["met:sell:count:quantity"]],
        dimensions: [dims["dim:seller:id"],dims["dim:provider:id"]]
    },
    "joinWithNoMetrics":
    {
        metrics: [],
        dimensions: [dims["dim:product:name"],dims["dim:seller:name"]]
    },
    "growOneView":
    {
        metrics: [mets["met:seller:min:age"]],
        dimensions: [dims["dim:seller:name"],dims["dim:seller:sex"]]
    },
    "multipleClause":
    {
        metrics: [mets["met:sell:avg:quantity"]],
        dimensions: [dims["dim:sell:datein"],dims["dim:seller:name"],
        dims["dim:client:name"]],
        // and between filters => (A) and (B)
        clauses: [clauses["expired"],clauses["averageBought"]],
    },
    "singleClause":
    {
        metrics: [mets["met:sell:avg:quantity"]],
        dimensions: [dims["dim:sell:datein"],dims["dim:seller:name"],
        dims["dim:client:name"]],
        clauses: [clauses["expiredAndAverage"]],
    },
    "equalfilter":
    {
        metrics: [],
        dimensions: [dims["dim:client:name"],dims["dim:product:validity"]],
        clauses: [clauses["lastDay"]],
    },
    "withSortView:0":
    {
        metrics: [mets["met:sell:sum:quantity"]],
        dimensions: [dims["dim:client:name"]],
        sort: [mets["met:sell:sum:quantity"]]
    },
    "withSortView:1":
    {
        metrics: [mets["met:sell:sum:quantity"]],
        dimensions: [dims["dim:client:id"]]
    },
    "subDimView:0":
    {
        metrics: [],
        dimensions : [dims["dim:sell:datein"],subdims["subdims_day"],
        subdims["subdims_month"],subdims["subdims_year"]]
    },
    "subDimView:1":
    {
        metrics: [],
        dimensions: [dims["dim:sell:datein"]]
    },
    "joinOneView":
    {
        metrics: [mets["met:product:avg:pricein"]],
        dimensions: [],
    },
    "reduceAsView":
    {
        metrics: [mets["met:sell:sum:quantity"],mets["met:sell:avg:quantity"],
    mets["met:sell:count:quantity"]],
        dimensions: [dims["dim:sell:registered"], dims["dim:product:id"],
    dims["dim:seller:id"], dims["dim:client:id"],dims["dim:sell:datein"]],
        sort: [mets["met:sell:sum:quantity"]]
    },
    "clientAverageBought:0":
    {
        metrics: [mets["met:sell:avg:quantity"]],
        dimensions: [dims["dim:client:name"]],
        clauses: [clauses["averageBought"]]
    },
    "clientAverageBought:1":
    {
        metrics: [mets["met:sell:avg:quantity"]],
        dimensions: [dims["dim:client:name"], dims["dim:seller:id"]]
    },
    "queryNoParent":
    {
        metrics: [mets["met:sell:count:quantity"]],
        dimensions: [subdims["subdims_none"]]
    },
    "queryMetsDims":
    {
        metrics : Object.keys(mets).map((key) => mets[key]),
        dimensions : Object.keys(dims).map((key) => dims[key])
    },
273
    "queryNoMets":
274 275 276 277
    {
        metrics: [wrongMet],
        dimensions: [dims["dim:product:name"]]
    },
278
    "queryNoDims":
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
    {
        metrics: [mets["met:buyout:min:quantity"]],
        dimensions: [wrongDim]
    },
    "queryProduct":
    {
        metrics: [mets["met:product:avg:pricein"], mets["met:product:max:pricein"], mets["met:product:min:pricein"],
        mets["met:product:avg:priceout"],mets["met:product:max:priceout"],mets["met:product:min:priceout"]],
        dimensions: [dims["dim:product:name"], dims["dim:product:validity"],dims["dim:product:id"]]
    },
    "queryActive":
    {
        metrics: [mets["met:seller:max:age"]],
        dimensions: [dims["dim:seller:name"],dims["dim:seller:status"]],
        clauses: [clauses["equalClauseView"]]
    },
    "querySubDim":
    {
        metrics : [],
        dimensions : [subdims["subdims_day"],subdims["subdims_month"]]
    },
300
    "wrongMet":
301 302 303 304
    {
        metrics: [wrongMet],
        dimensions: [dims["dim:product:id"]]
    },
305 306 307 308 309 310
    "seller":
    {
        metrics:[mets["met:sell:sum:quantity"]],
        dimensions:[dims["dim:sell:registered"],dims["dim:product:id"]]
    },
    "wrongDim":
311 312 313 314
    {
        metrics: [mets["met:sell:avg:quantity"]],
        dimensions: [wrongDim]
    },
315
    "correct":
316 317 318 319 320 321 322 323 324
    {
        metrics: [mets["met:buyout:avg:quantity"]],
        dimensions: [dims["dim:provider:id"]]
    },
    "clausal":
    {
        metrics: [mets["met:product:avg:pricein"]],
        dimensions: [dims["dim:product:name"],
        dims["dim:product:id"]]
325 326 327 328 329 330 331 332 333 334 335 336
    },
    "expensive":
    {
        metrics: [mets["met:product:max:pricein"]],
        dimensions: [dims["dim:product:name"],
        dims["dim:product:id"]]
    },
    "originfalse":
    {
        metrics: [mets["met:seller:count:age"]],
        dimensions: []
    },
337 338 339 340
    "csv": {
        metrics: [mets["met:seller:avg:age"]],
        dimensions: [dims["dim:seller:cpf"], dims["dim:seller:sex"]]
    }
341
}
342

343 344 345 346
const queries : {[key: string]: Query} = {};
for(let i in qOpts){
    queries[i] = new Query(qOpts[i]);
}
347

348 349
const JoinWithAncestors = ViewHandler.growView(queries["JoinWithAncestors"],
    [views["view:Sell"]]);
350

351 352 353 354
const joinWithNoMetrics = ViewHandler.growView(queries["joinWithNoMetrics"],
    [views["view:Product"],
    views["view:Sell"],
    views["view:Seller"]]);
355

356 357
const growOneView = ViewHandler.growView(queries["growOneView"],
    [views["view:Seller"]]);
358

359 360 361 362 363
const multipleClause = ViewHandler.growView(queries["multipleClause"],
    [views["view:Sell"],
    views["view:Seller"],
    views["view:Client"],
    views["view:Product"]]);
364

365 366 367 368 369
const singleClause = ViewHandler.growView(queries["singleClause"],
    [views["view:Sell"],
    views["view:Seller"],
    views["view:Client"],
    views["view:Product"]]);
370

371 372 373 374
const equalfilter = ViewHandler.queryJoin(queries["equalfilter"],
    [views["view:Sell"],
    views["view:Client"],
    views["view:Product"]]);
375

376 377 378 379
const withSortView = ViewHandler.queryJoin(queries["withSortView:0"],
    [ViewHandler.queryReduce(queries["withSortView:1"],
    views["view:Sell"]),
    views["view:Client"]]);
380

381 382 383 384
const subDimView = ViewHandler.queryJoin(queries["subDimView:0"],
    [ViewHandler.queryReduce(queries["subDimView:1"],
    views["view:Sell"]),
    views["view:Sell"]]);
385

386 387
const joinOneView = ViewHandler.queryJoin(queries["joinOneView"],
    [views["view:Product"]]);
388

389 390
const reduceAsView = ViewHandler.queryReduce(queries["reduceAsView"],
    views["view:Sell"]);
391

392 393 394
const clientAverageBought = ViewHandler.queryReduce(queries["clientAverageBought:0"],
    ViewHandler.queryJoin(queries["clientAverageBought:1"],
    [views["view:Sell"],views["view:Client"]]));
395

396
const viewProduct = views["view:Product"];
397

398
const viewActiveSeller = views["view:ActiveSeller"];
399

400 401
const viewSumCount =  ViewHandler.queryReduce(queries["originfalse"],views["view:SellerStatus"]);

402
// Exports
403

404
export const engineScenario: EngineScenario = {
405 406
    wrongMet: wrongMet,
    wrongDim: wrongDim,
407
    subDimensions: subdims,
408 409 410 411
    queryMetsDims: queries["queryMetsDims"],
    queryNoMets: queries["queryNoMets"],
    queryNoDims: queries["queryNoDims"],
    queryProduct: queries["queryProduct"],
412
    viewProduct: viewProduct,
413
    queryActive: queries["queryActive"],
414
    viewActiveSeller: viewActiveSeller,
415
    querySubDim: queries["querySubDim"],
416 417
    queryNoParent: queries["queryNoParent"],
    config: config
418 419 420
};

export const adapterScenario: AdapterScenario = {
421
    materializedView: views["view:Product"],
422 423 424 425 426 427 428
    sortView: withSortView,
    joinWithOneView: joinOneView,
    singleClause: singleClause,
    multipleClause: multipleClause,
    filterAverageBought: clientAverageBought,
    reduceAsView: reduceAsView,
    filterWithEqual: equalfilter,
429
    subDimensionView: subDimView,
430 431 432
    joinWithNoMetrics: joinWithNoMetrics,
    growOneView: growOneView,
    JoinWithAncestors:JoinWithAncestors,
433
    viewSumCount: viewSumCount,
434
    config: config
435
};
436 437

export const dataCtrlScenario: DataCtrlScenario = {
438
    seller: queries["seller"],
439 440 441 442
    wrongMet: queries["wrongMet"],
    wrongDim: queries["wrongDim"],
    correct: queries["correct"],
    clausal: queries["clausal"],
443
    expensive: queries["expensive"],
444
    csv: queries["csv"],
445
};