1// Copyright (C) 2020 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15import {ColumnDef} from '../../common/aggregation_data';
16import {Engine} from '../../common/engine';
17import {Area, Sorting} from '../../common/state';
18import {toNs} from '../../common/time';
19import {Config, COUNTER_TRACK_KIND} from '../../tracks/counter/common';
20import {globals} from '../globals';
21
22import {AggregationController} from './aggregation_controller';
23
24export class CounterAggregationController extends AggregationController {
25  async createAggregateView(engine: Engine, area: Area) {
26    await engine.query(`drop view if exists ${this.kind};`);
27
28    const ids = [];
29    for (const trackId of area.tracks) {
30      const track = globals.state.tracks[trackId];
31      // Track will be undefined for track groups.
32      if (track !== undefined && track.kind === COUNTER_TRACK_KIND) {
33        const config = track.config as Config;
34        // TODO(hjd): Also aggregate annotation (with namespace) counters.
35        if (config.namespace === undefined) {
36          ids.push(config.trackId);
37        }
38      }
39    }
40    if (ids.length === 0) return false;
41
42    const query = `create view ${this.kind} as select
43    name,
44    count(1) as count,
45    round(sum(weighted_value)/${
46        toNs(area.endSec) - toNs(area.startSec)}, 2) as avg_value,
47    last as last_value,
48    first as first_value,
49    max(last) - min(first) as delta_value,
50    round((max(last) - min(first))/${area.endSec - area.startSec}, 2) as rate,
51    min(value) as min_value,
52    max(value) as max_value
53    from
54        (select *,
55        (min(ts + dur, ${toNs(area.endSec)}) - max(ts,${toNs(area.startSec)}))
56        * value as weighted_value,
57        first_value(value) over
58        (partition by track_id order by ts) as first,
59        last_value(value) over
60        (partition by track_id order by ts
61            range between unbounded preceding and unbounded following) as last
62        from experimental_counter_dur
63        where track_id in (${ids})
64        and ts + dur >= ${toNs(area.startSec)} and
65        ts <= ${toNs(area.endSec)})
66    join counter_track
67    on track_id = counter_track.id
68    group by track_id`;
69
70    await engine.query(query);
71    return true;
72  }
73
74  getColumnDefinitions(): ColumnDef[] {
75    return [
76      {
77        title: 'Name',
78        kind: 'STRING',
79        columnConstructor: Uint16Array,
80        columnId: 'name',
81      },
82      {
83        title: 'Delta value',
84        kind: 'NUMBER',
85        columnConstructor: Float64Array,
86        columnId: 'delta_value'
87      },
88      {
89        title: 'Rate /s',
90        kind: 'Number',
91        columnConstructor: Float64Array,
92        columnId: 'rate'
93      },
94      {
95        title: 'Weighted avg value',
96        kind: 'Number',
97        columnConstructor: Float64Array,
98        columnId: 'avg_value'
99      },
100      {
101        title: 'Count',
102        kind: 'Number',
103        columnConstructor: Float64Array,
104        columnId: 'count',
105        sum: true,
106      },
107      {
108        title: 'First value',
109        kind: 'NUMBER',
110        columnConstructor: Float64Array,
111        columnId: 'first_value'
112      },
113      {
114        title: 'Last value',
115        kind: 'NUMBER',
116        columnConstructor: Float64Array,
117        columnId: 'last_value'
118      },
119      {
120        title: 'Min value',
121        kind: 'NUMBER',
122        columnConstructor: Float64Array,
123        columnId: 'min_value'
124      },
125      {
126        title: 'Max value',
127        kind: 'NUMBER',
128        columnConstructor: Float64Array,
129        columnId: 'max_value'
130      },
131
132    ];
133  }
134
135  async getExtra() {}
136
137  getTabName() {
138    return 'Counters';
139  }
140
141  getDefaultSorting(): Sorting {
142    return {column: 'name', direction: 'DESC'};
143  }
144}
145