commit feaf3f03574a949e1e2b9ce78791b2e85283a62f Author: thread Date: Wed Apr 15 20:02:05 2026 -0400 my solution diff --git a/.formatter.exs b/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e793a55 --- /dev/null +++ b/.gitignore @@ -0,0 +1,25 @@ +# The directory Mix will write compiled artifacts to. +/_build/ + +# If you run "mix test --cover", coverage assets end up here. +/cover/ + +# The directory Mix downloads your dependencies sources to. +/deps/ + +# Where third-party dependencies like ExDoc output generated docs. +/doc/ + +# Temporary files, for example, from tests. +/tmp/ + +# If the VM crashes, it generates a dump, let's ignore it too. +erl_crash.dump + +# Also ignore archive artifacts (built via "mix archive.build"). +*.ez + +# Ignore package tarball (built via "mix hex.build"). +jump-*.tar + +/jump diff --git a/README.md b/README.md new file mode 100644 index 0000000..763be1a --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +# Jump + +**TODO: Add description** + +## Installation + +If [available in Hex](https://hex.pm/docs/publish), the package can be installed +by adding `jump` to your list of dependencies in `mix.exs`: + +```elixir +def deps do + [ + {:jump, "~> 0.1.0"} + ] +end +``` + +Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc) +and published on [HexDocs](https://hexdocs.pm). Once published, the docs can +be found at . + diff --git a/config/config.exs b/config/config.exs new file mode 100644 index 0000000..981710b --- /dev/null +++ b/config/config.exs @@ -0,0 +1,3 @@ +import Config + +config :jump, :base_url, "https://wikimedia.org/api/rest_v1" diff --git a/lib/cli.ex b/lib/cli.ex new file mode 100644 index 0000000..a18802c --- /dev/null +++ b/lib/cli.ex @@ -0,0 +1,50 @@ +defmodule Jump.CLI do + def main(args \\ []) do + parsed_args = parse_args(args) + + parsed_args + |> fetch() + |> calculate(parsed_args) + |> finish() + end + + defp parse_args(args) do + {opts, _, _} = OptionParser.parse(args, switches: [article: :string, days: :integer]) + opts + end + + defp fetch(args) do + the_end = Calendar.strftime(DateTime.utc_now(), "%Y%m%d") + {the_end_int, ""} = Integer.parse(the_end) + + start = the_end_int - args[:days] + + Jump.fetch(args[:article], start, the_end) + end + + defp calculate(items, args) do + total_pageviews = Enum.sum_by(items, &Map.get(&1, "views")) + max_views_item = Enum.max_by(items, &Map.get(&1, "views")) + + %{ + event_name: "campaign_spike_conversion", + article: args[:article], + window_days: args[:days], + total_pageviews: total_pageviews, + avg_daily_pageviews: total_pageviews / length(items), + top_day: %{ + date: max_views_item["timestamp"], + pageviews: max_views_item["views"] + } + } + end + + defp finish(summary) do + Jump.post(summary) + + IO.inspect(summary) + + IO.puts("done.") + end + +end diff --git a/lib/jump.ex b/lib/jump.ex new file mode 100644 index 0000000..fc67e30 --- /dev/null +++ b/lib/jump.ex @@ -0,0 +1,19 @@ +defmodule Jump do + + @base_url "https://wikimedia.org/api/rest_v1" + + def fetch(article, start, the_end) do + url = "/metrics/pageviews/per-article/en.wikipedia.org/all-access/user/#{article}/daily/#{start}/#{the_end}" + + %{body: %{"items" => items}} = Req.get!(req(), url: url) + items + end + + def post(summary) do + Req.post!("https://postman-echo.com/post", body: Jason.encode!(summary)) + end + + def req do + Req.new(base_url: @base_url) + end +end diff --git a/mix.exs b/mix.exs new file mode 100644 index 0000000..7269b38 --- /dev/null +++ b/mix.exs @@ -0,0 +1,34 @@ +defmodule Jump.MixProject do + use Mix.Project + + def project do + [ + app: :jump, + version: "0.1.0", + elixir: "~> 1.19", + start_permanent: Mix.env() == :prod, + deps: deps(), + escript: escript() + ] + end + + # Run "mix help compile.app" to learn about applications. + def application do + [ + extra_applications: [:logger] + ] + end + + def escript do + [main_module: Jump.CLI] + end + + # Run "mix help deps" to learn about dependencies. + defp deps do + [ + # {:dep_from_hexpm, "~> 0.3.0"}, + # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"} + {:req, "~> 0.5.17"} + ] + end +end diff --git a/mix.lock b/mix.lock new file mode 100644 index 0000000..2992158 --- /dev/null +++ b/mix.lock @@ -0,0 +1,11 @@ +%{ + "finch": {:hex, :finch, "0.21.0", "b1c3b2d48af02d0c66d2a9ebfb5622be5c5ecd62937cf79a88a7f98d48a8290c", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.6.2 or ~> 1.7", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.4 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 1.1", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "87dc6e169794cb2570f75841a19da99cfde834249568f2a5b121b809588a4377"}, + "hpax": {:hex, :hpax, "1.0.3", "ed67ef51ad4df91e75cc6a1494f851850c0bd98ebc0be6e81b026e765ee535aa", [:mix], [], "hexpm", "8eab6e1cfa8d5918c2ce4ba43588e894af35dbd8e91e6e55c817bca5847df34a"}, + "jason": {:hex, :jason, "1.4.4", "b9226785a9aa77b6857ca22832cffa5d5011a667207eb2a0ad56adb5db443b8a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "c5eb0cab91f094599f94d55bc63409236a8ec69a21a67814529e8d5f6cc90b3b"}, + "mime": {:hex, :mime, "2.0.7", "b8d739037be7cd402aee1ba0306edfdef982687ee7e9859bee6198c1e7e2f128", [:mix], [], "hexpm", "6171188e399ee16023ffc5b76ce445eb6d9672e2e241d2df6050f3c771e80ccd"}, + "mint": {:hex, :mint, "1.7.1", "113fdb2b2f3b59e47c7955971854641c61f378549d73e829e1768de90fc1abf1", [:mix], [{:castore, "~> 0.1.0 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:hpax, "~> 0.1.1 or ~> 0.2.0 or ~> 1.0", [hex: :hpax, repo: "hexpm", optional: false]}], "hexpm", "fceba0a4d0f24301ddee3024ae116df1c3f4bb7a563a731f45fdfeb9d39a231b"}, + "nimble_options": {:hex, :nimble_options, "1.1.1", "e3a492d54d85fc3fd7c5baf411d9d2852922f66e69476317787a7b2bb000a61b", [:mix], [], "hexpm", "821b2470ca9442c4b6984882fe9bb0389371b8ddec4d45a9504f00a66f650b44"}, + "nimble_pool": {:hex, :nimble_pool, "1.1.0", "bf9c29fbdcba3564a8b800d1eeb5a3c58f36e1e11d7b7fb2e084a643f645f06b", [:mix], [], "hexpm", "af2e4e6b34197db81f7aad230c1118eac993acc0dae6bc83bac0126d4ae0813a"}, + "req": {:hex, :req, "0.5.17", "0096ddd5b0ed6f576a03dde4b158a0c727215b15d2795e59e0916c6971066ede", [:mix], [{:brotli, "~> 0.3.1", [hex: :brotli, repo: "hexpm", optional: true]}, {:ezstd, "~> 1.0", [hex: :ezstd, repo: "hexpm", optional: true]}, {:finch, "~> 0.17", [hex: :finch, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:mime, "~> 2.0.6 or ~> 2.1", [hex: :mime, repo: "hexpm", optional: false]}, {:nimble_csv, "~> 1.0", [hex: :nimble_csv, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "0b8bc6ffdfebbc07968e59d3ff96d52f2202d0536f10fef4dc11dc02a2a43e39"}, + "telemetry": {:hex, :telemetry, "1.4.1", "ab6de178e2b29b58e8256b92b382ea3f590a47152ca3651ea857a6cae05ac423", [:rebar3], [], "hexpm", "2172e05a27531d3d31dd9782841065c50dd5c3c7699d95266b2edd54c2dafa1c"}, +} diff --git a/test/jump_test.exs b/test/jump_test.exs new file mode 100644 index 0000000..80f24af --- /dev/null +++ b/test/jump_test.exs @@ -0,0 +1,8 @@ +defmodule JumpTest do + use ExUnit.Case + doctest Jump + + test "greets the world" do + assert Jump.hello() == :world + end +end diff --git a/test/test_helper.exs b/test/test_helper.exs new file mode 100644 index 0000000..869559e --- /dev/null +++ b/test/test_helper.exs @@ -0,0 +1 @@ +ExUnit.start()