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 {ParserTimestampConverter} from 'common/timestamp_converter';
19import {CoarseVersion} from 'trace/coarse_version';
20import {
21  CustomQueryParserResultTypeMap,
22  CustomQueryType,
23} from 'trace/custom_query';
24import {AbsoluteEntryIndex, EntriesRange} from 'trace/index_types';
25import {Parser} from 'trace/parser';
26import {TraceType} from 'trace/trace_type';
27
28export abstract class AbstractTracesParser<T> implements Parser<T> {
29  private timestamps: Timestamp[] | undefined;
30  protected timestampConverter: ParserTimestampConverter;
31
32  protected abstract getTimestamp(decodedEntry: any): Timestamp;
33
34  constructor(timestampConverter: ParserTimestampConverter) {
35    this.timestampConverter = timestampConverter;
36  }
37
38  getCoarseVersion(): CoarseVersion {
39    return CoarseVersion.LEGACY;
40  }
41
42  customQuery<Q extends CustomQueryType>(
43    type: Q,
44    entriesRange: EntriesRange,
45  ): Promise<CustomQueryParserResultTypeMap[Q]> {
46    throw new Error('Not implemented');
47  }
48
49  getTimestamps(): Timestamp[] | undefined {
50    return this.timestamps;
51  }
52
53  async createTimestamps() {
54    this.timestamps = await this.decodeTimestamps();
55  }
56
57  private async decodeTimestamps() {
58    const timestampsNs = [];
59    for (let index = 0; index < this.getLengthEntries(); index++) {
60      const entry = await this.getEntry(index);
61      const timestamp = this.getTimestamp(entry);
62      timestampsNs.push(timestamp);
63    }
64    return timestampsNs;
65  }
66
67  abstract parse(): Promise<void>;
68  abstract getDescriptors(): string[];
69  abstract getTraceType(): TraceType;
70  abstract getEntry(index: AbsoluteEntryIndex): Promise<T>;
71  abstract getLengthEntries(): number;
72  abstract getRealToMonotonicTimeOffsetNs(): bigint | undefined;
73  abstract getRealToBootTimeOffsetNs(): bigint | undefined;
74}
75