(ns metabase-enterprise.audit-app.pages.common.cards)

HoneySQL for a CTE to get latest QueryExecution for a Card.

(def latest-qe
  [:latest_qe {:select   [:query_execution.card_id :error :query_execution.started_at]
               :from     [:query_execution]
               :join     [[{:select [:card_id [:%max.started_at :started_at]]
                            :from [:query_execution]
                            :group-by [:card_id]} :inner_qe]
                          [:= :query_execution.started_at :inner_qe.started_at]]}])

HoneySQL for a CTE to include the total number of queries for each Card forever.

(def query-runs
  [:query_runs {:select   [:card_id
                           [:%count.* :count]]
                :from     [:query_execution]
                :group-by [:card_id]}])

HoneySQL for a CTE to enumerate the dashboards for a Card.

(def dashboards-count
  [:dash_card {:select [:card_id [:%count.* :count]]
               :from [:report_dashboardcard]
               :group-by [:card_id]}])