-
Notifications
You must be signed in to change notification settings - Fork 1
/
tsquery.go
113 lines (95 loc) · 2.85 KB
/
tsquery.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
// Copyright 2021 The Self-host Authors. All rights reserved.
// Use of this source code is governed by the GPLv3
// license that can be found in the LICENSE file.
package aapije
import (
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/self-host/self-host/api/aapije/rest"
ie "github.com/self-host/self-host/internal/errors"
"github.com/self-host/self-host/internal/services"
"github.com/self-host/self-host/pkg/util"
)
// FindTsdataByQuery query multiple time series for data
func (ra *RestApi) FindTsdataByQuery(w http.ResponseWriter, r *http.Request, p rest.FindTsdataByQueryParams) {
timezone := "UTC"
aggregate := "avg"
precision := "microseconds"
if time.Time(p.End).Sub(time.Time(p.Start)) > 31622401*time.Second {
// FIXME: these errors should be declared in one location
ie.SendHTTPError(w, ie.NewBadRequestError(fmt.Errorf("start to end range exceeds limit")))
return
}
if p.Timezone != nil {
timezone = string(*p.Timezone)
}
if p.Aggregate != nil {
aggregate = string(*p.Aggregate)
}
if p.Precision != nil {
precision = string(*p.Precision)
}
db, err := ra.GetDB(r)
if err != nil {
ie.SendHTTPError(w, ie.ErrorUndefined)
return
}
domaintoken, ok := r.Context().Value("domaintoken").(*services.DomainToken)
if ok == false {
ie.SendHTTPError(w, ie.ErrorUndefined)
return
}
svc := services.NewTimeseriesService(db)
policySvc := services.NewPolicyCheckService(db)
uuids, err := util.StringSliceToUuidSlice([]string(p.Uuids))
if err != nil {
ie.SendHTTPError(w, ie.NewBadRequestError(fmt.Errorf("uuids has invalid format")))
return
}
// Ensure all timeseries exists
for _, id := range uuids {
ok, err := svc.Exists(r.Context(), id)
if err != nil {
ie.SendHTTPError(w, ie.ParseDBError(err))
return
} else if ok == false {
ie.SendHTTPError(w, ie.ErrorNotFound)
return
}
}
// Generate check rules for access control
resources := make([]string, 0)
for _, id := range uuids {
resources = append(resources, fmt.Sprintf("timeseries/%v/data", id.String()))
}
// Ensure that the User has access to all requested items
ok, err = policySvc.UserHasManyAccessViaToken(r.Context(), []byte(domaintoken.Token), "read", resources)
if err != nil {
ie.SendHTTPError(w, ie.ParseDBError(err))
return
} else if ok == false {
// Access denied to one or more requested resources
ie.SendHTTPError(w, ie.ErrorForbidden)
return
}
params := services.QueryMultiSourceDataParams{
Uuids: uuids,
Start: time.Time(p.Start),
End: time.Time(p.End),
GreaterOrEq: (*float32)(p.Ge),
LessOrEq: (*float32)(p.Le),
Aggregate: aggregate,
Precision: precision,
Timezone: timezone,
}
data, err := svc.QueryMultiSourceData(r.Context(), params)
if err != nil {
ie.SendHTTPError(w, ie.ParseDBError(err))
return
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(data)
return
}