<commit_before>(ns akvo.lumen.lib.visualisation.maps
  (:require [akvo.lumen.lib :as lib]
            [cheshire.core :as json]
            [clj-http.client :as client]
            [clojure.pprint :refer [pprint]]))

;; Localhost to keep within pod?
(def url "http://windshaft:4000/lumen_tenant_1/layergroup")

(defn headers []
  {"x-db-host" "postgres"
   "x-db-last-update" (quot (System/currentTimeMillis) 1000)
   "x-db-password" "password"
   "x-db-port" 5432
   "x-db-user" "lumen"})

(def sql-statement
  "SELECT 'Batman cave' as label, ST_SetSRID(ST_MakePoint(12.069034,57.74119), 4326) AS geom;")

(def cartocss
  "#s { marker-width: 10; marker-fill: #e00050;}")

(defn map-config [cartocss geom-column label sql-statement]
  {"version" "1.6.0"
   "layers" [{"type" "mapnik"
              "options" {"cartocss" cartocss
                         "cartocss_version" "2.0.0"
                         "geom_column" geom-column
                         "interactivity" label
                         "sql" sql-statement
                         "srid" 4326}}]})

(defn create [tenant-db visualisation-spec]
  (prn tenant-db)
  (pprint visualisation-spec)
  ;; Handle if available
  ;; dataset -> yank out relevant ds_<...>,
  ;; geom
  ;; interactivity
  (let [resp (client/post url
                          {:body (json/encode (map-config cartocss
                                                          "geom"
                                                          "label"
                                                          sql-statement))
                           :headers (headers)
                           :content-type :json})]
    (case (:status resp)
      200 (lib/ok (assoc (json/decode (:body resp))
                         "tenantDB" tenant-db))
      (prn resp))))


(comment
  ;; Example map spec
  {"type" "visualisation",<commit_msg>[#1077] Small steps on WIP code<commit_after>(ns akvo.lumen.lib.visualisation.maps
  (:require [akvo.lumen.lib :as lib]
            [cheshire.core :as json]
            [clj-http.client :as client]
            [clojure.pprint :refer [pprint]]
            [clojure.string :as s]))



(defn- url
  "WIP, should talk over localhost within the pod"
  [tenant-db]
  (format "http://windshaft:4000/%s/layergroup" tenant-db))

(defn headers
  "WIP, should get this from tenant-manger"
  []
  {"x-db-host" "postgres"
   "x-db-last-update" (quot (System/currentTimeMillis) 1000)
   "x-db-password" "password"
   "x-db-port" 5432
   "x-db-user" "lumen"})

(def locations
  '({:label "HQ"
     :longitude 4.908075
     :latitude 52.372189}
    {:label "Bat cave"
     :longitude 12.069034
     :latitude 57.74119}))


(defn sql-values [locations]
  (s/join ", "
          (map (fn [{:keys [label longitude latitude]}]
                 (format "('%s', ST_SetSRID(ST_MakePoint(%s,%s), 4326))"
                         label longitude latitude))
               locations)))

(defn sql-statement
  [locations]
  (format "SELECT * FROM (VALUES %s ) AS t (label, geom);"
          (sql-values locations)))


(def cartocss
  "#s { marker-width: 10; marker-fill: #e00050;}")

(defn map-config [cartocss geom-column label sql-statement]
  {"version" "1.6.0"
   "layers" [{"type" "mapnik"
              "options" {"cartocss" cartocss
                         "cartocss_version" "2.0.0"
                         "geom_column" geom-column
                         "interactivity" label
                         "sql" sql-statement
                         "srid" 4326}}]})

(defn create [tenant-db visualisation-spec]
  (prn tenant-db)
  #_(pprint visualisation-spec)
  ;; Handle if available
  ;; dataset -> yank out relevant ds_<...>,
  ;; geom
  ;; interactivity
  (let [sql-statement (sql-statement locations)
        _ (prn sql-statement)
        resp (client/post (url tenant-db)
                          {:body (json/encode (map-config cartocss
                                                          "geom"
                                                          "label"
                                                          sql-statement))
                           :headers (headers)
                           :content-type :json})]
    (case (:status resp)
      200 (lib/ok (assoc (json/decode (:body resp))
                         "tenantDB" tenant-db))
      (prn resp))))


(comment

  ;; Example map spec
  {"type" "visualisation",
{'fraction_non_alphanumeric': 0.13345438039037677, 'fraction_numerical': 0.027689514298683614, 'mean_word_length': 1.646846846846847, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6809069', 'n_tokens_mistral': 1478, 'n_tokens_neox': 1351, 'n_words': 323}