From 6f64eeace1b66639b9380b44e88a8d54850a4306 Mon Sep 17 00:00:00 2001
From: Parsa Yousefi <parsa.yousefi@ionos.com>
Date: Thu, 5 Sep 2024 19:18:51 +0200
Subject: filter muted checks

This adds the possibility to filter for muted checks.
---
 cmd/monfront/filter.go                  | 21 +++++++++++++++++++++
 cmd/monfront/templates/checkfilter.html |  7 +++++++
 2 files changed, 28 insertions(+)

diff --git a/cmd/monfront/filter.go b/cmd/monfront/filter.go
index bd82c91..968cb36 100644
--- a/cmd/monfront/filter.go
+++ b/cmd/monfront/filter.go
@@ -64,6 +64,18 @@ func (f *filter) filterChecks(c *Context) {
 			}
 			f.Add("ac.mapping_id", "=", val[0], "int")
 			f.Vals[arg] = val[0]
+		case "mute":
+			if val[0] == "" {
+				continue
+			}
+			isNull := false
+			if val[0] == "true" {
+				isNull = true
+				f.Vals[arg] = "true"
+			} else if val[0] == "false" {
+				f.Vals[arg] = "false"
+			}
+			f.AddNull("cn.check_id", isNull)
 		}
 	}
 }
@@ -75,6 +87,14 @@ func (f *filter) Add(field, op string, arg interface{}, castTo string) {
 	f.params = append(f.params, arg)
 }
 
+func (f *filter) AddNull(field string, isNull bool) {
+	check := "is not null"
+	if isNull {
+		check = "is null"
+	}
+	f.where = append(f.where, fmt.Sprintf("%s %s", field, check))
+}
+
 // AddSpecial lets you add a special where clause comparison where you can
 // wrap the argument in whatevery you like.
 //
@@ -82,6 +102,7 @@ func (f *filter) Add(field, op string, arg interface{}, castTo string) {
 // in the query string.
 //
 // Example:
+//
 //	AddSpecial("foo", "=", "to_tsvector('english', $%d), search)
 func (f *filter) AddSpecial(field, op, special string, arg interface{}) {
 	f.idx += 1
diff --git a/cmd/monfront/templates/checkfilter.html b/cmd/monfront/templates/checkfilter.html
index f86ce3c..f04427b 100644
--- a/cmd/monfront/templates/checkfilter.html
+++ b/cmd/monfront/templates/checkfilter.html
@@ -38,6 +38,13 @@
           {{ end }}
         </select>
       </div>
+      <div class="option">
+        <select id="filter-mute" name="filter-mute">
+          <option value="">filter muted</option>
+          <option value="true" {{ if eq "true" (index .Filter.Vals "mute") }}selected{{ end }}>muted</option>
+          <option value="false" {{ if eq "false" (index .Filter.Vals "mute") }}selected{{ end }}>unmuted</option>
+        </select>
+      </div>
       <div class="option">
         <input name="filter-search" placeholder="hostname" value="{{ .Filter.Vals.search }}" />
       </div>
-- 
cgit v1.2.3-70-g09d2