xref: /aosp_15_r20/external/perfetto/ui/src/plugins/dev.perfetto.SqlModules/index.ts (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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 {assetSrc} from '../../base/assets';
16import {assertExists} from '../../base/logging';
17import {PerfettoPlugin} from '../../public/plugin';
18import {Trace} from '../../public/trace';
19import {SqlModules} from './sql_modules';
20import {SQL_MODULES_DOCS_SCHEMA, SqlModulesImpl} from './sql_modules_impl';
21import {extensions} from '../../components/extensions';
22
23export default class implements PerfettoPlugin {
24  static readonly id = 'dev.perfetto.SqlModules';
25  private sqlModules?: SqlModules;
26
27  async onTraceLoad(ctx: Trace) {
28    const resp = await fetch(assetSrc('stdlib_docs.json'));
29    const json = await resp.json();
30    const docs = SQL_MODULES_DOCS_SCHEMA.parse(json);
31    const sqlModules = new SqlModulesImpl(docs);
32    this.sqlModules = sqlModules;
33
34    ctx.commands.registerCommand({
35      id: 'perfetto.OpenSqlModulesTable',
36      name: 'Open table...',
37      callback: async () => {
38        const tables = sqlModules.listTables();
39        const tableName = await ctx.omnibox.prompt('Choose a table...', tables);
40        if (tableName === undefined) {
41          return;
42        }
43        const module = sqlModules.getModuleForTable(tableName);
44        if (module === undefined) {
45          return;
46        }
47        const sqlTable = module.getSqlTableDescription(tableName);
48        sqlTable &&
49          extensions.addSqlTableTab(ctx, {
50            table: sqlTable,
51          });
52      },
53    });
54  }
55
56  getSqlModules() {
57    return assertExists(this.sqlModules);
58  }
59}
60