From 33d5828bf0d7f6a3ecd78cb8255261a24beb3e42 Mon Sep 17 00:00:00 2001
From: Simon Belak <sb@Simons-MacBook-Pro.local>
Date: Wed, 21 Mar 2018 22:08:30 +0100
Subject: [PATCH] Redo cell api, fix decoding bug

---
 src/metabase/api/automagic_dashboards.clj     | 21 +++++++++----------
 .../automagic_dashboards/populate.clj         |  2 +-
 2 files changed, 11 insertions(+), 12 deletions(-)

diff --git a/src/metabase/api/automagic_dashboards.clj b/src/metabase/api/automagic_dashboards.clj
index 8d0ec3934f5..a749b8ffba3 100644
--- a/src/metabase/api/automagic_dashboards.clj
+++ b/src/metabase/api/automagic_dashboards.clj
@@ -1,5 +1,6 @@
 (ns metabase.api.automagic-dashboards
-  (:require [cheshire.core :as json]
+  (:require [buddy.core.codecs :as codecs]
+            [cheshire.core :as json]
             [compojure.core :refer [GET POST]]
             [metabase.api
              [card :as card.api]
@@ -22,6 +23,9 @@
              [db :as db]
              [hydrate :refer [hydrate]]]))
 
+(def ^:private ^{:arglists '([s])} decode-base64-json
+  (comp json/decode codecs/bytes->str codec/base64-decode))
+
 (api/defendpoint GET "/database/:id/candidates"
   "Return a list of candidates for automagic dashboards orderd by interestingness."
   [id]
@@ -57,23 +61,19 @@
       api/check-404
       (magic/automagic-dashboard (rules/load-rule (str prefix "/" rule ".yaml")))))
 
-(api/defendpoint GET "/question/:id/:cell-query"
+(api/defendpoint GET "/question/:id/cell/:cell-query"
   "Return an automagic dashboard analyzing cell in question  with id `id` defined by
    query `cell-querry`."
   [id cell-query]
-  (-> (card.api/adhoc-query {:query {:filter (-> cell-query
-                                                 codec/base64-decode
-                                                 json/decode)}})
+  (-> (card.api/adhoc-query {:query {:filter (decode-base64-json cell-query)}})
       (magic/inject-segment (-> id Card api/check-404))
       magic/automagic-dashboard))
 
-(api/defendpoint GET "/question/:id/:cell-query/:prefix/:rule"
+(api/defendpoint GET "/question/:id/cell/:cell-query/:prefix/:rule"
   "Return an automagic dashboard analyzing cell in question  with id `id` defined by
    query `cell-querry` using rule `rule`."
   [id cell-query prefix rule]
-  (-> (card.api/adhoc-query {:query {:filter (-> cell-query
-                                                 codec/base64-decode
-                                                 json/decode)}})
+  (-> (card.api/adhoc-query {:query {:filter (decode-base64-json cell-query)}})
       (magic/inject-segment (-> id Card api/check-404))
       (magic/automagic-dashboard (rules/load-rule (str prefix "/" rule ".yaml")))))
 
@@ -96,8 +96,7 @@
   "Return an automagic dashboard analyzing ad hoc query."
   [query]
   (-> query
-      codec/base64-decode
-      json/decode
+      decode-base64-json
       card.api/adhoc-query
       magic/automagic-analysis))
 
diff --git a/src/metabase/automagic_dashboards/populate.clj b/src/metabase/automagic_dashboards/populate.clj
index ecca1e71fe6..be4b3b5b275 100644
--- a/src/metabase/automagic_dashboards/populate.clj
+++ b/src/metabase/automagic_dashboards/populate.clj
@@ -32,7 +32,7 @@
    (or (db/select-one 'Collection
          :name "Automatically Generated Questions")
        (create-collection! "Automatically Generated Questions"
-                           "#000000"
+                           "#509EE3"
                            "Cards used in automatically generated dashboards."))))
 
 (defn- add-card
-- 
GitLab