<commit_before>(ns akvo.lumen.endpoint.visualisation
  (:require [akvo.lumen.protocols :as p]
            [akvo.lumen.lib.visualisation :as visualisation]
            [akvo.lumen.specs.visualisation :as visualisation.s]
            [akvo.lumen.specs.visualisation.maps :as visualisation.maps.s]
            [akvo.lumen.lib :as lib]
            [akvo.lumen.lib.visualisation.maps :as maps]
            [akvo.lumen.lib.auth :as l.auth]
            [clojure.spec.alpha :as s]
            [akvo.lumen.component.tenant-manager :as tenant-manager]
            [clojure.walk :as w]
            [clojure.tools.logging :as log]
            [integrant.core :as ig]))

(defn all-visualisations [auth-service tenant-conn]
  (let [visualisations      (visualisation/all tenant-conn)
        auth-visualisations (->> visualisations
                                 (l.auth/ids ::visualisation.s/visualisations)
                                 (p/auth? auth-service)
                                 :auth-visualisations)]
    (->> visualisations
         (filter #(contains? auth-visualisations (:id %)))
         (lib/ok))))

(defn routes [{:keys [windshaft-url tenant-manager] :as opts}]
  ["/visualisations"
   ["" {:get {:handler (fn [{tenant :tenant
                             auth-service :auth-service}]
                         (all-visualisations auth-service (p/connection tenant-manager tenant)))}
        :post {:parameters {:body map?}
               :handler (fn [{tenant :tenant
                              jwt-claims :jwt-claims
                              auth-service :auth-service
                              body :body}]
                          (let [vis-payload (w/keywordize-keys body)
                                ids (l.auth/ids ::visualisation.s/visualisation vis-payload)]
                            (if (p/allow? auth-service ids)
                              (lib/ok (visualisation/create (p/connection tenant-manager tenant) vis-payload jwt-claims))
                              (lib/not-authorized {:ids ids}))))}}]
   ["/maps" ["" {:post {:parameters {:body map?}
                        :handler (fn [{tenant :tenant
                                       body :body}]
                                   (let [{:strs [spec]} body
                                         layers (w/keywordize-keys (get-in spec ["layers"]))]
                                     (maps/create (p/connection tenant-manager tenant) windshaft-url layers)))}}]]
   ;; rasters don't depend on flow data (yet!), so no need to wrap this call 
   ["/rasters" ["" {:post {:parameters {:body map?}
                           :handler (fn [{tenant :tenant
                                          body :body}]
                                      (let [{:strs [rasterId spec]} body]
                                        (maps/create-raster (p/connection tenant-manager tenant) windshaft-url rasterId)))}}]]
   ;; todo: fix path routing inconsistency here 
   ["/:id" ["" {:get {:parameters {:path-params {:id string?}}
                      :handler (fn [{tenant :tenant
                                     {:keys [id]} :path-params}]
                                 (if-let [res (visualisation/fetch (p/connection tenant-manager tenant) id)]
                                     (lib/ok res)
                                     (lib/not-found {:error "Not found"})))}
                  :put {:parameters {:body map?<commit_msg>[#1949] Auth "GET /visualisations/:id" (coming and outcoming data!)<commit_after>(ns akvo.lumen.endpoint.visualisation
  (:require [akvo.lumen.protocols :as p]
            [akvo.lumen.lib.visualisation :as visualisation]
            [akvo.lumen.specs.visualisation :as visualisation.s]
            [akvo.lumen.specs.visualisation.maps :as visualisation.maps.s]
            [akvo.lumen.lib :as lib]
            [akvo.lumen.lib.visualisation.maps :as maps]
            [akvo.lumen.lib.auth :as l.auth]
            [clojure.spec.alpha :as s]
            [akvo.lumen.component.tenant-manager :as tenant-manager]
            [clojure.walk :as w]
            [clojure.tools.logging :as log]
            [integrant.core :as ig]))

(defn all-visualisations [auth-service tenant-conn]
  (let [visualisations      (visualisation/all tenant-conn)
        auth-visualisations (->> visualisations
                                 (l.auth/ids ::visualisation.s/visualisations)
                                 (p/auth? auth-service)
                                 :auth-visualisations)]
    (->> visualisations
         (filter #(contains? auth-visualisations (:id %)))
         (lib/ok))))

(defn routes [{:keys [windshaft-url tenant-manager] :as opts}]
  ["/visualisations"
   ["" {:get {:handler (fn [{tenant :tenant
                             auth-service :auth-service}]
                         (all-visualisations auth-service (p/connection tenant-manager tenant)))}
        :post {:parameters {:body map?}
               :handler (fn [{tenant :tenant
                              jwt-claims :jwt-claims
                              auth-service :auth-service
                              body :body}]
                          (let [vis-payload (w/keywordize-keys body)
                                ids (l.auth/ids ::visualisation.s/visualisation vis-payload)]
                            (if (p/allow? auth-service ids)
                              (lib/ok (visualisation/create (p/connection tenant-manager tenant) vis-payload jwt-claims))
                              (lib/not-authorized {:ids ids}))))}}]
   ["/maps" ["" {:post {:parameters {:body map?}
                        :handler (fn [{tenant :tenant
                                       body :body}]
                                   (let [{:strs [spec]} body
                                         layers (w/keywordize-keys (get-in spec ["layers"]))]
                                     (maps/create (p/connection tenant-manager tenant) windshaft-url layers)))}}]]
   ;; rasters don't depend on flow data (yet!), so no need to wrap this call 
   ["/rasters" ["" {:post {:parameters {:body map?}
                           :handler (fn [{tenant :tenant
                                          body :body}]
                                      (let [{:strs [rasterId spec]} body]
                                        (maps/create-raster (p/connection tenant-manager tenant) windshaft-url rasterId)))}}]]
   ;; todo: fix path routing inconsistency here 
   ["/:id" ["" {:get {:parameters {:path-params {:id string?}}
                      :handler (fn [{tenant :tenant
                                     auth-service :auth-service
                                     {:keys [id]} :path-params}]
                                 (if (p/allow? auth-service (l.auth/ids ::visualisation.s/id id))
                                   (if-let [res (visualisation/fetch (p/connection tenant-manager tenant) id)]
                                     (let [ids (l.auth/ids ::visualisation.s/visualisation res)]
                                       (if (p/allow? auth-service ids)
                                        (lib/ok res)
                                        (lib/not-authorized ids)))
                                     (lib/not-found {:error "Not found"}))
                                   (lib/not-authorized {:id id})))}
                  :put {:parameters {:body map?
{'fraction_non_alphanumeric': 0.12304159913560238, 'fraction_numerical': 0.0005402485143165856, 'mean_word_length': 1.33596214511041, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 46, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7551410', 'n_tokens_mistral': 2013, 'n_tokens_neox': 1875, 'n_words': 456}