json_utility.go
7.25 KB
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
package webutility
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"sync"
"time"
"git.to-net.rs/marko.tikvic/gologger"
"gopkg.in/rana/ora.v4"
)
var (
mu = &sync.Mutex{}
metadata = make(map[string]Payload)
updateQue = make(map[string][]byte)
metadataDB *ora.Ses
activeProject string
inited bool
)
var logger *gologger.Logger
func init() {
var err error
logger, err = gologger.New("webutility", gologger.MaxLogSize100KB)
if err != nil {
fmt.Printf("webutility: %s\n", err.Error())
}
}
type LangMap map[string]map[string]string
type Field struct {
Parameter string `json:"param"`
Type string `json:"type"`
Visible bool `json:"visible"`
Editable bool `json:"editable"`
}
type CorrelationField struct {
Result string `json:"result"`
Elements []string `json:"elements"`
Type string `json:"type"`
}
type Translation struct {
Language string `json:"language"`
FieldsLabels map[string]string `json:"fieldsLabels"`
}
type Payload struct {
Method string `json:"method"`
Params map[string]string `json:"params"`
Lang []Translation `json:"lang"`
Fields []Field `json:"fields"`
Correlations []CorrelationField `json:"correlationFields"`
IdField string `json:"idField"`
// Data holds JSON payload. It can't be used for itteration.
Data interface{} `json:"data"`
}
// LoadPayloadsdetaData loads all payloads' information into 'metadata' variable.
func LoadPayloadsMetadata(db *ora.Ses, project string, hotloading bool, hlPeriod int) error {
metadataDB = db
activeProject = project
mu.Lock()
defer mu.Unlock()
err := initMetadata(project)
if err != nil {
return err
}
if hotloading {
go hotload(hlPeriod)
}
inited = true
return nil
}
func GetMetadataForAllEntities() map[string]Payload {
return metadata
}
func GetMetadataForEntity(t string) (Payload, bool) {
p, ok := metadata[t]
return p, ok
}
func QueEntityModelUpdate(entityType string, v interface{}) {
updateQue[entityType], _ = json.Marshal(v)
}
func UpdateEntityModels(command string) (total, upd, add int, err error) {
if command != "force" && command != "missing" {
return total, 0, 0, errors.New("uknown command: " + command)
}
if !inited {
return 0, 0, 0, errors.New("webutil: metadata not initialized but update was tried.")
}
total = len(updateQue)
forUpdate := make([]string, 0)
forAdd := make([]string, 0)
for k, _ := range updateQue {
if _, exists := metadata[k]; exists {
if command == "force" {
forUpdate = append(forUpdate, k)
}
} else {
forAdd = append(forAdd, k)
}
}
for _, k := range forUpdate {
_, err := metadataDB.PrepAndExe(`update entities set
entity_model = :1
where entity_type = :2`,
string(updateQue[k]),
k)
if err != nil {
logger.Log("webutility: update metadata: prep and exe: %v\n", err)
continue
}
upd++
logger.Log("webutility: updated %s payload model\n", k)
}
blankPayload, _ := json.Marshal(Payload{})
for _, k := range forAdd {
_, err := metadataDB.PrepAndExe(`insert into entities
(projekat, metadata, entity_type, entity_model)
values(:1, :2, :3, :4)`,
activeProject,
string(blankPayload),
k,
string(updateQue[k]))
if err != nil {
logger.Log("webutility: add metadata: prep and exe: %v\n", err)
continue
}
metadata[k] = Payload{}
add++
logger.Log("webutility: added %s to the payload models\n", k)
}
return total, upd, add, nil
}
func ModifyMetadataForEntity(entityType string, p *Payload) error {
md, err := json.Marshal(*p)
if err != nil {
return err
}
mu.Lock()
defer mu.Unlock()
_, err = metadataDB.PrepAndExe(`update entities set
metadata = :1
where projekat = :2
and entity_type = :3`,
string(md),
activeProject,
entityType)
if err != nil {
return err
}
return nil
}
func DeleteEntityModel(entityType string) error {
_, err := metadataDB.PrepAndExe("delete from entities where entity_type = :1", entityType)
if err == nil {
mu.Lock()
delete(metadata, entityType)
mu.Unlock()
}
return err
}
// NewPayload returs a payload sceleton for entity described with etype.
func NewPayload(r *http.Request, etype string) Payload {
pload := metadata[etype]
pload.Method = r.Method + " " + r.RequestURI
return pload
}
// DecodeJSON decodes JSON data from r to v.
// Returns an error if it fails.
func DecodeJSON(r io.Reader, v interface{}) error {
return json.NewDecoder(r).Decode(v)
}
func initMetadata(project string) error {
metadataDB.SetCfg(metadataDB.Cfg().SetClob(ora.S))
stmt, err := metadataDB.Prep(`select
entity_type,
metadata
from entities
where projekat = `+fmt.Sprintf("'%s'", project),
ora.S,
ora.S)
defer stmt.Close()
if err != nil {
return err
}
rset, err := stmt.Qry()
if err != nil {
return err
}
count := 0
success := 0
if len(metadata) > 0 {
metadata = nil
}
metadata = make(map[string]Payload)
for rset.Next() {
name := rset.Row[0].(string)
load := []byte(rset.Row[1].(string))
p := Payload{}
err := json.Unmarshal(load, &p)
if err != nil {
logger.Log("couldn't init: '%s' metadata: %s\n%s\n", name, err.Error(), rset.Row[1].(string))
} else {
success++
metadata[name] = p
}
count++
}
logger.Log("webutility: successfully loaded %d/%d (%.1f%%) entities\n", success, count, float32(success)/float32(count)*100.0)
return nil
}
func hotload(n int) {
entityScan := make(map[string]int64)
firstCheck := true
for {
time.Sleep(time.Duration(n) * time.Second)
stmt, err := metadataDB.Prep(`select
ora_rowscn,
entity_type
from entities where projekat = `+fmt.Sprintf("'%s'", activeProject),
ora.I64,
ora.S)
if err != nil {
logger.Log("hotload failed: %v\n", err)
time.Sleep(time.Duration(n) * time.Second)
continue
}
rset, err := stmt.Qry()
if err != nil {
fmt.Fprintf(os.Stderr, "hotload failed: %v\n", err)
time.Sleep(time.Duration(n) * time.Second)
continue
}
var toRefresh []string
for rset.Next() {
scanID := rset.Row[0].(int64)
entity := rset.Row[1].(string)
oldID, ok := entityScan[entity]
if !ok || oldID != scanID {
entityScan[entity] = scanID
toRefresh = append(toRefresh, entity)
}
}
stmt.Close()
if rset.Err() != nil {
logger.Log("hotload rset error: %v\n", rset.Err())
time.Sleep(time.Duration(n) * time.Second)
continue
}
if len(toRefresh) > 0 && !firstCheck {
mu.Lock()
refreshMetadata(toRefresh)
mu.Unlock()
}
if firstCheck {
firstCheck = false
}
}
}
func refreshMetadata(entities []string) {
for _, e := range entities {
fmt.Printf("refreshing %s\n", e)
stmt, err := metadataDB.Prep(`select
metadata
from entities
where projekat = `+fmt.Sprintf("'%s'", activeProject)+
` and entity_type = `+fmt.Sprintf("'%s'", e),
ora.S)
if err != nil {
logger.Log("webutility: refresh: prep: %v\n", err)
stmt.Close()
continue
}
rset, err := stmt.Qry()
if err != nil {
fmt.Printf("webutility: refresh: query: %v\n", err)
stmt.Close()
continue
}
for rset.Next() {
load := []byte(rset.Row[0].(string))
p := Payload{}
err := json.Unmarshal(load, &p)
if err != nil {
logger.Log("couldn't refresh: '%s' metadata: %s\n%s\n", e, err.Error(), rset.Row[0].(string))
} else {
metadata[e] = p
}
}
stmt.Close()
}
}