TangCheng
2025-02-28 d787e447e95c7b897c2cc9c0e832f8d2e5084934
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package com.smtaiserver.smtaiserver.javaai.duckdb;
 
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
 
import com.smtaiserver.smtaiserver.core.SMTAIServerApp;
import com.smtaiserver.smtaiserver.core.SMTAIServerRequest;
import com.smtaiserver.smtaiserver.database.SMTDatabase;
import com.smtaiserver.smtaiserver.database.SMTDatabase.DBRecord;
import com.smtaiserver.smtaiserver.database.SMTDatabase.DBRecords;
import com.smtaiserver.smtaiserver.javaai.ast.ASTDimFilter;
import com.smtaiserver.smtaiserver.javaai.duckdb.DuckCubeRecs.DuckCubeColTitle;
import com.smtaiserver.smtaiserver.javaai.metrics.base.SMTDimensionDef;
import com.smtservlet.util.SMTJsonWriter;
import com.smtservlet.util.SMTStatic;
 
public class DuckMergeGroupRecord extends DuckMergeGroup
{
    private static class OutputColInfo
    {
        public String            _title;
        public String            _name;
        public boolean            _isGroup;
        public int                _pos;
        public boolean             _showColumn;
        public boolean            _isCount;
    
        public OutputColInfo(String name, String title, boolean isGroup, boolean showColumn, int pos)
        {
            _title = title;
            _name = name;
            _isGroup = isGroup;
            _pos = pos;
            _showColumn = showColumn;
        }
    }
    
    public String                _posXColName;
    public String                _posYColName;
    public String                _devKeyColName;
    public DuckCubeRecs         _astCubeRecs;
    public List<ASTDimFilter>     _listDimFilter;
    
    @SuppressWarnings("unused")
    private String getStartDate()
    {
        if(_astCubeRecs._timeRange != null && _astCubeRecs._timeRange._startTime != null)
            return SMTStatic.toString(_astCubeRecs._timeRange._startTime).substring(0, 11);
        
        return "";
    }
    
    private void outputDimFilterToJson(Set<String> setDimNames, DBRecords recsMeta, SMTJsonWriter jsonWr)
    {
        if(_listDimFilter != null && _listDimFilter.size() > 0)
        {
            jsonWr.beginArray("filter");
            for(ASTDimFilter dimFilter : _listDimFilter)
            {
                if(!recsMeta.getFieldMap().containsKey(dimFilter._dimDef.getId().toUpperCase()) && 
                   !setDimNames.contains(dimFilter._dimDef.getId()))
                    continue;
                
                jsonWr.beginMap(null);
                {
                    String type;
                    switch(dimFilter._dimDef.getType())
                    {
                    case 'T':
                        type = "time";
                        break;
                        
                    default:
                        type = "string";
                        break;
                    }
                    
                    jsonWr.addKeyValue("type", type);
                    jsonWr.addKeyValue("update", "set");
                    jsonWr.addKeyValue("title", dimFilter._dimDef.getName());
                    List<String> listValue = dimFilter._dimDef.getValueList();
                    if(listValue != null)
                    {
                        jsonWr.beginArray("value_list");
                        for(String value : listValue)
                        {
                            jsonWr.addKeyValue(null, value);
                        }
                        jsonWr.endArray();
                    }
                    
                    jsonWr.beginArray("path");
                    {
                        jsonWr.addKeyValue(null, dimFilter._dimPath);
                    }
                    jsonWr.endArray();
                }
                jsonWr.endMap();
            }
            jsonWr.endArray();
        }
        
        if(_astCubeRecs._timeRange != null)
        {
            jsonWr.beginArray("filter");
            {
                jsonWr.beginMap(null);
                {
                    jsonWr.addKeyValue("type", "time_range");
                    jsonWr.addKeyValue("update", "set");
                    jsonWr.addKeyValue("title", "时间范围");
                    jsonWr.addKeyValue("start_value", _astCubeRecs._timeRange._startTime);
                    jsonWr.addKeyValue("end_value", _astCubeRecs._timeRange._endTime);
 
                    long deltaDays = (_astCubeRecs._timeRange._endTime.getTime() - _astCubeRecs._timeRange._startTime.getTime()) / 1000 / 24 / 3600 - 60;
                    jsonWr.addKeyValue("time_step", deltaDays > 0 ? "month" : "days");
                    
                    jsonWr.beginArray("start_path");
                    {
                        jsonWr.addKeyValue(null, _astCubeRecs._timeRange._pathStartTime);
                    }
                    jsonWr.endArray();
                    
                    jsonWr.beginArray("end_path");
                    {
                        jsonWr.addKeyValue(null, _astCubeRecs._timeRange._pathEndTime);        
                    }
                    jsonWr.endArray();
                }
                jsonWr.endMap();
 
            }
            jsonWr.endArray();
 
        }
 
 
    }
    
