<commit_before>(ns akvo.lumen.lib.public-impl
  (:require [akvo.lumen.lib :as lib]
            [akvo.lumen.lib.aggregation :as aggregation]
            [akvo.lumen.lib.dashboard :as dashboard]
            [akvo.lumen.lib.dataset :as dataset]
            [akvo.lumen.lib.visualisation :as visualisation]
            [akvo.lumen.lib.visualisation.maps :as maps]
            [cheshire.core :as json]
            [clojure.set :as set]
            [hugsql.core :as hugsql]))

(hugsql/def-db-fns "akvo/lumen/lib/public.sql")

(defn get-share [tenant-conn id]
  (public-by-id tenant-conn {:id id}))

(defmulti visualisation (fn [_ visualisation config]
                          (:visualisationType visualisation)))

(defmethod visualisation "pivot table"
  [tenant-conn visualisation config]
  (let [dataset-id (:datasetId visualisation)
        [tag query-result] (aggregation/query tenant-conn
                                              dataset-id
                                              "pivot"
                                              (:spec visualisation))]
    (when (= tag ::lib/ok)
      {"visualisations" {(:id visualisation) (assoc visualisation :data query-result)}})))

(defmethod visualisation "pie"
  [tenant-conn visualisation config]
  (let [dataset-id (:datasetId visualisation)
        [tag query-result] (aggregation/query tenant-conn
                                              dataset-id
                                              "pie"
                                              (:spec visualisation))]
    (when (= tag ::lib/ok)
      {"visualisations" {(:id visualisation) (assoc visualisation :data query-result)}})))

(defmethod visualisation "donut"
  [tenant-conn visualisation config]
  (let [dataset-id (:datasetId visualisation)
        [tag query-result] (aggregation/query tenant-conn
                                              dataset-id
                                              "pie"
                                              (:spec visualisation))]
    (when (= tag ::lib/ok)
      {"visualisations" {(:id visualisation) (assoc visualisation :data query-result)}})))

(defmethod visualisation "map"
  [tenant-conn visualisation {:keys [windshaft-url]}]
  (let [layers (get-in visualisation [:spec "layers"])]
    (if (some #(get % "datasetId") layers)
      (let [dataset-id (some #(get % "datasetId") layers)
              [map-data-tag map-data] (maps/create tenant-conn windshaft-url layers)<commit_msg>[#1340] Add non-row dataset data to share endpoints<commit_after>(ns akvo.lumen.lib.public-impl
  (:require [akvo.lumen.lib :as lib]
            [akvo.lumen.lib.aggregation :as aggregation]
            [akvo.lumen.lib.dashboard :as dashboard]
            [akvo.lumen.lib.dataset :as dataset]
            [akvo.lumen.lib.visualisation :as visualisation]
            [akvo.lumen.lib.visualisation.maps :as maps]
            [cheshire.core :as json]
            [clojure.set :as set]
            [hugsql.core :as hugsql]))

(hugsql/def-db-fns "akvo/lumen/lib/public.sql")

(defn get-share [tenant-conn id]
  (public-by-id tenant-conn {:id id}))

(defmulti visualisation (fn [_ visualisation config]
                          (:visualisationType visualisation)))

(defmethod visualisation "pivot table"
  [tenant-conn visualisation config]
  (let [dataset-id (:datasetId visualisation)
        [dataset-tag dataset] (dataset/fetch tenant-conn dataset-id)
        [tag query-result] (aggregation/query tenant-conn
                                              dataset-id
                                              "pivot"
                                              (:spec visualisation))]
    (when (and (= tag ::lib/ok)
               (= dataset-tag ::lib/ok))
      {"visualisations" {(:id visualisation) (assoc visualisation :data query-result)}
        "datasets" { dataset-id (dissoc dataset :rows)}})))

(defmethod visualisation "pie"
  [tenant-conn visualisation config]
  (let [dataset-id (:datasetId visualisation)
        [dataset-tag dataset] (dataset/fetch tenant-conn dataset-id)
        [tag query-result] (aggregation/query tenant-conn
                                              dataset-id
                                              "pie"
                                              (:spec visualisation))]
    (when (and (= tag ::lib/ok)
               (= dataset-tag ::lib/ok))
      {"visualisations" {(:id visualisation) (assoc visualisation :data query-result)}
        "datasets" { dataset-id (dissoc dataset :rows)}})))

(defmethod visualisation "donut"
  [tenant-conn visualisation config]
  (let [dataset-id (:datasetId visualisation)
        [dataset-tag dataset] (dataset/fetch tenant-conn dataset-id)
        [tag query-result] (aggregation/query tenant-conn
                                              dataset-id
                                              "pie"
                                              (:spec visualisation))]
    (when (and (= tag ::lib/ok)
               (= dataset-tag ::lib/ok))
      {"visualisations" {(:id visualisation) (assoc visualisation :data query-result)}
        "datasets" { dataset-id (dissoc dataset :rows)}})))

(defmethod visualisation "map"
  [tenant-conn visualisation {:keys [windshaft-url]}]
  (let [layers (get-in visualisation [:spec "layers"])]
    (if (some #(get % "datasetId") layers)
      (let [dataset-id (some #(get % "datasetId") layers)
              [map-data-tag map-data] (maps/create tenant-conn windshaft-url layers)
{'fraction_non_alphanumeric': 0.12147149881624476, 'fraction_numerical': 0.0007284647605172099, 'mean_word_length': 1.8515057113187954, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5661213', 'n_tokens_mistral': 1520, 'n_tokens_neox': 1473, 'n_words': 369}