aboutsummaryrefslogtreecommitdiff
path: root/ui/src/frontend/pivot_table_helper.ts
blob: 88fd527e902a02c9fbd43f0709dabbab9c37d68a (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Copyright (C) 2021 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import {Actions} from '../common/actions';
import {
  AggregationAttrs,
  isStackPivot,
  PivotAttrs,
  TableAttrs
} from '../common/pivot_table_common';
import {globals} from './globals';

export function isAggregationAttrs(attrs: PivotAttrs|AggregationAttrs):
    attrs is AggregationAttrs {
  return (attrs as AggregationAttrs).aggregation !== undefined;
}

function equalTableAttrs(
    left: PivotAttrs|AggregationAttrs, right: PivotAttrs|AggregationAttrs) {
  if (left.columnName !== right.columnName) {
    return false;
  }

  if (left.tableName !== right.tableName) {
    return false;
  }

  if (isAggregationAttrs(left) && isAggregationAttrs(right)) {
    if (left.aggregation !== right.aggregation) {
      return false;
    }
  }
  return true;
}

export function getDataTransferType(isPivot: boolean) {
  if (isPivot) {
    return 'perfetto/pivot-table-dragged-pivot';
  }
  return 'perfetto/pivot-table-dragged-aggregation';
}

export class PivotTableHelper {
  readonly pivotTableId: string;
  readonly availableColumns: TableAttrs[];
  readonly availableAggregations: string[];
  readonly totalColumnsCount = 0;

  private _selectedPivots: PivotAttrs[] = [];
  private _selectedAggregations: AggregationAttrs[] = [];
  private _isPivot = true;
  private _selectedColumnIndex = 0;
  private _selectedAggregationIndex = 0;
  private _editPivotTableModalOpen = false;

  constructor(
      pivotTableId: string, availableColumns: TableAttrs[],
      availableAggregations: string[], selectedPivots: PivotAttrs[],
      selectedAggregations: AggregationAttrs[]) {
    this.pivotTableId = pivotTableId;
    this.availableColumns = availableColumns;
    for (const table of this.availableColumns) {
      this.totalColumnsCount += table.columns.length;
    }
    this.availableAggregations = availableAggregations;
    this.setSelectedPivotsAndAggregations(selectedPivots, selectedAggregations);
  }

  // Sets selected pivots and aggregations if the editor modal is not open.
  setSelectedPivotsAndAggregations(
      selectedPivots: PivotAttrs[], selectedAggregations: AggregationAttrs[]) {
    if (!this.editPivotTableModalOpen) {
      // Making a copy of selectedPivots and selectedAggregations to preserve
      // the original state.
      this._selectedPivots =
          selectedPivots.map(pivot => Object.assign({}, pivot));
      this._selectedAggregations = selectedAggregations.map(
          aggregation => Object.assign({}, aggregation));
    }
  }

  // Dictates if the selected indexes refer to a pivot or aggregation.
  togglePivotSelection() {
    this._isPivot = !this._isPivot;
    if (!this._isPivot) {
      const selectedColumn = this.getSelectedPivotTableColumnAttrs();
      if (isStackPivot(selectedColumn.tableName, selectedColumn.columnName)) {
        this._selectedColumnIndex = Math.max(0, this._selectedColumnIndex - 1);
      }
    }
  }

  setSelectedPivotTableColumnIndex(index: number) {
    if (index < 0 && index >= this.totalColumnsCount) {
      throw Error(`Selected column index "${index}" out of bounds.`);
    }
    this._selectedColumnIndex = index;
  }

  setSelectedPivotTableAggregationIndex(index: number) {
    if (index < 0 && index >= this.availableAggregations.length) {
      throw Error(`Selected aggregation index "${index}" out of bounds.`);
    }
    this._selectedAggregationIndex = index;
  }

  // Get column attributes on selectedColumnIndex and
  // selectedAggregationIndex.
  getSelectedPivotTableColumnAttrs(): PivotAttrs|AggregationAttrs {
    let tableName, columnName;
    // Finds column index relative to its table.
    let colIdx = this._selectedColumnIndex;
    for (const {tableName: tblName, columns} of this.availableColumns) {
      if (colIdx < columns.length) {
        tableName = tblName;
        columnName = columns[colIdx];
        break;
      }
      colIdx -= columns.length;
    }
    if (tableName === undefined || columnName === undefined) {
      throw Error(
          'Pivot table selected column does not exist in availableColumns.');
    }

    // Get aggregation if selected column is not a pivot, undefined otherwise.
    if (!this._isPivot) {
      const aggregation =
          this.availableAggregations[this._selectedAggregationIndex];
      return {tableName, columnName, aggregation, order: 'DESC'};
    }

    return {
      tableName,
      columnName,
      isStackPivot: isStackPivot(tableName, columnName)
    };
  }

  // Adds column based on selected index to selectedPivots or
  // selectedAggregations if it doesn't already exist, remove otherwise.
  updatePivotTableColumnOnSelectedIndex() {
    const columnAttrs = this.getSelectedPivotTableColumnAttrs();
    this.updatePivotTableColumnOnColumnAttributes(columnAttrs);
  }

  // Adds column based on column attributes to selectedPivots or
  // selectedAggregations if it doesn't already exist, remove otherwise.
  updatePivotTableColumnOnColumnAttributes(columnAttrs: PivotAttrs|
                                           AggregationAttrs) {
    let storage: Array<PivotAttrs|AggregationAttrs>;
    let attrs: PivotAttrs|AggregationAttrs;
    if (isAggregationAttrs(columnAttrs)) {
      if (isStackPivot(columnAttrs.tableName, columnAttrs.columnName)) {
        throw Error(
            `Stack column "${columnAttrs.tableName} ${
                columnAttrs.columnName}" should not ` +
            `be added as an aggregation.`);
      }
      storage = this._selectedAggregations;
      attrs = {
        tableName: columnAttrs.tableName,
        columnName: columnAttrs.columnName,
        aggregation: columnAttrs.aggregation,
        order: columnAttrs.order
      };
    } else {
      storage = this._selectedPivots;
      attrs = {
        tableName: columnAttrs.tableName,
        columnName: columnAttrs.columnName,
        isStackPivot: columnAttrs.isStackPivot
      };
    }
    const index =
        storage.findIndex(element => equalTableAttrs(element, columnAttrs));

    if (index === -1) {
      storage.push(attrs);
    } else {
      storage.splice(index, 1);
    }
  }

  clearPivotTableColumns() {
    this._selectedPivots = [];
    this._selectedAggregations = [];
  }

  // Changes aggregation sorting from DESC to ASC and vice versa.
  togglePivotTableAggregationSorting(index: number) {
    if (index < 0 || index >= this._selectedAggregations.length) {
      throw Error(`Column index "${index}" is out of bounds.`);
    }
    this._selectedAggregations[index].order =
        this._selectedAggregations[index].order === 'DESC' ? 'ASC' : 'DESC';
  }

  // Change aggregation function to existing column.
  changeAggregation(index: number, aggregation: string) {
    if (index < 0 || index >= this._selectedAggregations.length) {
      throw Error(`Column index "${index}" is out of bounds.`);
    }
    this._selectedAggregations[index].aggregation = aggregation;
  }

  // Moves target column to the requested destination.
  reorderPivotTableDraggedColumn(
      isPivot: boolean, targetColumnIdx: number, destinationColumnIdx: number) {
    let storage: Array<PivotAttrs|AggregationAttrs>;
    if (isPivot) {
      storage = this._selectedPivots;
    } else {
      storage = this._selectedAggregations;
    }

    if (targetColumnIdx < 0 || targetColumnIdx >= storage.length) {
      throw Error(`Target column index "${targetColumnIdx}" out of bounds.`);
    }
    if (destinationColumnIdx < 0 || destinationColumnIdx >= storage.length) {
      throw Error(
          `Destination column index "${destinationColumnIdx}" out of bounds.`);
    }

    const targetColumn: PivotAttrs|AggregationAttrs = storage[targetColumnIdx];
    storage.splice(targetColumnIdx, 1);
    storage.splice(destinationColumnIdx, 0, targetColumn);
  }

  selectedColumnOnDrag(e: DragEvent, isPivot: boolean, targetIdx: number) {
    const dataTransferType = getDataTransferType(isPivot);
    if (e.dataTransfer === null) {
      return;
    }
    e.dataTransfer.setData(dataTransferType, targetIdx.toString());
  }

  selectedColumnOnDrop(
      e: DragEvent, isPivot: boolean, destinationColumnIdx: number) {
    const dataTransferType = getDataTransferType(isPivot);
    if (e.dataTransfer === null) {
      return;
    }
    // Prevents dragging pivots to aggregations and vice versa.
    if (!e.dataTransfer.types.includes(dataTransferType)) {
      return;
    }

    const targetColumnIdxString = e.dataTransfer.getData(dataTransferType);
    const targetColumnIdx = Number(targetColumnIdxString);
    if (!Number.isInteger(targetColumnIdx)) {
      throw Error(
          `Target column index "${targetColumnIdxString}" is not valid.`);
    }

    this.reorderPivotTableDraggedColumn(
        isPivot, targetColumnIdx, destinationColumnIdx);
    e.dataTransfer.clearData(dataTransferType);
  }


  // Highlights valid drop locations when dragging over them.
  highlightDropLocation(e: DragEvent, isPivot: boolean) {
    if (e.dataTransfer === null) {
      return;
    }
    // Prevents highlighting aggregations when dragging pivots over them
    // and vice versa.
    if (!e.dataTransfer.types.includes(getDataTransferType(isPivot))) {
      return;
    }
    (e.target as HTMLTableDataCellElement).classList.add('drop-location');
  }

  removeHighlightFromDropLocation(e: DragEvent) {
    (e.target as HTMLTableDataCellElement).classList.remove('drop-location');
  }

  // Gets column index in availableColumns based on its attributes.
  getColumnIndex(columnAttrs: PivotAttrs|AggregationAttrs) {
    let index = 0;
    for (const {tableName, columns} of this.availableColumns) {
      if (tableName === columnAttrs.tableName) {
        const colIdx =
            columns.findIndex(column => column === columnAttrs.columnName);
        return colIdx === -1 ? -1 : index + colIdx;
      }
      index += columns.length;
    }
    return -1;
  }

  selectPivotTableColumn(columnAttrs: PivotAttrs|AggregationAttrs) {
    this._isPivot = !isAggregationAttrs(columnAttrs);

    const colIndex = this.getColumnIndex(columnAttrs);
    if (colIndex === -1) {
      throw Error(`Selected column "${columnAttrs.tableName} ${
          columnAttrs.columnName}" not found in availableColumns.`);
    }
    this.setSelectedPivotTableColumnIndex(colIndex);

    if (isAggregationAttrs(columnAttrs)) {
      const aggIndex = this.availableAggregations.findIndex(
          aggregation => aggregation === columnAttrs.aggregation);
      if (aggIndex === -1) {
        throw Error(`Selected aggregation "${
            columnAttrs.aggregation}" not found in availableAggregations.`);
      }
      this.setSelectedPivotTableAggregationIndex(aggIndex);
    }
  }

  queryPivotTableChanges() {
    globals.dispatch(Actions.setSelectedPivotsAndAggregations({
      pivotTableId: this.pivotTableId,
      selectedPivots: this._selectedPivots,
      selectedAggregations: this._selectedAggregations
    }));
    globals.dispatch(Actions.setPivotTableRequest(
        {pivotTableId: this.pivotTableId, action: 'QUERY'}));
  }

  toggleEditPivotTableModal() {
    this._editPivotTableModalOpen = !this._editPivotTableModalOpen;
  }

  get selectedPivots() {
    return this._selectedPivots.map(pivot => Object.assign({}, pivot));
  }

  get selectedAggregations() {
    return this._selectedAggregations.map(
        aggregation => Object.assign({}, aggregation));
  }

  get isPivot() {
    return this._isPivot;
  }

  get selectedColumnIndex() {
    return this._selectedColumnIndex;
  }

  get selectedAggregationIndex() {
    return this._selectedAggregationIndex;
  }

  get editPivotTableModalOpen() {
    return this._editPivotTableModalOpen;
  }
}