1/*
2 * Copyright (C) 2024 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 */
15import { query } from '../SqlLite';
16import { DmaFenceDataBean } from '../../../trace/component/trace/sheet/dma-fence/DmaFenceBean';
17
18export const queryDmaFenceName = (): Promise<Array<{timeline:string}>> =>
19  query(
20    'queryDmaFenceName',
21    `SELECT DISTINCT timeline  
22     FROM dma_fence;`
23  );
24export const queryDmaFenceIdAndCat = (): Promise<Array<{ id: number; cat: string; seqno: number;driver:string;context:string}>> =>
25  query(
26    'queryDmaFenceIdAndCat',
27    `SELECT
28       id,
29       cat,
30       seqno,
31       driver,
32       context 
33     FROM
34       dma_fence;`
35  );
36
37export const queryDmaFenceData = (leftNS: number, rightNS: number, nameList: String[]): Promise<Array<DmaFenceDataBean>> =>{
38  const inClause = nameList.map(name => `'${name}'`).join(', ');
39  const sql = `WITH state AS (
40        SELECT
41          id,
42          ts,
43          cat,
44          driver,
45          timeline,
46          context,
47          seqno,
48        CASE
49            WHEN LAG( cat ) OVER ( PARTITION BY timeline ORDER BY ts ) = 'dma_fence_init' AND  LAG(driver) OVER (PARTITION BY timeline ORDER BY ts) = ''
50            THEN LAG( ts, 2 ) OVER ( PARTITION BY timeline ORDER BY ts ) 
51            ELSE LAG( ts ) OVER ( PARTITION BY timeline ORDER BY ts ) 
52            END AS new_ts,
53        CASE
54            WHEN LAG( cat ) OVER ( PARTITION BY timeline ORDER BY ts ) = 'dma_fence_init' AND  LAG(driver) OVER (PARTITION BY timeline ORDER BY ts) = ''
55            THEN Dur + LAG( dur ) OVER ( PARTITION BY timeline ORDER BY ts ) 
56            ELSE Dur 
57            END AS newDur,
58        ROW_NUMBER( ) OVER ( PARTITION BY timeline ORDER BY ts ) AS rn
59        FROM
60            dma_fence 
61        WHERE
62            timeline IN ( ${inClause} ) 
63          )
64        SELECT
65          s.id,
66          s.new_ts - COALESCE( r.start_ts, 0 ) AS startTime,
67          s.newDur AS dur,
68          s.new_ts - COALESCE(r.start_ts, 0) + s.newDur AS endTime,    
69          s.cat,
70          s.driver,
71          s.timeline,
72          s.context,
73          s.seqno 
74        FROM
75          state s
76          LEFT JOIN trace_range r ON s.new_ts BETWEEN r.start_ts AND r.end_ts 
77        WHERE
78          s.driver <> '' 
79          AND s.cat <> 'dma_fence_init' 
80          AND s.rn > 1 
81          AND (  
82            s.new_ts BETWEEN ${leftNS} + r.start_ts AND ${rightNS} + r.start_ts   
83            OR   
84            (s.new_ts + s.newDur) BETWEEN ${leftNS} + r.start_ts AND ${rightNS} + r.start_ts 
85            OR 
86            (s.new_ts < ${leftNS} + r.start_ts AND (s.new_ts + s.newDur) > ${rightNS} + r.start_ts)
87          ) 
88        ORDER BY
89          s.new_ts;`;
90  return query('queryDmaFenceData', sql); 
91};