Blame view
json_utility.go
6.79 KB
ea858b8a7 refactoring |
1 |
package webutility |
64041a2ea first commit |
2 3 |
import ( |
8dbe745c3 merged tables uti... |
4 5 |
"encoding/json" "errors" |
66478e04f payload now retur... |
6 |
"fmt" |
8dbe745c3 merged tables uti... |
7 |
"io" |
d2ddf82ef started on new rbac |
8 |
"net/http" |
2ea67927f added support for... |
9 |
"os" |
17a4d0447 mutex lock for pa... |
10 |
"sync" |
2ea67927f added support for... |
11 |
"time" |
1d0f61553 can't fetch clob |
12 |
|
1d0f61553 can't fetch clob |
13 |
"gopkg.in/rana/ora.v4" |
64041a2ea first commit |
14 |
) |
2ea67927f added support for... |
15 |
var ( |
67337ffa8 payload editing |
16 17 18 |
mu = &sync.Mutex{} metadata = make(map[string]Payload) updateQue = make(map[string][]byte) |
2ea67927f added support for... |
19 20 21 22 23 24 |
metadataDB *ora.Ses activeProject string inited bool ) |
8dbe745c3 merged tables uti... |
25 |
|
64041a2ea first commit |
26 27 28 |
type LangMap map[string]map[string]string type Field struct { |
d2ddf82ef started on new rbac |
29 30 31 32 |
Parameter string `json:"param"` Type string `json:"type"` Visible bool `json:"visible"` Editable bool `json:"editable"` |
64041a2ea first commit |
33 |
} |
8dbe745c3 merged tables uti... |
34 35 36 37 38 39 40 |
type CorrelationField struct { Result string `json:"result"` Elements []string `json:"elements"` Type string `json:"type"` } type Translation struct { |
ecec68b18 updated todo list |
41 |
Language string `json:"language"` |
8dbe745c3 merged tables uti... |
42 |
FieldsLabels map[string]string `json:"fieldsLabels"` |
64041a2ea first commit |
43 |
} |
4a51e54d7 simplified |
44 |
type Payload struct { |
d2ddf82ef started on new rbac |
45 46 47 48 |
Method string `json:"method"` Params map[string]string `json:"params"` Lang []Translation `json:"lang"` Fields []Field `json:"fields"` |
4a51e54d7 simplified |
49 50 |
Correlations []CorrelationField `json:"correlationFields"` IdField string `json:"idField"` |
e1fbb41f9 added comments |
51 |
|
66478e04f payload now retur... |
52 |
// Data holds JSON payload. It can't be used for itteration. |
d2ddf82ef started on new rbac |
53 |
Data interface{} `json:"data"` |
4a51e54d7 simplified |
54 |
} |
2ea67927f added support for... |
55 56 57 58 |
// LoadPayloadsdetaData loads all payloads' information into 'metadata' variable. func LoadPayloadsMetadata(db *ora.Ses, project string, hotloading bool, hlPeriod int) error { metadataDB = db activeProject = project |
62a69beda Init/Reload Table... |
59 |
|
2ea67927f added support for... |
60 61 62 |
mu.Lock() defer mu.Unlock() err := initMetadata(project) |
d66628295 cleaned up |
63 64 65 |
if err != nil { return err } |
2ea67927f added support for... |
66 67 68 69 70 71 72 |
if hotloading { go hotload(hlPeriod) } inited = true return nil } |
67337ffa8 payload editing |
73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
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(forceUpdate bool) (total, upd, add int, err error) { |
2ea67927f added support for... |
87 |
if !inited { |
67337ffa8 payload editing |
88 |
return 0, 0, 0, errors.New("webutil: metadata not initialized but update was tried.") |
2ea67927f added support for... |
89 |
} |
67337ffa8 payload editing |
90 |
total = len(updateQue) |
2ea67927f added support for... |
91 |
forUpdate := make([]string, 0) |
67337ffa8 payload editing |
92 |
forAdd := make([]string, 0) |
2ea67927f added support for... |
93 |
|
67337ffa8 payload editing |
94 |
for k, _ := range updateQue { |
2ea67927f added support for... |
95 |
if _, exists := metadata[k]; exists { |
67337ffa8 payload editing |
96 97 98 |
if forceUpdate { forUpdate = append(forUpdate, k) } |
2ea67927f added support for... |
99 |
} else { |
67337ffa8 payload editing |
100 |
forAdd = append(forAdd, k) |
2ea67927f added support for... |
101 102 103 104 105 106 |
} } for _, k := range forUpdate { fmt.Printf("for update: %s ", k) |
67337ffa8 payload editing |
107 |
|
2ea67927f added support for... |
108 |
_, err := metadataDB.PrepAndExe(`update entities set |
67337ffa8 payload editing |
109 110 111 |
entity_model = :1 where entity_type = :2`, string(updateQue[k]), |
2ea67927f added support for... |
112 113 114 115 116 117 118 119 120 |
k) if err != nil { fmt.Printf("webutility: update metadata: prep and exe: %v ", err) continue } upd++ } |
67337ffa8 payload editing |
121 122 |
blankPayload, _ := json.Marshal(Payload{}) for _, k := range forAdd { |
2ea67927f added support for... |
123 124 |
fmt.Printf("for add: %s ", k) |
2ea67927f added support for... |
125 |
|
67337ffa8 payload editing |
126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
_, 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 { fmt.Printf("webutility: add metadata: prep and exe: %v ", err) continue } metadata[k] = Payload{} |
2ea67927f added support for... |
140 141 |
add++ } |
67337ffa8 payload editing |
142 |
return total, upd, add, nil |
2ea67927f added support for... |
143 |
} |
d66628295 cleaned up |
144 |
|
67337ffa8 payload editing |
145 146 |
func ModifyMetadataForEntity(entityType string, p *Payload) error { md, err := json.Marshal(*p) |
2ea67927f added support for... |
147 148 149 |
if err != nil { return err } |
67337ffa8 payload editing |
150 |
|
d66628295 cleaned up |
151 152 |
mu.Lock() defer mu.Unlock() |
2ea67927f added support for... |
153 154 155 156 157 158 159 160 161 |
_, err = metadataDB.PrepAndExe(`update entities set metadata = :1 where projekat = :2 and entity_type = :3`, string(md), activeProject, entityType) if err != nil { return err |
d66628295 cleaned up |
162 163 164 |
} return nil } |
67337ffa8 payload editing |
165 166 167 168 169 170 171 172 |
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 |
d66628295 cleaned up |
173 |
} |
2ea67927f added support for... |
174 175 176 |
// NewPayload returs a payload sceleton for entity described with etype. func NewPayload(r *http.Request, etype string) Payload { pload := metadata[etype] |
2d79a4120 Responses contain... |
177 |
pload.Method = r.Method + " " + r.RequestURI |
8dbe745c3 merged tables uti... |
178 179 |
return pload } |
64041a2ea first commit |
180 |
|
67337ffa8 payload editing |
181 182 183 184 185 |
// 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) } |
2ea67927f added support for... |
186 187 188 189 190 191 192 193 194 195 196 197 198 |
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 |
64041a2ea first commit |
199 |
} |
8dbe745c3 merged tables uti... |
200 |
|
2ea67927f added support for... |
201 202 203 204 |
rset, err := stmt.Qry() if err != nil { return err } |
8dbe745c3 merged tables uti... |
205 |
|
2ea67927f added support for... |
206 207 |
count := 0 success := 0 |
67337ffa8 payload editing |
208 209 210 |
if len(metadata) > 0 { metadata = nil } |
2ea67927f added support for... |
211 212 213 214 |
metadata = make(map[string]Payload) for rset.Next() { name := rset.Row[0].(string) load := []byte(rset.Row[1].(string)) |
8dbe745c3 merged tables uti... |
215 |
|
2ea67927f added support for... |
216 217 218 219 220 221 222 223 |
p := Payload{} err := json.Unmarshal(load, &p) if err != nil { fmt.Printf("couldn't init: '%s' metadata ", name) } else { success++ metadata[name] = p |
8dbe745c3 merged tables uti... |
224 |
} |
2ea67927f added support for... |
225 |
count++ |
8dbe745c3 merged tables uti... |
226 |
} |
2ea67927f added support for... |
227 228 229 |
fmt.Printf("webutility: successfully loaded %d/%d (%.1f%%) entities ", success, count, float32(success)/float32(count)*100.0) |
8dbe745c3 merged tables uti... |
230 |
|
2ea67927f added support for... |
231 |
return nil |
8dbe745c3 merged tables uti... |
232 |
} |
2ea67927f added support for... |
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
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 { fmt.Fprintf(os.Stderr, "hotload failed: %v ", err) time.Sleep(time.Duration(n) * time.Second) continue |
8dbe745c3 merged tables uti... |
249 |
} |
8dbe745c3 merged tables uti... |
250 |
|
2ea67927f added support for... |
251 252 253 254 255 256 257 |
rset, err := stmt.Qry() if err != nil { fmt.Fprintf(os.Stderr, "hotload failed: %v ", err) time.Sleep(time.Duration(n) * time.Second) continue } |
8dbe745c3 merged tables uti... |
258 |
|
2ea67927f added support for... |
259 260 261 262 263 264 265 266 |
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) |
8dbe745c3 merged tables uti... |
267 268 |
} } |
2ea67927f added support for... |
269 |
stmt.Close() |
8dbe745c3 merged tables uti... |
270 |
|
2ea67927f added support for... |
271 272 273 274 275 276 |
if rset.Err() != nil { fmt.Fprintf(os.Stderr, "hotload rset error: %v ", rset.Err()) time.Sleep(time.Duration(n) * time.Second) continue } |
8dbe745c3 merged tables uti... |
277 |
|
2ea67927f added support for... |
278 279 280 281 282 283 284 285 |
if len(toRefresh) > 0 && !firstCheck { mu.Lock() refreshMetadata(toRefresh) mu.Unlock() } if firstCheck { firstCheck = false } |
8dbe745c3 merged tables uti... |
286 |
} |
2ea67927f added support for... |
287 |
} |
8dbe745c3 merged tables uti... |
288 |
|
2ea67927f added support for... |
289 290 |
func refreshMetadata(entities []string) { for _, e := range entities { |
67337ffa8 payload editing |
291 292 |
fmt.Printf("refreshing %s ", e) |
2ea67927f added support for... |
293 294 295 296 297 298 |
stmt, err := metadataDB.Prep(`select metadata from entities where projekat = `+fmt.Sprintf("'%s'", activeProject)+ ` and entity_type = `+fmt.Sprintf("'%s'", e), ora.S) |
8dbe745c3 merged tables uti... |
299 |
|
2ea67927f added support for... |
300 301 302 303 304 305 |
if err != nil { fmt.Printf("webutility: refresh: prep: %v ", err) stmt.Close() continue } |
64041a2ea first commit |
306 |
|
2ea67927f added support for... |
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
rset, err := stmt.Qry() if err != nil { fmt.Printf("webutility: refresh: query: %v ", err) stmt.Close() continue } for rset.Next() { load := []byte(rset.Row[0].(string)) p := Payload{} err := json.Unmarshal(load, &p) if err != nil { fmt.Printf("couldn't refresh: '%s' metadata ", e) } else { metadata[e] = p |
2ea67927f added support for... |
324 325 326 327 |
} } stmt.Close() } |
64041a2ea first commit |
328 |
} |