1/*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import {Timestamp} from 'common/time';
18import {AbsoluteFrameIndex, Trace} from 'trace/trace';
19
20export class TraceUtils {
21  static async extractEntries<T>(trace: Trace<T>): Promise<T[]> {
22    const promises = trace.mapEntry(async (entry, index) => {
23      return await entry.getValue();
24    });
25    return await Promise.all(promises);
26  }
27
28  static extractTimestamps<T>(trace: Trace<T>): Timestamp[] {
29    const timestamps = new Array<Timestamp>();
30    trace.forEachTimestamp((timestamp) => {
31      timestamps.push(timestamp);
32    });
33    return timestamps;
34  }
35
36  static async extractFrames<T>(
37    trace: Trace<T>,
38  ): Promise<Map<AbsoluteFrameIndex, T[]>> {
39    const frames = new Map<AbsoluteFrameIndex, T[]>();
40    const promises = trace.mapFrame(async (frame, index) => {
41      frames.set(index, await TraceUtils.extractEntries(frame));
42    });
43    await Promise.all(promises);
44    return frames;
45  }
46}
47