Skip to content

Commit

Permalink
ci-health, plot: adds support for --start-date flag
Browse files Browse the repository at this point in the history
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 <dhiller@redhat.com>
  • Loading branch information
dhiller committed Apr 15, 2024
1 parent dc86e11 commit 499da3f
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions pkg/runner/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path"
"path/filepath"
"regexp"
"time"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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++ {
Expand All @@ -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
}
Expand All @@ -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)

Expand Down

0 comments on commit 499da3f

Please sign in to comment.