-
Notifications
You must be signed in to change notification settings - Fork 2
/
entity_gchart.go
445 lines (350 loc) · 13.4 KB
/
entity_gchart.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
* Copyright (c) 2015, 2016 Joern Rischmueller (joern.rm@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"encoding/binary"
"fmt"
"net/http"
"strconv"
"time"
"golang.org/x/net/context"
"google.golang.org/appengine"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/log"
b64 "encoding/base64"
"github.com/emicklei/go-restful"
)
// ---------------------------------------------------------------------------------------------------------------//
// Full Golden Cheetah chart definition (gchartentity) which is stored in DB
// ---------------------------------------------------------------------------------------------------------------//
type GChartEntity struct {
Header CommonEntityHeader
ChartSport string `datastore:",noindex"`
ChartType string `datastore:",noindex"`
ChartView string `datastore:",noindex"`
ChartDef string `datastore:",noindex"`
Image []byte `datastore:",noindex"`
CreatorNick string `datastore:",noindex"`
CreatorEmail string `datastore:",noindex"`
Internal GChartEntityInternal
}
type GChartEntityHeaderOnly struct {
Header CommonEntityHeader
ChartSport string
ChartType string
ChartView string
}
// Internal attributes which must not be filled by POST or PUT (but are returned on GET)
type GChartEntityInternal struct {
DLCounter int `datastore:",noindex"`
}
// ---------------------------------------------------------------------------------------------------------------//
// API View Definition
// ---------------------------------------------------------------------------------------------------------------//
// Full structure for GET
type GChartGetAPIv1 struct {
Header CommonAPIHeaderV1 `json:"header"`
ChartSport string `json:"chartSport"`
ChartType string `json:"chartType"`
ChartView string `json:"chartView"`
ChartDef string `json:"chartDef"`
Image string `json:"image"`
CreatorNick string `json:"creatorNick"`
CreatorEmail string `json:"creatorEmail"`
DLCounter int `json:"downloadCount"`
}
// Reduced structure for POST and PUT (without internal fields)
type GChartPostAPIv1 struct {
Header CommonAPIHeaderV1 `json:"header"`
ChartSport string `json:"chartSport"`
ChartType string `json:"chartType"`
ChartView string `json:"chartView"`
ChartDef string `json:"chartDef"`
Image string `json:"image"`
CreatorNick string `json:"creatorNick"`
CreatorEmail string `json:"creatorEmail"`
}
type GChartGetAPIv1List []GChartGetAPIv1
// Header only structure
type GChartAPIv1HeaderOnly struct {
Header CommonAPIHeaderV1 `json:"header"`
ChartSport string `json:"chartSport"`
ChartType string `json:"chartType"`
ChartView string `json:"chartView"`
}
type GChartAPIv1HeaderOnlyList []GChartAPIv1HeaderOnly
// ---------------------------------------------------------------------------------------------------------------//
// Data Storage View
// ---------------------------------------------------------------------------------------------------------------//
const gChartDBEntity = "gchartentity"
const gChartDBEntityRootKey = "gchartsroot"
func mapAPItoDBGChart(api *GChartPostAPIv1, db *GChartEntity) {
mapAPItoDBCommonHeader(&api.Header, &db.Header)
db.ChartSport = api.ChartSport
db.ChartType = api.ChartType
db.ChartView = api.ChartView
db.ChartDef = api.ChartDef
data, err := b64.StdEncoding.DecodeString(api.Image)
if err != nil {
data = nil
} else {
db.Image = data
}
// no picture bigger than 1000k please since Appengine
// datastore api V3 only supports calls up to 1536 KB
if binary.Size(db.Image) > 1024000 {
db.Image = nil
}
db.CreatorNick = api.CreatorNick
db.CreatorEmail = api.CreatorEmail
}
func mapDBtoAPIGChart(db *GChartEntity, api *GChartGetAPIv1) {
mapDBtoAPICommonHeader(&db.Header, &api.Header)
api.ChartSport = db.ChartSport
api.ChartType = db.ChartType
api.ChartView = db.ChartView
api.ChartDef = db.ChartDef
api.Image = b64.StdEncoding.EncodeToString(db.Image)
api.CreatorNick = db.CreatorNick
api.CreatorEmail = db.CreatorEmail
api.DLCounter = db.Internal.DLCounter
}
// supporting functions
// chartEntityKey returns the key used for all chartEntity entries.
func gchartEntityRootKey(ctx context.Context) *datastore.Key {
return datastore.NewKey(ctx, gChartDBEntity, gChartDBEntityRootKey, 0, nil)
}
// ---------------------------------------------------------------------------------------------------------------//
// request/response handler
// ---------------------------------------------------------------------------------------------------------------//
func insertGChart(request *restful.Request, response *restful.Response) {
ctx := appengine.NewContext(request.Request)
chart := new(GChartPostAPIv1)
if err := request.ReadEntity(chart); err != nil {
addPlainTextError(response, http.StatusInternalServerError, err.Error())
return
}
// No checks if the necessary fields are filed or not - since GoldenCheetah is
// the only consumer of the APIs - any checks/response are to support this use-case
chartDB := new(GChartEntity)
mapAPItoDBGChart(chart, chartDB)
// complete/set POST fields
chartDB.Header.LastChanged = time.Now()
chartDB.Header.Curated = false
chartDB.Header.Deleted = false
chartDB.Internal.DLCounter = 0
// auto-curate if a registered "curator" is adding a gchart
curatorQuery := datastore.NewQuery(curatorDBEntity).Filter("CuratorId =", chartDB.Header.CreatorId)
counter, _ := curatorQuery.Count(ctx) // ignore errors/just leave uncurated
if counter == 1 {
chartDB.Header.Curated = true
} else {
chartDB.Header.Curated = false
}
// and now store it
key := datastore.NewIncompleteKey(ctx, gChartDBEntity, gchartEntityRootKey(ctx))
key, err := datastore.Put(ctx, key, chartDB);
if err != nil {
commonResponseErrorProcessing (response, err)
return
}
// send back the key
response.WriteHeaderAndEntity(http.StatusCreated, strconv.FormatInt(key.IntID(), 10))
}
func updateGChart(request *restful.Request, response *restful.Response) {
ctx := appengine.NewContext(request.Request)
chart := new(GChartPostAPIv1)
if err := request.ReadEntity(chart); err != nil {
addPlainTextError(response, http.StatusInternalServerError, err.Error())
return
}
if chart.Header.Id == 0 {
addPlainTextError(response, http.StatusBadRequest, "Mandatory Id for Update is missing or invalid")
return
}
key := datastore.NewKey(ctx, gChartDBEntity, "", chart.Header.Id, gchartEntityRootKey(ctx))
// get the current chart to retrieve the current DL counter
currentChartDB := new(GChartEntity)
err := datastore.Get(ctx, key, currentChartDB)
if err != nil && !isErrFieldMismatch(err) {
commonResponseErrorProcessing(response, err)
return
}
// No more checks if the necessary fields are filed or not - since GoldenCheetah is
// the only consumer of the APIs - any checks/response are to support this use-case
chartDB := new(GChartEntity)
mapAPItoDBGChart(chart, chartDB)
chartDB.Internal.DLCounter = currentChartDB.Internal.DLCounter
chartDB.Header.LastChanged = time.Now()
// and now store it
if _, err := datastore.Put(ctx, key, chartDB); err != nil {
commonResponseErrorProcessing (response, err)
return
}
// Response is Empty for 204
response.WriteHeaderAndEntity(http.StatusNoContent, "")
}
func getGChartHeader(request *restful.Request, response *restful.Response) {
ctx := appengine.NewContext(request.Request)
var date time.Time
var err error
var dateString string
if dateString = request.QueryParameter("dateFrom"); dateString != "" {
date, err = time.Parse(time.RFC3339, dateString)
if err != nil {
addPlainTextError(response, http.StatusBadRequest, fmt.Sprint(err.Error(), " - Correct format is RFC3339"))
return
}
} else {
date = time.Time{}
}
const maxNumberOfHeadersPerCall = 200; // this has to be equal to GoldenCheetah - CloudDBChartClient class
q := datastore.NewQuery(gChartDBEntity).Filter("Header.LastChanged >=", date).Order("Header.LastChanged").Limit(maxNumberOfHeadersPerCall)
var chartHeaderList GChartAPIv1HeaderOnlyList
var chartsOnDBList []GChartEntityHeaderOnly
k, err := q.GetAll(ctx, &chartsOnDBList)
if err != nil && !isErrFieldMismatch(err) {
commonResponseErrorProcessing (response, err)
return
}
// DB Entity needs to be mapped back
for i, chartDB := range chartsOnDBList {
var chart GChartAPIv1HeaderOnly
mapDBtoAPICommonHeader(&chartDB.Header, &chart.Header)
chart.Header.Id = k[i].IntID()
chart.ChartSport = chartDB.ChartSport
chart.ChartView = chartDB.ChartView
chart.ChartType = chartDB.ChartType
chartHeaderList = append(chartHeaderList, chart)
}
// write Info Log
log.Infof(ctx, "GetHeader from: %s", dateString )
response.WriteHeaderAndEntity(http.StatusOK, chartHeaderList)
}
func getGChartHeaderCount(request *restful.Request, response *restful.Response) {
ctx := appengine.NewContext(request.Request)
var date time.Time
var err error
if dateString := request.QueryParameter("dateFrom"); dateString != "" {
date, err = time.Parse(time.RFC3339, dateString)
if err != nil {
addPlainTextError(response, http.StatusBadRequest, fmt.Sprint(err.Error(), " - Correct format is RFC3339"))
return
}
} else {
date = time.Time{}
}
q := datastore.NewQuery(gChartDBEntity).Filter("Header.LastChanged >=", date).Order("-Header.LastChanged")
counter, _ := q.Count(ctx)
response.WriteHeaderAndEntity(http.StatusOK, counter)
}
func getGChartById(request *restful.Request, response *restful.Response) {
ctx := appengine.NewContext(request.Request)
id := request.PathParameter("id")
i, err := strconv.ParseInt(id, 10, 64)
if err != nil {
commonResponseErrorProcessing (response, err)
return
}
key := datastore.NewKey(ctx, gChartDBEntity, "", i, gchartEntityRootKey(ctx))
chartDB := new(GChartEntity)
err = datastore.Get(ctx, key, chartDB)
if err != nil && !isErrFieldMismatch(err) {
commonResponseErrorProcessing (response, err)
return
}
// now map and respond
chart := new(GChartGetAPIv1)
mapDBtoAPIGChart(chartDB, chart)
chart.Header.Id = key.IntID()
response.WriteHeaderAndEntity(http.StatusOK, chart)
}
func deleteGChartById(request *restful.Request, response *restful.Response) {
changeGChartById(request, response, true, false, true)
}
func incrementGChartUsageById(request *restful.Request, response *restful.Response) {
ctx := appengine.NewContext(request.Request)
id := request.PathParameter("id")
i, err := strconv.ParseInt(id, 10, 64)
if err != nil {
commonResponseErrorProcessing (response, err)
return
}
key := datastore.NewKey(ctx, gChartDBEntity, "", i, gchartEntityRootKey(ctx))
chartDB := new(GChartEntity)
err = datastore.Get(ctx, key, chartDB)
if err != nil && !isErrFieldMismatch(err) {
commonResponseErrorProcessing (response, err)
return
}
// update the download counter but ignore any errors on writing
chartDB.Internal.DLCounter += 1
datastore.Put(ctx, key, chartDB)
response.WriteHeaderAndEntity(http.StatusNoContent, "")
}
func curateGChartById(request *restful.Request, response *restful.Response) {
newStatusString := request.QueryParameter("newStatus")
b, err := strconv.ParseBool(newStatusString)
if err != nil {
commonResponseErrorProcessing (response, err)
return
}
changeGChartById(request, response, false, true, b)
}
// ------------------- supporting functions ------------------------------------------------
func changeGChartById(request *restful.Request, response *restful.Response, changeDeleted bool, changeCurated bool, newStatus bool) {
ctx := appengine.NewContext(request.Request)
id := request.PathParameter("id")
i, err := strconv.ParseInt(id, 10, 64)
if err != nil {
addPlainTextError(response, http.StatusBadRequest, err.Error())
return
}
key := datastore.NewKey(ctx, gChartDBEntity, "", i, gchartEntityRootKey(ctx))
chartDB := new(GChartEntity)
err = datastore.Get(ctx, key, chartDB)
if err != nil && !isErrFieldMismatch(err) {
commonResponseErrorProcessing (response, err)
return
}
// now update like requested
if changeDeleted {
chartDB.Header.Deleted = newStatus
if newStatus {
chartDB.ChartType = ""
chartDB.ChartView = ""
chartDB.ChartDef = ""
chartDB.Image = nil
}
chartDB.Header.LastChanged = time.Now()
}
if changeCurated {
chartDB.Header.Curated = newStatus
chartDB.Header.LastChanged = time.Now()
}
if _, err := datastore.Put(ctx, key, chartDB); err != nil {
if appengine.IsOverQuota(err) {
// return 503 and a text similar to what GAE is returning as well
addPlainTextError(response, http.StatusServiceUnavailable, "503 - Over Quota")
} else {
addPlainTextError(response, http.StatusInternalServerError, err.Error())
}
return
}
// Response is Empty for 204
response.WriteHeaderAndEntity(http.StatusNoContent, "")
}