Skip to content

Instantly share code, notes, and snippets.

@nathanmarz
Created April 16, 2012 01:58
Show Gist options
  • Save nathanmarz/2395978 to your computer and use it in GitHub Desktop.
Save nathanmarz/2395978 to your computer and use it in GitHub Desktop.
(defn vec-cookies [s]
"Return vector of cookies and their values."
(vec
(for [triplet (re-seq cookies s)]
[(second triplet) (last triplet)]))
(defmapop logl [s]
"Vector consisting of log line components, for a single line."
[(re-find remote-addr s)
(second (re-find timestamp s))
(second (re-find method-url s))
(last (re-find method-url s))
(second (re-find response-status s))
(second (re-find upstream-time s))
(second (re-find request-time s))
(second (re-find host-upstream-addr s))
(last (re-find host-upstream-addr s))
(vec-cookies s)])
(defn logdata [inpath]
"Return parsed logfile from HFS tap."
;; Create a textline tap on the target logfile.
(let [src (hfs-textline inpath)
fields ["?remote-addr" "?timestamp" "?method-url1" "?method-url2" "?response-status"
"?upstream-time" "?request-time" "?host-upstream-addr1" "?host-upstream-addr2"
"?cookies"]]
;; Use another textline tap as output target.
(<- fields (src ?c) (logl ?c :>> fields) (:distinct false))))
(defmapcatop explode [l] l)
(defn cookie-line [inpath]
"Get cookie data from the logfile."
(let [data (select-fields (logdata inpath) ["?cookie"])]
(<- [?key ?val]
(data ?cookie)
(explode ?cookie :> ?key ?val)
(:distinct false))))
(let [cl (cookie-line "resources/snippet.log")]
(?<- (stdout) [?key ?count]
(cl ?key _)
(c/count ?count)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment