|
4 | 4 | "fmt"
|
5 | 5 | "regexp"
|
6 | 6 | "strconv"
|
7 |
| - "strings" |
8 | 7 | "time"
|
9 | 8 |
|
10 | 9 | "github.com/grafana/grafana-plugin-sdk-go/backend"
|
@@ -154,15 +153,37 @@ func GetIntervalFrom(dsInterval, queryInterval string, queryIntervalMS int64, de
|
154 | 153 | // ParseIntervalStringToTimeDuration converts a string representation of a expected (i.e. 1m30s) to time.Duration
|
155 | 154 | // this method copied from grafana/grafana/pkg/tsdb/intervalv2.go
|
156 | 155 | func ParseIntervalStringToTimeDuration(interval string) (time.Duration, error) {
|
157 |
| - formattedInterval := strings.Replace(strings.Replace(interval, "<", "", 1), ">", "", 1) |
158 |
| - isPureNum, err := regexp.MatchString(`^\d+$`, formattedInterval) |
159 |
| - if err != nil { |
160 |
| - return time.Duration(0), err |
| 156 | + if len(interval) == 0 { |
| 157 | + return 0, backend.DownstreamError(fmt.Errorf("invalid interval")) |
| 158 | + } |
| 159 | + |
| 160 | + // extract the interval if it is inside brackets i.e. <10m> |
| 161 | + if interval[0] == '<' { |
| 162 | + interval = interval[1:] |
| 163 | + } |
| 164 | + if len(interval) > 0 && interval[len(interval)-1] == '>' { |
| 165 | + interval = interval[:len(interval)-1] |
161 | 166 | }
|
| 167 | + |
| 168 | + // Check if string contains only digits |
| 169 | + isPureNum := true |
| 170 | + for _, c := range interval { |
| 171 | + if c < '0' || c > '9' { |
| 172 | + isPureNum = false |
| 173 | + break |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + // if it is number than return it immediately |
162 | 178 | if isPureNum {
|
163 |
| - formattedInterval += "s" |
| 179 | + num, err := strconv.ParseInt(interval, 10, 64) |
| 180 | + if err != nil { |
| 181 | + return 0, err |
| 182 | + } |
| 183 | + return time.Duration(num) * time.Second, nil |
164 | 184 | }
|
165 |
| - parsedInterval, err := ParseDuration(formattedInterval) |
| 185 | + |
| 186 | + parsedInterval, err := ParseDuration(interval) |
166 | 187 | if err != nil {
|
167 | 188 | return time.Duration(0), err
|
168 | 189 | }
|
|
0 commit comments