The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
(ns metabase-enterprise.content-diagnostics.task.finding-trimmer
"Scheduled task to delete invalidated Content Diagnostics findings once they pass the retention
window. Nothing else deletes them - the scan and the archive event handlers only stamp
`invalidated_at` - so without this the table grows without bound."
(:require
[clojurewerkz.quartzite.jobs :as jobs]
[clojurewerkz.quartzite.schedule.cron :as cron]
[clojurewerkz.quartzite.triggers :as triggers]
[java-time.api :as t]
[metabase-enterprise.content-diagnostics.models.finding :as finding]
[metabase-enterprise.content-diagnostics.settings :as cd.settings]
[metabase.task-history.core :as task-history]
[metabase.task.core :as task]
[metabase.util.log :as log]))
(set! *warn-on-reflection* true)
(def ^:private trimmer-job-key
(jobs/key "metabase.task.content-diagnostics-finding-trimmer.job"))
(def ^:private trimmer-trigger-key
(triggers/key "metabase.task.content-diagnostics-finding-trimmer.trigger"))
(defn- trim-old-findings!
"Delete findings invalidated longer ago than the retention window. Runs whatever the token features
are, unlike the scan job - a lapsed `:content-diagnostics` token would otherwise strand invalidated
rows forever."
[]
(let [cutoff (t/minus (t/offset-date-time)
(t/days (long (cd.settings/content-diagnostics-finding-retention-days))))
deleted (finding/delete-invalidated-before! cutoff)]
(log/infof "Trimmed %d Content Diagnostics finding(s) invalidated before %s" deleted cutoff)))
(defn- trim-with-history!
"Run [[trim-old-findings!]], recording the run in `task_history`."
[]
(task-history/with-task-history {:task "content-diagnostics-trimmer"}
(trim-old-findings!)))
(task/defjob ContentDiagnosticsFindingTrimmer
"Content Diagnostics - delete invalidated findings past retention."
{:saved-class "metabase_enterprise.content_diagnostics.task.finding_trimmer.ContentDiagnosticsFindingTrimmer"
:concurrent? false}
[_ctx]
(trim-with-history!))
(defmethod task/init! ::ContentDiagnosticsFindingTrimmer [_]
(let [job (jobs/build
(jobs/of-type ContentDiagnosticsFindingTrimmer)
(jobs/store-durably)
(jobs/with-identity trimmer-job-key)
(jobs/with-description "Content Diagnostics finding trimmer"))
trigger (triggers/build
(triggers/with-identity trimmer-trigger-key)
(triggers/for-job trimmer-job-key)
(triggers/with-schedule
(cron/schedule
;; 16:00, twelve hours off the scan's 04:00 - both jobs write to
;; content_diagnostics_finding, so they are deliberately kept apart
(cron/cron-schedule "0 0 16 * * ? *")
(cron/with-misfire-handling-instruction-fire-and-proceed))))]
(task/schedule-task! job trigger)))