aboutsummaryrefslogtreecommitdiff
path: root/ui/src/frontend/named_slice_track.ts
blob: 5eff6af74fc67a57b49f4ecb143f18562491fe68 (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
// 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 {getColorForSlice} from '../core/colorizer';
import {STR_NULL} from '../trace_processor/query_result';

import {
  BASE_ROW,
  BaseSliceTrack,
  BaseSliceTrackTypes,
  OnSliceClickArgs,
  OnSliceOverArgs,
  SLICE_FLAGS_INCOMPLETE,
  SLICE_FLAGS_INSTANT,
} from './base_slice_track';
import {globals} from './globals';
import {NewTrackArgs} from './track';
import {renderDuration} from './widgets/duration';

export const NAMED_ROW = {
  // Base columns (tsq, ts, dur, id, depth).
  ...BASE_ROW,

  // Impl-specific columns.
  name: STR_NULL,
};
export type NamedRow = typeof NAMED_ROW;

export interface NamedSliceTrackTypes extends BaseSliceTrackTypes {
  row: NamedRow;
}

export abstract class NamedSliceTrack<
  T extends NamedSliceTrackTypes = NamedSliceTrackTypes,
> extends BaseSliceTrack<T> {
  constructor(args: NewTrackArgs) {
    super(args);
  }

  // This is used by the base class to call iter().
  getRowSpec(): T['row'] {
    return NAMED_ROW;
  }

  // Converts a SQL result row to an "Impl" Slice.
  rowToSlice(row: T['row']): T['slice'] {
    const baseSlice = super.rowToSlice(row);
    // Ignore PIDs or numeric arguments when hashing.
    const name = row.name || '';
    const colorScheme = getColorForSlice(name);
    return {...baseSlice, title: name, colorScheme};
  }

  onSliceOver(args: OnSliceOverArgs<T['slice']>) {
    const {title, dur, flags} = args.slice;
    let duration;
    if (flags & SLICE_FLAGS_INCOMPLETE) {
      duration = 'Incomplete';
    } else if (flags & SLICE_FLAGS_INSTANT) {
      duration = 'Instant';
    } else {
      duration = renderDuration(dur);
    }
    args.tooltip = [`${title} - [${duration}]`];
  }

  onSliceClick(args: OnSliceClickArgs<T['slice']>) {
    globals.setLegacySelection(
      {
        kind: 'CHROME_SLICE',
        id: args.slice.id,
        trackKey: this.trackKey,
        // |table| here can be either 'slice' or 'annotation'. The
        // AnnotationSliceTrack overrides the onSliceClick and sets this to
        // 'annotation'
        table: 'slice',
      },
      {
        clearSearch: true,
        pendingScrollId: undefined,
        switchToCurrentSelectionTab: true,
      },
    );
  }
}