1fb726d48Sopenharmony_ci// Copyright (c) 2021 Huawei Device Co., Ltd.
2fb726d48Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License");
3fb726d48Sopenharmony_ci// you may not use this file except in compliance with the License.
4fb726d48Sopenharmony_ci// You may obtain a copy of the License at
5fb726d48Sopenharmony_ci//
6fb726d48Sopenharmony_ci//     http://www.apache.org/licenses/LICENSE-2.0
7fb726d48Sopenharmony_ci//
8fb726d48Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software
9fb726d48Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS,
10fb726d48Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11fb726d48Sopenharmony_ci// See the License for the specific language governing permissions and
12fb726d48Sopenharmony_ci// limitations under the License.
13fb726d48Sopenharmony_ci
14fb726d48Sopenharmony_ciimport { TraficEnum } from './utils/QueryEnum';
15fb726d48Sopenharmony_ciimport { filterDataByGroup } from './utils/DataFilter';
16fb726d48Sopenharmony_ciimport { clockList } from './utils/AllMemoryCache';
17fb726d48Sopenharmony_ciimport { Args } from './CommonArgs';
18fb726d48Sopenharmony_ci
19fb726d48Sopenharmony_ciexport const chartClockDataSql = (args: Args): string => {
20fb726d48Sopenharmony_ci  if (args.sqlType === 'clockFrequency') {
21fb726d48Sopenharmony_ci    return `
22fb726d48Sopenharmony_ci    with freq as (
23fb726d48Sopenharmony_ci        select measure.filter_id,
24fb726d48Sopenharmony_ci               measure.value,
25fb726d48Sopenharmony_ci               measure.ts,
26fb726d48Sopenharmony_ci               measure.type
27fb726d48Sopenharmony_ci        from measure
28fb726d48Sopenharmony_ci        where measure.filter_id in (select id
29fb726d48Sopenharmony_ci                                    from clock_event_filter
30fb726d48Sopenharmony_ci                                    where clock_event_filter.name = '${args.clockName}'
31fb726d48Sopenharmony_ci                                      and clock_event_filter.type = 'clock_set_rate')
32fb726d48Sopenharmony_ci                                      --and startNs >= ${Math.floor(args.startNS)}
33fb726d48Sopenharmony_ci                                      --and startNs <= ${Math.floor(args.endNS)}
34fb726d48Sopenharmony_ci    )
35fb726d48Sopenharmony_ci    select freq.filter_id as filterId, freq.value, freq.ts - ${args.recordStartNS} as startNs, freq.type
36fb726d48Sopenharmony_ci    from freq
37fb726d48Sopenharmony_ci    order by startNs;
38fb726d48Sopenharmony_ci    `;
39fb726d48Sopenharmony_ci  } else if (args.sqlType === 'clockState') {
40fb726d48Sopenharmony_ci    return `select measure.filter_id                                     as filterId,
41fb726d48Sopenharmony_ci                   measure.value                                         as value,
42fb726d48Sopenharmony_ci                   measure.ts - ${args.recordStartNS}                    as startNs,
43fb726d48Sopenharmony_ci                   (lead(ts, 1, null) over ( order by measure.ts)) - ts as dur,
44fb726d48Sopenharmony_ci                   measure.type                                          as type
45fb726d48Sopenharmony_ci            from measure
46fb726d48Sopenharmony_ci            where measure.filter_id in (select id
47fb726d48Sopenharmony_ci                from clock_event_filter
48fb726d48Sopenharmony_ci                where clock_event_filter.name = '${args.clockName}'
49fb726d48Sopenharmony_ci                and clock_event_filter.type != 'clock_set_rate')
50fb726d48Sopenharmony_ci            --and startNs + dur >= ${Math.floor(args.startNS)}
51fb726d48Sopenharmony_ci            --and startNs <= ${Math.floor(args.endNS)}`;
52fb726d48Sopenharmony_ci  } else if (args.sqlType === 'screenState') {
53fb726d48Sopenharmony_ci    return `select filter_id as filterId,value,  m.ts - ${args.recordStartNS} as startNs, m.type
54fb726d48Sopenharmony_ci            from measure m
55fb726d48Sopenharmony_ci            where filter_id in (select id from process_measure_filter where name = 'ScreenState')
56fb726d48Sopenharmony_ci            --and startNs >= ${Math.floor(args.startNS)}
57fb726d48Sopenharmony_ci            --and startNs <= ${Math.floor(args.endNS)};`;
58fb726d48Sopenharmony_ci  } else {
59fb726d48Sopenharmony_ci    return '';
60fb726d48Sopenharmony_ci  }
61fb726d48Sopenharmony_ci};
62fb726d48Sopenharmony_ci
63fb726d48Sopenharmony_ciexport const chartClockDataSqlMem = (args: Args): string => {
64fb726d48Sopenharmony_ci  if (args.sqlType === 'clockFrequency') {
65fb726d48Sopenharmony_ci    return `
66fb726d48Sopenharmony_ci        with freq as (  select measure.filter_id, measure.ts, measure.type, measure.value from clock_event_filter
67fb726d48Sopenharmony_ci                                                                                                   left join measure
68fb726d48Sopenharmony_ci                        where clock_event_filter.name = '${args.clockName}' and clock_event_filter.type = 'clock_set_rate' and clock_event_filter.id = measure.filter_id
69fb726d48Sopenharmony_ci                        order by measure.ts)
70fb726d48Sopenharmony_ci        select freq.filter_id as filterId,freq.ts - r.start_ts as startNs,freq.type,freq.value from freq,trace_range r order by startNs;
71fb726d48Sopenharmony_ci    `;
72fb726d48Sopenharmony_ci  } else if (args.sqlType === 'clockState') {
73fb726d48Sopenharmony_ci    return `with state as (
74fb726d48Sopenharmony_ci        select filter_id, ts, endts, endts-ts as dur, type, value from
75fb726d48Sopenharmony_ci            (select measure.filter_id, measure.ts, lead(ts, 1, null) over( order by measure.ts) endts, measure.type, measure.value from clock_event_filter,trace_range
76fb726d48Sopenharmony_ci                                                                                                                                                               left join measure
77fb726d48Sopenharmony_ci             where clock_event_filter.name = '${args.clockName}' and clock_event_filter.type != 'clock_set_rate' and clock_event_filter.id = measure.filter_id
78fb726d48Sopenharmony_ci             order by measure.ts))
79fb726d48Sopenharmony_ci            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`;
80fb726d48Sopenharmony_ci  } else if (args.sqlType === 'screenState') {
81fb726d48Sopenharmony_ci    return `select m.type, m.ts-r.start_ts as startNs, value, filter_id  as filterId 
82fb726d48Sopenharmony_ci    from measure m,trace_range r 
83fb726d48Sopenharmony_ci    where filter_id in (select id from process_measure_filter where name = 'ScreenState')  order by startNs;`;
84fb726d48Sopenharmony_ci  } else {
85fb726d48Sopenharmony_ci    return '';
86fb726d48Sopenharmony_ci  }
87fb726d48Sopenharmony_ci};
88fb726d48Sopenharmony_ci
89fb726d48Sopenharmony_ciexport function clockDataReceiver(data: unknown, proc: Function): void {
90fb726d48Sopenharmony_ci  // @ts-ignore
91fb726d48Sopenharmony_ci  if (data.params.trafic === TraficEnum.Memory) {
92fb726d48Sopenharmony_ci    let res: unknown[];
93fb726d48Sopenharmony_ci    let list: unknown[];
94fb726d48Sopenharmony_ci    // @ts-ignore
95fb726d48Sopenharmony_ci    if (!clockList.has(data.params.sqlType + data.params.clockName)) {
96fb726d48Sopenharmony_ci      // @ts-ignore
97fb726d48Sopenharmony_ci      let sql = chartClockDataSqlMem(data.params);
98fb726d48Sopenharmony_ci      // @ts-ignore
99fb726d48Sopenharmony_ci      list = proc(sql);
100fb726d48Sopenharmony_ci      for (let j = 0; j < list.length; j++) {
101fb726d48Sopenharmony_ci        if (j === list.length - 1) {
102fb726d48Sopenharmony_ci          // @ts-ignore
103fb726d48Sopenharmony_ci          list[j].dur = (data.params.totalNS || 0) - (list[j].startNs || 0);
104fb726d48Sopenharmony_ci        } else {
105fb726d48Sopenharmony_ci          // @ts-ignore
106fb726d48Sopenharmony_ci          list[j].dur = (list[j + 1].startNs || 0) - (list[j].startNs || 0);
107fb726d48Sopenharmony_ci        }
108fb726d48Sopenharmony_ci      }
109fb726d48Sopenharmony_ci      // @ts-ignore
110fb726d48Sopenharmony_ci      clockList.set(data.params.sqlType + data.params.clockName, list);
111fb726d48Sopenharmony_ci    } else {
112fb726d48Sopenharmony_ci      // @ts-ignore
113fb726d48Sopenharmony_ci      list = clockList.get(data.params.sqlType + data.params.clockName) || [];
114fb726d48Sopenharmony_ci    }
115fb726d48Sopenharmony_ci    // @ts-ignore
116fb726d48Sopenharmony_ci    if (data.params.queryAll) {
117fb726d48Sopenharmony_ci      //框选时候取数据,只需要根据时间过滤数据
118fb726d48Sopenharmony_ci      res = (list || []).filter(
119fb726d48Sopenharmony_ci        // @ts-ignore
120fb726d48Sopenharmony_ci        (it) => it.startNs + it.dur >= data.params.selectStartNS && it.startNs <= data.params.selectEndNS
121fb726d48Sopenharmony_ci      );
122fb726d48Sopenharmony_ci    } else {
123fb726d48Sopenharmony_ci      res = filterDataByGroup(
124fb726d48Sopenharmony_ci        list || [],
125fb726d48Sopenharmony_ci        'startNs',
126fb726d48Sopenharmony_ci        'dur',
127fb726d48Sopenharmony_ci        // @ts-ignore
128fb726d48Sopenharmony_ci        data.params.startNS,
129fb726d48Sopenharmony_ci        // @ts-ignore
130fb726d48Sopenharmony_ci        data.params.endNS,
131fb726d48Sopenharmony_ci        // @ts-ignore
132fb726d48Sopenharmony_ci        data.params.width,
133fb726d48Sopenharmony_ci        'value'
134fb726d48Sopenharmony_ci      );
135fb726d48Sopenharmony_ci    }
136fb726d48Sopenharmony_ci    arrayBufferHandler(data, res, true);
137fb726d48Sopenharmony_ci  } else {
138fb726d48Sopenharmony_ci    // @ts-ignore
139fb726d48Sopenharmony_ci    let sql = chartClockDataSql(data.params);
140fb726d48Sopenharmony_ci    let res = proc(sql);
141fb726d48Sopenharmony_ci    // @ts-ignore
142fb726d48Sopenharmony_ci    arrayBufferHandler(data, res, data.params.trafic !== TraficEnum.SharedArrayBuffer);
143fb726d48Sopenharmony_ci  }
144fb726d48Sopenharmony_ci}
145fb726d48Sopenharmony_ci
146fb726d48Sopenharmony_cifunction arrayBufferHandler(data: unknown, res: unknown[], transfer: boolean): void {
147fb726d48Sopenharmony_ci  // @ts-ignore
148fb726d48Sopenharmony_ci  let dur = new Float64Array(transfer ? res.length : data.params.sharedArrayBuffers.dur);
149fb726d48Sopenharmony_ci  // @ts-ignore
150fb726d48Sopenharmony_ci  let startNS = new Float64Array(transfer ? res.length : data.params.sharedArrayBuffers.startNS);
151fb726d48Sopenharmony_ci  // @ts-ignore
152fb726d48Sopenharmony_ci  let value = new Int32Array(transfer ? res.length : data.params.sharedArrayBuffers.value);
153fb726d48Sopenharmony_ci  // @ts-ignore
154fb726d48Sopenharmony_ci  let filterId = new Int32Array(transfer ? res.length : data.params.sharedArrayBuffers.filterId);
155fb726d48Sopenharmony_ci  res.forEach((it, i) => {
156fb726d48Sopenharmony_ci    // @ts-ignore
157fb726d48Sopenharmony_ci    data.params.trafic === TraficEnum.ProtoBuffer && (it = it.clockData);
158fb726d48Sopenharmony_ci    // @ts-ignore
159fb726d48Sopenharmony_ci    dur[i] = it.dur;
160fb726d48Sopenharmony_ci    // @ts-ignore
161fb726d48Sopenharmony_ci    startNS[i] = it.startNs;
162fb726d48Sopenharmony_ci    // @ts-ignore
163fb726d48Sopenharmony_ci    filterId[i] = it.filterId;
164fb726d48Sopenharmony_ci    // @ts-ignore
165fb726d48Sopenharmony_ci    value[i] = it.value;
166fb726d48Sopenharmony_ci  });
167fb726d48Sopenharmony_ci
168fb726d48Sopenharmony_ci  (self as unknown as Worker).postMessage(
169fb726d48Sopenharmony_ci    {
170fb726d48Sopenharmony_ci      // @ts-ignore
171fb726d48Sopenharmony_ci      id: data.id,
172fb726d48Sopenharmony_ci      // @ts-ignore
173fb726d48Sopenharmony_ci      action: data.action,
174fb726d48Sopenharmony_ci      results: transfer
175fb726d48Sopenharmony_ci        ? {
176fb726d48Sopenharmony_ci            dur: dur.buffer,
177fb726d48Sopenharmony_ci            startNS: startNS.buffer,
178fb726d48Sopenharmony_ci            value: value.buffer,
179fb726d48Sopenharmony_ci            filterId: filterId.buffer,
180fb726d48Sopenharmony_ci          }
181fb726d48Sopenharmony_ci        : {},
182fb726d48Sopenharmony_ci      len: res.length,
183fb726d48Sopenharmony_ci      transfer: transfer,
184fb726d48Sopenharmony_ci    },
185fb726d48Sopenharmony_ci    transfer ? [dur.buffer, startNS.buffer, value.buffer, filterId.buffer] : []
186fb726d48Sopenharmony_ci  );
187fb726d48Sopenharmony_ci}
188