• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { BaseElement, element } from '../../../../../base-ui/BaseElement.js';
17import { LitTable } from '../../../../../base-ui/table/lit-table.js';
18import { SelectionParam } from '../../../../bean/BoxSelection.js';
19import { getTabPaneFrequencySampleData } from '../../../../database/SqlLite.js';
20import { LitProgressBar } from '../../../../../base-ui/progress-bar/LitProgressBar.js';
21import { Utils } from '../../base/Utils.js';
22import { ColorUtils } from '../../base/ColorUtils.js';
23import { resizeObserver } from '../SheetUtils.js';
24
25@element('tabpane-frequency-sample')
26export class TabPaneFrequencySample extends BaseElement {
27  private frequencySampleTbl: LitTable | null | undefined;
28  private range: HTMLLabelElement | null | undefined;
29  private loadDataInCache: boolean = true;
30  private selectionParam: SelectionParam | null | undefined;
31  private frequencyProgressEL: LitProgressBar | null | undefined;
32  private frequencyLoadingPage: any;
33  private frequencyLoadingList: number[] = [];
34  private frequencySampleSource: any[] = [];
35  private frequencySampleSortKey: string = 'counter';
36  private frequencySampleSortType: number = 0;
37
38  set data(frequencySampleValue: SelectionParam | any) {
39    if (frequencySampleValue == this.selectionParam) {
40      return;
41    }
42    this.frequencyProgressEL!.loading = true;
43    this.frequencyLoadingPage.style.visibility = 'visible';
44    this.selectionParam = frequencySampleValue;
45    // @ts-ignore
46    this.frequencySampleTbl!.shadowRoot?.querySelector('.table').style.height =
47      this.parentElement!.clientHeight - 25 + 'px';
48    this.queryDataByDB(frequencySampleValue);
49  }
50
51  initElements(): void {
52    this.frequencyProgressEL = this.shadowRoot!.querySelector<LitProgressBar>('.progressFre');
53    this.frequencyLoadingPage = this.shadowRoot!.querySelector('.loadingFre');
54    this.frequencySampleTbl = this.shadowRoot!.querySelector<LitTable>('#tb-states');
55    this.frequencySampleTbl!.addEventListener('column-click', (evt) => {
56      // @ts-ignore
57      this.frequencySampleSortKey = evt.detail.key;
58      // @ts-ignore
59      this.frequencySampleSortType = evt.detail.sort;
60      // @ts-ignore
61      this.sortTable(evt.detail.key, evt.detail.sort);
62    });
63  }
64
65  connectedCallback() {
66    super.connectedCallback();
67    resizeObserver(this.parentElement!, this.frequencySampleTbl!, 25, this.frequencyLoadingPage, 24);
68  }
69
70  queryDataByDB(frqSampleParam: SelectionParam | any) {
71    this.frequencyLoadingList.push(1);
72    this.frequencyProgressEL!.loading = true;
73    this.frequencyLoadingPage.style.visibility = 'visible';
74
75    getTabPaneFrequencySampleData(
76      frqSampleParam.leftNs + frqSampleParam.recordStartNs,
77      frqSampleParam.rightNs + frqSampleParam.recordStartNs,
78      frqSampleParam.cpuFreqFilterIds
79    ).then((result) => {
80      this.frequencyLoadingList.splice(0, 1);
81      if (this.frequencyLoadingList.length == 0) {
82        this.frequencyProgressEL!.loading = false;
83        this.frequencyLoadingPage.style.visibility = 'hidden';
84      }
85      let sampleMap = new Map<any, any>();
86      frqSampleParam.cpuFreqFilterIds.forEach((a: number) => {
87        this.getInitTime(
88          result.filter((f) => f.filterId == a),
89          sampleMap,
90          frqSampleParam
91        );
92      });
93
94      let frqSampleList: Array<any> = [];
95      sampleMap.forEach((a) => {
96        a.timeStr = parseFloat((a.time / 1000000.0).toFixed(6));
97        frqSampleList.push(a);
98      });
99      this.frequencySampleSource = frqSampleList;
100      this.sortTable(this.frequencySampleSortKey, this.frequencySampleSortType);
101    });
102  }
103
104  getInitTime(initFreqResult: Array<any>, sampleMap: Map<any, any>, selectionParam: SelectionParam) {
105    let leftStartNs = selectionParam.leftNs + selectionParam.recordStartNs;
106    let rightEndNs = selectionParam.rightNs + selectionParam.recordStartNs;
107    if (initFreqResult.length == 0) return;
108    let includeData = initFreqResult.findIndex((a) => a.ts >= leftStartNs);
109    if (includeData !== 0) {
110      initFreqResult = initFreqResult.slice(
111        includeData == -1 ? initFreqResult.length - 1 : includeData - 1,
112        initFreqResult.length
113      );
114    }
115    if (initFreqResult[0].ts < leftStartNs && includeData !== 0) initFreqResult[0].ts = leftStartNs;
116    initFreqResult.forEach((item, idx) => {
117      if (idx + 1 == initFreqResult.length) {
118        item.time = rightEndNs - item.ts;
119      } else {
120        item.time = initFreqResult[idx + 1].ts - item.ts;
121      }
122      if (sampleMap.has(item.filterId + '-' + item.value)) {
123        let obj = sampleMap.get(item.filterId + '-' + item.value);
124        obj.time += item.time;
125      } else {
126        sampleMap.set(item.filterId + '-' + item.value, {
127          ...item,
128          counter: 'Cpu ' + item.cpu,
129          valueStr: ColorUtils.formatNumberComma(item.value),
130        });
131      }
132    });
133  }
134
135  sortTable(key: string, type: number) {
136    if (type == 0) {
137      this.frequencySampleTbl!.recycleDataSource = this.frequencySampleSource;
138    } else {
139      let arr = Array.from(this.frequencySampleSource);
140      arr.sort((frequencySampleLeftData, frequencySampleRightData): number => {
141        if (key == 'timeStr') {
142          if (type == 1) {
143            return frequencySampleLeftData.time - frequencySampleRightData.time;
144          } else {
145            return frequencySampleRightData.time - frequencySampleLeftData.time;
146          }
147        } else if (key == 'counter') {
148          if (frequencySampleLeftData.counter > frequencySampleRightData.counter) {
149            return type === 2 ? -1 : 1;
150          } else if (frequencySampleLeftData.counter == frequencySampleRightData.counter) {
151            return 0;
152          } else {
153            return type === 2 ? 1 : -1;
154          }
155        } else if (key == 'valueStr') {
156          if (type == 1) {
157            return frequencySampleLeftData.value - frequencySampleRightData.value;
158          } else {
159            return frequencySampleRightData.value - frequencySampleLeftData.value;
160          }
161        } else {
162          return 0;
163        }
164      });
165      this.frequencySampleTbl!.recycleDataSource = arr;
166    }
167  }
168
169  initHtml(): string {
170    return `
171        <style>
172        .progressFre{
173            bottom: 5px;
174            position: absolute;
175            height: 1px;
176            left: 0;
177            right: 0;
178        }
179        :host{
180            padding: 10px 10px;
181            display: flex;
182            flex-direction: column;
183        }
184        .loadingFre{
185            bottom: 0;
186            position: absolute;
187            left: 0;
188            right: 0;
189            width:100%;
190            background:transparent;
191            z-index: 999999;
192        }
193        </style>
194        <lit-table id="tb-states" style="height: auto" >
195            <lit-table-column class="freq-sample-column" width="20%" title="Cpu" data-index="counter" key="counter" align="flex-start" order>
196            </lit-table-column>
197            <lit-table-column class="freq-sample-column" width="1fr" title="Time(ms)" data-index="timeStr" key="timeStr" align="flex-start" order>
198            </lit-table-column>
199            <lit-table-column class="freq-sample-column" width="1fr" title="Value(kHz)" data-index="valueStr" key="valueStr" align="flex-start" order>
200            </lit-table-column>
201        </lit-table>
202        <lit-progress-bar class="progressFre"></lit-progress-bar>
203        <div class="loadingFre"></div>
204        `;
205  }
206}
207