Skip to content

Instantly share code, notes, and snippets.

@Poogles
Last active January 18, 2017 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Poogles/4bce0b780354b536ae714fdc998b14e5 to your computer and use it in GitHub Desktop.
Save Poogles/4bce0b780354b536ae714fdc998b14e5 to your computer and use it in GitHub Desktop.
Full example.
;; Full example of the size of events being transformed.
;; This has a typical structure, some events however can be flatter,
;; while some will be more nested.
(def example {:root-stats {:event-id "somehash", :user-group 4149, :more-numbers [2817 2818 1753 2604 1211 2837 1522 2509 1776 1208 1123 2819], :utc-how 109, :some-sizes [1000 120], :partner-event-id "5603583573512567772", :partner-user-id "6XNDU_j_IFM", :partner-currency "usd", :language "german", :restrictied-cats [33], :restricted-bad-cats [33], :some-numbers [60 94 97 113 144 145 182 220 232 233 238 334 394 410 445 454 551 42 198 242 255 332 335 474 476 477 481 486 543 562 575], :a-path "http://example.com/some/path", :keywords ["one" "two" "three" "four" "five" "six" "seven" "eight" "nine" "ten"], :timestamp 1473946074, :user-id "foo", :location {:location-id "123455", :location-domain "example.org", :reason :foo}, :geo {:country "deu", :region "by", :city "bayreuth", :postal-code "12345", :metro "123456"}, :isp {:isp "deutsche telekom ag", :homebiz :business},:device {:device-browser :ie, :device-os :windows},:other-data {:foo 6, :bar :no, :baz :no}, :types {:some-other-types [1 2]}, :browser-data {:ip 1234567890, :ua "Mozilla/5.0...."}}, :previous-stats {:previous-id "someprevioushash", :previous-group 1235, :previous-group-meta 12345, :targeting 12}, :other-stats {:value 0.1444544828, :predicted-value 0.1914021849, :measurement "1234567", :other-cost 0.02342021, :decision-confidence 0.9039528962, :host "eu1-foo-bar"}})
;; An updated match "table" to go with the larger example.
(def match-table {:event-id :e_id
:user-group :u_group
:more-numbers :m_num
:utc-how :how
:some-sizes :sizes
:partner-event-id :p_eid
:partner-user-id :p_uid
:partner-currency :p_cur
:restrictied-cats :r_cat
:some-numbers :s_long_ids
:a-path :url
:timestamp :ts
:user-id :user_id
:location-id :l_id
:location-domain :domain
:postal-code :pc
:device-browser :browser
:some-other-types :types
:previous-id :prv_id
:previous-group :prv_group
:targeting :tgt
:predicted-value :f_val
:other-cost :o_cost
:decision-confidence :d_conf})
(defn flatten-record
"Take a nested record and recursively flatten."
[^clojure.lang.PersistentArrayMap record]
(into {}
(for [[k v] record]
(if (map? v)
(flatten-record v)
;; If the key's not found return itself as the default.
{(get match-table k k) v}))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment