 (defn- execution-metrics
   "Get metrics based on executions.
   This should be done in a single pass, as there might
-  be a LOT of query executions in a normal instance
-  TODO: characterize by ad hoc vs cards"
-  []
-  (let [executions (db/select ['QueryExecution :executor_id :running_time :status])]
-    {:executions (count executions)
-     :by_status (frequencies (map :status executions))
-     :num_per_user (large-histogram executions :executor_id)
+  be a LOT of query executions in a normal instance."
+  ;; TODO - This looks dangerous, pulling the entire set of QueryExecutions into memory at once to calculate these metrics (!)
+  []
+  (let [executions (db/select [QueryExecution :executor_id :running_time :status])]
+    {:executions     (count executions)
+     :by_status      (frequencies (map :status executions))
+     :num_per_user   (large-histogram executions :executor_id)
      :num_by_latency (bin-latencies executions)}))
 
 

  (:require [clojure.tools.logging :as log]
            [clj-http.client :as client]
            [toucan.db :as db]
            [metabase.config :as config]
            (metabase.models [field :as field]
                             [humanization :as humanization]
                             [table :as table]
                             [setting :as setting])
            [metabase.public-settings :as public-settings]
            [metabase.util :as u]))


  "Get metrics based on groups:
  TODO characterize by # w/ sql access, # of users, no self-serve data access"
  []
  {:groups (db/count PermissionsGroup)})

;; Artifact Metrics
(defn- question-dims
  "Characterize a saved QUESTION.
  TODO: characterize by whether it has params, # of revisions, created by an admin"
  [{query-type :query_type}]
    {:total  1
     :native (one-if (= query-type "native"))
     :gui    (one-if (not= query-type "native"))})

(defn- question-metrics
  "Get metrics based on questions
  TODO characterize by # executions and avg latency"
  []
  (let [questions (db/select [Card :id :query_type])]
    {:questions (apply add-summaries (map question-dims questions))}))

(defn- dashboard-metrics
  "Get metrics based on dashboards
  TODO characterize by # of revisions, and created by an admin"
  []
  (let [dashboards (db/select [Dashboard :id :creator_id])
        dashcards  (db/select [DashboardCard :id :card_id :dashboard_id])]
    {:dashboards         (count dashboards)
     :num_dashs_per_user (medium-histogram dashboards :creator_id)
     :num_cards_per_dash (medium-histogram dashcards :dashboard_id)
     :num_dashs_per_card (medium-histogram dashcards :card_id)}))