    private void outputHideColumns(LinkedHashMap<String, OutputColInfo> mapName2ColPos, SMTJsonWriter jsonWr)
    {
        OutputColInfo posX = null;
        OutputColInfo posY = null;
        OutputColInfo posDevKey = null;
        
        if(!SMTStatic.isNullOrEmpty(_posXColName) && !SMTStatic.isNullOrEmpty(_posYColName))
        {
            posX = mapName2ColPos.remove(_posXColName.toUpperCase());
            posY = mapName2ColPos.remove(_posYColName.toUpperCase());
        }
        
        if(!SMTStatic.isNullOrEmpty(_devKeyColName))
        {
            posDevKey = mapName2ColPos.remove(_devKeyColName.toUpperCase());
 
        }    
        
        if(posX != null && posY != null)
        {
            jsonWr.beginMap("map");
            {
                posX._showColumn = false;
                jsonWr.addKeyValue("pos_x", mapName2ColPos.size());
                mapName2ColPos.put(_posXColName.toUpperCase(), posX);
 
                posY._showColumn = false;
                jsonWr.addKeyValue("pos_y", mapName2ColPos.size());
                mapName2ColPos.put(_posYColName.toUpperCase(), posY);
            }
            jsonWr.endMap();
        }
        
        if(posDevKey != null)
        {
            jsonWr.beginMap("quota_chart");
            {
                posDevKey._showColumn = false;
                jsonWr.addKeyValue("col", mapName2ColPos.size());
                mapName2ColPos.put(_devKeyColName.toUpperCase(), posDevKey);
            }
            jsonWr.endMap();
        }
    }
    
