Skip to content

Commit

Permalink
Update messages with time functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
MDrakos committed Nov 15, 2024
1 parent b97540c commit ec67e95
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 18 deletions.
17 changes: 17 additions & 0 deletions cmd/state-svc/internal/messages/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"regexp"
"strings"
"text/template"
"time"

"github.com/ActiveState/cli/internal/multilog"
"github.com/ActiveState/cli/pkg/sysinfo"
Expand Down Expand Up @@ -54,6 +55,22 @@ func conditionFuncMap() template.FuncMap {
"contains": funk.Contains,
"hasSuffix": strings.HasSuffix,
"hasPrefix": strings.HasPrefix,
"dateBefore": func(date string) bool {
parsedDate, err := time.Parse(time.RFC3339, date)
if err != nil {
multilog.Error("Messages: Could not parse date: %s", err)
return false
}
return time.Now().Before(parsedDate)
},
"dateAfter": func(date string) bool {
parsedDate, err := time.Parse(time.RFC3339, date)
if err != nil {
multilog.Error("Messages: Could not parse date: %s", err)
return false
}
return time.Now().After(parsedDate)
},
"regexMatch": func(str, pattern string) bool {
rx, err := regexp.Compile(pattern)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/state-svc/internal/messages/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func New(cfg *config.Instance, auth *auth.Auth) (*Messages, error) {
return nil, errs.Wrap(err, "Could not parse state version")
}

poll := poller.New(1*time.Hour, func() (interface{}, error) {
poll := poller.New(10*time.Minute, func() (interface{}, error) {
defer func() {
panics.LogAndPanic(recover(), debug.Stack())
}()
Expand Down
74 changes: 58 additions & 16 deletions cmd/state-svc/internal/messages/messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,48 @@ func Test_check(t *testing.T) {
[]string{"A", "B", "C", "D"},
false,
},
{
"Date Condition before",
args{
params: &ConditionParams{},
messages: []*graph.MessageInfo{
{ID: "A", Condition: `dateBefore "2025-11-15T14:45:28Z"`},
{ID: "B", Condition: `dateBefore "2015-11-15T14:45:28Z"`},
},
lastReportMap: map[string]interface{}{},
baseTime: time.Now(),
},
[]string{"A"},
false,
},
{
"Date Condition after",
args{
params: &ConditionParams{},
messages: []*graph.MessageInfo{
{ID: "A", Condition: `dateAfter "2015-11-15T14:45:28Z"`},
{ID: "B", Condition: `dateAfter "2025-11-15T14:45:28Z"`},
},
lastReportMap: map[string]interface{}{},
baseTime: time.Now(),
},
[]string{"A"},
false,
},
{
"Condition Date Range",
args{
params: &ConditionParams{},
messages: []*graph.MessageInfo{
{ID: "A", Condition: `and (dateAfter "2015-11-15T14:45:28Z") (dateBefore "2025-11-15T14:45:28Z")`},
{ID: "B", Condition: `and (dateAfter "2025-11-15T14:45:28Z") (dateBefore "2035-11-15T14:45:28Z")`},
},
lastReportMap: map[string]interface{}{},
baseTime: time.Now(),
},
[]string{"A"},
false,
},
{
"Repeat Disabled",
args{
Expand All @@ -165,8 +207,8 @@ func Test_check(t *testing.T) {
{ID: "C", Repeat: graph.MessageRepeatTypeDisabled},
},
lastReportMap: map[string]interface{}{
"A": time.Now(),
"C": time.Now(),
"A": time.Now().Format(time.RFC3339),
"C": time.Now().Format(time.RFC3339),
},
baseTime: time.Now(),
},
Expand All @@ -183,8 +225,8 @@ func Test_check(t *testing.T) {
{ID: "C", Repeat: graph.MessageRepeatTypeConstantly},
},
lastReportMap: map[string]interface{}{
"A": time.Now(),
"C": time.Now().Add(-time.Hour * 24 * 30),
"A": time.Now().Format(time.RFC3339),
"C": time.Now().Add(-time.Hour * 24 * 30).Format(time.RFC3339),
},
baseTime: time.Now(),
},
Expand All @@ -201,9 +243,9 @@ func Test_check(t *testing.T) {
{ID: "C", Repeat: graph.MessageRepeatTypeHourly},
},
lastReportMap: map[string]interface{}{
"A": time.Now(),
"B": time.Now().Add(-time.Hour),
"C": time.Now(),
"A": time.Now().Format(time.RFC3339),
"B": time.Now().Add(-time.Hour).Format(time.RFC3339),
"C": time.Now().Format(time.RFC3339),
},
baseTime: time.Now(),
},
Expand All @@ -220,9 +262,9 @@ func Test_check(t *testing.T) {
{ID: "C", Repeat: graph.MessageRepeatTypeHourly},
},
lastReportMap: map[string]interface{}{
"A": time.Now(),
"B": time.Now().Add(-time.Hour * 24),
"C": time.Now(),
"A": time.Now().Format(time.RFC3339),
"B": time.Now().Add(-time.Hour * 24).Format(time.RFC3339),
"C": time.Now().Format(time.RFC3339),
},
baseTime: time.Now(),
},
Expand All @@ -239,9 +281,9 @@ func Test_check(t *testing.T) {
{ID: "C", Repeat: graph.MessageRepeatTypeHourly},
},
lastReportMap: map[string]interface{}{
"A": time.Now(),
"B": time.Now().Add(-time.Hour * 24 * 7),
"C": time.Now(),
"A": time.Now().Format(time.RFC3339),
"B": time.Now().Add(-time.Hour * 24 * 7).Format(time.RFC3339),
"C": time.Now().Format(time.RFC3339),
},
baseTime: time.Now(),
},
Expand All @@ -258,9 +300,9 @@ func Test_check(t *testing.T) {
{ID: "C", Repeat: graph.MessageRepeatTypeHourly},
},
lastReportMap: map[string]interface{}{
"A": time.Now(),
"B": time.Now().Add(-time.Hour * 24 * 7 * 30),
"C": time.Now(),
"A": time.Now().Format(time.RFC3339),
"B": time.Now().Add(-time.Hour * 24 * 7 * 30).Format(time.RFC3339),
"C": time.Now().Format(time.RFC3339),
},
baseTime: time.Now(),
},
Expand Down
68 changes: 67 additions & 1 deletion test/integration/msg_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func (suite *MsgIntegrationTestSuite) TestMessage_Basic() {
Name string
MessageJson string
ExpectRepeat bool
ExpectShown bool
}{
{
"Defaults",
Expand All @@ -44,6 +45,7 @@ func (suite *MsgIntegrationTestSuite) TestMessage_Basic() {
"Message": "This is a [NOTICE]simple[/RESET] message"
}]`,
false,
true,
},
{
"Repeat Hourly",
Expand All @@ -53,6 +55,7 @@ func (suite *MsgIntegrationTestSuite) TestMessage_Basic() {
"Repeat": "Hourly"
}]`,
false,
true,
},
{
"Repeat Constantly",
Expand All @@ -62,6 +65,67 @@ func (suite *MsgIntegrationTestSuite) TestMessage_Basic() {
"Repeat": "Constantly"
}]`,
true,
true,
},
{
"Condition DateAfter Shown",
`[{
"ID": "simple",
"Message": "This is a [NOTICE]simple[/RESET] message",
"Condition": "dateAfter \"2015-11-15T14:45:28Z\""
}]`,
false,
true,
},
{
"Condition DateAfter Not Shown",
`[{
"ID": "simple",
"Message": "This is a [NOTICE]simple[/RESET] message",
"Condition": "dateAfter \"2025-11-15T14:45:28Z\""
}]`,
false,
false,
},
{
"Condition DateBefore Shown",
`[{
"ID": "simple",
"Message": "This is a [NOTICE]simple[/RESET] message",
"Condition": "dateBefore \"2025-11-15T14:45:28Z\""
}]`,
false,
false,
},
{
"Condition DateBefore Not Shown",
`[{
"ID": "simple",
"Message": "This is a [NOTICE]simple[/RESET] message",
"Condition": "dateBefore \"2015-11-15T14:45:28Z\""
}]`,
false,
false,
},
{
"Condition Date Range Shown",
`[{
"ID": "simple",
"Message": "This is a [NOTICE]simple[/RESET] message",
"Condition": "and (dateAfter \"2015-11-15T14:45:28Z\") (dateBefore \"2025-11-15T14:45:28Z\")"
}]`,
false,
true,
},
{
"Condition Date Range Not Shown",
`[{
"ID": "simple",
"Message": "This is a [NOTICE]simple[/RESET] message",
"Condition": "and (dateAfter \"2025-11-15T14:45:28Z\") (dateBefore \"2035-11-15T14:45:28Z\")"
}]`,
false,
false,
},
}
for _, tt := range tests {
Expand All @@ -73,7 +137,9 @@ func (suite *MsgIntegrationTestSuite) TestMessage_Basic() {
suite.Require().NoError(err)

cp := ts.SpawnWithOpts(e2e.OptArgs("--version"), e2e.OptAppendEnv(constants.MessagesOverrideEnvVarName+"="+msgFile))
cp.Expect(`This is a simple message`)
if tt.ExpectShown {
cp.Expect(`This is a simple message`)
}
cp.Expect("ActiveState CLI by ActiveState Software Inc.")
cp.ExpectExitCode(0)

Expand Down

0 comments on commit ec67e95

Please sign in to comment.