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_ci
18fb726d48Sopenharmony_ciimport '../../base-ui/table/lit-table';
19fb726d48Sopenharmony_ciimport { LitProgressBar } from '../../base-ui/progress-bar/LitProgressBar';
20fb726d48Sopenharmony_ciimport { SpStatisticsHttpUtil } from '../../statistics/util/SpStatisticsHttpUtil';
21fb726d48Sopenharmony_ciimport { queryMetric } from '../database/sql/SqlLite.sql';
22fb726d48Sopenharmony_ciimport { SpMetricsHtml } from './SpMetrics.html';
23fb726d48Sopenharmony_ci
24fb726d48Sopenharmony_ci@element('sp-metrics')
25fb726d48Sopenharmony_ciexport class SpMetrics extends BaseElement {
26fb726d48Sopenharmony_ci  private selectMetricEl: HTMLSelectElement | undefined;
27fb726d48Sopenharmony_ci  private runButtonEl: HTMLButtonElement | undefined | null;
28fb726d48Sopenharmony_ci  private responseJson: HTMLPreElement | undefined | null;
29fb726d48Sopenharmony_ci  private metricProgressLoad: LitProgressBar | undefined;
30fb726d48Sopenharmony_ci
31fb726d48Sopenharmony_ci  reset(): void {
32fb726d48Sopenharmony_ci    this.selectMetricEl!.selectedIndex = 0;
33fb726d48Sopenharmony_ci    this.responseJson!.textContent = '';
34fb726d48Sopenharmony_ci  }
35fb726d48Sopenharmony_ci
36fb726d48Sopenharmony_ci  initElements(): void {
37fb726d48Sopenharmony_ci    this.metricProgressLoad = this.shadowRoot?.querySelector('.sp-load-metric') as LitProgressBar;
38fb726d48Sopenharmony_ci    this.selectMetricEl = this.shadowRoot?.querySelector('.sql-select') as HTMLSelectElement;
39fb726d48Sopenharmony_ci    this.runButtonEl = this.shadowRoot?.querySelector('.sql-select-button') as HTMLButtonElement;
40fb726d48Sopenharmony_ci    this.responseJson = this.shadowRoot?.querySelector('.response-json') as HTMLPreElement;
41fb726d48Sopenharmony_ci  }
42fb726d48Sopenharmony_ci
43fb726d48Sopenharmony_ci  runClickListener = (): void => {
44fb726d48Sopenharmony_ci    SpStatisticsHttpUtil.addOrdinaryVisitAction({
45fb726d48Sopenharmony_ci      event: 'metrics',
46fb726d48Sopenharmony_ci      action: 'metrics',
47fb726d48Sopenharmony_ci    });
48fb726d48Sopenharmony_ci    this.responseJson!.textContent = '';
49fb726d48Sopenharmony_ci    this.metricProgressLoad!.loading = true;
50fb726d48Sopenharmony_ci    let index = this.selectMetricEl!.selectedIndex;
51fb726d48Sopenharmony_ci    let optionEl = this.selectMetricEl?.querySelectorAll<HTMLOptionElement>('option')[index];
52fb726d48Sopenharmony_ci    if (optionEl && optionEl.value !== '') {
53fb726d48Sopenharmony_ci      queryMetric(optionEl.value).then((result) => {
54fb726d48Sopenharmony_ci        this.metricProgressLoad!.loading = false;
55fb726d48Sopenharmony_ci        this.responseJson!.textContent = result.toString();
56fb726d48Sopenharmony_ci      });
57fb726d48Sopenharmony_ci    } else {
58fb726d48Sopenharmony_ci      this.metricProgressLoad!.loading = false;
59fb726d48Sopenharmony_ci    }
60fb726d48Sopenharmony_ci  };
61fb726d48Sopenharmony_ci
62fb726d48Sopenharmony_ci  connectedCallback(): void {
63fb726d48Sopenharmony_ci    this.runButtonEl?.addEventListener('click', this.runClickListener);
64fb726d48Sopenharmony_ci  }
65fb726d48Sopenharmony_ci
66fb726d48Sopenharmony_ci  disconnectedCallback(): void {
67fb726d48Sopenharmony_ci    this.runButtonEl?.removeEventListener('click', this.runClickListener);
68fb726d48Sopenharmony_ci  }
69fb726d48Sopenharmony_ci
70fb726d48Sopenharmony_ci  initHtml(): string {
71fb726d48Sopenharmony_ci    return SpMetricsHtml;
72fb726d48Sopenharmony_ci  }
73fb726d48Sopenharmony_ci}
74