2019-09-02 14:29:19 +03:00
|
|
|
# Script for populating the database. You can run it as:
|
|
|
|
#
|
|
|
|
# mix run priv/repo/seeds.exs
|
|
|
|
#
|
|
|
|
# Inside the script, you can read and write to any of your
|
|
|
|
# repositories directly:
|
|
|
|
#
|
|
|
|
# Plausible.Repo.insert!(%Plausible.SomeSchema{})
|
|
|
|
#
|
|
|
|
# We recommend using the bang functions (`insert!`, `update!`
|
|
|
|
# and so on) as they will fail if something goes wrong.
|
2022-11-18 03:46:42 +03:00
|
|
|
|
|
|
|
user = Plausible.Factory.insert(:user, email: "user@plausible.test", password: "plausible")
|
|
|
|
|
2023-03-01 15:11:31 +03:00
|
|
|
beginning_of_time = NaiveDateTime.add(NaiveDateTime.utc_now(), -721, :day)
|
|
|
|
|
|
|
|
site =
|
|
|
|
Plausible.Factory.insert(:site, domain: "dummy.site", native_stats_start_at: beginning_of_time)
|
2022-11-18 03:46:42 +03:00
|
|
|
|
2023-02-14 15:32:18 +03:00
|
|
|
_membership = Plausible.Factory.insert(:site_membership, user: user, site: site, role: :owner)
|
2022-11-18 03:46:42 +03:00
|
|
|
|
2023-03-01 15:11:31 +03:00
|
|
|
put_random_time = fn
|
|
|
|
date, 0 ->
|
|
|
|
current_hour = Time.utc_now().hour
|
|
|
|
current_minute = Time.utc_now().minute
|
|
|
|
random_time = Time.new!(:rand.uniform(current_hour), :rand.uniform(current_minute - 1), 0)
|
|
|
|
|
|
|
|
date
|
|
|
|
|> NaiveDateTime.new!(random_time)
|
|
|
|
|> NaiveDateTime.truncate(:second)
|
|
|
|
|
|
|
|
date, _ ->
|
|
|
|
random_time = Time.new!(:rand.uniform(23), :rand.uniform(59), 0)
|
2022-11-18 03:46:42 +03:00
|
|
|
|
2023-03-01 15:11:31 +03:00
|
|
|
date
|
|
|
|
|> NaiveDateTime.new!(random_time)
|
|
|
|
|> NaiveDateTime.truncate(:second)
|
2022-11-18 03:46:42 +03:00
|
|
|
end
|
|
|
|
|
2023-01-31 18:15:01 +03:00
|
|
|
geolocations = [
|
|
|
|
[
|
|
|
|
country_code: "IT",
|
|
|
|
subdivision1_code: "IT-62",
|
|
|
|
subdivision2_code: "IT-RM",
|
|
|
|
city_geoname_id: 3_169_070
|
|
|
|
],
|
|
|
|
[
|
|
|
|
country_code: "EE",
|
|
|
|
subdivision1_code: "EE-37",
|
|
|
|
subdivision2_code: "EE-784",
|
|
|
|
city_geoname_id: 588_409
|
|
|
|
],
|
|
|
|
[
|
|
|
|
country_code: "BR",
|
|
|
|
subdivision1_code: "BR-SP",
|
|
|
|
subdivision2_code: "",
|
|
|
|
city_geoname_id: 3_448_439
|
|
|
|
],
|
|
|
|
[
|
|
|
|
country_code: "PL",
|
|
|
|
subdivision1_code: "PL-14",
|
|
|
|
subdivision2_code: "",
|
|
|
|
city_geoname_id: 756_135
|
|
|
|
],
|
|
|
|
[
|
|
|
|
country_code: "DE",
|
|
|
|
subdivision1_code: "DE-BE",
|
|
|
|
subdivision2_code: "",
|
|
|
|
city_geoname_id: 2_950_159
|
|
|
|
],
|
|
|
|
[
|
|
|
|
country_code: "US",
|
|
|
|
subdivision1_code: "US-CA",
|
|
|
|
subdivision2_code: "",
|
|
|
|
city_geoname_id: 5_391_959
|
|
|
|
],
|
|
|
|
[]
|
|
|
|
]
|
|
|
|
|
2022-11-18 03:46:42 +03:00
|
|
|
Enum.flat_map(-720..0, fn day_index ->
|
|
|
|
date = Date.add(Date.utc_today(), day_index)
|
2023-01-31 18:15:01 +03:00
|
|
|
number_of_events = 0..:rand.uniform(500)
|
|
|
|
|
2023-02-16 11:00:26 +03:00
|
|
|
Enum.map(number_of_events, fn _ ->
|
|
|
|
geolocation = Enum.random(geolocations)
|
2022-11-18 03:46:42 +03:00
|
|
|
|
2023-04-04 11:55:12 +03:00
|
|
|
if Plausible.v2?() do
|
|
|
|
[
|
|
|
|
site_id: site.id,
|
|
|
|
hostname: site.domain,
|
|
|
|
timestamp: put_random_time.(date, day_index),
|
|
|
|
referrer_source: Enum.random(["", "Facebook", "Twitter", "DuckDuckGo", "Google"]),
|
|
|
|
browser: Enum.random(["Edge", "Chrome", "Safari", "Firefox", "Vivaldi"]),
|
|
|
|
browser_version: to_string(Enum.random(0..50)),
|
|
|
|
screen_size: Enum.random(["Mobile", "Tablet", "Desktop", "Laptop"]),
|
|
|
|
operating_system: Enum.random(["Windows", "macOS", "Linux"]),
|
|
|
|
operating_system_version: to_string(Enum.random(0..15))
|
|
|
|
]
|
|
|
|
else
|
|
|
|
[
|
|
|
|
domain: site.domain,
|
|
|
|
hostname: site.domain,
|
|
|
|
timestamp: put_random_time.(date, day_index),
|
|
|
|
referrer_source: Enum.random(["", "Facebook", "Twitter", "DuckDuckGo", "Google"]),
|
|
|
|
browser: Enum.random(["Edge", "Chrome", "Safari", "Firefox", "Vivaldi"]),
|
|
|
|
browser_version: to_string(Enum.random(0..50)),
|
|
|
|
screen_size: Enum.random(["Mobile", "Tablet", "Desktop", "Laptop"]),
|
|
|
|
operating_system: Enum.random(["Windows", "macOS", "Linux"]),
|
|
|
|
operating_system_version: to_string(Enum.random(0..15))
|
|
|
|
]
|
|
|
|
end
|
2023-02-16 11:00:26 +03:00
|
|
|
|> Keyword.merge(geolocation)
|
|
|
|
|> then(&Plausible.Factory.build(:pageview, &1))
|
|
|
|
end)
|
2022-11-18 03:46:42 +03:00
|
|
|
end)
|
|
|
|
|> Plausible.TestUtils.populate_stats()
|