1/*
2 * Copyright (C) 2024 Shenzhen Kaihong Digital Industry Development 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 { FlagsConfig } from '../../component/SpFlags';
17import { query } from '../SqlLite';
18import { HangStruct } from '../ui-worker/ProcedureWorkerHang';
19
20function getMinDur(): string {
21  let flagsItemJson = JSON.parse(window.localStorage.getItem(FlagsConfig.FLAGS_CONFIG_KEY)!);
22  let minDur = flagsItemJson.hangValue;
23  return minDur;
24}
25
26export const queryHangData = (): Promise<Array<{
27  id: number,
28  name: string,
29  num: number
30}>> => query(
31  'queryHangData',
32  `
33SELECT
34  p.pid as id,
35  p.name as name,
36  count(*) as num
37FROM
38  callstack c
39LEFT JOIN thread t ON
40  t.itid = c.callid
41LEFT JOIN process p ON
42  p.ipid = t.ipid
43WHERE
44  c.name LIKE 'H:Et:%'
45  AND t.is_main_thread = 1
46  AND c.dur >= ${getMinDur()}
47GROUP BY
48  p.pid
49`.trim()
50);
51
52
53export const queryAllHangs = (): Promise<Array<HangStruct>> => query(
54  'queryAllHangs',
55  `
56SELECT
57  c.id as id,
58  c.ts - r.start_ts as startNS,
59  c.dur as dur,
60  t.tid as tid,
61  p.pid as pid,
62  p.name as pname,
63  c.name as content
64FROM
65  callstack c, trace_range r
66LEFT JOIN thread t ON
67  t.itid = c.callid
68LEFT JOIN process p ON
69  p.ipid = t.ipid
70WHERE
71  c.dur >= ${getMinDur()}
72  AND t.is_main_thread = 1
73  AND c.name LIKE 'H:Et:%'
74`.trim()
75);