forked from metabase/metabase
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request metabase#8397 from metabase/store-sync-metrics
Add TaskHistory for storage of sync metrics
- Loading branch information
Showing
15 changed files
with
509 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
(ns metabase.models.task-history | ||
(:require [metabase.models.interface :as i] | ||
[metabase.util :as u] | ||
[toucan | ||
[db :as db] | ||
[models :as models]])) | ||
|
||
(models/defmodel TaskHistory :task_history) | ||
|
||
(defn cleanup-task-history! | ||
"Deletes older TaskHistory rows. Will order TaskHistory by `ended_at` and delete everything after | ||
`num-rows-to-keep`. This is intended for a quick cleanup of old rows." | ||
[num-rows-to-keep] | ||
;; Ideally this would be one query, but MySQL does not allow nested queries with a limit. The query below orders the | ||
;; tasks by the time they finished, newest first. Then finds the first row after skipping `num-rows-to-keep`. Using | ||
;; the date that task finished, it deletes everything after that. As we continue to add TaskHistory entries, this | ||
;; ensures we'll have a good amount of history for debugging/troubleshooting, but not grow too large and fill the | ||
;; disk. | ||
(when-let [clean-before-date (db/select-one-field :ended_at TaskHistory {:limit 1 | ||
:offset num-rows-to-keep | ||
:order-by [[:ended_at :desc]]})] | ||
(db/simple-delete! TaskHistory :ended_at [:<= clean-before-date]))) | ||
|
||
(u/strict-extend (class TaskHistory) | ||
models/IModel | ||
(merge models/IModelDefaults | ||
{:types (constantly {:task_details :json})}) | ||
i/IObjectPermissions | ||
(merge i/IObjectPermissionsDefaults | ||
{:can-read? i/superuser? | ||
:can-write? i/superuser?})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
(ns metabase.task.task-history-cleanup | ||
(:require [clj-time.core :as time] | ||
[clojure.tools.logging :as log] | ||
[clojurewerkz.quartzite | ||
[jobs :as jobs] | ||
[triggers :as triggers]] | ||
[clojurewerkz.quartzite.schedule.cron :as cron] | ||
[metabase.models.task-history :as thist :refer [TaskHistory]] | ||
[metabase.task :as task] | ||
[metabase.util.date :as du] | ||
[puppetlabs.i18n.core :refer [trs]] | ||
[toucan.db :as db])) | ||
|
||
(def ^:private ^:const job-name "task-history-cleanup") | ||
(def ^:private ^:const job-key (format "metabase.task.%s.job" job-name)) | ||
(def ^:private ^:const trigger-key (format "metabase.task.%s.trigger" job-name)) | ||
|
||
(defonce ^:private job (atom nil)) | ||
(defonce ^:private trigger (atom nil)) | ||
|
||
(def ^:private history-rows-to-keep | ||
"Maximum number of TaskHistory rows. This is not a `const` so that we can redef it in tests" | ||
100000) | ||
|
||
(defn- task-history-cleanup! | ||
[] | ||
(log/debug "Cleaning up task history") | ||
(let [before-cleanup (time/now) | ||
result (thist/cleanup-task-history! history-rows-to-keep) | ||
after-cleanup (time/now)] | ||
(db/insert! TaskHistory {:task job-name | ||
:started_at (du/->Timestamp before-cleanup) | ||
:ended_at (du/->Timestamp after-cleanup) | ||
:duration (du/calculate-duration before-cleanup after-cleanup)}) | ||
(log/debug (trs "Task history cleanup successful, rows were {0}deleted" | ||
(when-not result (str (trs "not") | ||
" ")))))) | ||
|
||
(jobs/defjob TaskHistoryCleanup | ||
[_] | ||
(task-history-cleanup!)) | ||
|
||
(defn task-init | ||
"Job initialization" | ||
[] | ||
;; build our job | ||
(reset! job (jobs/build | ||
(jobs/of-type TaskHistoryCleanup) | ||
(jobs/with-identity (jobs/key job-key)))) | ||
;; build our trigger | ||
(reset! trigger (triggers/build | ||
(triggers/with-identity (triggers/key trigger-key)) | ||
(triggers/start-now) | ||
(triggers/with-schedule | ||
;; run every day at midnight | ||
(cron/cron-schedule "0 0 * * * ? *")))) | ||
;; submit ourselves to the scheduler | ||
(task/schedule-task! @job @trigger)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.