Commit a4723a16 authored by Ahmad Sattar's avatar Ahmad Sattar

A big upgrade: Added custom ranges for different seed data. Seed can randomly…

A big upgrade: Added custom ranges for different seed data. Seed can randomly generate data given by a set of dates and range of xp along with a set of languages
parent 4851d7a8
......@@ -9,18 +9,59 @@
#
# We recommend using the bang functions (`insert!`, `update!`
# and so on) as they will fail if something goes wrong.
user = %{
email: "test@test.test",
username: "test",
password: "test",
terms_version: CodeStats.LegalTerms.get_latest_version()
}
user = %{
email: "test@test.test",
username: "test",
password: "test",
terms_version: CodeStats.LegalTerms.get_latest_version()
}
languages = ["elixir", "javascript"]
defmodule Seeds do
@day_seconds 86400
def create_date_list(%{"from" => in_from, "to" => in_to}) do
from = Calendar.DateTime.from_erl!({in_from, {16, 00, 00}}, "Etc/UTC")
to = Calendar.DateTime.from_erl!({in_to, {16, 00, 00}}, "Etc/UTC")
{:ok, diff, _, _} = Calendar.DateTime.diff(to, from)
diff_days = div(diff, @day_seconds)
dates = 0..diff_days
|> Enum.map(&advance_by_day(&1, from))
end
def create_date_xp_list(dates, %{"min" => min, "max" => max}) do
random_xp = (1..Enum.count(dates))
|> Enum.map(fn x -> Enum.random(min..max) end)
IO.inspect Enum.count(random_xp)
IO.inspect Enum.count(dates)
date_xp = Enum.zip(dates, random_xp)
end
def create_data_for({:ok, language}, user, machine) do
{:ok, sent_at} = Calendar.DateTime.from_erl({{2018, 11, 27}, {23, 00, 00}}, "Etc/UTC")
local_datetime = Calendar.DateTime.add!(sent_at, 3600) |> Calendar.DateTime.to_naive()
#{:ok, sent_at} = Calendar.DateTime.from_erl({{2018, 11, 27}, {23, 00, 00}}, "Etc/UTC")
#local_datetime = Calendar.DateTime.add!(sent_at, 60*60*24) |> Calendar.DateTime.to_naive()
dates = create_date_list(%{
"from" => {2018,01,01},
"to" => {2018,02,01}
})
dates_xp = create_date_xp_list(dates, %{
"min" => 500,
"max" => 1000
})
Enum.map(dates_xp, fn {sent_at, xp} ->
create_seed_data(sent_at, xp, user, machine, language)
end)
end
def create_seed_data(sent_at, xp, user, machine, language) do
local_datetime = Calendar.DateTime.add!(sent_at, 60*60*24) |> Calendar.DateTime.to_naive()
{:ok, pulse} =
CodeStats.User.Pulse.changeset(%CodeStats.User.Pulse{sent_at: sent_at, tz_offset: 60, sent_at_local: local_datetime}, %{})
......@@ -29,7 +70,7 @@ defmodule Seeds do
|> CodeStats.Repo.insert()
{:ok, _} =
CodeStats.XP.changeset(%CodeStats.XP{amount: 1})
CodeStats.XP.changeset(%CodeStats.XP{amount: xp})
|> Ecto.Changeset.put_change(:pulse_id, pulse.id)
|> Ecto.Changeset.put_change(:language_id, language.id)
|> Ecto.Changeset.put_change(:original_language_id, language.id)
......@@ -54,6 +95,10 @@ defmodule Seeds do
{:ok, fetched_user, machine}
end
defp advance_by_day(additional_day, from) do
Calendar.DateTime.add!(from, additional_day * @day_seconds)
end
end
{:ok, new_user, machine} = Seeds.create_user(user.email, user.username)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment