|
31 | 31 | #_(.render ts-chart)
|
32 | 32 | #_(def hover-detail (Rickshaw.Graph.HoverDetail. (clj->js {:graph ts-chart})))
|
33 | 33 |
|
34 |
| -(defn random-data [] (let [series-data (array (array)) |
35 |
| - random (Rickshaw.Fixtures.RandomData. 150)] |
36 |
| - (dotimes [i 100] (.addData random series-data)) |
37 |
| - series-data)) |
| 34 | +(defn random-data [] |
| 35 | + (let [series-data (array (array)) |
| 36 | + random (Rickshaw.Fixtures.RandomData. 150)] |
| 37 | + (dotimes [i 100] (.addData random series-data)) |
| 38 | + series-data)) |
38 | 39 |
|
39 | 40 | ;; https://gist.github.com/msgodf/8495781
|
40 | 41 | (def graph-with-legend
|
41 |
| - (let [series-data (array (array)) |
42 |
| - random (Rickshaw.Fixtures.RandomData. 150)] |
43 |
| - (dotimes [i 10] (.addData random series-data)) |
44 |
| - (doto |
45 |
| - (Rickshaw.Graph. (clj->js {:element ts-elem |
46 |
| - :renderer "bar" |
47 |
| - :width ts-w |
48 |
| - :height 100 |
49 |
| - :series [{:color "steelblue" |
50 |
| - :data (nth series-data 0) |
51 |
| - :name "Tweets"}]})) |
52 |
| - (.render)))) |
53 |
| - |
54 |
| -(defn update [chart] |
55 |
| - (aset graph-with-legend "series" "0" "data" (nth (random-data) 0)) |
56 |
| - (.update chart)) |
| 42 | + (doto |
| 43 | + (Rickshaw.Graph. (clj->js {:element ts-elem |
| 44 | + :renderer "bar" |
| 45 | + :width ts-w |
| 46 | + :height 100 |
| 47 | + :series [{:color "steelblue" |
| 48 | + :data (nth (random-data) 0) |
| 49 | + :name "Tweets"}]})) |
| 50 | + (.render))) |
57 | 51 |
|
58 |
| -;(js/setInterval #(update graph-with-legend) 5000) |
| 52 | +(defn floor [x] (Math/floor x)) |
59 | 53 |
|
| 54 | +(defn date-round [s] |
| 55 | + "return function that rounds the provided seconds since epoch down to the nearest time interval s |
| 56 | + e.g. (date-round 60) creates a function that takes seconds t and rounds them to the nearest minute" |
| 57 | + (fn [t] (* s (floor (/ t s))))) |
| 58 | + |
| 59 | +(def m 60) |
| 60 | +(def hr (* 60 m)) |
| 61 | +(def day (* 24 hr)) |
| 62 | + |
| 63 | +(defn grouper [newest oldest] |
| 64 | + (cond |
| 65 | + (> (- newest oldest) (* 20 day)) (date-round (* 24 60 60)) ;round by nearest day |
| 66 | + (> (- newest oldest) (* 5 day)) (date-round (* 6 60 60)) ;round by nearest quarter day |
| 67 | + (> (- newest oldest) (* 10 hr)) (date-round (* 60 60)) ;round by nearest hour |
| 68 | + (> (- newest oldest) (* 2 hr)) (date-round (* 15 60)) ;round by nearest quarter hour |
| 69 | + :else (date-round 60))) ;round by nearest minute |
| 70 | + |
| 71 | +(defn ts-data [app] |
| 72 | + (let [state @app |
| 73 | + by-id (util/tweets-by-order :tweets-map :by-id) |
| 74 | + tweets-by-id (by-id state 10000) |
| 75 | + oldest (js/moment. (:created_at (last tweets-by-id))) |
| 76 | + newest (js/moment. (:created_at (first tweets-by-id))) |
| 77 | + rounder (grouper (.unix newest) (.unix oldest)) |
| 78 | + ] |
| 79 | + (print "oldest" (.toLocaleString oldest)) |
| 80 | + (print "oldest unix" (.unix oldest)) |
| 81 | + (print "oldest min rounded unix" (round-min (.unix oldest))) |
| 82 | + (print "oldest min rounded parsed" (.toLocaleString (.unix js/moment (round-min (.unix oldest))))) |
| 83 | + (print "oldest hr rounded unix" (round-hr (.unix oldest))) |
| 84 | + (print "oldest hr rounded parsed" (.toLocaleString (.unix js/moment (round-hr (.unix oldest))))) |
| 85 | + (print "oldest day rounded unix" (round-day (.unix oldest))) |
| 86 | + (print "oldest day rounded parsed" (.toLocaleString (.unix js/moment (round-day (.unix oldest))))) |
| 87 | + (print "oldest rounded unix" (rounder (.unix oldest))) |
| 88 | + (print "oldest rounded parsed" (.toLocaleString (.unix js/moment (rounder (.unix oldest))))) |
| 89 | + (print "---") |
| 90 | + (print "newest" (.toLocaleString newest)) |
| 91 | + (print "newest" (.unix newest)) |
| 92 | + (print "---") |
| 93 | + )) |
| 94 | + |
| 95 | +(defn update [chart app] |
| 96 | + (ts-data app) |
| 97 | + (aset graph-with-legend "series" "0" "data" (nth (random-data) 0)) |
| 98 | + (.update chart)) |
0 commit comments