Skip to content

Commit

Permalink
collapse-and-fill-range: joins both ops into one
Browse files Browse the repository at this point in the history
It doesn't make sense to use them separately for now. It helps to ensure the
same step is passed to both fns
  • Loading branch information
nberger committed May 14, 2015
1 parent ad05ae2 commit 385df38
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 30 deletions.
8 changes: 4 additions & 4 deletions src/time_series_storage/postgres.clj
Expand Up @@ -100,10 +100,10 @@
query-data
(tcoerce/from-date start)
(tcoerce/from-date finish))
(collapse (or step :none))
(fill-range start
finish
(or step :none)))
(collapse-and-fill-range
start
finish
(or step :none)))
(throw (Exception. (format "Non existent dimension %s specified. Please check your schema" dimension))))
(throw (Exception. (format "Non existent fact %s specified. Please check your schema." fact)))))

Expand Down
6 changes: 6 additions & 0 deletions src/time_series_storage/query.clj
Expand Up @@ -108,3 +108,9 @@
step)]
;;TODO the filler should be by dimension definition
{(tcoerce/to-date date) (or (get series date) 0)}))}))))

(defn collapse-and-fill-range [data start finish step]
(-> (collapse data step)
(fill-range start
finish
step)))
49 changes: 23 additions & 26 deletions test/time_series_storage/query_test.clj
Expand Up @@ -9,69 +9,66 @@
(let [start #inst "2015-03-21T09:00:00"
data-points [{:timestamp (tcoerce/to-string start) :counter 2 :key "some-key"}]]
(is (= {{:key "some-key"} {#inst "2015-03-21T09:00" 2}}
(fill-range (collapse data-points :hour)
start
#inst "2015-03-21T09:40:00"
:hour)))))
(collapse-and-fill-range data-points
start
#inst "2015-03-21T09:40:00"
:hour)))))

(testing "1 data point trivial timeseries without step"
(let [start #inst "2015-03-21T09:00:00"
data-points [{:timestamp (tcoerce/to-string start) :counter 2 :key "some-key"}]]
(is (= {{:key "some-key"} {:all 2}}
(fill-range (collapse data-points :none)
start
#inst "2015-03-21T09:40:00"
:none)))))
(collapse-and-fill-range data-points
start
#inst "2015-03-21T09:40:00"
:none)))))

(testing "many data points timeseries"
(let [start (tcoerce/from-date #inst "2015-03-21T09:00:00")
data-points [{:timestamp (tcoerce/to-string start) :counter 2 :key "some-key"}
{:timestamp (tcoerce/to-string (t/plus- start (t/minutes 40))) :counter 1 :key "some-key"}
{:timestamp (tcoerce/to-string (t/plus- start (t/minutes 80))) :counter 8 :key "some-key"}
{:timestamp (tcoerce/to-string (t/plus- start (t/minutes 130))) :counter 4 :key "some-key"}]
collapsed (collapse data-points :hour)]
{:timestamp (tcoerce/to-string (t/plus- start (t/minutes 130))) :counter 4 :key "some-key"}]]
(is (= {{:key "some-key"} {(tcoerce/to-date start) 3
(tcoerce/to-date (t/plus- start (t/hours 1))) 8
(tcoerce/to-date (t/plus- start (t/hours 2))) 4}}
(fill-range collapsed
(tcoerce/to-date start)
#inst "2015-03-21T11:40:00"
:hour))))))
(collapse-and-fill-range data-points
(tcoerce/to-date start)
#inst "2015-03-21T11:40:00"
:hour))))))

(deftest fill-range-collapse-average-test
(testing "1 data point trivial timeseries without step"
(let [start #inst "2015-03-21T09:00:00"
step :none
data-points [{:timestamp (tcoerce/to-string start) :total 20 :counter 2 :key "some-key"}]]
(is (= {{:key "some-key"} {:all {:total 20 :counter 2}}}
(fill-range (collapse data-points step)
start
#inst "2015-03-21T09:40:00"
step)))))
(collapse-and-fill-range data-points
start
#inst "2015-03-21T09:40:00"
step)))))

(testing "1 data point trivial timeseries with step"
(let [start #inst "2015-03-21T09:00:00"
step :hour
data-points [{:timestamp (tcoerce/to-string start) :total 20 :counter 2 :key "some-key"}]]
(is (= {{:key "some-key"} {#inst "2015-03-21T09:00" {:total 20 :counter 2}}}
(fill-range (collapse data-points step)
start
#inst "2015-03-21T09:40:00"
step)))))
(collapse-and-fill-range data-points
start
#inst "2015-03-21T09:40:00"
step)))))

(testing "many data points timeseries"
(let [start (tcoerce/from-date #inst "2015-03-21T09:00:00")
step :hour
data-points [{:timestamp (tcoerce/to-string start) :total 20 :counter 2 :key "some-key"}
{:timestamp (tcoerce/to-string (t/plus- start (t/minutes 40))) :total 30 :counter 1 :key "some-key"}
{:timestamp (tcoerce/to-string (t/plus- start (t/minutes 80))) :total 34 :counter 8 :key "some-key"}
{:timestamp (tcoerce/to-string (t/plus- start (t/minutes 130))) :total 42 :counter 4 :key "some-key"}
]
collapsed (collapse data-points step)]
{:timestamp (tcoerce/to-string (t/plus- start (t/minutes 130))) :total 42 :counter 4 :key "some-key"}]]
(is (= {{:key "some-key"} {(tcoerce/to-date start) {:total 50 :counter 3}
(tcoerce/to-date (t/plus- start (t/hours 1))) {:total 34 :counter 8}
(tcoerce/to-date (t/plus- start (t/hours 2))) {:total 42 :counter 4}}}
(fill-range collapsed
(collapse-and-fill-range data-points
(tcoerce/to-date start)
#inst "2015-03-21T11:40:00"
step))))))

0 comments on commit 385df38

Please sign in to comment.