Skip to content

Instantly share code, notes, and snippets.

@oitee
Created September 27, 2022 07:14
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 oitee/e0708e3325b2c80e3433a53d687d3a87 to your computer and use it in GitHub Desktop.
Save oitee/e0708e3325b2c80e3433a53d687d3a87 to your computer and use it in GitHub Desktop.
(ns scratch.read-csv
(:require [clojure.string :as string]))
(defn find-val [s key]
(let [s-coll (string/split s key)
val-str (first (string/split (last s-coll) #"[,}]"))
val-cleaned (string/replace val-str #"[\"\\\:]" "")]
val-cleaned))
(defn extract-all-vals [s]
(let [f-name (find-val s #":f-name ")
response-time (find-val s #":rsp-tm-in-mills ")
hs-req-id (find-val s #":x-hs-request-id #uuid ")
app-id (find-val s #"app_id")
lang (find-val s #":lang ")
query (find-val s #":query ")
is-sdkx? (find-val s #":is-sdkx\? ")
is-preview? (find-val s #":is-preview\? ")]
(list f-name response-time hs-req-id app-id lang query is-sdkx? is-preview?)))
(def valid-keys
["f-name" "rsp-tm-in-mills" "x-hs-request-id" "app_id" "lang" "query" "is-sdkx" "is-preview"])
(defn check-valid-keys [s]
(every? (fn [sub-str] (string/includes? s sub-str))
valid-keys))
(defn append-to-file [file s]
(spit file (str s "\n") :append true))
(defn append-header [file-name]
(append-to-file file-name (string/join "," valid-keys)))
(defn populate-file [coll file-name]
(append-header file-name)
(map (fn [l]
(append-to-file file-name (string/join "," (extract-all-vals l))))
(filter check-valid-keys coll)))
(defn read-and-populate [read-file output-file]
(let [contents (slurp read-file)
logs (string/split contents #"\[DEBUG\] HC Search Latency Log")]
(populate-file logs output-file)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment