1// Copyright (C) 2024 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 {globals} from '../../frontend/globals'; 16import {LONG, LONG_NULL, NUM} from '../../public'; 17import { 18 BaseCounterTrack, 19 BaseCounterTrackArgs, 20} from '../../frontend/base_counter_track'; 21 22interface TraceProcessorCounterTrackArgs extends BaseCounterTrackArgs { 23 trackId: number; 24 rootTable?: string; 25} 26 27export class TraceProcessorCounterTrack extends BaseCounterTrack { 28 private trackId: number; 29 private rootTable: string; 30 31 constructor(args: TraceProcessorCounterTrackArgs) { 32 super(args); 33 this.trackId = args.trackId; 34 this.rootTable = args.rootTable ?? 'counter'; 35 } 36 37 getSqlSource() { 38 return ` 39 select 40 ts, 41 value 42 from ${this.rootTable} 43 where track_id = ${this.trackId} 44 `; 45 } 46 47 onMouseClick({x}: {x: number}): boolean { 48 const {visibleTimeScale} = globals.timeline; 49 const time = visibleTimeScale.pxToHpTime(x).toTime('floor'); 50 51 const query = ` 52 select 53 id, 54 ts as leftTs, 55 ( 56 select ts 57 from ${this.rootTable} 58 where 59 track_id = ${this.trackId} 60 and ts >= ${time} 61 order by ts 62 limit 1 63 ) as rightTs 64 from ${this.rootTable} 65 where 66 track_id = ${this.trackId} 67 and ts < ${time} 68 order by ts DESC 69 limit 1 70 `; 71 72 this.engine.query(query).then((result) => { 73 const it = result.iter({ 74 id: NUM, 75 leftTs: LONG, 76 rightTs: LONG_NULL, 77 }); 78 if (!it.valid()) { 79 return; 80 } 81 const id = it.id; 82 globals.selectSingleEvent(this.trackKey, id); 83 }); 84 85 return true; 86 } 87} 88