Skip to content

Commit

Permalink
Merge pull request #29 from iomz/fix-rule-conditions
Browse files Browse the repository at this point in the history
test: add match test
  • Loading branch information
iomz authored Jun 28, 2023
2 parents fd78d66 + 928aad0 commit 3a9e1ed
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 56 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ on:
pull_request:
branches:
- main
push:
branches:
- main
jobs:
build:
strategy:
Expand Down
3 changes: 0 additions & 3 deletions .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ on:
pull_request:
branches:
- main
push:
branches:
- main
jobs:
codecov:
runs-on: ubuntu-latest
Expand Down
3 changes: 0 additions & 3 deletions .github/workflows/golanci-lint.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
name: golangci-lint
on:
push:
branches:
- main
pull_request:
branches:
- main
Expand Down
39 changes: 24 additions & 15 deletions rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,8 @@ type Rule struct {
// 4. match the criteria
func (r *Rule) Match(stationID string, p *Prog) bool {
// 1. check Window
if r.HasWindow() {
startTime, err := time.ParseInLocation(DatetimeLayout, p.Ft, Location)
if err != nil {
log.Printf("invalid start time format '%s': %s", p.Ft, err)
return false
}
fetchWindow, err := time.ParseDuration(r.Window)
if err != nil {
log.Printf("parsing [%s].past failed: %v (using 24h)", r.Name, err)
fetchWindow = time.Hour * 24
}
if startTime.Add(fetchWindow).Before(CurrentTime) {
return false // skip before the fetch window
}
if !r.MatchWindow(p.Ft) {
return false
}
// 2. check dow
if !r.MatchDoW(p.Ft) {
Expand Down Expand Up @@ -145,7 +133,7 @@ func (r *Rule) MatchKeyword(p *Prog) bool {
log.Printf("rule[%s] matched with pfm: '%s'", r.Name, p.Pfm)
return true
} else if strings.Contains(p.Info, r.Keyword) {
log.Printf("rule[%s] matched with info: \n%s", r.Name, strings.ReplaceAll(p.Info, "\n", ""))
log.Printf("rule[%s] matched with info: %s", r.Name, strings.ReplaceAll(p.Info, "\n", ""))
return true
} else if strings.Contains(p.Desc, r.Keyword) {
log.Printf("rule[%s] matched with desc: '%s'", r.Name, strings.ReplaceAll(p.Desc, "\n", ""))
Expand Down Expand Up @@ -192,6 +180,27 @@ func (r *Rule) MatchTitle(title string) bool {
return false
}

func (r *Rule) MatchWindow(ft string) bool {
if !r.HasWindow() {
return true
}
startTime, err := time.ParseInLocation(DatetimeLayout, ft, Location)
if err != nil {
log.Printf("invalid start time format '%s': %s", ft, err)
return false
}
fetchWindow, err := time.ParseDuration(r.Window)
if err != nil {
log.Printf("parsing [%s].window failed: %v (using 24h)", r.Name, err)
fetchWindow = time.Hour * 24
}
if startTime.Add(fetchWindow).Before(CurrentTime) {
return false // skip the program outside the fetch window
}

return true
}

func (r *Rule) SetName(name string) {
r.Name = name
}
Loading

0 comments on commit 3a9e1ed

Please sign in to comment.