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 { SpFFRTConfigHtml } from './SpFFRTConfig.html';
18fb726d48Sopenharmony_ciimport { log } from '../../../log/Log';
19fb726d48Sopenharmony_ciimport { NUM_16384, NUM_5 } from '../../bean/NumBean';
20fb726d48Sopenharmony_ciimport { LitSelectV } from '../../../base-ui/select/LitSelectV';
21fb726d48Sopenharmony_ciimport LitSwitch from '../../../base-ui/switch/lit-switch';
22fb726d48Sopenharmony_ciimport { LitSelect } from '../../../base-ui/select/LitSelect';
23fb726d48Sopenharmony_ciimport { Cmd } from '../../../command/Cmd';
24fb726d48Sopenharmony_ciimport { SpRecordTrace } from '../SpRecordTrace';
25fb726d48Sopenharmony_ci
26fb726d48Sopenharmony_ci@element('sp-record-ffrt')
27fb726d48Sopenharmony_ciexport class SpFFRTConfig extends BaseElement {
28fb726d48Sopenharmony_ci  private processIdEl: LitSelectV | undefined;
29fb726d48Sopenharmony_ci  private processIdInputEl: HTMLInputElement | undefined;
30fb726d48Sopenharmony_ci  private startupProcessNameEl: LitSelectV | undefined;
31fb726d48Sopenharmony_ci  private restartProcessNameEl: LitSelectV | undefined;
32fb726d48Sopenharmony_ci  private restartProcessNameInputEl: HTMLInputElement | undefined;
33fb726d48Sopenharmony_ci  private useBlockSwitchEl: LitSwitch | undefined;
34fb726d48Sopenharmony_ci  private smbPagesInputEl: HTMLInputElement | undefined;
35fb726d48Sopenharmony_ci  private flushIntervalInputEl: HTMLInputElement | undefined;
36fb726d48Sopenharmony_ci  private clockTypeSelectEl: LitSelect | undefined;
37fb726d48Sopenharmony_ci  private selectProcessNameList: Array<string> = [];
38fb726d48Sopenharmony_ci
39fb726d48Sopenharmony_ci  set startSamp(allocationStart: boolean) {
40fb726d48Sopenharmony_ci    if (allocationStart) {
41fb726d48Sopenharmony_ci      this.setAttribute('startSamp', '');
42fb726d48Sopenharmony_ci    } else {
43fb726d48Sopenharmony_ci      this.removeAttribute('startSamp');
44fb726d48Sopenharmony_ci    }
45fb726d48Sopenharmony_ci  }
46fb726d48Sopenharmony_ci
47fb726d48Sopenharmony_ci  get startSamp(): boolean {
48fb726d48Sopenharmony_ci    return this.hasAttribute('startSamp');
49fb726d48Sopenharmony_ci  }
50fb726d48Sopenharmony_ci
51fb726d48Sopenharmony_ci  get processList(): string {
52fb726d48Sopenharmony_ci    return this.processIdEl!.value || '';
53fb726d48Sopenharmony_ci  }
54fb726d48Sopenharmony_ci
55fb726d48Sopenharmony_ci  get processIds(): number[] {
56fb726d48Sopenharmony_ci    let allPidList: number[] = [];
57fb726d48Sopenharmony_ci    if (this.processIdEl!.value.trim() !== '') {
58fb726d48Sopenharmony_ci      let result = this.processIdEl?.value.match(/\((.+?)\)/g);
59fb726d48Sopenharmony_ci      if (result) {
60fb726d48Sopenharmony_ci        for (let index = 0; index < result.length; index++) {
61fb726d48Sopenharmony_ci          let item = result[index];
62fb726d48Sopenharmony_ci          let currentPid = item!.replace('(', '').replace(')', '');
63fb726d48Sopenharmony_ci          allPidList.push(Number(currentPid));
64fb726d48Sopenharmony_ci        }
65fb726d48Sopenharmony_ci      }
66fb726d48Sopenharmony_ci    }
67fb726d48Sopenharmony_ci    return allPidList;
68fb726d48Sopenharmony_ci  }
69fb726d48Sopenharmony_ci
70fb726d48Sopenharmony_ci  get processNames(): string[] {
71fb726d48Sopenharmony_ci    let allPNameList: string[] = [];
72fb726d48Sopenharmony_ci    if (this.processIdEl!.value.trim() !== '') {
73fb726d48Sopenharmony_ci      let result = this.processIdEl?.value.replace(/[$(\d+)$]/g, '');
74fb726d48Sopenharmony_ci      if (result) {
75fb726d48Sopenharmony_ci        allPNameList = result.split(',');
76fb726d48Sopenharmony_ci      }
77fb726d48Sopenharmony_ci    }
78fb726d48Sopenharmony_ci    if (this.restartProcessNameEl!.value.trim() !== '') {
79fb726d48Sopenharmony_ci      let result = this.restartProcessNameEl?.value.replace(/[$(\d+)$]/g, '');
80fb726d48Sopenharmony_ci      if (result) {
81fb726d48Sopenharmony_ci        allPNameList.push(...result.split(','));
82fb726d48Sopenharmony_ci      }
83fb726d48Sopenharmony_ci    }
84fb726d48Sopenharmony_ci    return allPNameList;
85fb726d48Sopenharmony_ci  }
86fb726d48Sopenharmony_ci
87fb726d48Sopenharmony_ci  get startupProcessNames(): string[] {
88fb726d48Sopenharmony_ci    let allPNameList: string[] = [];
89fb726d48Sopenharmony_ci    if (this.startupProcessNameEl!.value.trim() !== '') {
90fb726d48Sopenharmony_ci      allPNameList = this.startupProcessNameEl!.value.trim().split(',');
91fb726d48Sopenharmony_ci      if (allPNameList.length === 0) {
92fb726d48Sopenharmony_ci        allPNameList = this.startupProcessNameEl!.value.trim().split(';');
93fb726d48Sopenharmony_ci      }
94fb726d48Sopenharmony_ci    }
95fb726d48Sopenharmony_ci    return allPNameList;
96fb726d48Sopenharmony_ci  }
97fb726d48Sopenharmony_ci
98fb726d48Sopenharmony_ci  get restartProcessNames(): string[] {
99fb726d48Sopenharmony_ci    let allPNameList: string[] = [];
100fb726d48Sopenharmony_ci    if (this.restartProcessNameEl!.value.trim() !== '') {
101fb726d48Sopenharmony_ci      let result = this.restartProcessNameEl?.value.replace(/[$(\d+)$]/g, '');
102fb726d48Sopenharmony_ci      if (result) {
103fb726d48Sopenharmony_ci        allPNameList = result.split(',');
104fb726d48Sopenharmony_ci      }
105fb726d48Sopenharmony_ci    }
106fb726d48Sopenharmony_ci    return allPNameList;
107fb726d48Sopenharmony_ci  }
108fb726d48Sopenharmony_ci
109fb726d48Sopenharmony_ci  get useBlock(): boolean {
110fb726d48Sopenharmony_ci    let value = this.useBlockSwitchEl?.checked;
111fb726d48Sopenharmony_ci    if (value !== undefined) {
112fb726d48Sopenharmony_ci      return value;
113fb726d48Sopenharmony_ci    }
114fb726d48Sopenharmony_ci    return true;
115fb726d48Sopenharmony_ci  }
116fb726d48Sopenharmony_ci
117fb726d48Sopenharmony_ci  get smbPages(): number {
118fb726d48Sopenharmony_ci    let value = this.smbPagesInputEl?.value || '';
119fb726d48Sopenharmony_ci    log(`smbPages value is :${value}`);
120fb726d48Sopenharmony_ci    if (value !== '') {
121fb726d48Sopenharmony_ci      return Number(this.smbPagesInputEl?.value) || NUM_16384;
122fb726d48Sopenharmony_ci    }
123fb726d48Sopenharmony_ci    return NUM_16384;
124fb726d48Sopenharmony_ci  }
125fb726d48Sopenharmony_ci
126fb726d48Sopenharmony_ci  get flushInterval(): number {
127fb726d48Sopenharmony_ci    let value = this.flushIntervalInputEl?.value || '';
128fb726d48Sopenharmony_ci    log(`flushInterval value is :${value}`);
129fb726d48Sopenharmony_ci    if (value !== '') {
130fb726d48Sopenharmony_ci      return Number(this.flushIntervalInputEl?.value) || NUM_5;
131fb726d48Sopenharmony_ci    }
132fb726d48Sopenharmony_ci    return NUM_5;
133fb726d48Sopenharmony_ci  }
134fb726d48Sopenharmony_ci
135fb726d48Sopenharmony_ci  get clockType(): string {
136fb726d48Sopenharmony_ci    return this.clockTypeSelectEl?.value || 'BOOTTIME';
137fb726d48Sopenharmony_ci  }
138fb726d48Sopenharmony_ci
139fb726d48Sopenharmony_ci  initElements(): void {
140fb726d48Sopenharmony_ci    this.processIdEl = this.shadowRoot?.getElementById('process-ids') as LitSelectV;
141fb726d48Sopenharmony_ci    this.startupProcessNameEl = this.shadowRoot?.getElementById('startup-process-names') as LitSelectV;
142fb726d48Sopenharmony_ci    this.restartProcessNameEl = this.shadowRoot?.getElementById('restart-process-names') as LitSelectV;
143fb726d48Sopenharmony_ci    this.useBlockSwitchEl = this.shadowRoot?.getElementById('use_block_switch') as LitSwitch;
144fb726d48Sopenharmony_ci    this.smbPagesInputEl = this.shadowRoot?.getElementById('smb-pages') as HTMLInputElement;
145fb726d48Sopenharmony_ci    this.flushIntervalInputEl = this.shadowRoot?.getElementById('flush-interval') as HTMLInputElement;
146fb726d48Sopenharmony_ci    this.clockTypeSelectEl = this.shadowRoot?.getElementById('clock-type') as LitSelect;
147fb726d48Sopenharmony_ci    this.processIdInputEl = this.processIdEl!.shadowRoot?.querySelector('input') as HTMLInputElement;
148fb726d48Sopenharmony_ci    this.restartProcessNameInputEl = this.restartProcessNameEl!.shadowRoot?.querySelector('input') as HTMLInputElement;
149fb726d48Sopenharmony_ci    let litSwitch = this.shadowRoot?.querySelector('#switch-disabled') as LitSwitch;
150fb726d48Sopenharmony_ci    litSwitch.addEventListener('change', (event: Event): void => {
151fb726d48Sopenharmony_ci      // @ts-ignore
152fb726d48Sopenharmony_ci      let detail = event.detail;
153fb726d48Sopenharmony_ci      if (detail.checked) {
154fb726d48Sopenharmony_ci        this.unDisable();
155fb726d48Sopenharmony_ci      } else {
156fb726d48Sopenharmony_ci        this.disable();
157fb726d48Sopenharmony_ci      }
158fb726d48Sopenharmony_ci    });
159fb726d48Sopenharmony_ci    let packageInput = this.startupProcessNameEl!.shadowRoot?.querySelector('input') as HTMLInputElement;
160fb726d48Sopenharmony_ci    packageInput.addEventListener('mousedown', (): void => {
161fb726d48Sopenharmony_ci      this.startupProcessMouseDownHandler(this.startupProcessNameEl, packageInput);
162fb726d48Sopenharmony_ci    });
163fb726d48Sopenharmony_ci    packageInput.value = '';
164fb726d48Sopenharmony_ci    this.disable();
165fb726d48Sopenharmony_ci  }
166fb726d48Sopenharmony_ci
167fb726d48Sopenharmony_ci  initHtml(): string {
168fb726d48Sopenharmony_ci    return SpFFRTConfigHtml;
169fb726d48Sopenharmony_ci  }
170fb726d48Sopenharmony_ci
171fb726d48Sopenharmony_ci  connectedCallback(): void {
172fb726d48Sopenharmony_ci    this.processIdInputEl?.addEventListener('mousedown', this.processMouseDownHandler);
173fb726d48Sopenharmony_ci    this.restartProcessNameInputEl?.addEventListener('mousedown', this.restartProcessMouseDownHandler);
174fb726d48Sopenharmony_ci    this.smbPagesInputEl?.addEventListener('keydown', this.handleInputChangeEvent);
175fb726d48Sopenharmony_ci    this.flushIntervalInputEl?.addEventListener('keydown', this.handleInputChangeEvent);
176fb726d48Sopenharmony_ci  }
177fb726d48Sopenharmony_ci
178fb726d48Sopenharmony_ci  disconnectedCallback(): void {
179fb726d48Sopenharmony_ci    this.processIdInputEl?.removeEventListener('mousedown', this.processMouseDownHandler);
180fb726d48Sopenharmony_ci    this.restartProcessNameInputEl?.removeEventListener('mousedown', this.restartProcessMouseDownHandler);
181fb726d48Sopenharmony_ci    this.smbPagesInputEl?.removeEventListener('keydown', this.handleInputChangeEvent);
182fb726d48Sopenharmony_ci    this.flushIntervalInputEl?.removeEventListener('keydown', this.handleInputChangeEvent);
183fb726d48Sopenharmony_ci  }
184fb726d48Sopenharmony_ci
185fb726d48Sopenharmony_ci  processMouseDownHandler = (): void => {
186fb726d48Sopenharmony_ci    this.setData(this.processIdEl);
187fb726d48Sopenharmony_ci  };
188fb726d48Sopenharmony_ci
189fb726d48Sopenharmony_ci  startupProcessMouseDownHandler(startupPNameEl: LitSelectV | undefined, packageInput: HTMLInputElement): void {
190fb726d48Sopenharmony_ci    if (!startupPNameEl) {
191fb726d48Sopenharmony_ci      return;
192fb726d48Sopenharmony_ci    }
193fb726d48Sopenharmony_ci    let processInputEl = startupPNameEl.shadowRoot?.querySelector('input') as HTMLInputElement;
194fb726d48Sopenharmony_ci    if (this.startSamp) {
195fb726d48Sopenharmony_ci      Cmd.getPackage().then((packageList: string[]): void => {
196fb726d48Sopenharmony_ci        let finalDataList = packageList.map(str => str.replace(/\t/g, ''));
197fb726d48Sopenharmony_ci        if (finalDataList.length > 0) {
198fb726d48Sopenharmony_ci          startupPNameEl.dataSource(finalDataList, 'ALL-Process');
199fb726d48Sopenharmony_ci        } else {
200fb726d48Sopenharmony_ci          startupPNameEl.dataSource([], '');
201fb726d48Sopenharmony_ci        }
202fb726d48Sopenharmony_ci      });
203fb726d48Sopenharmony_ci      processInputEl.readOnly = false;
204fb726d48Sopenharmony_ci    } else {
205fb726d48Sopenharmony_ci      processInputEl.readOnly = true;
206fb726d48Sopenharmony_ci      return;
207fb726d48Sopenharmony_ci    }
208fb726d48Sopenharmony_ci    if (this.startSamp && (SpRecordTrace.serialNumber === '')) {
209fb726d48Sopenharmony_ci      startupPNameEl.dataSource([], '');
210fb726d48Sopenharmony_ci    }
211fb726d48Sopenharmony_ci  };
212fb726d48Sopenharmony_ci
213fb726d48Sopenharmony_ci  restartProcessMouseDownHandler = (): void => {
214fb726d48Sopenharmony_ci    this.setData(this.restartProcessNameEl);
215fb726d48Sopenharmony_ci  };
216fb726d48Sopenharmony_ci
217fb726d48Sopenharmony_ci  handleInputChangeEvent = (ev: KeyboardEvent): void => {
218fb726d48Sopenharmony_ci    // @ts-ignore
219fb726d48Sopenharmony_ci    if (ev.key === '0' && ev.target.value.length === 1 && ev.target.value === '0') {
220fb726d48Sopenharmony_ci      ev.preventDefault();
221fb726d48Sopenharmony_ci    }
222fb726d48Sopenharmony_ci  };
223fb726d48Sopenharmony_ci
224fb726d48Sopenharmony_ci  private unDisable(): void {
225fb726d48Sopenharmony_ci    this.startSamp = true;
226fb726d48Sopenharmony_ci    if (this.useBlockSwitchEl) {
227fb726d48Sopenharmony_ci      this.useBlockSwitchEl.disabled = false;
228fb726d48Sopenharmony_ci    }
229fb726d48Sopenharmony_ci    this.processIdEl!.removeAttribute('disabled');
230fb726d48Sopenharmony_ci    this.startupProcessNameEl!.removeAttribute('disabled');
231fb726d48Sopenharmony_ci    this.restartProcessNameEl!.removeAttribute('disabled');
232fb726d48Sopenharmony_ci    this.clockTypeSelectEl!.removeAttribute('disabled');
233fb726d48Sopenharmony_ci    let inputBoxes = this.shadowRoot?.querySelectorAll<HTMLInputElement>('.input-content');
234fb726d48Sopenharmony_ci    inputBoxes!.forEach((item: HTMLInputElement): void => {
235fb726d48Sopenharmony_ci      item.disabled = false;
236fb726d48Sopenharmony_ci    });
237fb726d48Sopenharmony_ci  }
238fb726d48Sopenharmony_ci
239fb726d48Sopenharmony_ci  private disable(): void {
240fb726d48Sopenharmony_ci    this.startSamp = false;
241fb726d48Sopenharmony_ci    if (this.useBlockSwitchEl) {
242fb726d48Sopenharmony_ci      this.useBlockSwitchEl.disabled = true;
243fb726d48Sopenharmony_ci    }
244fb726d48Sopenharmony_ci    this.processIdEl!.setAttribute('disabled', '');
245fb726d48Sopenharmony_ci    this.startupProcessNameEl!.setAttribute('disabled', '');
246fb726d48Sopenharmony_ci    this.restartProcessNameEl!.setAttribute('disabled', '');
247fb726d48Sopenharmony_ci    this.clockTypeSelectEl!.setAttribute('disabled', '');
248fb726d48Sopenharmony_ci    let inputBoxes = this.shadowRoot?.querySelectorAll<HTMLInputElement>('.input-content');
249fb726d48Sopenharmony_ci    inputBoxes!.forEach((item: HTMLInputElement): void => {
250fb726d48Sopenharmony_ci      item.disabled = true;
251fb726d48Sopenharmony_ci    });
252fb726d48Sopenharmony_ci  }
253fb726d48Sopenharmony_ci
254fb726d48Sopenharmony_ci  private setData(selectInputEl: LitSelectV | undefined): void {
255fb726d48Sopenharmony_ci    if (!selectInputEl) {
256fb726d48Sopenharmony_ci      return;
257fb726d48Sopenharmony_ci    }
258fb726d48Sopenharmony_ci    let processInputEl = selectInputEl.shadowRoot?.querySelector('input') as HTMLInputElement;
259fb726d48Sopenharmony_ci    if (this.startSamp) {
260fb726d48Sopenharmony_ci      Cmd.getProcess().then((processList: string[]): void => {
261fb726d48Sopenharmony_ci        selectInputEl.dataSource(processList, '');
262fb726d48Sopenharmony_ci        if (processList.length > 0) {
263fb726d48Sopenharmony_ci          selectInputEl.dataSource(processList, 'ALL-Process');
264fb726d48Sopenharmony_ci        } else {
265fb726d48Sopenharmony_ci          selectInputEl.dataSource([], '');
266fb726d48Sopenharmony_ci        }
267fb726d48Sopenharmony_ci      });
268fb726d48Sopenharmony_ci      processInputEl.readOnly = false;
269fb726d48Sopenharmony_ci    } else {
270fb726d48Sopenharmony_ci      processInputEl.readOnly = true;
271fb726d48Sopenharmony_ci      return;
272fb726d48Sopenharmony_ci    }
273fb726d48Sopenharmony_ci    if (this.startSamp && (SpRecordTrace.serialNumber === '')) {
274fb726d48Sopenharmony_ci      selectInputEl.dataSource([], '');
275fb726d48Sopenharmony_ci    }
276fb726d48Sopenharmony_ci  }
277fb726d48Sopenharmony_ci}
278