|
|
@@ -2,17 +2,17 @@ package kubecost
|
|
|
|
|
|
import (
|
|
|
"bytes"
|
|
|
+ "encoding/json"
|
|
|
"fmt"
|
|
|
- "github.com/opencost/opencost/pkg/log"
|
|
|
"math"
|
|
|
"regexp"
|
|
|
"strconv"
|
|
|
"time"
|
|
|
|
|
|
- "github.com/opencost/opencost/pkg/util/timeutil"
|
|
|
-
|
|
|
"github.com/opencost/opencost/pkg/env"
|
|
|
+ "github.com/opencost/opencost/pkg/log"
|
|
|
"github.com/opencost/opencost/pkg/thanos"
|
|
|
+ "github.com/opencost/opencost/pkg/util/timeutil"
|
|
|
)
|
|
|
|
|
|
const (
|
|
|
@@ -479,7 +479,6 @@ func (w Window) IsOpen() bool {
|
|
|
return w.start == nil || w.end == nil
|
|
|
}
|
|
|
|
|
|
-// TODO:CLEANUP make this unmarshalable (make Start and End public)
|
|
|
func (w Window) MarshalJSON() ([]byte, error) {
|
|
|
buffer := bytes.NewBufferString("{")
|
|
|
if w.start != nil {
|
|
|
@@ -496,6 +495,42 @@ func (w Window) MarshalJSON() ([]byte, error) {
|
|
|
return buffer.Bytes(), nil
|
|
|
}
|
|
|
|
|
|
+func (w *Window) UnmarshalJSON(bs []byte) error {
|
|
|
+ // Due to the behavior of our custom MarshalJSON, we unmarshal as strings
|
|
|
+ // and then manually handle the weird quoted "null" case.
|
|
|
+ type PubWindow struct {
|
|
|
+ Start string `json:"start"`
|
|
|
+ End string `json:"end"`
|
|
|
+ }
|
|
|
+ var pw PubWindow
|
|
|
+ err := json.Unmarshal(bs, &pw)
|
|
|
+ if err != nil {
|
|
|
+ return fmt.Errorf("half unmarshal: %w", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ var start *time.Time
|
|
|
+ var end *time.Time
|
|
|
+
|
|
|
+ if pw.Start != "null" {
|
|
|
+ t, err := time.Parse(time.RFC3339, pw.Start)
|
|
|
+ if err != nil {
|
|
|
+ return fmt.Errorf("parsing start as RFC3339: %w", err)
|
|
|
+ }
|
|
|
+ start = &t
|
|
|
+ }
|
|
|
+ if pw.End != "null" {
|
|
|
+ t, err := time.Parse(time.RFC3339, pw.End)
|
|
|
+ if err != nil {
|
|
|
+ return fmt.Errorf("parsing end as RFC3339: %w", err)
|
|
|
+ }
|
|
|
+ end = &t
|
|
|
+ }
|
|
|
+
|
|
|
+ w.start = start
|
|
|
+ w.end = end
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
func (w Window) Minutes() float64 {
|
|
|
if w.IsOpen() {
|
|
|
return math.Inf(1)
|