    @Override
    public void outputGroupToJson(String agentKey, SMTAIServerRequest tranReq) throws Exception
    {
        SMTJsonWriter jsonWrResult = tranReq.getResultJsonWr();
        
        int maxRecs = SMTAIServerApp.getApp().getOutputAITableMax();
        
        // 获取排序SQL
        String[] orderDim = tranReq.getChatOrderDimName();
 
        
        // 打开数据库,获取字段列表
        SMTDatabase dbResult = tranReq.getResultDB();
        DBRecords recsMeta = dbResult.querySQL("SELECT * FROM " + _astCubeRecs._tableName + " LIMIT 0", null);
        Map<String, SMTDimensionDef> mapId2DimDef = SMTAIServerApp.getApp().getDimensionDefMap();
        
        jsonWrResult.beginMap(null);
        {
            // 设置基本信息
            jsonWrResult.addKeyValue("type", "recordset");
            jsonWrResult.addKeyValue("agent_key", agentKey);
            jsonWrResult.addKeyValue("chart", "table");
            jsonWrResult.addKeyValue("title", _title);    
            jsonWrResult.addKeyValue("max_cols", _astCubeRecs._colKeyCount);
            
            // 获取维度列表
            Set<String> setDimNames = new HashSet<>();
            for(String dimName : _astCubeRecs._dimNames)
            {
                setDimNames.add(dimName);
            }
            
            // 如果存在过滤条件,则将过滤条件输出
            this.outputDimFilterToJson(setDimNames, recsMeta, jsonWrResult);
            
            // 生成字段名
            LinkedHashMap<String, OutputColInfo> mapName2ColPos = new LinkedHashMap<>();
            
            // 如果存在维度分组,则将分组字段加入
            for(String dimName : _astCubeRecs._dimNames)
            {
                int pos = recsMeta.getColIndex(dimName);
                if(pos < 0)
                    continue;
                
                SMTDimensionDef dimDef = mapId2DimDef.get(dimName.toLowerCase());
                String title = dimDef == null ? dimName : dimDef.getName();
                
                mapName2ColPos.put(dimName.toUpperCase(), new OutputColInfo(dimName, title, true, true, pos));
            }
            
            // 如果存在时间字段,则将时间字段加入
            if(_astCubeRecs._timeRange != null)
            {
                int pos = recsMeta.getColIndex(_astCubeRecs._timeRange._timeField);
                if(pos >= 0)
                    mapName2ColPos.put(
                        _astCubeRecs._timeRange._timeField.toUpperCase(), 
                        new OutputColInfo(_astCubeRecs._timeRange._timeField, "时间", false, true, pos));
            }
            
            // 加入其余字段
            for(Entry<String, Integer> entry : recsMeta.getFieldMap().entrySet())
            {
                if(mapName2ColPos.containsKey(entry.getKey()))
                    continue;
                
                String dimName = entry.getKey();
                SMTDimensionDef dimDef = mapId2DimDef.get(dimName.toLowerCase());
                
                String title;
                boolean isCount = false;
                if(dimDef != null)
                {
                    title = dimDef.getName();
                }
                else
                {
                    DuckCubeColTitle titleInfo = _astCubeRecs._mapCol2Title.get(entry.getKey());
                    if(titleInfo != null)
                    {
                        title = titleInfo._title;
                        isCount = titleInfo._isCount;
                    }
                    else
                        title = entry.getKey();
                }
                OutputColInfo colInfo = new OutputColInfo(dimName, title, false, true, entry.getValue());
                colInfo._isCount = isCount;
                
                mapName2ColPos.put(dimName.toUpperCase(), colInfo);
            }
            
            // 输出隐藏字段
            this.outputHideColumns(mapName2ColPos, jsonWrResult); 
 
            // 输入字段名到前端
            jsonWrResult.beginArray("cols");
            
            int colIndex = 0;
            int colCount = -1;
            for(OutputColInfo colInfo : mapName2ColPos.values())
            {
                if(!colInfo._showColumn)
                    break;
                
                if(colInfo._isCount)
                    colCount = colIndex;
                
                jsonWrResult.beginMap(null);
                {
                    jsonWrResult.addKeyValue("title", colInfo._title);
                    if(!colInfo._isGroup)
                        jsonWrResult.addKeyValue("name", colInfo._name);
                    jsonWrResult.addKeyValue("type", "text");
                    jsonWrResult.addKeyValue("group", colInfo._isGroup);
                }
                jsonWrResult.endMap();
                colIndex ++;
            }
            jsonWrResult.endArray();
            
            // 输出统计个数那列
            if(colCount >= 0)
            {
                jsonWrResult.addKeyValue("agg_count_col", colCount);
            }
            
            // 生成查询SQL
            StringBuilder sbSQL = new StringBuilder();
            StringBuilder sbSORT = new StringBuilder();
            sbSQL.append("SELECT * FROM " + this._astCubeRecs._tableName);
            
            // 如果有维度,则先按维度排序
            boolean sortInGroup = false;
            if(_astCubeRecs._dimNames.size() > 0)
            {
                for(int i = 0; i < _astCubeRecs._dimNames.size(); i ++)
                {
                    if(!mapName2ColPos.containsKey(_astCubeRecs._dimNames.get(i).toUpperCase()))
                        continue;
                    
                    if(sbSORT.length() > 0)
                        sbSORT.append(",");
                    
                    String dimName = _astCubeRecs._dimNames.get(i);
                    if(orderDim != null && orderDim[0].equalsIgnoreCase(dimName))
                    {
                        sortInGroup = true;
                        sbSORT.append(dimName + " " + orderDim[1]);
                    }
                    else
                    {
                        sbSORT.append(dimName);
                    }
                }
            }
            
            // 如果排序字段没在分组里,则单独设置
            if(!sortInGroup && orderDim != null && mapName2ColPos.containsKey(orderDim[0].toUpperCase()))
            {
                if(sbSORT.length() > 0)
                    sbSORT.append(",");
                sbSORT.append(orderDim[0] + " " + orderDim[1]);
            }
            
            // 如果存在流程测排序,则加入
            if(this._astCubeRecs._orderCol != null)
            {
                if(sbSORT.length() > 0)
                    sbSORT.append(",");
                
                if(recsMeta.getColIndex(this._astCubeRecs._orderCol[0]) >= 0)
                    sbSORT.append(this._astCubeRecs._orderCol[0] + " " + this._astCubeRecs._orderCol[1]);
            }
            
            // 如果有时间,则按照时间排序
            if(_astCubeRecs._timeRange != null && mapName2ColPos.containsKey(_astCubeRecs._timeRange._timeField.toUpperCase()))
            {
                if(sbSORT.length() > 0)
                    sbSORT.append(",");
                sbSORT.append(_astCubeRecs._timeRange._timeField);
            }
            
            // 组织出最终的查询sql
            String sql = sbSQL.toString() + (sbSORT.length() == 0 ? "" : " ORDER BY " + sbSORT.toString()) + " LIMIT " + maxRecs;
            
            // 查询结果并输出
            jsonWrResult.beginArray("values");
            dbResult.querySQLNotify(sql, null, new SMTDatabase.DBQueryNotify() 
            {
                @Override
                public boolean onNextRecord(DBRecord rec) throws Exception
                {
                    jsonWrResult.beginArray(null);
                    for(OutputColInfo colInfo : mapName2ColPos.values())
                    {
                        jsonWrResult.addKeyValue(null, rec.getString(colInfo._pos));
                    }
                    jsonWrResult.endArray();
                    return true;
                }
                
            });
            jsonWrResult.endArray();
        }
        jsonWrResult.endMap();
        
    }
}