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 {ParserTimestampConverter} from 'common/timestamp_converter';
18import {SetFormatters} from 'parsers/operations/set_formatters';
19import {
20  MakeTimestampStrategyType,
21  TransformToTimestamp,
22} from 'parsers/operations/transform_to_timestamp';
23import {TIMESTAMP_NODE_FORMATTER} from 'trace/tree_node/formatters';
24import {PropertyTreeBuilderFromProto} from 'trace/tree_node/property_tree_builder_from_proto';
25import {PropertyTreeNode} from 'trace/tree_node/property_tree_node';
26import {LogMessage} from './log_message';
27
28export class ParserProtologUtils {
29  static makeMessagePropertiesTree(
30    logMessage: LogMessage,
31    timestampConverter: ParserTimestampConverter,
32    isMonotonic: boolean,
33  ): PropertyTreeNode {
34    const tree = new PropertyTreeBuilderFromProto()
35      .setData(logMessage)
36      .setRootId('ProtoLogTrace')
37      .setRootName('entry')
38      .setVisitPrototype(false)
39      .build();
40
41    const customFormatters = new Map([['timestamp', TIMESTAMP_NODE_FORMATTER]]);
42
43    const strategy: MakeTimestampStrategyType = (valueNs: bigint) => {
44      if (isMonotonic) {
45        return timestampConverter.makeTimestampFromMonotonicNs(valueNs);
46      }
47      return timestampConverter.makeTimestampFromBootTimeNs(valueNs);
48    };
49
50    new TransformToTimestamp(['timestamp'], strategy).apply(tree);
51    new SetFormatters(undefined, customFormatters).apply(tree);
52    return tree;
53  }
54}
55