/
data_source_api.go
262 lines (226 loc) · 5.84 KB
/
data_source_api.go
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
package datahub
import (
"context"
"fmt"
"github.com/sagoo-cloud/sagooiot/internal/dao"
"github.com/sagoo-cloud/sagooiot/internal/model"
"github.com/sagoo-cloud/sagooiot/internal/service"
"reflect"
"strings"
"sync"
"github.com/gogf/gf/v2/container/gmap"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/text/gregex"
"github.com/gogf/gf/v2/util/gconv"
)
func (s *sDataSource) updateDataForApi(ctx context.Context, sourceId uint64) error {
// 数据节点
nodeList, err := service.DataNode().List(ctx, sourceId)
if err != nil {
return err
}
if len(nodeList) == 0 {
return gerror.New("数据源未添加数据节点")
}
// 获取api数据
apiDataArr, err := s.GetApiData(ctx, sourceId)
if err != nil {
return err
}
// 数据映射
var insertData []*gmap.AnyAnyMap
for _, apiData := range apiDataArr {
j := gjson.New(apiData)
m := gmap.New(true)
var wg sync.WaitGroup
for _, v := range nodeList {
wg.Add(1)
go func(v *model.DataNodeOutput) {
defer wg.Done()
// 规则过滤,启用节点规则
var rule *model.DataSourceRule
if len(v.NodeRule) > 0 {
rule = v.NodeRule[0]
}
rs := getValue(v.Value, j, rule)
if rs != "" {
m.Set(v.Key, rs)
}
}(v)
}
wg.Wait()
insertData = append(insertData, m)
}
// 入库
if len(insertData) > 0 {
table := getTableName(sourceId)
err = g.DB(DataCenter()).GetCore().ClearTableFields(ctx, table)
if err != nil {
return err
}
_, err = g.DB(DataCenter()).Save(ctx, table, insertData)
if err != nil {
return err
}
}
return nil
}
// 获取api数据
func (s *sDataSource) GetApiData(ctx context.Context, sourceId uint64) (apiData []string, err error) {
p, _ := s.Detail(ctx, sourceId)
if p == nil || p.ApiConfig == nil {
err = gerror.New("数据源不存在或未进行配置")
return
}
// api配置
config := p.ApiConfig
// api数据获取
get := func(header, data g.MapStrStr) (string, error) {
client := g.Client()
client.Header(header)
res, err := client.DoRequest(ctx, config.Method, config.Url, data)
if err != nil {
return "", err
}
defer res.Close()
return res.ReadAllString(), nil
}
if len(config.RequestParams) > 0 {
for _, param := range config.RequestParams {
header := g.MapStrStr{}
data := g.MapStrStr{}
for _, v := range param {
if v.Type == "header" {
header[v.Key] = v.Value
} else {
data[v.Key] = v.Value
}
}
rs, err := get(header, data)
if err != nil {
return nil, err
}
apiData = append(apiData, rs)
}
} else {
rs, err := get(nil, nil)
if err != nil {
return nil, err
}
apiData = append(apiData, rs)
}
return
}
// api数据提取值
func getValue(value string, apiData *gjson.Json, rule *model.DataSourceRule) string {
sv := apiData.Get(value)
if sv == nil {
return ""
}
s := sv.String()
if rule != nil && rule.Expression != "" {
// 正则过滤数据
rs, _ := gregex.ReplaceString(rule.Expression, rule.Replace, s)
return gconv.String(rs)
}
return s
}
// api源数据记录列表
func (s *sDataSource) getApiDataRecord(ctx context.Context, in *model.DataSourceDataInput, ds *model.DataSourceOutput) (out *model.DataSourceDataOutput, err error) {
out = new(model.DataSourceDataOutput)
table := getTableName(in.SourceId)
// 搜索条件
var exp []string
var value []any
if in.Param != nil {
for k, v := range in.Param {
exp = append(exp, k)
if reflect.TypeOf(v).Kind() == reflect.Slice {
s := reflect.ValueOf(v)
for i := 0; i < s.Len(); i++ {
ele := s.Index(i)
value = append(value, ele.Interface())
}
} else {
value = append(value, v)
}
}
}
where := ""
if len(exp) > 0 {
where = " where " + strings.Join(exp, " and ")
}
sql := "select * from " + table + where + " order by created_at desc"
out.Total, _ = g.DB(DataCenter()).GetCount(ctx, sql, value...)
out.CurrentPage = in.PageNum
sql += fmt.Sprintf(" limit %d, %d", (in.PageNum-1)*in.PageSize, in.PageSize)
rs, err := g.DB(DataCenter()).GetAll(ctx, sql, value...)
if err != nil {
return
}
out.List = rs.Json()
return
}
// api源数据记录列表,非分页
func (s *sDataSource) getApiDataAllRecord(ctx context.Context, in *model.SourceDataAllInput, ds *model.DataSourceOutput) (out *model.SourceDataAllOutput, err error) {
out = new(model.SourceDataAllOutput)
table := getTableName(in.SourceId)
// 搜索条件
var exp []string
var value []any
if in.Param != nil {
for k, v := range in.Param {
exp = append(exp, k)
if reflect.TypeOf(v).Kind() == reflect.Slice {
s := reflect.ValueOf(v)
for i := 0; i < s.Len(); i++ {
ele := s.Index(i)
value = append(value, ele.Interface())
}
} else {
value = append(value, v)
}
}
}
where := ""
if len(exp) > 0 {
where = " where " + strings.Join(exp, " and ")
}
sql := "select * from " + table + where + " order by created_at desc"
rs, err := g.DB(DataCenter()).GetAll(ctx, sql, value...)
if err != nil {
return
}
out.List = rs.Json()
return
}
// 复制API数据源
func (s *sDataSource) copeApiSource(ctx context.Context, ds *model.DataSourceOutput) (newSourceId uint64, err error) {
err = dao.DataSource.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
// 复制源
in := new(model.DataSourceApiAddInput)
in.DataSource = model.DataSource{}
in.Config = model.DataSourceConfigApi{}
err = gconv.Scan(ds.DataSource, &in.DataSource)
if err != nil {
return err
}
in.DataSource.Key += "_" + gtime.Now().Format("YmdHis")
err = gconv.Scan(ds.ApiConfig, &in.Config)
if err != nil {
return err
}
newSourceId, err = s.Add(ctx, in)
if err != nil {
return err
}
// 复制节点
err = s.copeNode(ctx, ds.SourceId, newSourceId)
return err
})
return
}