diff --git a/todo.md b/todo.md index b74552a..d1c68b5 100644 --- a/todo.md +++ b/todo.md @@ -1,2 +1 @@ -# Todo -- add 'disable schedules' filter, useful for child of 'expect' filter \ No newline at end of file +# Todo \ No newline at end of file diff --git a/web/scraping.go b/web/scraping.go index 1142510..19b5571 100644 --- a/web/scraping.go +++ b/web/scraping.go @@ -234,6 +234,10 @@ func getFilterResult(filters []Filter, filter *Filter, watch *Watch, web *Web, d { getFilterResultExpect(filter, web, debug) } + case filter.Type == "disable": + { + getFilterResultDisableSchedules(filter, web, debug) + } case filter.Type == "notify": { notifyFilter(filters, filter, watch, web, debug) @@ -1291,6 +1295,32 @@ func getFilterResultExpect(filter *Filter, web *Web, debug bool) { web.db.Create(&expectFail) } +// getFilterResultDisableSchedules disables all schedules of a watch if it gets any inputs +func getFilterResultDisableSchedules(filter *Filter, web *Web, debug bool) { + if len(filter.Parents) == 0 { + filter.Logs = append(filter.Logs, "Need Parents") + return + } + anyParentWithResults := false + for i := range filter.Parents { + parent := filter.Parents[i] + if len(parent.Results) > 0 { + anyParentWithResults = true + break + } + } + if !anyParentWithResults { + return + } + + if debug { + filter.Results = append(filter.Results, "Would have disabled Schedules") + return + } + + web.db.Model(&Filter{}).Where("watch_id = ?", filter.WatchID).Update("Var2", "no") +} + // getFilterResultEcho is a debug filter type, used to bootstrap some tests func getFilterResultEcho(filter *Filter) { filter.Results = append(filter.Results, filter.Var1) diff --git a/web/scraping_test.go b/web/scraping_test.go index f237a04..e1237a2 100644 --- a/web/scraping_test.go +++ b/web/scraping_test.go @@ -2222,7 +2222,6 @@ func TestWatchWithExpect3TriggeringDB(t *testing.T) { db.Model(&ExpectFail{}).Find(&expectFails, "watch_id = ?", watch.ID) if len(expectFails) != 1 { t.Errorf("Found %d ExpectFail values, expected 1!", len(expectFails)) - log.Println(expectFails) } TriggerSchedule(watch.ID, &Web{db: db}, &scheduleFilter.ID) @@ -2230,7 +2229,6 @@ func TestWatchWithExpect3TriggeringDB(t *testing.T) { db.Model(&ExpectFail{}).Find(&expectFails, "watch_id = ?", watch.ID) if len(expectFails) != 2 { t.Errorf("Found %d ExpectFail values, expected 2!", len(expectFails)) - log.Println(expectFails) } TriggerSchedule(watch.ID, &Web{db: db}, &scheduleFilter.ID) @@ -2238,14 +2236,208 @@ func TestWatchWithExpect3TriggeringDB(t *testing.T) { db.Model(&ExpectFail{}).Find(&expectFails, "watch_id = ?", watch.ID) if len(expectFails) != 3 { t.Errorf("Found %d ExpectFail values, expected 3! (1)", len(expectFails)) - log.Println(expectFails) } TriggerSchedule(watch.ID, &Web{db: db}, &scheduleFilter.ID) db.Model(&ExpectFail{}).Find(&expectFails, "watch_id = ?", watch.ID) if len(expectFails) != 3 { t.Errorf("Found %d ExpectFail values, expected 3! (2)", len(expectFails)) - log.Println(expectFails) + } + + err := os.Remove("./test.db") + if err != nil { + log.Println("Could not remove test db:", err) + } +} + +func TestWatchWithExpectNotTriggeringWithDisableDB(t *testing.T) { + db := getTestDB() + watch := Watch{ + Name: "Test", + } + db.Create(&watch) + filters := []Filter{ + { + WatchID: watch.ID, + Name: "Schedule", + Type: "cron", + Var2: "yes", + }, + { + WatchID: watch.ID, + Name: "Echo", + Type: "echo", + Var1: HTML_STRING, + }, + { + WatchID: watch.ID, + Name: "XPath", + Type: "xpath", + Var1: "//td[@class='price']", + }, + { + WatchID: watch.ID, + Name: "Expect", + Type: "expect", + Var1: "1", + }, + { + WatchID: watch.ID, + Name: "Disable", + Type: "disable", + }, + } + db.Create(&filters) + scheduleFilter := &filters[0] + echoFilter := &filters[1] + xpathFilter := &filters[2] + expectFilter := &filters[3] + disableFilter := &filters[4] + + connections := []FilterConnection{ + { + WatchID: watch.ID, + OutputID: scheduleFilter.ID, + InputID: echoFilter.ID, + }, + { + WatchID: watch.ID, + OutputID: echoFilter.ID, + InputID: xpathFilter.ID, + }, + { + WatchID: watch.ID, + OutputID: xpathFilter.ID, + InputID: expectFilter.ID, + }, + { + WatchID: watch.ID, + OutputID: expectFilter.ID, + InputID: disableFilter.ID, + }, + } + db.Create(&connections) + + TriggerSchedule(watch.ID, &Web{db: db}, &scheduleFilter.ID) + + var expectFails []ExpectFail + db.Model(&ExpectFail{}).Find(&expectFails, "watch_id = ?", watch.ID) + if len(expectFails) > 0 { + t.Errorf("Found ExpectFail values expected none!") + } + + var scheduleFilterFromDb Filter + db.Model(&Filter{}).First(&scheduleFilterFromDb, scheduleFilter.ID) + if scheduleFilterFromDb.Var2 != "yes" { + t.Errorf("Schedule filter is disabled!") + } + + err := os.Remove("./test.db") + if err != nil { + log.Println("Could not remove test db:", err) + } +} + +func TestWatchWithExpect3TriggeringAndDisableDB(t *testing.T) { + db := getTestDB() + watch := Watch{ + Name: "Test", + } + db.Create(&watch) + filters := []Filter{ + { + WatchID: watch.ID, + Name: "Schedule", + Type: "cron", + Var2: "yes", + }, + { + WatchID: watch.ID, + Name: "Echo", + Type: "echo", + Var1: HTML_STRING, + }, + { + WatchID: watch.ID, + Name: "XPath", + Type: "xpath", + Var1: "//div[@class='price']", + }, + { + WatchID: watch.ID, + Name: "Expect", + Type: "expect", + Var1: "3", + }, + { + WatchID: watch.ID, + Name: "Disable", + Type: "disable", + }, + } + db.Create(&filters) + scheduleFilter := &filters[0] + echoFilter := &filters[1] + xpathFilter := &filters[2] + expectFilter := &filters[3] + disableFilter := &filters[4] + + connections := []FilterConnection{ + { + WatchID: watch.ID, + OutputID: scheduleFilter.ID, + InputID: echoFilter.ID, + }, + { + WatchID: watch.ID, + OutputID: echoFilter.ID, + InputID: xpathFilter.ID, + }, + { + WatchID: watch.ID, + OutputID: xpathFilter.ID, + InputID: expectFilter.ID, + }, + { + WatchID: watch.ID, + OutputID: expectFilter.ID, + InputID: disableFilter.ID, + }, + } + db.Create(&connections) + + var expectFails []ExpectFail + TriggerSchedule(watch.ID, &Web{db: db}, &scheduleFilter.ID) + + db.Model(&ExpectFail{}).Find(&expectFails, "watch_id = ?", watch.ID) + if len(expectFails) != 1 { + t.Errorf("Found %d ExpectFail values, expected 1!", len(expectFails)) + } + + TriggerSchedule(watch.ID, &Web{db: db}, &scheduleFilter.ID) + + db.Model(&ExpectFail{}).Find(&expectFails, "watch_id = ?", watch.ID) + if len(expectFails) != 2 { + t.Errorf("Found %d ExpectFail values, expected 2!", len(expectFails)) + } + + TriggerSchedule(watch.ID, &Web{db: db}, &scheduleFilter.ID) + + db.Model(&ExpectFail{}).Find(&expectFails, "watch_id = ?", watch.ID) + if len(expectFails) != 3 { + t.Errorf("Found %d ExpectFail values, expected 3! (1)", len(expectFails)) + } + TriggerSchedule(watch.ID, &Web{db: db}, &scheduleFilter.ID) + + db.Model(&ExpectFail{}).Find(&expectFails, "watch_id = ?", watch.ID) + if len(expectFails) != 3 { + t.Errorf("Found %d ExpectFail values, expected 3! (2)", len(expectFails)) + } + + var scheduleFilterFromDb Filter + db.Model(&Filter{}).First(&scheduleFilterFromDb, scheduleFilter.ID) + if scheduleFilterFromDb.Var2 != "no" { + t.Errorf("Schedule filter not disabled!") } err := os.Remove("./test.db") diff --git a/web/static/edit.js b/web/static/edit.js index 8694a7e..eeaef78 100644 --- a/web/static/edit.js +++ b/web/static/edit.js @@ -351,6 +351,26 @@ function onTypeChange(node) { var2Div.appendChild(var2Input); break; } + case "disable": { + var var1Input = document.createElement("input"); + var1Input.name = "var1"; + var1Input.id = "var1Input"; + var1Input.value = var1Value; + var1Input.classList.add("form-control"); + var1Input.disabled = true; + var1Label.innerHTML = "-"; + var1Input.placeholder = ""; + var1Div.appendChild(var1Input); + var var2Input = document.createElement("input"); + var2Input.name = "var2"; + var2Input.id = "var2Input"; + var2Input.value = var2Value; + var2Input.classList.add("form-control"); + var2Input.disabled = true; + var2Label.innerHTML = "-"; + var2Div.appendChild(var2Input); + break; + } case "unique": { var var1Input = document.createElement("input"); var1Input.name = "var1"; diff --git a/web/static/edit.ts b/web/static/edit.ts index fd415a6..487c9e6 100644 --- a/web/static/edit.ts +++ b/web/static/edit.ts @@ -330,6 +330,27 @@ function onTypeChange(node: DiagramNode | null = null){ var2Div.appendChild(var2Input); break; } + case "disable": { + let var1Input = document.createElement("input"); + var1Input.name = "var1"; + var1Input.id = "var1Input"; + var1Input.value = var1Value; + var1Input.classList.add("form-control") + var1Input.disabled = true; + var1Label.innerHTML = "-"; + var1Input.placeholder = ""; + var1Div.appendChild(var1Input); + + let var2Input = document.createElement("input"); + var2Input.name = "var2"; + var2Input.id = "var2Input"; + var2Input.value = var2Value; + var2Input.classList.add("form-control") + var2Input.disabled = true; + var2Label.innerHTML = "-"; + var2Div.appendChild(var2Input); + break; + } case "unique": { let var1Input = document.createElement("input"); var1Input.name = "var1"; diff --git a/web/templates/watch/edit.html b/web/templates/watch/edit.html index 516a46c..8a705fc 100644 --- a/web/templates/watch/edit.html +++ b/web/templates/watch/edit.html @@ -89,6 +89,7 @@ GoWatch Edit {{ .Watch.Name }} + @@ -104,8 +105,7 @@ GoWatch Edit {{ .Watch.Name }}