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 {assertDefined} from 'common/assert_utils';
18import {AbsoluteFrameIndex, Trace} from 'trace/trace';
19import {Traces} from 'trace/traces';
20import {TraceType} from 'trace/trace_type';
21import {TraceUtils} from './trace_utils';
22
23export class TracesUtils {
24  static extractTraces(traces: Traces): Array<Trace<{}>> {
25    return traces.mapTrace((trace) => trace);
26  }
27
28  static async extractEntries(
29    traces: Traces,
30  ): Promise<Map<TraceType, Array<{}>>> {
31    const entries = new Map<TraceType, Array<{}>>();
32
33    const promises = traces.mapTrace(async (trace) => {
34      entries.set(trace.type, await TraceUtils.extractEntries(trace));
35    });
36    await Promise.all(promises);
37
38    return entries;
39  }
40
41  static async extractFrames(
42    traces: Traces,
43  ): Promise<Map<AbsoluteFrameIndex, Map<TraceType, Array<{}>>>> {
44    const frames = new Map<AbsoluteFrameIndex, Map<TraceType, Array<{}>>>();
45
46    const framePromises = traces.mapFrame(async (frame, index) => {
47      frames.set(index, new Map<TraceType, Array<{}>>());
48      const tracePromises = frame.mapTrace(async (trace, type) => {
49        assertDefined(frames.get(index)).set(
50          type,
51          await TraceUtils.extractEntries(trace),
52        );
53      });
54      await Promise.all(tracePromises);
55    });
56    await Promise.all(framePromises);
57
58    return frames;
59  }
60}
61