xref: /aosp_15_r20/development/tools/winscope/src/viewers/common/log_filters.ts (revision 90c8c64db3049935a07c6143d7fd006e26f8ecca)
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 {StringFilterPredicate} from 'viewers/common/string_filter_predicate';
18import {TextFilter} from 'viewers/common/text_filter';
19
20export abstract class LogFilter {
21  constructor(
22    public innerFilterWidthCss: string,
23    public outerFilterWidthCss: string,
24  ) {}
25
26  abstract updateFilterValue(value: string[]): void;
27  abstract getFilterPredicate(): StringFilterPredicate;
28}
29
30export class LogTextFilter extends LogFilter {
31  constructor(
32    public textFilter: TextFilter,
33    innerFilterWidthCss = '100',
34    outerFilterWidthCss = '100%',
35  ) {
36    super(innerFilterWidthCss, outerFilterWidthCss);
37  }
38
39  override updateFilterValue(value: string[]): void {
40    this.textFilter.filterString = value.at(0) ?? '';
41  }
42
43  override getFilterPredicate(): StringFilterPredicate {
44    return this.textFilter.getFilterPredicate();
45  }
46}
47
48export class LogSelectFilter extends LogFilter {
49  private filterValue: string[] = [];
50  private readonly filterPredicate: StringFilterPredicate;
51
52  constructor(
53    public options: string[],
54    readonly shouldFilterBySubstring = false,
55    innerFilterWidthCss = '100',
56    outerFilterWidthCss = '100%',
57  ) {
58    super(innerFilterWidthCss, outerFilterWidthCss);
59
60    if (this.shouldFilterBySubstring) {
61      this.filterPredicate = (entryString) => {
62        if (this.filterValue.length === 0) return true;
63        return this.filterValue.some((val) => entryString.includes(val));
64      };
65    } else {
66      this.filterPredicate = (entryString) => {
67        if (this.filterValue.length === 0) return true;
68        return this.filterValue.includes(entryString);
69      };
70    }
71  }
72
73  override updateFilterValue(value: string[]) {
74    this.filterValue = value;
75  }
76
77  override getFilterPredicate(): StringFilterPredicate {
78    return this.filterPredicate;
79  }
80}
81