On Thursday, July 17, 2014 8:49:12 AM UTC-4, [email protected] wrote:
>
> Hi,
>
> I have a list of epoch times which map to HTTP requests.
>
> '(1405060202611
> 1405060201157
> 1405060201361
> 1405060201261
> 1405060200391
> 1405060201458
> 1405060201705
> 1405060201058
> 1405060205062
> 1405060201558
> 1405060201761
> ....)
>
> I am trying to find out how many HTTP requests I have in a specified time
> period, so I can graph requests per second.
>
>
Hi Aidy,
It sounds like you're looking to create a histogram.
There are probably a number of libraries around to do that, but here's
something I whipped up:
~~~clojure
#!/usr/bin/env lein-exec
(def min-val 0)
(def max-val 100)
(def raw-data (sort (for [i (range 20)]
(+ (rand (- max-val min-val))
min-val))))
(println "raw data:" raw-data)
(newline)
(def interval-size 10)
;; e.g.: ((0 10) (10 20) (20 30) ... (90 100))
(def intervals (partition 2
1
(range min-val
(+ max-val interval-size)
interval-size)))
(defn interval-for
"Finds you which interval `x` lands in."
[x]
(first (drop-while (fn [i] (not (and (>= x (first i))
(< x (second i)))))
intervals)))
(let [histo-data (loop [accum (zipmap (map vec intervals)
(repeat []))
d raw-data]
(if (empty? d)
accum
(recur (update-in accum
[(interval-for (first d))]
conj
(first d))
(rest d))))]
(doseq [d (sort histo-data)]
(println d)))
~~~
Hm... my crystal ball tells me that in few minutes someone will point out
how I can replace that `loop` with a `reduce`...
-- John
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.