aboutsummaryrefslogtreecommitdiff
path: root/app/components/plan-view/plan-view.ts
blob: 93b3a97d4a9ce368a5b95862aa6e1df1b228051a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
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
import {Component, OnInit} from 'angular2/core';
import {ROUTER_DIRECTIVES, RouteParams} from 'angular2/router';

import {IPlan} from '../../interfaces/iplan';
import {HighlightType} from '../../enums';
import {PlanNode} from '../plan-node/plan-node';
import {PlanService} from '../../services/plan-service';
import {SyntaxHighlightService} from '../../services/syntax-highlight-service';

@Component({
    selector: 'plan-view',
    templateUrl: './components/plan-view/plan-view.html',
    directives: [ROUTER_DIRECTIVES, PlanNode],
    providers: [PlanService, SyntaxHighlightService]
})
export class PlanView {
    id: string;
    plan: IPlan;
    rootContainer: any;
    executionTime: string;
    executionTimeUnit: string;
    hideMenu: boolean = true;

    viewOptions: any = {
        showPlanStats: true,
        showHighlightBar: true,
        showPlannerEstimate: false,
        showTags: true,
        highlightType: HighlightType.NONE
    };

    showPlannerEstimate: boolean = true;
    showMenu: boolean = false;

    highlightTypes = HighlightType; // exposing the enum to the view

    constructor(private _planService: PlanService, routeParams: RouteParams) {
        this.id = routeParams.get('id');
    }

    getPlan() {
        if (!this.id) {
            return;
        }

        this.plan = this._planService.getPlan(this.id);
        this.rootContainer = this.plan.content;

        var executionTime: number = this.rootContainer['Execution Time'] || this.rootContainer['Total Runtime'];
        [this.executionTime, this.executionTimeUnit] = this.calculateDuration(executionTime);

        this.plan.planStats = {
            executionTime: executionTime,
            planningTime: this.rootContainer['Planning Time'] || 0,
            maxRows: this.rootContainer[this._planService.MAXIMUM_ROWS_PROP] || 0,
            maxCost: this.rootContainer[this._planService.MAXIMUM_COSTS_PROP] || 0,
            maxDuration: this.rootContainer[this._planService.MAXIMUM_DURATION_PROP] || 0
        };
    }

    ngOnInit() {
        this.getPlan();
    }

    toggleHighlight(type: HighlightType) {
        this.viewOptions.highlightType = type;
    }

    analyzePlan() {
        this._planService.analyzePlan(this.plan);
    }

    calculateDuration(originalValue: number) {
        var duration: string = '';
        var unit: string = '';

        if (originalValue < 1) {
            duration = '<1';
            unit = 'ms';
        } else if (originalValue > 1 && originalValue < 1000) {
            duration = originalValue.toString();
            unit = 'ms';
        } else {
            duration = _.round(originalValue / 1000, 2).toString();
            unit = 'mins';
        }
        return [duration, unit];
    }
}