1fb726d48Sopenharmony_ci/*
2fb726d48Sopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd.
3fb726d48Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fb726d48Sopenharmony_ci * you may not use this file except in compliance with the License.
5fb726d48Sopenharmony_ci * You may obtain a copy of the License at
6fb726d48Sopenharmony_ci *
7fb726d48Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fb726d48Sopenharmony_ci *
9fb726d48Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fb726d48Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fb726d48Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fb726d48Sopenharmony_ci * See the License for the specific language governing permissions and
13fb726d48Sopenharmony_ci * limitations under the License.
14fb726d48Sopenharmony_ci */
15fb726d48Sopenharmony_ci
16fb726d48Sopenharmony_ciimport { BaseElement, element } from '../../base-ui/BaseElement';
17fb726d48Sopenharmony_ciimport { LitTable } from '../../base-ui/table/lit-table';
18fb726d48Sopenharmony_ciimport '../../base-ui/table/lit-table';
19fb726d48Sopenharmony_ciimport { info } from '../../log/Log';
20fb726d48Sopenharmony_ciimport { LitProgressBar } from '../../base-ui/progress-bar/LitProgressBar';
21fb726d48Sopenharmony_ciimport { querySelectTraceStats, queryTraceMetaData } from '../database/sql/SqlLite.sql';
22fb726d48Sopenharmony_ciimport { SpInfoAndStatHtml } from './SpInfoAndStas.html';
23fb726d48Sopenharmony_ci
24fb726d48Sopenharmony_ci@element('sp-info-and-stats')
25fb726d48Sopenharmony_ciexport class SpInfoAndStats extends BaseElement {
26fb726d48Sopenharmony_ci  private metaData: Array<MetaDataTable> = [];
27fb726d48Sopenharmony_ci  private infoData: Array<InfoDataTable> = [];
28fb726d48Sopenharmony_ci  private metaTableEl: LitTable | undefined;
29fb726d48Sopenharmony_ci  private infoTableEl: LitTable | undefined;
30fb726d48Sopenharmony_ci
31fb726d48Sopenharmony_ci  initElements(): void {
32fb726d48Sopenharmony_ci    this.metaTableEl = this.shadowRoot!.querySelector<LitTable>('#metaData-table') as LitTable;
33fb726d48Sopenharmony_ci    this.infoTableEl = this.shadowRoot!.querySelector<LitTable>('#stats-table') as LitTable;
34fb726d48Sopenharmony_ci  }
35fb726d48Sopenharmony_ci
36fb726d48Sopenharmony_ci  initInfoAndStatsData(): void {
37fb726d48Sopenharmony_ci    let progressLoad = this.shadowRoot?.querySelector('.load-metric') as LitProgressBar;
38fb726d48Sopenharmony_ci    progressLoad!.loading = true;
39fb726d48Sopenharmony_ci    let time = new Date().getTime();
40fb726d48Sopenharmony_ci    this.initMetricItemData().then(() => {
41fb726d48Sopenharmony_ci      let durTime = new Date().getTime() - time;
42fb726d48Sopenharmony_ci      info(`InfoAndStatsData query time is: ${durTime}ms`);
43fb726d48Sopenharmony_ci      if (this.metaData.length > 0) {
44fb726d48Sopenharmony_ci        this.metaTableEl!.recycleDataSource = this.metaData;
45fb726d48Sopenharmony_ci      } else {
46fb726d48Sopenharmony_ci        this.metaTableEl!.recycleDataSource = [];
47fb726d48Sopenharmony_ci      }
48fb726d48Sopenharmony_ci      info('metaData(metric) size is: ', this.metaData.length);
49fb726d48Sopenharmony_ci      if (this.infoData.length > 0) {
50fb726d48Sopenharmony_ci        this.infoTableEl!.recycleDataSource = this.infoData;
51fb726d48Sopenharmony_ci      } else {
52fb726d48Sopenharmony_ci        this.infoTableEl!.recycleDataSource = [];
53fb726d48Sopenharmony_ci      }
54fb726d48Sopenharmony_ci      new ResizeObserver(() => {
55fb726d48Sopenharmony_ci        if (this.parentElement?.clientHeight !== 0) {
56fb726d48Sopenharmony_ci          this.metaTableEl!.style.height = '100%';
57fb726d48Sopenharmony_ci          this.metaTableEl!.reMeauseHeight();
58fb726d48Sopenharmony_ci          this.infoTableEl!.reMeauseHeight();
59fb726d48Sopenharmony_ci        }
60fb726d48Sopenharmony_ci      }).observe(this.parentElement!);
61fb726d48Sopenharmony_ci      info('infoData(metric) size is: ', this.infoData.length);
62fb726d48Sopenharmony_ci      let metaDataStyle: HTMLDivElement | undefined | null = this.metaTableEl!.shadowRoot?.querySelector(
63fb726d48Sopenharmony_ci        'div.body'
64fb726d48Sopenharmony_ci      ) as HTMLDivElement;
65fb726d48Sopenharmony_ci      let metaDataHeadStyle: HTMLDivElement | undefined | null = this.metaTableEl!.shadowRoot?.querySelector(
66fb726d48Sopenharmony_ci        'div.thead'
67fb726d48Sopenharmony_ci      ) as HTMLDivElement;
68fb726d48Sopenharmony_ci      let statsStyle: HTMLDivElement | undefined | null = this.infoTableEl!.shadowRoot?.querySelector(
69fb726d48Sopenharmony_ci        'div.body'
70fb726d48Sopenharmony_ci      ) as HTMLDivElement;
71fb726d48Sopenharmony_ci      let statsHeadStyle: HTMLDivElement | undefined | null = this.infoTableEl!.shadowRoot?.querySelector(
72fb726d48Sopenharmony_ci        'div.thead'
73fb726d48Sopenharmony_ci      ) as HTMLDivElement;
74fb726d48Sopenharmony_ci      let timeOutTs = 20;
75fb726d48Sopenharmony_ci      setTimeout(() => {
76fb726d48Sopenharmony_ci        this.initDataTableStyle(metaDataStyle!);
77fb726d48Sopenharmony_ci        this.initDataTableStyle(metaDataHeadStyle!);
78fb726d48Sopenharmony_ci        this.initDataTableStyle(statsStyle!);
79fb726d48Sopenharmony_ci        this.initDataTableStyle(statsHeadStyle!);
80fb726d48Sopenharmony_ci      }, timeOutTs);
81fb726d48Sopenharmony_ci      progressLoad!.loading = false;
82fb726d48Sopenharmony_ci    });
83fb726d48Sopenharmony_ci  }
84fb726d48Sopenharmony_ci
85fb726d48Sopenharmony_ci  initDataTableStyle(styleTable: HTMLDivElement): void {
86fb726d48Sopenharmony_ci    for (let index = 0; index < styleTable.children.length; index++) {
87fb726d48Sopenharmony_ci      // @ts-ignore
88fb726d48Sopenharmony_ci      styleTable.children[index].style.backgroundColor = 'var(--dark-background5,#F6F6F6)';
89fb726d48Sopenharmony_ci    }
90fb726d48Sopenharmony_ci    this.metaTableEl!.style.height = 'auto';
91fb726d48Sopenharmony_ci    this.metaTableEl!.style.minHeight = '80%';
92fb726d48Sopenharmony_ci    this.metaTableEl!.style.borderRadius = '16';
93fb726d48Sopenharmony_ci    this.infoTableEl!.style.height = '300px';
94fb726d48Sopenharmony_ci    this.infoTableEl!.style.minHeight = '80%';
95fb726d48Sopenharmony_ci    this.infoTableEl!.style.borderRadius = '16';
96fb726d48Sopenharmony_ci  }
97fb726d48Sopenharmony_ci
98fb726d48Sopenharmony_ci  async initMetricItemData(): Promise<void> {
99fb726d48Sopenharmony_ci    this.metaData = [];
100fb726d48Sopenharmony_ci    this.infoData = [];
101fb726d48Sopenharmony_ci    let mete = await queryTraceMetaData();
102fb726d48Sopenharmony_ci    if (mete) {
103fb726d48Sopenharmony_ci      for (let index = 0; index < mete.length; index++) {
104fb726d48Sopenharmony_ci        this.metaData.push({
105fb726d48Sopenharmony_ci          name: mete[index].name,
106fb726d48Sopenharmony_ci          value: mete[index].valueText,
107fb726d48Sopenharmony_ci        });
108fb726d48Sopenharmony_ci      }
109fb726d48Sopenharmony_ci    }
110fb726d48Sopenharmony_ci    let info = await querySelectTraceStats();
111fb726d48Sopenharmony_ci    if (info) {
112fb726d48Sopenharmony_ci      for (let index = 0; index < info.length; index++) {
113fb726d48Sopenharmony_ci        this.infoData.push({
114fb726d48Sopenharmony_ci          eventName: info[index].event_name,
115fb726d48Sopenharmony_ci          statType: info[index].stat_type,
116fb726d48Sopenharmony_ci          count: info[index].count,
117fb726d48Sopenharmony_ci        });
118fb726d48Sopenharmony_ci      }
119fb726d48Sopenharmony_ci    }
120fb726d48Sopenharmony_ci  }
121fb726d48Sopenharmony_ci
122fb726d48Sopenharmony_ci  initHtml(): string {
123fb726d48Sopenharmony_ci    return SpInfoAndStatHtml;
124fb726d48Sopenharmony_ci  }
125fb726d48Sopenharmony_ci}
126fb726d48Sopenharmony_ci
127fb726d48Sopenharmony_ciexport class MetaDataTable {
128fb726d48Sopenharmony_ci  name: string | undefined;
129fb726d48Sopenharmony_ci  value: string | undefined;
130fb726d48Sopenharmony_ci  type?: string | undefined;
131fb726d48Sopenharmony_ci}
132fb726d48Sopenharmony_ci
133fb726d48Sopenharmony_ciexport class InfoDataTable {
134fb726d48Sopenharmony_ci  eventName: string | undefined;
135fb726d48Sopenharmony_ci  statType: string | undefined;
136fb726d48Sopenharmony_ci  count: number | undefined;
137fb726d48Sopenharmony_ci  source?: string | undefined;
138fb726d48Sopenharmony_ci  serverity?: string | undefined;
139fb726d48Sopenharmony_ci}
140