Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(require '[cheshire.core :refer [parse-string]])
(defn- json->data [key m]
(update-in m [key] #(parse-string % true)))
(defn- select-campaign [car+campaigns]
(first car+campaigns))
(defn- jdbc-array-to-set
[key m]
(update-in m [key] #(apply hash-set (.getArray %))))
(defn refine-cars-simple
"Process get-cars query result set - derive additional data, transform values into better ones
There is one row per car and campaign, a car may have more campaigns - we pick the best one.
"
[cars-raw]
(->>
cars-raw
(map (partial json->data :json)) ;; <- this I originally forgot
;; group rows for the same car => [[car1 ..][car2 ..]]
(group-by :id)
(vals)
;; join all car1 vectors into one car ..
(map select-campaign)
(map (fn [car]
(->>
car
(jdbc-array-to-set :category_ref)
(jdbc-array-to-set :keywords))))))
(require '[cheshire.core :refer [parse-string]])
(require '[clojure.set :refer [subset? difference]])
(defn- car? [{:keys [id] :as car}]
(and (map? car) id))
(defn- json->data [key m]
{:pre [(contains? m key) (string? (get m key))], :post [(map? (get % key))]}
(update-in m [key] parse-string true))
(defn- select-campaign [[first-car :as all]]
{:pre [(sequential? all) (car? first-car)], :post [(car? %)]}
first-car)
(defn- jdbc-array-to-set
[key m]
{:pre [(contains? m key) (instance? java.sql.Array (get m key))], :post [(set? (get % key))]}
(update-in m [key] #(apply hash-set (.getArray %))))
(defn group-rows-by-car [cars-raw]
{:pre [(sequential? cars-raw) (car? (first cars-raw))]
:post [(sequential? %) (vector? (first %))]}
(vals (group-by :id cars-raw)))
(defn refine-car [car]
{:pre [(car? car) (:keywords car) (:category_ref car)]}
(->> car
(jdbc-array-to-set :category_ref)
(jdbc-array-to-set :keywords)))
(defn refine-cars-simple
"Process get-cars query result set - derive additional data, transform values into better ones
There is one row per car and campaign, a car may have more campaigns - we pick the best one.
"
[cars-raw]
(->>
cars-raw
(map (partial json->data :json)) ;; <- this I originally forgot
(group-rows-by-car)
(map select-campaign)
(map refine-car)))
(defn empty-array [] (reify java.sql.Array (getArray [_] (object-array []))))
(refine-cars-simple [{:id 1, :json "{\"discount\":5000}", :campaign_discount 3000, :category_ref (empty-array), :keywords (empty-array)}])
@holyjak

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.