|
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+package com.water.bi.service.impl;
|
|
|
2
|
+
|
|
|
3
|
+import com.water.bi.service.BISupersetMetabaseService;
|
|
|
4
|
+import com.water.bi.entity.DataSource;
|
|
|
5
|
+import com.water.bi.entity.BIDashboard;
|
|
|
6
|
+import com.water.bi.entity.DataVisualization;
|
|
|
7
|
+import org.springframework.stereotype.Service;
|
|
|
8
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
9
|
+import java.util.*;
|
|
|
10
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
11
|
+
|
|
|
12
|
+/**
|
|
|
13
|
+ * BI工具集成服务实现 - 支持Superset和Metabase集成
|
|
|
14
|
+ */
|
|
|
15
|
+@Service
|
|
|
16
|
+public class BISupersetMetabaseServiceImpl implements BISupersetMetabaseService {
|
|
|
17
|
+
|
|
|
18
|
+ // 存储连接信息
|
|
|
19
|
+ private final Map<String, ConnectionInfo> connections = new ConcurrentHashMap<>();
|
|
|
20
|
+
|
|
|
21
|
+ // 模拟Superset和Metabase的API调用
|
|
|
22
|
+ @Override
|
|
|
23
|
+ public String connectToSuperset(String supersetUrl, String username, String password) {
|
|
|
24
|
+ String connectionId = "superset_" + System.currentTimeMillis();
|
|
|
25
|
+ ConnectionInfo connection = new ConnectionInfo();
|
|
|
26
|
+ connection.setType("superset");
|
|
|
27
|
+ connection.setUrl(supersetUrl);
|
|
|
28
|
+ connection.setUsername(username);
|
|
|
29
|
+ connection.setPassword(password);
|
|
|
30
|
+ connection.setStatus("connected");
|
|
|
31
|
+ connection.setConnectedAt(new Date());
|
|
|
32
|
+
|
|
|
33
|
+ connections.put(connectionId, connection);
|
|
|
34
|
+
|
|
|
35
|
+ // 模拟创建一些默认图表和数据集
|
|
|
36
|
+ createMockSupersetResources(connectionId);
|
|
|
37
|
+
|
|
|
38
|
+ return connectionId;
|
|
|
39
|
+ }
|
|
|
40
|
+
|
|
|
41
|
+ @Override
|
|
|
42
|
+ public String connectToMetabase(String metabaseUrl, String sessionId) {
|
|
|
43
|
+ String connectionId = "metabase_" + System.currentTimeMillis();
|
|
|
44
|
+ ConnectionInfo connection = new ConnectionInfo();
|
|
|
45
|
+ connection.setType("metabase");
|
|
|
46
|
+ connection.setUrl(metabaseUrl);
|
|
|
47
|
+ connection.setSessionId(sessionId);
|
|
|
48
|
+ connection.setStatus("connected");
|
|
|
49
|
+ connection.setConnectedAt(new Date());
|
|
|
50
|
+
|
|
|
51
|
+ connections.put(connectionId, connection);
|
|
|
52
|
+
|
|
|
53
|
+ // 模拟创建一些默认图表和数据集
|
|
|
54
|
+ createMockMetabaseResources(connectionId);
|
|
|
55
|
+
|
|
|
56
|
+ return connectionId;
|
|
|
57
|
+ }
|
|
|
58
|
+
|
|
|
59
|
+ @Override
|
|
|
60
|
+ public String createDataset(String connectionId, Map<String, Object> datasetConfig) {
|
|
|
61
|
+ if (!connections.containsKey(connectionId)) {
|
|
|
62
|
+ throw new IllegalArgumentException("连接不存在: " + connectionId);
|
|
|
63
|
+ }
|
|
|
64
|
+
|
|
|
65
|
+ String datasetId = "dataset_" + System.currentTimeMillis();
|
|
|
66
|
+ ConnectionInfo connection = connections.get(connectionId);
|
|
|
67
|
+
|
|
|
68
|
+ // 模拟创建数据集
|
|
|
69
|
+ Map<String, Object> dataset = new HashMap<>();
|
|
|
70
|
+ dataset.put("id", datasetId);
|
|
|
71
|
+ dataset.put("name", datasetConfig.getOrDefault("name", "默认数据集"));
|
|
|
72
|
+ dataset.put("description", datasetConfig.getOrDefault("description", "数据集描述"));
|
|
|
73
|
+ dataset.put("type", "table");
|
|
|
74
|
+ dataset.put("database", connection.getUrl());
|
|
|
75
|
+ dataset.put("status", "active");
|
|
|
76
|
+
|
|
|
77
|
+ // 存储数据集信息(实际应用中应该调用对应的API)
|
|
|
78
|
+ connection.getDatasets().put(datasetId, dataset);
|
|
|
79
|
+
|
|
|
80
|
+ return datasetId;
|
|
|
81
|
+ }
|
|
|
82
|
+
|
|
|
83
|
+ @Override
|
|
|
84
|
+ public String createChart(String connectionId, Map<String, Object> chartConfig) {
|
|
|
85
|
+ if (!connections.containsKey(connectionId)) {
|
|
|
86
|
+ throw new IllegalArgumentException("连接不存在: " + connectionId);
|
|
|
87
|
+ }
|
|
|
88
|
+
|
|
|
89
|
+ String chartId = "chart_" + System.currentTimeMillis();
|
|
|
90
|
+ ConnectionInfo connection = connections.get(connectionId);
|
|
|
91
|
+
|
|
|
92
|
+ // 模拟创建图表
|
|
|
93
|
+ Map<String, Object> chart = new HashMap<>();
|
|
|
94
|
+ chart.put("id", chartId);
|
|
|
95
|
+ chart.put("name", chartConfig.getOrDefault("name", "默认图表"));
|
|
|
96
|
+ chart.put("type", chartConfig.getOrDefault("type", "line"));
|
|
|
97
|
+ chart.put("description", chartConfig.getOrDefault("description", "图表描述"));
|
|
|
98
|
+ chart.put("datasetId", chartConfig.getOrDefault("datasetId", "default_dataset"));
|
|
|
99
|
+ chart.put("display_name", chartConfig.getOrDefault("display_name", "图表显示名称"));
|
|
|
100
|
+ chart.put("status", "published");
|
|
|
101
|
+
|
|
|
102
|
+ // 存储图表信息
|
|
|
103
|
+ connection.getCharts().put(chartId, chart);
|
|
|
104
|
+
|
|
|
105
|
+ return chartId;
|
|
|
106
|
+ }
|
|
|
107
|
+
|
|
|
108
|
+ @Override
|
|
|
109
|
+ public String createDashboard(String connectionId, Map<String, Object> dashboardConfig) {
|
|
|
110
|
+ if (!connections.containsKey(connectionId)) {
|
|
|
111
|
+ throw new IllegalArgumentException("连接不存在: " + connectionId);
|
|
|
112
|
+ }
|
|
|
113
|
+
|
|
|
114
|
+ String dashboardId = "dashboard_" + System.currentTimeMillis();
|
|
|
115
|
+ ConnectionInfo connection = connections.get(connectionId);
|
|
|
116
|
+
|
|
|
117
|
+ // 模拟创建仪表盘
|
|
|
118
|
+ Map<String, Object> dashboard = new HashMap<>();
|
|
|
119
|
+ dashboard.put("id", dashboardId);
|
|
|
120
|
+ dashboard.put("name", dashboardConfig.getOrDefault("name", "默认仪表盘"));
|
|
|
121
|
+ dashboard.put("description", dashboardConfig.getOrDefault("description", "仪表盘描述"));
|
|
|
122
|
+ dashboard.put("charts", dashboardConfig.getOrDefault("charts", new ArrayList<>()));
|
|
|
123
|
+ dashboard.put("layout", dashboardConfig.getOrDefault("layout", "grid"));
|
|
|
124
|
+ dashboard.put("published", true);
|
|
|
125
|
+ dashboard.put("slug", dashboardConfig.getOrDefault("slug", "default-dashboard"));
|
|
|
126
|
+ dashboard.put("status", "published");
|
|
|
127
|
+
|
|
|
128
|
+ // 存储仪表盘信息
|
|
|
129
|
+ connection.getDashboards().put(dashboardId, dashboard);
|
|
|
130
|
+
|
|
|
131
|
+ return dashboardId;
|
|
|
132
|
+ }
|
|
|
133
|
+
|
|
|
134
|
+ @Override
|
|
|
135
|
+ public List<Map<String, Object>> getAvailableCharts(String connectionId) {
|
|
|
136
|
+ if (!connections.containsKey(connectionId)) {
|
|
|
137
|
+ return Collections.emptyList();
|
|
|
138
|
+ }
|
|
|
139
|
+
|
|
|
140
|
+ ConnectionInfo connection = connections.get(connectionId);
|
|
|
141
|
+ return new ArrayList<>(connection.getCharts().values());
|
|
|
142
|
+ }
|
|
|
143
|
+
|
|
|
144
|
+ @Override
|
|
|
145
|
+ public List<Map<String, Object>> getAvailableDatasets(String connectionId) {
|
|
|
146
|
+ if (!connections.containsKey(connectionId)) {
|
|
|
147
|
+ return Collections.emptyList();
|
|
|
148
|
+ }
|
|
|
149
|
+
|
|
|
150
|
+ ConnectionInfo connection = connections.get(connectionId);
|
|
|
151
|
+ return new ArrayList<>(connection.getDatasets().values());
|
|
|
152
|
+ }
|
|
|
153
|
+
|
|
|
154
|
+ @Override
|
|
|
155
|
+ public Map<String, Object> exportDashboard(String dashboardId, String format) {
|
|
|
156
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
157
|
+ result.put("dashboardId", dashboardId);
|
|
|
158
|
+ result.put("format", format);
|
|
|
159
|
+ result.put("status", "success");
|
|
|
160
|
+ result.put("downloadUrl", "/api/bi/export/" + dashboardId + "." + format);
|
|
|
161
|
+ result.put("size", "2.5MB");
|
|
|
162
|
+ result.put("createdAt", new Date());
|
|
|
163
|
+
|
|
|
164
|
+ return result;
|
|
|
165
|
+ }
|
|
|
166
|
+
|
|
|
167
|
+ @Override
|
|
|
168
|
+ public String createSelfServiceDashboard(Map<String, Object> config) {
|
|
|
169
|
+ String dashboardId = "selfservice_" + System.currentTimeMillis();
|
|
|
170
|
+
|
|
|
171
|
+ // 创建自助服务看板配置
|
|
|
172
|
+ Map<String, Object> dashboard = new HashMap<>();
|
|
|
173
|
+ dashboard.put("id", dashboardId);
|
|
|
174
|
+ dashboard.put("name", config.getOrDefault("name", "自助分析看板"));
|
|
|
175
|
+ dashboard.put("description", config.getOrDefault("description", "用户可拖拽自定义的分析看板"));
|
|
|
176
|
+ dashboard.put("type", "selfservice");
|
|
|
177
|
+ dashboard.put("features", Arrays.asList("drag_drop", "real_time", "export"));
|
|
|
178
|
+ dashboard.put("theme", config.getOrDefault("theme", "light"));
|
|
|
179
|
+ dashboard.put("layout", config.getOrDefault("layout", "responsive"));
|
|
|
180
|
+ dashboard.put("createdBy", "system");
|
|
|
181
|
+ dashboard.put("createdAt", new Date());
|
|
|
182
|
+ dashboard.put("published", true);
|
|
|
183
|
+
|
|
|
184
|
+ // 这里可以根据需要保存到数据库或返回
|
|
|
185
|
+ return dashboardId;
|
|
|
186
|
+ }
|
|
|
187
|
+
|
|
|
188
|
+ /**
|
|
|
189
|
+ * 模拟创建Superset资源
|
|
|
190
|
+ */
|
|
|
191
|
+ private void createMockSupersetResources(String connectionId) {
|
|
|
192
|
+ ConnectionInfo connection = connections.get(connectionId);
|
|
|
193
|
+
|
|
|
194
|
+ // 创建示例数据集
|
|
|
195
|
+ Map<String, Object> dataset1 = new HashMap<>();
|
|
|
196
|
+ dataset1.put("id", "water_consumption_ds");
|
|
|
197
|
+ dataset1.put("name", "用水量数据集");
|
|
|
198
|
+ dataset1.put("description", "各区域用水量统计");
|
|
|
199
|
+ dataset1.put("type", "table");
|
|
|
200
|
+ dataset1.put("database", "water_db");
|
|
|
201
|
+ dataset1.put("status", "active");
|
|
|
202
|
+ connection.getDatasets().put("water_consumption_ds", dataset1);
|
|
|
203
|
+
|
|
|
204
|
+ Map<String, Object> dataset2 = new HashMap<>();
|
|
|
205
|
+ dataset2.put("id", "quality_metrics_ds");
|
|
|
206
|
+ dataset2.put("name", "水质指标数据集");
|
|
|
207
|
+ dataset2.put("description", "水质监测指标数据");
|
|
|
208
|
+ dataset2.put("type", "table");
|
|
|
209
|
+ dataset2.put("database", "quality_db");
|
|
|
210
|
+ dataset2.put("status", "active");
|
|
|
211
|
+ connection.getDatasets().put("quality_metrics_ds", dataset2);
|
|
|
212
|
+
|
|
|
213
|
+ // 创建示例图表
|
|
|
214
|
+ Map<String, Object> chart1 = new HashMap<>();
|
|
|
215
|
+ chart1.put("id", "daily_consumption_chart");
|
|
|
216
|
+ chart1.put("name", "日用水量趋势");
|
|
|
217
|
+ chart1.put("type", "line");
|
|
|
218
|
+ chart1.put("description", "每日用水量变化趋势");
|
|
|
219
|
+ chart1.put("datasetId", "water_consumption_ds");
|
|
|
220
|
+ chart1.put("display_name", "日用水量趋势图");
|
|
|
221
|
+ chart1.put("status", "published");
|
|
|
222
|
+ connection.getCharts().put("daily_consumption_chart", chart1);
|
|
|
223
|
+
|
|
|
224
|
+ Map<String, Object> chart2 = new HashMap<>();
|
|
|
225
|
+ chart2.put("id", "quality_gauge_chart");
|
|
|
226
|
+ chart2.put("name", "水质达标率仪表盘");
|
|
|
227
|
+ chart2.put("type", "gauge");
|
|
|
228
|
+ chart2.put("description", "水质各项指标达标情况");
|
|
|
229
|
+ chart2.put("datasetId", "quality_metrics_ds");
|
|
|
230
|
+ chart2.put("display_name", "水质达标率");
|
|
|
231
|
+ chart2.put("status", "published");
|
|
|
232
|
+ connection.getCharts().put("quality_gauge_chart", chart2);
|
|
|
233
|
+
|
|
|
234
|
+ Map<String, Object> chart3 = new HashMap<>();
|
|
|
235
|
+ chart3.put("id", "region_consumption_chart");
|
|
|
236
|
+ chart3.put("name", "区域用水量对比");
|
|
|
237
|
+ chart3.put("type", "bar");
|
|
|
238
|
+ chart3.put("description", "不同区域用水量对比");
|
|
|
239
|
+ chart3.put("datasetId", "water_consumption_ds");
|
|
|
240
|
+ chart3.put("display_name", "区域用水量对比");
|
|
|
241
|
+ chart3.put("status", "published");
|
|
|
242
|
+ connection.getCharts().put("region_consumption_chart", chart3);
|
|
|
243
|
+ }
|
|
|
244
|
+
|
|
|
245
|
+ /**
|
|
|
246
|
+ * 模拟创建Metabase资源
|
|
|
247
|
+ */
|
|
|
248
|
+ private void createMockMetabaseResources(String connectionId) {
|
|
|
249
|
+ ConnectionInfo connection = connections.get(connectionId);
|
|
|
250
|
+
|
|
|
251
|
+ // 创建示例数据集
|
|
|
252
|
+ Map<String, Object> dataset1 = new HashMap<>();
|
|
|
253
|
+ dataset1.put("id", "metabase_water_ds");
|
|
|
254
|
+ dataset1.put("name", "供水数据库");
|
|
|
255
|
+ dataset1.put("description", "供水系统业务数据");
|
|
|
256
|
+ dataset1.put("type", "table");
|
|
|
257
|
+ dataset1.put("status", "synced");
|
|
|
258
|
+ connection.getDatasets().put("metabase_water_ds", dataset1);
|
|
|
259
|
+
|
|
|
260
|
+ // 创建示例问题/图表
|
|
|
261
|
+ Map<String, Object> question1 = new HashMap<>();
|
|
|
262
|
+ question1.put("id", "monthly_consumption_q");
|
|
|
263
|
+ question1.put("name", "月度用水量统计");
|
|
|
264
|
+ question1.put("type", "question");
|
|
|
265
|
+ question1.put("description", "按月统计用水量");
|
|
|
266
|
+ question1.put("datasetId", "metabase_water_ds");
|
|
|
267
|
+ question1.put("display_name", "月度用水量");
|
|
|
268
|
+ question1.put("status", "archived");
|
|
|
269
|
+ connection.getCharts().put("monthly_consumption_q", question1);
|
|
|
270
|
+ }
|
|
|
271
|
+
|
|
|
272
|
+ /**
|
|
|
273
|
+ * 连接信息内部类
|
|
|
274
|
+ */
|
|
|
275
|
+ private static class ConnectionInfo {
|
|
|
276
|
+ private String type;
|
|
|
277
|
+ private String url;
|
|
|
278
|
+ private String username;
|
|
|
279
|
+ private String password;
|
|
|
280
|
+ private String sessionId;
|
|
|
281
|
+ private String status;
|
|
|
282
|
+ private Date connectedAt;
|
|
|
283
|
+ private final Map<String, Object> datasets = new HashMap<>();
|
|
|
284
|
+ private final Map<String, Object> charts = new HashMap<>();
|
|
|
285
|
+ private final Map<String, Object> dashboards = new HashMap<>();
|
|
|
286
|
+
|
|
|
287
|
+ // Getters and Setters
|
|
|
288
|
+ public String getType() { return type; }
|
|
|
289
|
+ public void setType(String type) { this.type = type; }
|
|
|
290
|
+ public String getUrl() { return url; }
|
|
|
291
|
+ public void setUrl(String url) { this.url = url; }
|
|
|
292
|
+ public String getUsername() { return username; }
|
|
|
293
|
+ public void setUsername(String username) { this.username = username; }
|
|
|
294
|
+ public String getPassword() { return password; }
|
|
|
295
|
+ public void setPassword(String password) { this.password = password; }
|
|
|
296
|
+ public String getSessionId() { return sessionId; }
|
|
|
297
|
+ public void setSessionId(String sessionId) { this.sessionId = sessionId; }
|
|
|
298
|
+ public String getStatus() { return status; }
|
|
|
299
|
+ public void setStatus(String status) { this.status = status; }
|
|
|
300
|
+ public Date getConnectedAt() { return connectedAt; }
|
|
|
301
|
+ public void setConnectedAt(Date connectedAt) { this.connectedAt = connectedAt; }
|
|
|
302
|
+ public Map<String, Object> getDatasets() { return datasets; }
|
|
|
303
|
+ public Map<String, Object> getCharts() { return charts; }
|
|
|
304
|
+ public Map<String, Object> getDashboards() { return dashboards; }
|
|
|
305
|
+ }
|
|
|
306
|
+}
|