Bläddra i källkod

fix sparkline inserting zeroes where none should be

master
Inhji Y. 1 år sedan
förälder
incheckning
23c42764e0
2 ändrade filer med 37 tillägg och 8 borttagningar
  1. +13
    -8
      lib/dagon/listens/sparkline.ex
  2. +24
    -0
      test/dagon/listens/sparkline_test.exs

+ 13
- 8
lib/dagon/listens/sparkline.ex Visa fil

@ -6,12 +6,8 @@ defmodule Dagon.Listens.Sparkline do
beginning_interval = get_duration(oldest_listen.listened_at, date_first)
end_interval = get_duration(date_last, newest_listen.listened_at)
filled_listens =
listens_per_month
|> fill_gaps()
fill_list(beginning_interval) ++
filled_listens ++
fill_gaps(listens_per_month) ++
fill_list(end_interval)
end
@ -27,7 +23,16 @@ defmodule Dagon.Listens.Sparkline do
end
end
defp fill_gaps(listens) do
def stupid_diff(from, until) do
from = Timex.to_date(from)
until = Timex.to_date(until)
(until.month - from.month) + (year_diff(from, until) * 12)
end
def year_diff(from, until), do: until.year - from.year
def fill_gaps(listens) do
new_list =
for {[count, listen], index} <- Enum.with_index(listens) do
case Enum.at(listens, index + 1) do
@ -36,8 +41,8 @@ defmodule Dagon.Listens.Sparkline do
[_, next_listen] ->
duration =
Timex.Interval.new(from: listen, until: next_listen)
|> Timex.Interval.duration(:months)
(stupid_diff(listen, next_listen) - 1)
|> IO.inspect()
|> fill_list()
[count] ++ duration


+ 24
- 0
test/dagon/listens/sparkline_test.exs Visa fil

@ -0,0 +1,24 @@
defmodule Dagon.Listens.SparklineTest do
use Dagon.DataCase
alias Dagon.Listens.Sparkline
@august ~D[2008-08-31]
@september ~D[2008-09-30]
@october ~D[2008-10-31]
@december ~D[2008-12-31]
@january ~D[2009-01-31]
describe "sparkline" do
test "stupid_diff correctly diffs months" do
assert Sparkline.stupid_diff(@august, @september) == 1
assert Sparkline.stupid_diff(@august, @october) == 2
assert Sparkline.stupid_diff(@december, @january) == 1
end
test "year_diff correctly diffs years" do
assert Sparkline.year_diff(@august, @september) == 0
end
end
end

Laddar…
Avbryt
Spara