aboutsummaryrefslogtreecommitdiff
path: root/src/client/northstar/dash-fields/HistogramField.ts
blob: c699691a4f0e20231cae293dd07c5c2a06ed56e2 (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
import { action } from "mobx";
import { ColumnAttributeModel } from "../../../client/northstar/core/attribute/AttributeModel";
import { AttributeTransformationModel } from "../../../client/northstar/core/attribute/AttributeTransformationModel";
import { HistogramOperation } from "../../../client/northstar/operations/HistogramOperation";
import { BasicField } from "../../../fields/BasicField";
import { Field, FieldId } from "../../../fields/Field";
import { CurrentUserUtils } from "../../../server/authentication/models/current_user_utils";
import { Types } from "../../../server/Message";
import { OmitKeys } from "../../../Utils";


export class HistogramField extends BasicField<HistogramOperation> {
    constructor(data?: HistogramOperation, id?: FieldId, save: boolean = true) {
        super(data ? data : HistogramOperation.Empty, save, id);
    }

    toString(): string {
        return JSON.stringify(OmitKeys(this.Data, ['Links', 'BrushLinks', 'Result', 'BrushColors', 'FilterModels', 'FilterOperand']));
    }

    Copy(): Field {
        return new HistogramField(this.Data.Copy());
    }

    ToScriptString(): string {
        return `new HistogramField("${this.Data}")`;
    }


    ToJson() {
        return {
            type: Types.HistogramOp,
            data: this.toString(),
            id: this.Id
        };
    }

    @action
    static FromJson(id: string, data: any): HistogramField {
        let jp = JSON.parse(data);
        let X: AttributeTransformationModel | undefined;
        let Y: AttributeTransformationModel | undefined;
        let V: AttributeTransformationModel | undefined;

        let schema = CurrentUserUtils.GetNorthstarSchema(jp.SchemaName);
        if (schema) {
            CurrentUserUtils.GetAllNorthstarColumnAttributes(schema).map(attr => {
                if (attr.displayName === jp.X.AttributeModel.Attribute.DisplayName) {
                    X = new AttributeTransformationModel(new ColumnAttributeModel(attr), jp.X.AggregateFunction);
                }
                if (attr.displayName === jp.Y.AttributeModel.Attribute.DisplayName) {
                    Y = new AttributeTransformationModel(new ColumnAttributeModel(attr), jp.Y.AggregateFunction);
                }
                if (attr.displayName === jp.V.AttributeModel.Attribute.DisplayName) {
                    V = new AttributeTransformationModel(new ColumnAttributeModel(attr), jp.V.AggregateFunction);
                }
            });
            if (X && Y && V) {
                return new HistogramField(new HistogramOperation(jp.SchemaName, X, Y, V, jp.Normalization), id, false);
            }
        }
        return new HistogramField(HistogramOperation.Empty, id, false);
    }
}