-
Notifications
You must be signed in to change notification settings - Fork 40
/
vis.go
772 lines (690 loc) · 22.1 KB
/
vis.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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
com "github.com/sqlitebrowser/dbhub.io/common"
"github.com/sqlitebrowser/dbhub.io/common/config"
"github.com/sqlitebrowser/dbhub.io/common/database"
)
func visualisePage(w http.ResponseWriter, r *http.Request) {
var pageData struct {
DB database.SQLiteDBinfo
PageMeta PageMetaInfo
Branches map[string]database.BranchEntry
Visualisations map[string]database.VisParamsV2
}
// Get all meta information
errCode, err := collectPageMetaInfo(w, r, &pageData.PageMeta)
if err != nil {
errorPage(w, r, errCode, err.Error())
return
}
dbName, err := getDatabaseName(r)
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
// Check if a specific database commit ID was given
commitID, err := com.GetFormCommit(r)
if err != nil {
errorPage(w, r, http.StatusBadRequest, "Invalid database commit ID")
return
}
// Check if a branch name was requested
branchName, err := com.GetFormBranch(r)
if err != nil {
errorPage(w, r, http.StatusBadRequest, "Validation failed for branch name")
return
}
// Check if a named tag was requested
tagName, err := com.GetFormTag(r)
if err != nil {
errorPage(w, r, http.StatusBadRequest, "Validation failed for tag name")
return
}
// Check if a specific release was requested
releaseName := r.FormValue("release")
if releaseName != "" {
err = com.ValidateBranchName(releaseName)
if err != nil {
errorPage(w, r, http.StatusBadRequest, "Validation failed for release name")
return
}
}
// Check if the database exists and the user has access to view it
exists, err := database.CheckDBPermissions(pageData.PageMeta.LoggedInUser, dbName.Owner, dbName.Database, false)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
if !exists {
errorPage(w, r, http.StatusNotFound, fmt.Sprintf("Database '%s%s%s' doesn't exist", dbName.Owner, "/",
dbName.Database))
return
}
// * Execution can only get here if the user has access to the requested database *
// Check if this is a live database
isLive, liveNode, err := database.CheckDBLive(dbName.Owner, dbName.Database)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
// Live databases are handled differently to standard ones
if !isLive {
// If a specific commit was requested, make sure it exists in the database commit history
if commitID != "" {
commitList, err := database.GetCommitList(dbName.Owner, dbName.Database)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
if _, ok := commitList[commitID]; !ok {
// The requested commit isn't one in the database commit history so error out
errorPage(w, r, http.StatusNotFound, fmt.Sprintf("Unknown commit for database '%s/%s'", dbName.Owner,
dbName.Database))
return
}
}
// If a specific release was requested, and no commit ID was given, retrieve the commit ID matching the release
if commitID == "" && releaseName != "" {
releases, err := database.GetReleases(dbName.Owner, dbName.Database)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, "Couldn't retrieve releases for database")
return
}
rls, ok := releases[releaseName]
if !ok {
errorPage(w, r, http.StatusInternalServerError, "Unknown release requested for this database")
return
}
commitID = rls.Commit
}
// Read the branch heads list from the database
pageData.Branches, err = database.GetBranches(dbName.Owner, dbName.Database)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
// If a specific branch was requested and no commit ID was given, use the latest commit for the branch
if commitID == "" && branchName != "" {
c, ok := pageData.Branches[branchName]
if !ok {
errorPage(w, r, http.StatusInternalServerError, "Unknown branch requested for this database")
return
}
commitID = c.Commit
}
// If a specific tag was requested, and no commit ID was given, retrieve the commit ID matching the tag
if commitID == "" && tagName != "" {
tags, err := database.GetTags(dbName.Owner, dbName.Database)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, "Couldn't retrieve tags for database")
return
}
tg, ok := tags[tagName]
if !ok {
errorPage(w, r, http.StatusInternalServerError, "Unknown tag requested for this database")
return
}
commitID = tg.Commit
}
// If we still haven't determined the required commit ID, use the head commit of the default branch
if commitID == "" {
commitID, err = database.DefaultCommit(dbName.Owner, dbName.Database)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
}
// Retrieve default branch name details
if branchName == "" {
branchName, err = database.GetDefaultBranchName(dbName.Owner, dbName.Database)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, "Error retrieving default branch name")
return
}
}
pageData.DB.Info.Branch = branchName
}
// Retrieve the database details
err = database.DBDetails(&pageData.DB, pageData.PageMeta.LoggedInUser, dbName.Owner, dbName.Database, commitID)
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
// Get a list of all saved visualisations for this database
pageData.Visualisations, err = database.GetVisualisations(dbName.Owner, dbName.Database)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
// For live databases, we ask the job queue backend for its file size
if isLive {
pageData.DB.Info.DBEntry.Size, err = com.LiveSize(liveNode, pageData.PageMeta.LoggedInUser, dbName.Owner, dbName.Database)
if err != nil {
log.Println(err)
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
}
// Fill out various metadata fields
pageData.PageMeta.Title = fmt.Sprintf("Visualisations - %s %s %s", dbName.Owner, "/", dbName.Database)
pageData.PageMeta.PageSection = "db_vis"
// Render the visualisation page
t := tmpl.Lookup("visualisePage")
err = t.Execute(w, pageData)
if err != nil {
log.Printf("Error: %s", err)
}
}
// This function handles requests to delete a saved database visualisation
func visDel(w http.ResponseWriter, r *http.Request) {
// Retrieve user, database
dbOwner, dbName, err := com.GetOD(2, r) // 2 = Ignore "/x/visdel/" at the start of the URL
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, "Required information is missing")
return
}
// Retrieve session data (if any)
var loggedInUser string
var u interface{}
validSession := false
if config.Conf.Environment.Environment == "production" {
sess, err := store.Get(r, "dbhub-user")
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
u = sess.Values["UserName"]
} else {
u = config.Conf.Environment.UserOverride
}
if u != nil {
loggedInUser = u.(string)
validSession = true
}
// Ensure we have a valid logged in user
if validSession != true {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprint(w, "You need to be logged in")
return
}
// Make sure the logged in user has the permissions to proceed
allowed, err := database.CheckDBPermissions(loggedInUser, dbOwner, dbName, true)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, err)
return
}
if allowed == false {
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, "Database not found")
return
}
// Initial sanity check of the visualisation name
visName := r.FormValue("visname")
err = com.ValidateVisualisationName(visName)
if err != nil {
log.Printf("Input validation error for visDel(): %s", err)
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "Error when validating input: %s", err)
return
}
// Delete the saved visualisation for this database
err = database.VisualisationDeleteParams(dbOwner, dbName, visName)
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, err)
return
}
// Deletion succeeded
w.WriteHeader(http.StatusOK)
}
func visEmbedPage(w http.ResponseWriter, r *http.Request) {
var pageData struct {
DB database.SQLiteDBinfo
PageMeta PageMetaInfo
Branches map[string]database.BranchEntry
Visualisation database.VisParamsV2
VisName string
}
// Get all meta information
errCode, err := collectPageMetaInfo(w, r, &pageData.PageMeta)
if err != nil {
errorPage(w, r, errCode, err.Error())
return
}
dbName, err := getDatabaseName(r)
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
// Check if a specific database commit ID was given
commitID, err := com.GetFormCommit(r)
if err != nil {
errorPage(w, r, http.StatusBadRequest, "Invalid database commit ID")
return
}
// Check if a branch name was requested
branchName, err := com.GetFormBranch(r)
if err != nil {
errorPage(w, r, http.StatusBadRequest, "Validation failed for branch name")
return
}
// Check if a named tag was requested
tagName, err := com.GetFormTag(r)
if err != nil {
errorPage(w, r, http.StatusBadRequest, "Validation failed for tag name")
return
}
// Check if a specific release was requested
releaseName := r.FormValue("release")
if releaseName != "" {
err = com.ValidateBranchName(releaseName)
if err != nil {
errorPage(w, r, http.StatusBadRequest, "Validation failed for release name")
return
}
}
// Check if the database exists and the user has access to view it
exists, err := database.CheckDBPermissions(pageData.PageMeta.LoggedInUser, dbName.Owner, dbName.Database, false)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
if !exists {
errorPage(w, r, http.StatusNotFound, fmt.Sprintf("Database '%s%s%s' doesn't exist", dbName.Owner, "/",
dbName.Database))
return
}
// * Execution can only get here if the user has access to the requested database *
// Check if this is a live database
isLive, _, err := database.CheckDBLive(dbName.Owner, dbName.Database)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
// Live databases are handled differently to standard ones
if !isLive {
// If a specific commit was requested, make sure it exists in the database commit history
if commitID != "" {
commitList, err := database.GetCommitList(dbName.Owner, dbName.Database)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
if _, ok := commitList[commitID]; !ok {
// The requested commit isn't one in the database commit history so error out
errorPage(w, r, http.StatusNotFound, fmt.Sprintf("Unknown commit for database '%s/%s'", dbName.Owner,
dbName.Database))
return
}
}
// If a specific release was requested, and no commit ID was given, retrieve the commit ID matching the release
if commitID == "" && releaseName != "" {
releases, err := database.GetReleases(dbName.Owner, dbName.Database)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, "Couldn't retrieve releases for database")
return
}
rls, ok := releases[releaseName]
if !ok {
errorPage(w, r, http.StatusInternalServerError, "Unknown release requested for this database")
return
}
commitID = rls.Commit
}
// Read the branch heads list from the database
pageData.Branches, err = database.GetBranches(dbName.Owner, dbName.Database)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
// If a specific branch was requested and no commit ID was given, use the latest commit for the branch
if commitID == "" && branchName != "" {
c, ok := pageData.Branches[branchName]
if !ok {
errorPage(w, r, http.StatusInternalServerError, "Unknown branch requested for this database")
return
}
commitID = c.Commit
}
// If a specific tag was requested, and no commit ID was given, retrieve the commit ID matching the tag
if commitID == "" && tagName != "" {
tags, err := database.GetTags(dbName.Owner, dbName.Database)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, "Couldn't retrieve tags for database")
return
}
tg, ok := tags[tagName]
if !ok {
errorPage(w, r, http.StatusInternalServerError, "Unknown tag requested for this database")
return
}
commitID = tg.Commit
}
// If we still haven't determined the required commit ID, use the head commit of the default branch
if commitID == "" {
commitID, err = database.DefaultCommit(dbName.Owner, dbName.Database)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
}
// Retrieve default branch name details
if branchName == "" {
branchName, err = database.GetDefaultBranchName(dbName.Owner, dbName.Database)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, "Error retrieving default branch name")
return
}
}
pageData.DB.Info.Branch = branchName
}
// Retrieve the database details
err = database.DBDetails(&pageData.DB, pageData.PageMeta.LoggedInUser, dbName.Owner, dbName.Database, commitID)
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
// Initial sanity check of the visualisation name
pageData.VisName = r.FormValue("visname")
err = com.ValidateVisualisationName(pageData.VisName)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
// Get a list of all saved visualisations for this database
visualisations, err := database.GetVisualisations(dbName.Owner, dbName.Database)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
// Get visualisation data
var ok bool
pageData.Visualisation, ok = visualisations[pageData.VisName]
if ok == false {
errorPage(w, r, http.StatusNotFound, "visualisation not found")
return
}
// Page title
pageData.PageMeta.Title = fmt.Sprintf("Visualisation %s - %s %s %s", pageData.VisName, dbName.Owner, "/", dbName.Database)
// Render the visualisation page
t := tmpl.Lookup("visembedPage")
err = t.Execute(w, pageData)
if err != nil {
log.Printf("Error: %s", err)
}
}
// visExecuteSQL executes a custom SQLite SELECT query.
func visExecuteSQL(w http.ResponseWriter, r *http.Request) {
// Retrieve session data (if any)
var loggedInUser string
var u interface{}
if config.Conf.Environment.Environment == "production" {
sess, err := store.Get(r, "dbhub-user")
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
u = sess.Values["UserName"]
} else {
u = config.Conf.Environment.UserOverride
}
if u != nil {
loggedInUser = u.(string)
}
// Retrieve user, database, and commit ID
dbOwner, dbName, commitID, err := com.GetODC(2, r) // 2 = Ignore "/x/execsql/" at the start of the URL
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
// Grab the incoming SQLite query
bodyData, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, err)
return
}
var reqData ExecuteSqlRequest
err = json.Unmarshal([]byte(bodyData), &reqData)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, err)
return
}
decodedStr, err := com.CheckUnicode(reqData.Sql, false)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, err)
return
}
// Check if the requested database exists
exists, err := database.CheckDBPermissions(loggedInUser, dbOwner, dbName, false)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, err)
return
}
if !exists {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "Database '%s/%s' doesn't exist", dbOwner, dbName)
return
}
// Check if this is a live database
isLive, liveNode, err := database.CheckDBLive(dbOwner, dbName)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, err)
return
}
// Run the visualisation query
var data com.SQLiteRecordSet
if !isLive {
data, err = com.SQLiteRunQueryDefensive(w, r, com.QuerySourceVisualisation, dbOwner, dbName, commitID, loggedInUser, decodedStr)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, err)
return
}
} else {
// Send the query to the appropriate backend live node
data, err = com.LiveQuery(liveNode, loggedInUser, dbOwner, dbName, decodedStr)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, err.Error())
return
}
}
// Return the results as JSON
jsonResponse, err := json.Marshal(data)
if err != nil {
log.Println(err)
return
}
fmt.Fprintf(w, "%s", jsonResponse)
}
// This function handles requests to rename an existing saved visualisation
func visRename(w http.ResponseWriter, r *http.Request) {
// Retrieve user and database
dbOwner, dbName, err := com.GetOD(2, r) // 2 = Ignore "/x/visrename/" at the start of the URL
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
// Retrieve session data (if any)
var loggedInUser string
var u interface{}
validSession := false
if config.Conf.Environment.Environment == "production" {
sess, err := store.Get(r, "dbhub-user")
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
u = sess.Values["UserName"]
} else {
u = config.Conf.Environment.UserOverride
}
if u != nil {
loggedInUser = u.(string)
validSession = true
}
// Ensure we have a valid logged in user
if validSession != true {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprint(w, "You need to be logged in")
return
}
// Make sure the logged in user has the permissions to proceed
allowed, err := database.CheckDBPermissions(loggedInUser, dbOwner, dbName, true)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, err)
return
}
if allowed == false {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "%s", "Database not found")
return
}
// Initial sanity check of the visualisation name
visName := r.FormValue("visname")
err = com.ValidateVisualisationName(visName)
if err != nil {
log.Printf("Input validation error for visRename(): %s", err)
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "Error when validating input: %s", err)
return
}
// Initial sanity check of the new visualisation name
visNewName := r.FormValue("visnewname")
err = com.ValidateVisualisationName(visNewName)
if err != nil {
log.Printf("Input validation error for visRename(): %s", err)
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "Error when validating input: %s", err)
return
}
// Perform rename
err = database.VisualisationRename(dbOwner, dbName, visName, visNewName)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
}
// This function handles requests to save the database visualisation parameters
func visSave(w http.ResponseWriter, r *http.Request) {
// Retrieve user, database, and commit ID
dbOwner, dbName, commitID, err := com.GetODC(2, r) // 2 = Ignore "/x/vissave/" at the start of the URL
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
// Retrieve session data (if any)
var loggedInUser string
var u interface{}
validSession := false
if config.Conf.Environment.Environment == "production" {
sess, err := store.Get(r, "dbhub-user")
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
u = sess.Values["UserName"]
} else {
u = config.Conf.Environment.UserOverride
}
if u != nil {
loggedInUser = u.(string)
validSession = true
}
// Ensure we have a valid logged in user
if validSession != true {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprint(w, "You need to be logged in")
return
}
// Make sure the logged in user has the permissions to proceed
allowed, err := database.CheckDBPermissions(loggedInUser, dbOwner, dbName, true)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, err)
return
}
if allowed == false {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "%s", "Database not found")
return
}
// Initial sanity check of the visualisation name
visName := r.FormValue("visname")
err = com.ValidateVisualisationName(visName)
if err != nil {
log.Printf("Input validation error for visSave(): %s", err)
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "Error when validating input: %s", err)
return
}
// Grab the incoming visualisation object
bodyData, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, err)
return
}
var data database.VisParamsV2
err = json.Unmarshal([]byte(bodyData), &data)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, err)
return
}
// Ensure minimum viable parameters are present
if data.ChartType == "" || data.XAXisColumn == "" || data.YAXisColumn == "" || visName == "" || data.SQL == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
// Ensure only valid chart types are accepted
if data.ChartType != "hbc" && data.ChartType != "vbc" && data.ChartType != "lc" && data.ChartType != "pie" {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, "Unknown chart type")
return
}
// Validate the X axis field name
err = com.ValidateFieldName(data.XAXisColumn)
if err != nil {
log.Printf("Validation failed on requested X axis field name '%v': %v", com.SanitiseLogString(data.XAXisColumn), err.Error())
w.WriteHeader(http.StatusBadRequest)
return
}
// Validate the Y axis field name
err = com.ValidateFieldName(data.YAXisColumn)
if err != nil {
log.Printf("Validation failed on requested Y axis field name '%v': %v", com.SanitiseLogString(data.YAXisColumn), err.Error())
w.WriteHeader(http.StatusBadRequest)
return
}
// Validate SQL string
_, err = com.CheckUnicode(data.SQL, false)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
// Save the SQLite visualisation parameters
err = database.VisualisationSaveParams(dbOwner, dbName, visName, data)
if err != nil {
log.Printf("Error occurred when saving visualisation '%s' for' '%s/%s', commit '%s': %s", com.SanitiseLogString(visName),
com.SanitiseLogString(dbOwner), com.SanitiseLogString(dbName), com.SanitiseLogString(commitID), err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
// Save succeeded
w.WriteHeader(http.StatusOK)
}