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 { query } from '../SqlLite';
17import { ClockStruct } from '../ui-worker/ProcedureWorkerClock';
18
19export const queryClockData = (traceId?: string): Promise<
20  Array<{
21    name: string;
22    num: number;
23    srcname: string;
24    maxValue?: number;
25  }>
26> =>
27  query(
28    'queryClockData',
29    `
30    select name || ' Frequency' name, COUNT(*) num, name srcname
31from (select id, name
32      from clock_event_filter
33      where type = 'clock_set_rate')
34group by name
35union
36select name || ' State' name, COUNT(*) num, name srcname
37from (select id, name
38      from clock_event_filter
39      where type != 'clock_set_rate')
40group by name;
41`, {}, {traceId: traceId}
42  );
43
44export const queryClockFrequency = (clockName: string): Promise<Array<ClockStruct>> =>
45  query(
46    'queryClockFrequency',
47    `with freq as (  select measure.filter_id, measure.ts, measure.type, measure.value from clock_event_filter
48      left join measure
49      where 
50      clock_event_filter.name = $clockName 
51      and 
52      clock_event_filter.type = 'clock_set_rate' 
53      and 
54      clock_event_filter.id = measure.filter_id
55      order by measure.ts)
56      select 
57      freq.filter_id as filterId,
58      freq.ts - r.start_ts as startNS,freq.type,freq.value from freq,trace_range r order by startNS`,
59    { $clockName: clockName }
60  );
61
62export const queryClockState = (clockName: string): Promise<Array<ClockStruct>> =>
63  query(
64    'queryClockState',
65    `with state as (
66          select 
67          filter_id, 
68          ts, 
69          endts, 
70          endts-ts as dur, 
71          type, 
72          value 
73          from
74            (select 
75            measure.filter_id, 
76            measure.ts, 
77            lead(ts, 1, null) over( order by measure.ts) endts, 
78            measure.type, 
79            measure.value 
80            from clock_event_filter,trace_range
81            left join measure
82            where 
83            clock_event_filter.name = $clockName 
84            and clock_event_filter.type != 'clock_set_rate' and clock_event_filter.id = measure.filter_id
85            order by measure.ts))
86            select s.filter_id as filterId,s.ts-r.start_ts as startNS,s.type,s.value,s.dur from state s,trace_range r`,
87    { $clockName: clockName }
88  );
89
90export const queryBootTime = (): //@ts-ignore
91Promise<Array<unknown>> =>
92  query(
93    'queryBootTime',
94    `select CS.ts -TR.start_ts as ts ,clock_name from clock_snapshot as CS ,trace_range as TR
95      where clock_name = 'boottime'`,
96    {}
97  );
98
99export const queryScreenState = (): Promise<Array<ClockStruct>> =>
100  query(
101    'queryScreenState',
102    `select 
103    m.type, 
104    m.ts-r.start_ts as startNS, 
105    value, filter_id as filterId 
106    from 
107    measure m,trace_range r 
108    where filter_id in (select id from process_measure_filter where name = 'ScreenState')  order by startNS;
109`
110  );
111
112export const queryRealTime = (): Promise<
113  Array<{
114    ts: number;
115    name: string;
116  }>
117> =>
118  query(
119    'queryRealTime',
120    `SELECT
121  ( CASE WHEN CS.clock_name = 'realtime' THEN CS.ts ELSE CS.ts - TR.start_ts END ) AS ts,
122  CS.clock_name AS name 
123  FROM
124  clock_snapshot AS CS,
125  trace_range AS TR 
126  WHERE
127  CS.clock_name = 'realtime' 
128  OR CS.clock_name = 'boottime';`
129  );
130