• 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 {z} from 'zod';
16import {TARGET_PLATFORMS, TargetPlatformId} from './interfaces/target_platform';
17
18// Overall view
19// RECORD_PLUGIN_SCHEMA:
20//   target: TARGET_SCHEMA
21//   lastSession: RECORD_SESSION_SCHEMA
22//      probes: PROBES_SCHEMA{}
23//   savedSessions: Array<RECORD_SESSION_SCHEMA>
24//      probes: PROBES_SCHEMA{}
25
26// Holds the state of the PROBES_PAGE subpages (e.g., Memory).
27// We don't define a strongly-typed schema for each probes as they are
28// changed frequently. Each probe is modelled as:
29// - An enable/disable boolean (the presence of the key)
30// - A map of "settings". Each setting widget (Slider, Textarea, Toggle)
31//   takes care of its own de/serialization.
32export const PROBES_SCHEMA = z
33  .record(
34    z.string(), // key: the RecordProbe.id (it's globally unique).
35    z.object({
36      settings: z
37        .record(
38          z.string(), // key: the key in the RecordProbe.settings map.
39          z.unknown(), // value: The result of ProbeSetting.serialize().
40        )
41        .default({}),
42    }),
43  )
44  .default({});
45export type ProbesSchema = z.infer<typeof PROBES_SCHEMA>;
46
47// The schema that holds the settings for a recording session, that is, the
48// state of the probes and the buffer size & type.
49// This does NOT include the state of the other recording pages (e.g. the
50// Target device selector, the "saved sessions", etc)
51export const RECORD_SESSION_SCHEMA = z
52  .object({
53    mode: z
54      .enum(['STOP_WHEN_FULL', 'RING_BUFFER', 'LONG_TRACE'])
55      .default('STOP_WHEN_FULL'),
56    bufSizeKb: z.number().default(64 * 1024),
57    durationMs: z.number().default(10_000),
58    maxFileSizeMb: z.number().default(500),
59    fileWritePeriodMs: z.number().default(2500),
60    compression: z.boolean().default(false),
61    probes: PROBES_SCHEMA,
62  })
63  .default({});
64export type RecordSessionSchema = z.infer<typeof RECORD_SESSION_SCHEMA>;
65
66// The schema for the target selection page.
67export const TARGET_SCHEMA = z
68  .object({
69    platformId: z
70      .enum(TARGET_PLATFORMS.map((p) => p.id) as [TargetPlatformId])
71      .optional(),
72    transportId: z.string().optional(),
73    targetId: z.string().optional(),
74  })
75  .default({});
76export type TargetSchema = z.infer<typeof TARGET_SCHEMA>;
77
78export const SAVED_SESSION_SCHEMA = z.object({
79  name: z.string(),
80  config: RECORD_SESSION_SCHEMA,
81});
82export type SavedSessionSchema = z.infer<typeof SAVED_SESSION_SCHEMA>;
83
84// The schema for the root object that holds the whole state of the record
85// plugin.
86export const RECORD_PLUGIN_SCHEMA = z
87  .object({
88    target: TARGET_SCHEMA,
89    autoOpenTrace: z.boolean().default(true),
90    lastSession: RECORD_SESSION_SCHEMA.default({}),
91    savedSessions: z.array(SAVED_SESSION_SCHEMA).default([]),
92  })
93  .default({});
94export type RecordPluginSchema = z.infer<typeof RECORD_PLUGIN_SCHEMA>;
95