• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2024 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 {MakeTimestampStrategyType} from 'common/time/time';
18import {AddOperation} from 'trace/tree_node/operations/add_operation';
19import {PropertyTreeNode} from 'trace/tree_node/property_tree_node';
20import {DEFAULT_PROPERTY_TREE_NODE_FACTORY} from 'trace/tree_node/property_tree_node_factory';
21
22export class TransformToTimestamp extends AddOperation<PropertyTreeNode> {
23  constructor(
24    private readonly timestampNames: string[],
25    private readonly makeTimestampStrategy: MakeTimestampStrategyType,
26  ) {
27    super();
28  }
29
30  override makeProperties(value: PropertyTreeNode): PropertyTreeNode[] {
31    const timestampNodes: PropertyTreeNode[] = [];
32
33    for (const property of value.getAllChildren()) {
34      if (this.timestampNames.includes(property.name)) {
35        const nanosLong = property.getValue();
36        if (nanosLong === undefined || nanosLong === null) {
37          return [];
38        }
39        const timestamp = this.makeTimestampStrategy(
40          BigInt(nanosLong.toString()),
41        );
42        const timestampNode =
43          DEFAULT_PROPERTY_TREE_NODE_FACTORY.makeProtoProperty(
44            value.id,
45            property.name,
46            timestamp,
47          );
48        timestampNodes.push(timestampNode);
49      }
50    }
51
52    value.getAllChildren().forEach((property) => {
53      this.apply(property);
54    });
55
56    return timestampNodes;
57  }
58}
59