From 499da3fcae82e7a8fd76dd7daaf8925a3c8622b8 Mon Sep 17 00:00:00 2001 From: Daniel Hiller Date: Thu, 11 Jan 2024 18:27:23 +0100 Subject: [PATCH] ci-health, plot: adds support for --start-date flag Adds a filter function looking at the result file path, and comparing the date extracted to given start date. Also add parameter to signature to have it. Example: using newly introduced flag `start-data` as below ``` go run ./cmd/batch \ --gh-token /home/dhiller/.tokens/github/kubevirt-bot/oauth \ --path $(pwd)/output \ --mode plot \ --target-metric merged-prs \ --log-level debug \ --start-date $(date -d "- 3 months" -I) ``` would only plot the graph for last 3 months. Signed-off-by: Daniel Hiller --- pkg/runner/batch.go | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/pkg/runner/batch.go b/pkg/runner/batch.go index 23a0b24fe9..c616196bbd 100644 --- a/pkg/runner/batch.go +++ b/pkg/runner/batch.go @@ -8,6 +8,7 @@ import ( "os" "path" "path/filepath" + "regexp" "time" log "github.com/sirupsen/logrus" @@ -79,7 +80,7 @@ func batchPlotRun(o *types.Options) (*types.Results, error) { // read batch fetch results in batch data dir dataBase := batchDataPath(o.Path, o.Source, string(o.TargetMetric)) - curves, err := gatherPlotData(dataBase, types.Metric(o.TargetMetric)) + curves, err := gatherPlotData(dataBase, types.Metric(o.TargetMetric), o.StartDate) if err != nil { return nil, err } @@ -119,7 +120,7 @@ func batchPlotPath(base, source, metric string) string { ) } -func gatherPlotData(basePath string, metric types.Metric) ([]types.Curve, error) { +func gatherPlotData(basePath string, metric types.Metric, startDate string) ([]types.Curve, error) { totalCurves := 2 curves := make([]types.Curve, totalCurves) for i := 0; i < totalCurves; i++ { @@ -134,7 +135,29 @@ func gatherPlotData(basePath string, metric types.Metric) ([]types.Curve, error) metricName := metric.ResultsName() - err := filepath.Walk(basePath, func(entryPath string, info os.FileInfo, err error) error { + dateExtractRegex := regexp.MustCompile(`([0-9]{4}-[0-9]{2}-[0-9]{2})`) + startDateToCheck, err := time.Parse("2006-01-02", startDate) + if err != nil { + return nil, err + } + shouldFilterByStartDate := func(entryPath string) (bool, error) { + if startDate == "" { + return false, nil + } + if !dateExtractRegex.MatchString(entryPath) { + return false, nil + } + parsedDate, err := time.Parse("2006-01-02", dateExtractRegex.FindString(entryPath)) + if err != nil { + return false, err + } + if !parsedDate.Before(startDateToCheck) { + return false, nil + } + return true, nil + } + + err = filepath.Walk(basePath, func(entryPath string, info os.FileInfo, err error) error { if err != nil { return err } @@ -144,6 +167,13 @@ func gatherPlotData(basePath string, metric types.Metric) ([]types.Curve, error) if entryPath == basePath { return nil } + filter, err := shouldFilterByStartDate(entryPath) + if err != nil { + return err + } + if filter { + return nil + } dataFile := path.Join(entryPath, constants.JSONResultsFileName)