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 { SpSystemTrace } from '../SpSystemTrace';
17fb726d48Sopenharmony_ciimport { TraceRow } from '../trace/base/TraceRow';
18fb726d48Sopenharmony_ciimport { renders } from '../../database/ui-worker/ProcedureWorker';
19fb726d48Sopenharmony_ciimport { CpuFreqStruct } from '../../database/ui-worker/ProcedureWorkerFreq';
20fb726d48Sopenharmony_ciimport {
21fb726d48Sopenharmony_ci  queryAppStartupProcessIds,
22fb726d48Sopenharmony_ci  queryProcessStartup,
23fb726d48Sopenharmony_ci  querySingleAppStartupsName,
24fb726d48Sopenharmony_ci} from '../../database/sql/ProcessThread.sql';
25fb726d48Sopenharmony_ciimport { FlagsConfig } from '../SpFlags';
26fb726d48Sopenharmony_ciimport { AllAppStartupStruct, AllAppStartupRender } from '../../database/ui-worker/ProcedureWorkerAllAppStartup';
27fb726d48Sopenharmony_ci
28fb726d48Sopenharmony_ciexport class SpAllAppStartupsChart {
29fb726d48Sopenharmony_ci  private readonly trace: SpSystemTrace | undefined;
30fb726d48Sopenharmony_ci  static APP_STARTUP_PID_ARR: Array<number> = [];
31fb726d48Sopenharmony_ci  static jsonRow: TraceRow<CpuFreqStruct> | undefined;
32fb726d48Sopenharmony_ci  static trace: SpSystemTrace;
33fb726d48Sopenharmony_ci  static AllAppStartupsNameArr: unknown[] = [];
34fb726d48Sopenharmony_ci  static allAppStartupsAva: number[] = [];
35fb726d48Sopenharmony_ci
36fb726d48Sopenharmony_ci  constructor(trace: SpSystemTrace) {
37fb726d48Sopenharmony_ci    SpAllAppStartupsChart.trace = trace;
38fb726d48Sopenharmony_ci  }
39fb726d48Sopenharmony_ci
40fb726d48Sopenharmony_ci  async init(): Promise<void> {
41fb726d48Sopenharmony_ci    SpAllAppStartupsChart.APP_STARTUP_PID_ARR = [];
42fb726d48Sopenharmony_ci    let appStartUpPids = await queryAppStartupProcessIds();
43fb726d48Sopenharmony_ci    appStartUpPids.forEach((it) => SpAllAppStartupsChart.APP_STARTUP_PID_ARR.push(it.pid));
44fb726d48Sopenharmony_ci    SpAllAppStartupsChart.AllAppStartupsNameArr = [];
45fb726d48Sopenharmony_ci    SpAllAppStartupsChart.allAppStartupsAva = [];
46fb726d48Sopenharmony_ci    for (let i = 0; i < SpAllAppStartupsChart.APP_STARTUP_PID_ARR.length; i++) {
47fb726d48Sopenharmony_ci      let tmpSingleApp: unknown[] = await queryProcessStartup(SpAllAppStartupsChart.APP_STARTUP_PID_ARR[i]!);
48fb726d48Sopenharmony_ci      if (tmpSingleApp.length === 8) {
49fb726d48Sopenharmony_ci        let avilSingleName = await querySingleAppStartupsName(SpAllAppStartupsChart.APP_STARTUP_PID_ARR[i]!);
50fb726d48Sopenharmony_ci        SpAllAppStartupsChart.allAppStartupsAva.push(SpAllAppStartupsChart.APP_STARTUP_PID_ARR[i]); //@ts-ignore
51fb726d48Sopenharmony_ci        SpAllAppStartupsChart.AllAppStartupsNameArr.push(avilSingleName![0].name);
52fb726d48Sopenharmony_ci      }
53fb726d48Sopenharmony_ci    }
54fb726d48Sopenharmony_ci    let loadAppStartup: boolean = FlagsConfig.getFlagsConfigEnableStatus('AppStartup');
55fb726d48Sopenharmony_ci    if (loadAppStartup && SpAllAppStartupsChart.allAppStartupsAva.length) {
56fb726d48Sopenharmony_ci      await this.initFolder();
57fb726d48Sopenharmony_ci    }
58fb726d48Sopenharmony_ci  }
59fb726d48Sopenharmony_ci
60fb726d48Sopenharmony_ci  private supplier(row: TraceRow<AllAppStartupStruct>): void {
61fb726d48Sopenharmony_ci    row.supplier = async (): Promise<Array<AllAppStartupStruct>> => {
62fb726d48Sopenharmony_ci      let sendRes: AllAppStartupStruct[] | PromiseLike<AllAppStartupStruct[]> = [];
63fb726d48Sopenharmony_ci      for (let i = 0; i < SpAllAppStartupsChart.allAppStartupsAva.length; i++) {
64fb726d48Sopenharmony_ci        let tmpResArr = await queryProcessStartup(SpAllAppStartupsChart.allAppStartupsAva[i]);
65fb726d48Sopenharmony_ci        let maxStartTs: number | undefined = tmpResArr[0].startTs;
66fb726d48Sopenharmony_ci        let minStartTs: number | undefined = tmpResArr[0].startTs;
67fb726d48Sopenharmony_ci        let singleDur = tmpResArr[0].dur;
68fb726d48Sopenharmony_ci        let endTs: number | undefined = tmpResArr[0].startTs;
69fb726d48Sopenharmony_ci        if (tmpResArr.length > 1) {
70fb726d48Sopenharmony_ci          for (let j = 0; j < tmpResArr.length; j++) {
71fb726d48Sopenharmony_ci            if (Number(tmpResArr[j].startTs) > Number(maxStartTs)) {
72fb726d48Sopenharmony_ci              maxStartTs = tmpResArr[j].startTs;
73fb726d48Sopenharmony_ci            } else if (Number(tmpResArr[j].startTs) < Number(minStartTs)) {
74fb726d48Sopenharmony_ci              minStartTs = tmpResArr[j].startTs;
75fb726d48Sopenharmony_ci            }
76fb726d48Sopenharmony_ci          }
77fb726d48Sopenharmony_ci          tmpResArr.forEach((item) => {
78fb726d48Sopenharmony_ci            if (item.startTs === maxStartTs) {
79fb726d48Sopenharmony_ci              endTs = Number(item.startTs) + Number(item.dur);
80fb726d48Sopenharmony_ci              singleDur = Number(endTs) - Number(minStartTs);
81fb726d48Sopenharmony_ci            }
82fb726d48Sopenharmony_ci          });
83fb726d48Sopenharmony_ci        } else if (tmpResArr.length === 1) {
84fb726d48Sopenharmony_ci          minStartTs = tmpResArr[0].startTs;
85fb726d48Sopenharmony_ci          singleDur = tmpResArr[0].dur;
86fb726d48Sopenharmony_ci        }
87fb726d48Sopenharmony_ci        sendRes.push({
88fb726d48Sopenharmony_ci          dur: singleDur,
89fb726d48Sopenharmony_ci          startTs: minStartTs,
90fb726d48Sopenharmony_ci          startName: undefined, // @ts-ignore
91fb726d48Sopenharmony_ci          stepName: SpAllAppStartupsChart.AllAppStartupsNameArr[i],
92fb726d48Sopenharmony_ci          translateY: undefined,
93fb726d48Sopenharmony_ci          frame: undefined,
94fb726d48Sopenharmony_ci          isHover: false,
95fb726d48Sopenharmony_ci          value: undefined,
96fb726d48Sopenharmony_ci          pid: undefined,
97fb726d48Sopenharmony_ci          process: undefined,
98fb726d48Sopenharmony_ci          tid: undefined,
99fb726d48Sopenharmony_ci          itid: undefined,
100fb726d48Sopenharmony_ci          endItid: undefined,
101fb726d48Sopenharmony_ci        });
102fb726d48Sopenharmony_ci      }
103fb726d48Sopenharmony_ci      return sendRes;
104fb726d48Sopenharmony_ci    };
105fb726d48Sopenharmony_ci  }
106fb726d48Sopenharmony_ci
107fb726d48Sopenharmony_ci  async initFolder(): Promise<void> {
108fb726d48Sopenharmony_ci    let row: TraceRow<AllAppStartupStruct> = TraceRow.skeleton<AllAppStartupStruct>();
109fb726d48Sopenharmony_ci    row.setAttribute('hasStartup', 'true');
110fb726d48Sopenharmony_ci    row.rowId = `all-app-start-${SpAllAppStartupsChart.APP_STARTUP_PID_ARR![0]}`;
111fb726d48Sopenharmony_ci    row.index = 0;
112fb726d48Sopenharmony_ci    row.rowType = TraceRow.ROW_TYPE_ALL_APPSTARTUPS;
113fb726d48Sopenharmony_ci    row.rowParentId = '';
114fb726d48Sopenharmony_ci    row.folder = false;
115fb726d48Sopenharmony_ci    row.style.height = '40px';
116fb726d48Sopenharmony_ci    row.name = 'All App Startups';
117fb726d48Sopenharmony_ci    row.addTemplateTypes('AppStartup');
118fb726d48Sopenharmony_ci    row.selectChangeHandler = SpAllAppStartupsChart.trace.selectChangeHandler;
119fb726d48Sopenharmony_ci    row.favoriteChangeHandler = SpAllAppStartupsChart.trace.favoriteChangeHandler;
120fb726d48Sopenharmony_ci    this.supplier(row);
121fb726d48Sopenharmony_ci
122fb726d48Sopenharmony_ci    row.onThreadHandler = (useCache): void => {
123fb726d48Sopenharmony_ci      let context: CanvasRenderingContext2D;
124fb726d48Sopenharmony_ci      if (row.currentContext) {
125fb726d48Sopenharmony_ci        context = row.currentContext;
126fb726d48Sopenharmony_ci      } else {
127fb726d48Sopenharmony_ci        context = row.collect
128fb726d48Sopenharmony_ci          ? SpAllAppStartupsChart.trace.canvasFavoritePanelCtx!
129fb726d48Sopenharmony_ci          : SpAllAppStartupsChart.trace.canvasPanelCtx!;
130fb726d48Sopenharmony_ci      }
131fb726d48Sopenharmony_ci      row.canvasSave(context);
132fb726d48Sopenharmony_ci      (renders['all-app-start-up'] as AllAppStartupRender).renderMainThread(
133fb726d48Sopenharmony_ci        {
134fb726d48Sopenharmony_ci          appStartupContext: context,
135fb726d48Sopenharmony_ci          useCache: useCache,
136fb726d48Sopenharmony_ci          type: `app-startup ${row.rowId}`,
137fb726d48Sopenharmony_ci        },
138fb726d48Sopenharmony_ci        row
139fb726d48Sopenharmony_ci      );
140fb726d48Sopenharmony_ci      row.canvasRestore(context, this.trace);
141fb726d48Sopenharmony_ci    };
142fb726d48Sopenharmony_ci    SpAllAppStartupsChart.trace.rowsEL?.appendChild(row);
143fb726d48Sopenharmony_ci  }
144fb726d48Sopenharmony_ci}
145