 ;;         vedges (aggregate/evaluate db "featureOf" [[:where {:from gid :to {:$in fea}}]])]
 ;;     {:environments [(count env) (count compounds) (count cedges)]
 ;;      :features [(count fea) (count variants) (count vedges)]}))
+
+
+;; (def do-percentile
+;;   [{:$facet
+;;     {:start
+;;      [{:$bucketAuto
+;;        {:groupBy "$start"
+;;         :buckets 100}}]}}
+;;    {:$project
+;;     {:start
+;;      {:$map
+;;       {:input [1 5 10]
+;;        :as "index"
+;;        :in
+;;        {:percentile "$$index"
+;;         :start
+;;         {:$arrayElemAt ["$start", "$$index"]}}}}}}
+;;    {:$project
+;;     {:start
+;;      {:$map
+;;       {:input "$start"
+;;        :as "index"
+;;        :in
+;;        {:percentile "$$index.percentile"
+;;         :start "$$index.start._id.max"}}}}}])
+
+;; [{:$group {:_id {:$multiply [100000 {:$ceil {:$divide ["$end" 100000]}}]} :count {:$sum 1}}} {:$sort {"_id" -1}}]
+

        path (or (:config env) "resources/config/ophion.clj")
        config (config/read-path path)
        graph (mongo/connect! (:mongo config))]
    ;; (ingest-incrementally graph (:input env))
    (ingest-labels-from-path! graph (:input env))
    (log/info "ingest complete")))


    [{:$facet queries}]))

(defn render-terms
  [outer limit]
  (let [dollar (str "$" (name outer))]
    [outer
     [[{:$sortByCount dollar}
       {:$limit (or limit 10)}]
      dollar
      dollar]]))

(defn render-percentile
  [outer percentiles]
  (let [key (keyword outer)
        dollar (str "$" (name outer))]
    [outer
     [[{:$bucketAuto
        {:groupBy dollar
         :buckets 100}}]
      {:$map
       {:input percentiles
        :as "index"
        :in
        {:percentile "$$index"
         key {:$arrayElemAt [dollar, "$$index"]}}}}
      {:$map
       {:input dollar
        :as "index"
        :in
        {:percentile "$$index.percentile"
         :start (str "$$index." (name outer) "._id.max")}}}]]))

(defn render-histogram
  [outer interval]
  (let [dollar (str "$" (name outer))])
  [outer
   [[{:$group
      {:_id
       {:$multiply
        [interval
         {:$ceil
          {:$divide
           [dollar interval]}}]}
       :count {:$sum 1}}}
     {:$sort {"_id" -1}}]
    dollar
    dollar]])

(defn render-aggregate
  [[key query]]
