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 {tpDurationToSeconds} from '../../common/time'; 19import {globals} from '../../frontend/globals'; 20import {Config, COUNTER_TRACK_KIND} from '../../tracks/counter'; 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 const duration = area.end - area.start; 42 const durationSec = tpDurationToSeconds(duration); 43 44 const query = `create view ${this.kind} as select 45 name, 46 count(1) as count, 47 round(sum(weighted_value)/${duration}, 2) as avg_value, 48 last as last_value, 49 first as first_value, 50 max(last) - min(first) as delta_value, 51 round((max(last) - min(first))/${durationSec}, 2) as rate, 52 min(value) as min_value, 53 max(value) as max_value 54 from 55 (select *, 56 (min(ts + dur, ${area.end}) - max(ts,${area.start})) 57 * value as weighted_value, 58 first_value(value) over 59 (partition by track_id order by ts) as first, 60 last_value(value) over 61 (partition by track_id order by ts 62 range between unbounded preceding and unbounded following) as last 63 from experimental_counter_dur 64 where track_id in (${ids}) 65 and ts + dur >= ${area.start} and 66 ts <= ${area.end}) 67 join counter_track 68 on track_id = counter_track.id 69 group by track_id`; 70 71 await engine.query(query); 72 return true; 73 } 74 75 getColumnDefinitions(): ColumnDef[] { 76 return [ 77 { 78 title: 'Name', 79 kind: 'STRING', 80 columnConstructor: Uint16Array, 81 columnId: 'name', 82 }, 83 { 84 title: 'Delta value', 85 kind: 'NUMBER', 86 columnConstructor: Float64Array, 87 columnId: 'delta_value', 88 }, 89 { 90 title: 'Rate /s', 91 kind: 'Number', 92 columnConstructor: Float64Array, 93 columnId: 'rate', 94 }, 95 { 96 title: 'Weighted avg value', 97 kind: 'Number', 98 columnConstructor: Float64Array, 99 columnId: 'avg_value', 100 }, 101 { 102 title: 'Count', 103 kind: 'Number', 104 columnConstructor: Float64Array, 105 columnId: 'count', 106 sum: true, 107 }, 108 { 109 title: 'First value', 110 kind: 'NUMBER', 111 columnConstructor: Float64Array, 112 columnId: 'first_value', 113 }, 114 { 115 title: 'Last value', 116 kind: 'NUMBER', 117 columnConstructor: Float64Array, 118 columnId: 'last_value', 119 }, 120 { 121 title: 'Min value', 122 kind: 'NUMBER', 123 columnConstructor: Float64Array, 124 columnId: 'min_value', 125 }, 126 { 127 title: 'Max value', 128 kind: 'NUMBER', 129 columnConstructor: Float64Array, 130 columnId: 'max_value', 131 }, 132 133 ]; 134 } 135 136 async getExtra() {} 137 138 getTabName() { 139 return 'Counters'; 140 } 141 142 getDefaultSorting(): Sorting { 143 return {column: 'name', direction: 'DESC'}; 144 } 145} 146