-
Notifications
You must be signed in to change notification settings - Fork 177
/
handler_rollup.go
131 lines (109 loc) · 3.58 KB
/
handler_rollup.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
/*
* Copyright 2018 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package irondb
import (
"context"
"net/http"
"time"
"github.com/tricksterproxy/trickster/pkg/proxy/engines"
"github.com/tricksterproxy/trickster/pkg/proxy/errors"
"github.com/tricksterproxy/trickster/pkg/proxy/params"
"github.com/tricksterproxy/trickster/pkg/proxy/request"
"github.com/tricksterproxy/trickster/pkg/proxy/urls"
"github.com/tricksterproxy/trickster/pkg/timeseries"
)
// RollupHandler handles requests for numeric timeseries data with specified
// spans and processes them through the delta proxy cache.
func (c *Client) RollupHandler(w http.ResponseWriter, r *http.Request) {
r.URL = urls.BuildUpstreamURL(r, c.baseUpstreamURL)
engines.DeltaProxyCacheRequest(w, r)
}
// rollupHandlerSetExtent will change the upstream request query to use the
// provided Extent.
func (c Client) rollupHandlerSetExtent(r *http.Request,
trq *timeseries.TimeRangeQuery,
extent *timeseries.Extent) {
if r == nil || extent == nil || (extent.Start.IsZero() && extent.End.IsZero()) {
return
}
var err error
if trq == nil {
if trq, err = c.ParseTimeRangeQuery(r); err != nil {
return
}
}
st := extent.Start.UnixNano() - (extent.Start.UnixNano() % int64(trq.Step))
et := extent.End.UnixNano() - (extent.End.UnixNano() % int64(trq.Step))
if st == et {
et += int64(trq.Step)
}
q := r.URL.Query()
q.Set(upStart, formatTimestamp(time.Unix(0, st), true))
q.Set(upEnd, formatTimestamp(time.Unix(0, et), true))
r.URL.RawQuery = q.Encode()
}
// rollupHandlerParseTimeRangeQuery parses the key parts of a TimeRangeQuery
// from the inbound HTTP Request.
func (c *Client) rollupHandlerParseTimeRangeQuery(
r *http.Request) (*timeseries.TimeRangeQuery, error) {
trq := ×eries.TimeRangeQuery{}
trq.Statement = r.URL.Path
qp := r.URL.Query()
var err error
p := ""
if p = qp.Get(upStart); p == "" {
return nil, errors.MissingURLParam(upStart)
}
if trq.Extent.Start, err = parseTimestamp(p); err != nil {
return nil, err
}
if p = qp.Get(upEnd); p == "" {
return nil, errors.MissingURLParam(upEnd)
}
if trq.Extent.End, err = parseTimestamp(p); err != nil {
return nil, err
}
if p = qp.Get(upSpan); p == "" {
return nil, errors.MissingURLParam(upSpan)
}
if trq.Step, err = parseDuration(p); err != nil {
return nil, err
}
return trq, nil
}
// rollupHandlerFastForwardURL returns the url to fetch the Fast Forward value
// based on a timerange URL.
func (c *Client) rollupHandlerFastForwardRequest(
r *http.Request) (*http.Request, error) {
rsc := request.GetResources(r)
trq := rsc.TimeRangeQuery
nr := r.Clone(context.Background())
v, _, _ := params.GetRequestValues(nr)
var err error
if trq == nil {
trq, err = c.ParseTimeRangeQuery(r)
if err != nil {
return nil, err
}
}
now := time.Now().Unix()
start := now - (now % int64(trq.Step.Seconds()))
end := start + int64(trq.Step.Seconds())
v.Set(upStart, formatTimestamp(time.Unix(start, 0), true))
v.Set(upEnd, formatTimestamp(time.Unix(end, 0), true))
params.SetRequestValues(nr, v)
return nr, nil
}