Răsfoiți Sursa

Merge pull request #1878 from r2k1/fix-panic-csv

Fix panic in CSV Export
Ajay Tripathy 3 ani în urmă
părinte
comite
46a5f57dc5
2 a modificat fișierele cu 7 adăugiri și 2 ștergeri
  1. 0 1
      pkg/cmd/costmodel/costmodel.go
  2. 7 1
      pkg/costmodel/allocation.go

+ 0 - 1
pkg/cmd/costmodel/costmodel.go

@@ -49,7 +49,6 @@ func Execute(opts *CostModelOpts) error {
 }
 
 func StartExportWorker(ctx context.Context, model costmodel.AllocationModel) {
-	// TODO: there should be a better way to load the configuration
 	exportPath := os.Getenv(env.ExportCSVFile)
 	if exportPath == "" {
 		log.Infof("%s is not set, skipping CSV exporter", env.ExportCSVFile)

+ 7 - 1
pkg/costmodel/allocation.go

@@ -290,11 +290,17 @@ func (cm *CostModel) DateRange() (time.Time, time.Time, error) {
 	if err != nil {
 		return time.Time{}, time.Time{}, fmt.Errorf("querying oldest sample: %w", err)
 	}
+	if len(resOldest) == 0 || len(resOldest[0].Values) == 0 {
+		return time.Time{}, time.Time{}, fmt.Errorf("querying oldest sample: no results")
+	}
 	oldest := time.Unix(int64(resOldest[0].Values[0].Value), 0)
 
 	resNewest, _, err := ctx.QuerySync(fmt.Sprintf(queryFmtNewestSample, "90d", "1h"))
 	if err != nil {
-		return time.Time{}, time.Time{}, fmt.Errorf("querying oldest sample: %w", err)
+		return time.Time{}, time.Time{}, fmt.Errorf("querying newest sample: %w", err)
+	}
+	if len(resNewest) == 0 || len(resNewest[0].Values) == 0 {
+		return time.Time{}, time.Time{}, fmt.Errorf("querying newest sample: no results")
 	}
 	newest := time.Unix(int64(resNewest[0].Values[0].Value), 0)