Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Incanter - prepare usage data
(ns clj-charting.usage-chart-preparation
[incanter.core :refer :all]
[incanter.stats :as s]
[ :as io]))
(defn- resolve-unit-suffix
"Replace values such as 333k, 800m, 1.2g with the corresponding value in bytes"
(if-let [[_ val unit] (and
(string? val-suffixed)
(re-find #"(\d+)([kmg])" val-suffixed))]
(let [order (case unit
"k" 1
"m" 2
"g" 3)
scale (apply * (take order (repeat 1024)))]
(* (Integer/parseInt val) scale))))
(defn read-usage-data
"Read usage data in the form `sec.ns memory_with_scale_suffix CPU_percentage` into a dataset with
`ms memory_in_bytes CPU_percentage`"
(let [data (io/read-dataset
:delim \space)]
(-> data
;; Memory: from 300m or 1g to a number:
;; CPU: From <sec>.<nano> to <ms>:
#(long (* 1000 %))))))
(defn moving-window-means
"Given very scattered data, produce a similar sequence of 'moving window mean' where we
replace each point by the mean of it and the preceding/following `radius` points.
[radius col]
(let [x' (concat (repeat radius nil) col)
parts (drop-last radius (partition (inc (* 2 radius)) 1 x'))
means (map #(-> (remove nil? %) s/mean long)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment