This repository has been archived by the owner on Oct 28, 2022. It is now read-only.
/
apiMethods.go
335 lines (271 loc) · 9.65 KB
/
apiMethods.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
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
package main
import (
"fmt"
"net/http"
"time"
"github.com/gorilla/rpc/v2/json2"
"github.com/patrickmn/go-cache"
"github.com/riftbit/ALS-Go/httpmodels"
"github.com/riftbit/ALS-Go/mongomodels"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
func makeLogDBConnection(db, category, connectionString string) (*mgo.Session, *mgo.Collection, *json2.Error) {
session, err := createMGOConnection(connectionString)
if err != nil {
Logger.Error("[" + getFuncName(1) + "] createMGOConnection: " + err.Error())
return session, nil, &json2.Error{Code: json2.E_INTERNAL, Message: "Log Connection Problems"}
}
collection, err := useMGOCol(useMGODB(session, db), category)
if err != nil {
Logger.Error("useMGOCol: " + err.Error())
return session, collection, &json2.Error{Code: json2.E_INTERNAL, Message: "Log Select Collection Problems"}
}
return session, collection, nil
}
//Log area
type Log struct{}
//Add Method to add Log
func (h *Log) Add(r *http.Request, args *httpmodels.RequestLogAdd, reply *httpmodels.ResponseLogAdd) error {
errs := args.Validate()
if errs != nil {
return &json2.Error{Code: json2.E_BAD_PARAMS, Message: errs.Error()}
}
session, collection, errjs := makeLogDBConnection(getUser(r), args.Category, getConnectionStringByCategory(args.Category))
if errjs != nil {
return errjs
}
defer session.Close()
args.ID = bson.NewObjectId()
logData := mongomodels.MongoLog{}
logData.ID = args.ID
logData.Level = args.Level
logData.Message = args.Message
logData.Timestamp = args.Timestamp
logData.ExpiresAt = time.Unix(args.ExpiresAt, 0)
logData.ExpiresAtShow = args.ExpiresAt
if err := insertMGO(collection, logData); err != nil {
Logger.Error("InsertMGO: " + err.Error())
return &json2.Error{Code: json2.E_INTERNAL, Message: "Log Insert Problems"}
}
reply.LogID = args.ID.Hex()
return nil
}
//AddCustom Method to add Log with additional params
func (h *Log) AddCustom(r *http.Request, args *httpmodels.RequestLogAddCustom, reply *httpmodels.ResponseLogAdd) error {
errs := args.Validate()
if errs != nil {
return &json2.Error{Code: json2.E_BAD_PARAMS, Message: errs.Error()}
}
session, collection, errjs := makeLogDBConnection(getUser(r), args.Category, getConnectionStringByCategory(args.Category))
if errjs != nil {
return errjs
}
defer session.Close()
args.ID = bson.NewObjectId()
logData := mongomodels.MongoCustomLog{}
logData.ID = args.ID
logData.Level = args.Level
logData.Message = args.Message
logData.Timestamp = args.Timestamp
logData.ExpiresAt = time.Unix(args.ExpiresAt, 0)
logData.ExpiresAtShow = args.ExpiresAt
logData.Tags = args.Tags
logData.AdditionalData = args.AdditionalData
if err := insertMGO(collection, logData); err != nil {
Logger.Error("InsertMGO: " + err.Error())
return &json2.Error{Code: json2.E_INTERNAL, Message: "Log Insert Problems"}
}
reply.LogID = args.ID.Hex()
return nil
}
//Get Method to get Log/Logs
func (h *Log) Get(r *http.Request, args *httpmodels.RequestLogGetLog, reply *httpmodels.ResponseLogGet) error {
errs := args.Validate()
if errs != nil {
return &json2.Error{Code: json2.E_BAD_PARAMS, Message: errs.Error()}
}
session, collection, errjs := makeLogDBConnection(getUser(r), args.Category, getConnectionStringByCategory(args.Category))
if errjs != nil {
return errjs
}
defer session.Close()
reply.LogList = getFromMGO(collection, args.SearchFilter, args.Limit, args.Offset, args.Sort)
return nil
}
//GetCount Method to get Log counts
func (h *Log) GetCount(r *http.Request, args *httpmodels.RequestLogGetCount, reply *httpmodels.ResponseLogGetCount) error {
errs := args.Validate()
if errs != nil {
return &json2.Error{Code: json2.E_BAD_PARAMS, Message: errs.Error()}
}
session, collection, errjs := makeLogDBConnection(getUser(r), args.Category, getConnectionStringByCategory(args.Category))
if errjs != nil {
return errjs
}
defer session.Close()
count, err := getCountMGO(collection, args.SearchFilter)
if err != nil {
Logger.Error("GetCountMGO: " + err.Error())
return &json2.Error{Code: json2.E_INTERNAL, Message: "Log Select Data Problems"}
}
reply.LogCount = count
return nil
}
//GetCategories Method to get Log categories
func (h *Log) GetCategories(r *http.Request, args *struct{}, reply *httpmodels.ResponseLogGetCategories) error {
serverList := getServersList()
for _, srv := range serverList {
collections, err := getCollectionsList(srv, getUser(r))
if err != nil {
Logger.Error("GetCollectionsList: " + err.Error())
return &json2.Error{Code: json2.E_INTERNAL, Message: "Log Categories Getting Problems"}
}
for _, col := range collections {
reply.CategoriesList = append(reply.CategoriesList, col)
}
}
return nil
}
//Remove Method to remove Log/Logs
func (h *Log) Remove(r *http.Request, args *httpmodels.RequestLogRemoveLog, reply *httpmodels.ResponseLogRemoveLog) error {
errs := args.Validate()
if errs != nil {
return &json2.Error{Code: json2.E_BAD_PARAMS, Message: errs.Error()}
}
session, collection, errjs := makeLogDBConnection(getUser(r), args.Category, getConnectionStringByCategory(args.Category))
if errjs != nil {
return errjs
}
defer session.Close()
info, err := removeAllFromMGO(collection, args.SearchFilter)
if err != nil {
Logger.Error("RemoveAllFromMGO: " + err.Error())
return &json2.Error{Code: json2.E_INTERNAL, Message: fmt.Sprintf("%s", err)}
}
reply.Matched = info.Matched
reply.Removed = info.Removed
return nil
}
//RemoveCategory Method to remove Log category
func (h *Log) RemoveCategory(r *http.Request, args *httpmodels.RequestLogRemoveCategory, reply *httpmodels.ResponseLogRemoveCategory) error {
errs := args.Validate()
if errs != nil {
return &json2.Error{Code: json2.E_BAD_PARAMS, Message: errs.Error()}
}
session, collection, errjs := makeLogDBConnection(getUser(r), args.Category, getConnectionStringByCategory(args.Category))
if errjs != nil {
return errjs
}
defer session.Close()
if err := collection.DropCollection(); err != nil {
Logger.Error("DropCollection: " + err.Error())
return &json2.Error{Code: json2.E_INTERNAL, Message: fmt.Sprintf("%s", err)}
}
reply.Success = 1
return nil
}
//Transfer Method to transfer Log/Logs to another category
func (h *Log) Transfer(r *http.Request, args *httpmodels.RequestLogTransferLog, reply *httpmodels.ResponseLogTransferLog) error {
errs := args.Validate()
if errs != nil {
return &json2.Error{Code: json2.E_BAD_PARAMS, Message: errs.Error()}
}
//CONNECT TO "FROM" DB AND GET DATA
sessionFrom, collectionFrom, errjs := makeLogDBConnection(getUser(r), args.OldCategory, getConnectionStringByCategory(args.OldCategory))
if errjs != nil {
return errjs
}
defer sessionFrom.Close()
found := getFromMGO(collectionFrom, args.SearchFilter, -1, 0, nil)
//CONNECT TO "TO" DB
sessionTo, collectionTo, errjs := makeLogDBConnection(getUser(r), args.NewCategory, getConnectionStringByCategory(args.NewCategory))
if errjs != nil {
return errjs
}
defer sessionTo.Close()
//TRANSFERING DATA "FROM" -> "TO"
for _, element := range found {
if err := collectionFrom.RemoveId(element.ID); err != nil {
return &json2.Error{Code: json2.E_INTERNAL, Message: "Log Removing from Collection Problems: " + err.Error()}
}
if err := collectionTo.Insert(element); err != nil {
return &json2.Error{Code: json2.E_INTERNAL, Message: "Log Insert to Collection Problems: " + err.Error()}
}
reply.TransferedLogID = append(reply.TransferedLogID, element.ID)
}
return nil
}
// ModifyTTL Method to modify Log/Logs TTL value
func (h *Log) ModifyTTL(r *http.Request, args *httpmodels.RequestLogModifyTTL, reply *httpmodels.ResponseLogModifyTTL) error {
errs := args.Validate()
if errs != nil {
return &json2.Error{Code: json2.E_BAD_PARAMS, Message: errs.Error()}
}
session, collection, errjs := makeLogDBConnection(getUser(r), args.Category, getConnectionStringByCategory(args.Category))
if errjs != nil {
return errjs
}
defer session.Close()
updateData := bson.M{"$set": bson.M{"expiresAt": time.Unix(args.NewTTL, 0), "expiresAtIntJustToShow": args.NewTTL}}
info, err := updateAllMGO(collection, args.SearchFilter, updateData)
if err != nil {
Logger.Error("UpdateAllMGO: " + err.Error())
return &json2.Error{Code: json2.E_INTERNAL, Message: fmt.Sprintf("%s", err)}
}
reply.Matched = info.Matched
reply.Updated = info.Updated
reply.UpsertedID = info.UpsertedId
return nil
}
/*
-------------------------------------------------
INTERNAL CALL: Some general system tests, checking/showing how errors and other conditions are handled
-------------------------------------------------
*/
//System ...
type System struct{}
// GetCacheAll cache records
func (h *System) GetCacheAll(r *http.Request, args *struct{}, reply *struct {
Count int
Items map[string]cache.Item
}) error {
reply.Count = Cache.ItemCount()
reply.Items = Cache.Items()
return nil
}
// GetCache record by key
func (h *System) GetCache(r *http.Request, args *struct{ Key string }, reply *struct {
Key string
Data interface{}
}) error {
var found bool
reply.Key = args.Key
reply.Data, found = Cache.Get(args.Key)
if found == false {
return json2.ErrNullResult
}
return nil
}
// DeleteCache record by key
func (h *System) DeleteCache(r *http.Request, args *struct{ Key string }, reply *struct{ Status int }) error {
Cache.Delete(args.Key)
reply.Status = 1
return nil
}
// SetCache for cache element
func (h *System) SetCache(r *http.Request, args *struct {
Key string
Data interface{}
TTL time.Duration
}, reply *struct{ Status int }) error {
Cache.Set(args.Key, args.Data, args.TTL)
reply.Status = 1
return nil
}
// FlushCache clean all cached data
func (h *System) FlushCache(r *http.Request, args *struct{}, reply *struct{ Status int }) error {
Cache.Flush()
reply.Status = 1
return nil
}