Skip to content

Commit

Permalink
Display cron for humans
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Nov 17, 2023
1 parent 34aeb51 commit 25b9074
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
11 changes: 8 additions & 3 deletions cmd/plugin/rpaasv2/cmd/autoscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io"
"strings"

"github.com/lnquy/cron"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli/v2"

Expand Down Expand Up @@ -250,7 +251,7 @@ func writeAutoscale(w io.Writer, autoscale *autogenerated.Autoscale) {
}

var schedules strings.Builder

exprDesc, _ := cron.NewDescriptor()
for i, s := range autoscale.Schedules {
if i > 0 {
fmt.Fprintln(&schedules)
Expand All @@ -259,8 +260,12 @@ func writeAutoscale(w io.Writer, autoscale *autogenerated.Autoscale) {

fmt.Fprintf(&schedules, "Window %d:\n", i+1)
fmt.Fprintln(&schedules, " Min replicas:", s.MinReplicas)
fmt.Fprintf(&schedules, " Start: %q\n", s.Start)
fmt.Fprintf(&schedules, " End: %q", s.End)

human, _ := exprDesc.ToDescription(s.Start, cron.Locale_en)
fmt.Fprintf(&schedules, " Start: %s (%s)\n", human, s.Start)

human, _ = exprDesc.ToDescription(s.Start, cron.Locale_en)
fmt.Fprintf(&schedules, " End: %s (%s)", human, s.End)
}

if text := schedules.String(); text != "" {
Expand Down
36 changes: 18 additions & 18 deletions cmd/plugin/rpaasv2/cmd/autoscale_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,24 +76,24 @@ max replicas: 5
}),
expected: `min replicas: 0
max replicas: 100
+-------------+--------------------------+
| Triggers | trigger details |
+-------------+--------------------------+
| Schedule(s) | Window 1: |
| | Min replicas: 1 |
| | Start: "00 08 * * 1-5" |
| | End: "00 20 * * 1-5" |
| | |
| | Window 2: |
| | Min replicas: 5 |
| | Start: "00 20 * * 2" |
| | End: "00 01 * * 3" |
| | |
| | Window 3: |
| | Min replicas: 5 |
| | Start: "00 22 * * 0" |
| | End: "00 02 * * 1" |
+-------------+--------------------------+
+-------------+-------------------------------------------------------------+
| Triggers | trigger details |
+-------------+-------------------------------------------------------------+
| Schedule(s) | Window 1: |
| | Min replicas: 1 |
| | Start: At 08:00 AM, Monday through Friday (00 08 * * 1-5) |
| | End: At 08:00 AM, Monday through Friday (00 20 * * 1-5) |
| | |
| | Window 2: |
| | Min replicas: 5 |
| | Start: At 08:00 PM, only on Tuesday (00 20 * * 2) |
| | End: At 08:00 PM, only on Tuesday (00 01 * * 3) |
| | |
| | Window 3: |
| | Min replicas: 5 |
| | Start: At 10:00 PM, only on Sunday (00 22 * * 0) |
| | End: At 10:00 PM, only on Sunday (00 02 * * 1) |
+-------------+-------------------------------------------------------------+
`,
},

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/labstack/gommon v0.4.0 // indirect
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
github.com/lnquy/cron v1.1.1 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,8 @@ github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de h1:9TO3cAIGXtEhnIaL+V+BEER86oLrvS+kWobKpbJuye0=
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de/go.mod h1:zAbeS9B/r2mtpb6U+EI2rYA5OAXxsYw6wTamcNW+zcE=
github.com/lithammer/dedent v1.1.0/go.mod h1:jrXYCQtgg0nJiN+StA2KgR7w6CiQNv9Fd/Z9BP0jIOc=
github.com/lnquy/cron v1.1.1 h1:iaDX1ublgQ9LBhA8l9BVU+FrTE1PPSPAuvAdhgdnXgA=
github.com/lnquy/cron v1.1.1/go.mod h1:hu2Y7H68/8oKk6T4+K4qdbopbnaP4rGltK3ylWiiDss=
github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
Expand Down

0 comments on commit 25b9074

Please sign in to comment.