• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 
16 package ohos.devtools.views.trace.metrics.strategy;
17 
18 import ohos.devtools.views.trace.metrics.MetricsDb;
19 import ohos.devtools.views.trace.metrics.MetricsSql;
20 import ohos.devtools.views.trace.metrics.bean.DistributeTerm;
21 
22 import java.util.ArrayList;
23 import java.util.List;
24 
25 /**
26  * Distribute Term Strategy
27  *
28  * @since 2021/5/19 16:39
29  */
30 public class DistributeTermStrategy implements Strategy {
31     /**
32      * Query DistributeTerm Result
33      *
34      * @param sql sql
35      * @return string string
36      */
37     @Override
getQueryResult(MetricsSql sql)38     public String getQueryResult(MetricsSql sql) {
39         List<DistributeTerm> list = new ArrayList<>() {
40         };
41         MetricsDb.getInstance().query(sql, list);
42         return handleDistributeTermInfo(list);
43     }
44 
handleDistributeTermInfo(List<DistributeTerm> result)45     private String handleDistributeTermInfo(List<DistributeTerm> result) {
46         StringBuilder builder = new StringBuilder();
47         builder.append("distributed_term {").append(System.lineSeparator());
48         for (DistributeTerm item : result) {
49             String[] threadIds = item.getThreadId().split(",");
50             String[] threadNames = item.getThreadName().split(",");
51             String[] processIds = item.getProcessId().split(",");
52             String[] processNames =
53                 item.getProcessName() == null ? new String[threadIds.length] : item.getProcessName().split(",");
54             String[] funNames = item.getFunName().split(",");
55             String[] durations = item.getDur().split(",");
56             String[] times = item.getTime().split(",");
57             String[] flags = item.getFlag().split(",");
58             builder.append("\tdistributed_term_item{").append(System.lineSeparator());
59             long receiverTime = 0L;
60             long senderTime = 0L;
61             for (int index = 0; index < flags.length; index++) {
62                 String sendOrRecv = "C".equals(flags[index]) ? "\t\tsender {" : "\t\treceiver {";
63                 builder.append(sendOrRecv).append(System.lineSeparator());
64                 if (item.getFlag().contains("C,S") || item.getFlag().contains("S,C")) {
65                     builder.append("\t\t\tAcross the device:").append(false).append(System.lineSeparator());
66                     if ("S".equals(flags[index])) {
67                         receiverTime = Long.parseLong(times[index]);
68                     }
69                     if ("C".equals(flags[index])) {
70                         senderTime = Long.parseLong(times[index]);
71                     }
72                 } else {
73                     builder.append("\t\t\tAcross the device:").append(true).append(System.lineSeparator());
74                 }
75                 appendId(builder, item);
76                 builder.append("\t\t\tfunction_name:").append(funNames[index]).append(System.lineSeparator());
77                 builder.append("\t\t\tprocess_info{").append(System.lineSeparator());
78                 builder.append("\t\t\t\tprocess_id:").append(processIds[index]).append(System.lineSeparator());
79                 builder.append("\t\t\t\tprocess_name:").append(processNames[index]).append(System.lineSeparator());
80                 builder.append("\t\t\t}").append(System.lineSeparator());
81                 builder.append("\t\t\tthread_info{").append(System.lineSeparator());
82                 builder.append("\t\t\t\tthread_id:").append(threadIds[index]).append(System.lineSeparator());
83                 builder.append("\t\t\t\tthread_name:").append(threadNames[index]).append(System.lineSeparator());
84                 builder.append("\t\t\t}").append(System.lineSeparator());
85                 builder.append("\t\t\tdur:").append(durations[index]).append(System.lineSeparator());
86                 appendDelay(item, builder, flags, index, receiverTime - senderTime);
87                 builder.append("\t\t }").append(System.lineSeparator());
88             }
89             builder.append("\t}").append(System.lineSeparator());
90         }
91         builder.append("}").append(System.lineSeparator());
92         return builder.toString();
93     }
94 
appendDelay(DistributeTerm item, StringBuilder builder, String[] flags, int index, long delay)95     private void appendDelay(DistributeTerm item, StringBuilder builder, String[] flags, int index, long delay) {
96         if (item.getFlag().contains("C,S") || item.getFlag().contains("S,C")) {
97             if ("S".equals(flags[index])) {
98                 builder.append("\t\t\tdelay:").append(delay).append(System.lineSeparator());
99             }
100         } else {
101             if ("S".equals(flags[index])) {
102                 builder.append("\t\t\tdelay:").append("").append(System.lineSeparator());
103             }
104         }
105     }
106 
appendId(StringBuilder builder, DistributeTerm item)107     private void appendId(StringBuilder builder, DistributeTerm item) {
108         builder.append("\t\t\ttrace_name:").append(item.getTraceName()).append(System.lineSeparator());
109         builder.append("\t\t\ttrace_id {").append(System.lineSeparator());
110         builder.append("\t\t\t\tchainID:").append(item.getChainId()).append(System.lineSeparator());
111         builder.append("\t\t\t\tspanID:").append(item.getSpanId()).append(System.lineSeparator());
112         builder.append("\t\t\t\tparentSpanID:").append(item.getParentSpanId()).append(System.lineSeparator());
113         builder.append("\t\t\t}").append(System.lineSeparator());
114     }
115 
116 }
117