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 { SpSystemTrace } from '../SpSystemTrace';
17import { TraceRow } from '../trace/base/TraceRow';
18import { renders } from '../../database/ui-worker/ProcedureWorker';
19import { HiSysEventRender, HiSysEventStruct } from '../../database/ui-worker/ProcedureWorkerHiSysEvent';
20import { hiSysEventDataSender } from '../../database/data-trafic/HiSysEventDataSender';
21import { queryHiSysEventData } from '../../database/sql/Perf.sql';
22import { SpStatisticsHttpUtil } from '../../../statistics/util/SpStatisticsHttpUtil';
23
24export class SpHiSysEventChart {
25  private trace: SpSystemTrace;
26
27  constructor(trace: SpSystemTrace) {
28    this.trace = trace;
29  }
30
31  async init(): Promise<void> {
32    let hiSysEventData = await queryHiSysEventData();
33    if (hiSysEventData.length === 0) {
34      return;
35    }
36    let eventRow = await this.initRow();
37    this.trace.rowsEL?.appendChild(eventRow);
38    // 统计hiSysevent插件
39    let requestBody = {
40      eventData:{
41        plugin:['hisysevent']
42      }
43    };
44    SpStatisticsHttpUtil.recordPluginUsage(requestBody);
45  }
46
47  async initRow(): Promise<TraceRow<HiSysEventStruct>> {
48    let hiSysEventRow = TraceRow.skeleton<HiSysEventStruct>();
49    hiSysEventRow.rowParentId = '';
50    hiSysEventRow.rowId = 'Hisysevent';
51    hiSysEventRow.rowType = TraceRow.ROW_TYPE_HI_SYSEVENT;
52    hiSysEventRow.name = 'Hisysevent';
53    hiSysEventRow.style.width = '100%';
54    hiSysEventRow.style.height = '40px';
55    hiSysEventRow.setAttribute('height', '40px');
56    hiSysEventRow.setAttribute('children', '');
57    hiSysEventRow.supplierFrame = (): Promise<HiSysEventStruct[]> => {
58      return hiSysEventDataSender(hiSysEventRow).then((res) => {
59        return res;
60      });
61    };
62    hiSysEventRow.addTemplateTypes('HiSysEvent');
63    hiSysEventRow.favoriteChangeHandler = this.trace.favoriteChangeHandler;
64    hiSysEventRow.selectChangeHandler = this.trace.selectChangeHandler;
65    hiSysEventRow.onThreadHandler = (useCache: boolean): void => {
66      let context: CanvasRenderingContext2D;
67      if (hiSysEventRow.currentContext) {
68        context = hiSysEventRow.currentContext;
69      } else {
70        context = hiSysEventRow.collect ? this.trace.canvasFavoritePanelCtx! : this.trace.canvasPanelCtx!;
71      }
72      hiSysEventRow!.canvasSave(context);
73      (renders.hiSysEvent as HiSysEventRender).renderMainThread(
74        {
75          context: context,
76          useCache: useCache,
77          type: 'hisys_event',
78        },
79        hiSysEventRow!
80      );
81      hiSysEventRow!.canvasRestore(context, this.trace);
82    };
83    return hiSysEventRow;
84  }
85}
86