-
Notifications
You must be signed in to change notification settings - Fork 0
/
tagDataHandlers.go
96 lines (84 loc) · 2.4 KB
/
tagDataHandlers.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
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func CreateTagData(context *gin.Context) {
partitionKey := context.Request.Header.Get("PartitionKey")
if len(partitionKey) != 36 {
context.JSON(http.StatusBadRequest, gin.H{
"error": "Expecting PartitionKey in the Header",
})
return
}
var dataIngest []DataIngestByTagName
if err := context.Bind(&dataIngest); err != nil {
context.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
var tagMap = make(map[string]*Tag)
for index := range dataIngest {
tagName := dataIngest[index].TagName
if _, ok := tagMap[tagName]; !ok {
tag, err := env.PgDb.GetTagByTagKey(&Key{
TagName: dataIngest[index].TagName,
PartitionKey: partitionKey,
})
if err != nil {
context.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if tag == nil {
context.JSON(http.StatusBadRequest, gin.H{"error": "Tag Not Found"})
return
}
tagMap[tagName] = tag
}
}
var allDataIngestById []*DataIngestById
for index := range dataIngest {
tagName := dataIngest[index].TagName
var dataIngestById = &DataIngestById{
SeriesId: tagMap[tagName].SeriesId,
Dt: dataIngest[index].Dt,
Val: dataIngest[index].Val,
}
allDataIngestById = append(allDataIngestById, dataIngestById)
}
err := env.PgDb.CreateTagData(allDataIngestById)
if err != nil {
context.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
}
func GetTagData(context *gin.Context) {
partitionKey := context.Request.Header.Get("PartitionKey")
if len(partitionKey) != 36 {
context.JSON(http.StatusBadRequest, gin.H{
"error": "Expecting PartitionKey in the Header",
})
return
}
var dataReqeust DataRequest
if err := context.Bind(&dataReqeust); err != nil {
context.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
tag, err := env.PgDb.GetTagByTagKey(&Key{
TagName: dataReqeust.TagName,
PartitionKey: partitionKey,
})
if err != nil {
context.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if tag == nil {
context.JSON(http.StatusBadRequest, gin.H{"error": "Tag Not Found"})
}
series, err := env.PgDb.GetTagDataByRange(tag.SeriesId, dataReqeust.Start, dataReqeust.End)
if err != nil {
context.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
context.JSON(http.StatusOK, series)
}