• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {Engine, TrackContext} from '../public';
16import {BaseCounterTrack, CounterOptions} from './base_counter_track';
17import {CounterColumns, SqlDataSource} from './debug_tracks/debug_tracks';
18import {uuidv4Sql} from '../base/uuid';
19import {createPerfettoTable} from '../trace_processor/sql_utils';
20
21export type SimpleCounterTrackConfig = {
22  data: SqlDataSource;
23  columns: CounterColumns;
24  options?: Partial<CounterOptions>;
25};
26
27export class SimpleCounterTrack extends BaseCounterTrack {
28  private config: SimpleCounterTrackConfig;
29  private sqlTableName: string;
30
31  constructor(
32    engine: Engine,
33    ctx: TrackContext,
34    config: SimpleCounterTrackConfig,
35  ) {
36    super({
37      engine,
38      trackKey: ctx.trackKey,
39      options: config.options,
40    });
41    this.config = config;
42    this.sqlTableName = `__simple_counter_${uuidv4Sql()}`;
43  }
44
45  async onInit() {
46    return await createPerfettoTable(
47      this.engine,
48      this.sqlTableName,
49      `
50        with data as (
51          ${this.config.data.sqlSource}
52        )
53        select
54          ${this.config.columns.ts} as ts,
55          ${this.config.columns.value} as value
56        from data
57        order by ts
58      `,
59    );
60  }
61
62  getSqlSource(): string {
63    return `select * from ${this.sqlTableName}`;
64  }
65}
66