From 1a9e5f31a8d6906cfd756cefb8fd9f6a9f027ade Mon Sep 17 00:00:00 2001 From: Marcos Oliveira Date: Sat, 20 Oct 2018 00:38:45 -0700 Subject: [PATCH 1/4] add pagination for jobs in the data layer --- lib/elixir_bench/benchmarks/benchmarks.ex | 20 ++++++++++++++- test/elixir_bench/jobs/jobs_test.exs | 31 +++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 test/elixir_bench/jobs/jobs_test.exs diff --git a/lib/elixir_bench/benchmarks/benchmarks.ex b/lib/elixir_bench/benchmarks/benchmarks.ex index 9b6bba3..0f800b1 100644 --- a/lib/elixir_bench/benchmarks/benchmarks.ex +++ b/lib/elixir_bench/benchmarks/benchmarks.ex @@ -7,6 +7,9 @@ defmodule ElixirBench.Benchmarks do alias ElixirBench.Github alias ElixirBench.Benchmarks.{Benchmark, Measurement, Job, Runner, Config} + @jobs_default_limit 10 + @jobs_max_limit 50 + def data() do Dataloader.Ecto.new(Repo, query: &query/2) end @@ -69,8 +72,23 @@ defmodule ElixirBench.Benchmarks do Repo.all(from(j in Job, where: j.repo_id in ^repo_ids)) end + def paginate(query, page, size) do + from(query, + limit: ^size, + offset: ^((page - 1) * size) + ) + end + def list_jobs() do - Repo.all(Job) + Job + |> paginate(1, @jobs_default_limit) + |> Repo.all() + end + + def list_jobs(page, size) do + Job + |> paginate(page, size) + |> Repo.all() end def get_or_create_job(repo, %{commit_sha: commit_sha} = attrs) do diff --git a/test/elixir_bench/jobs/jobs_test.exs b/test/elixir_bench/jobs/jobs_test.exs new file mode 100644 index 0000000..fcc1180 --- /dev/null +++ b/test/elixir_bench/jobs/jobs_test.exs @@ -0,0 +1,31 @@ +defmodule ElixirBench.JobsTest do + use ElixirBench.DataCase + import ElixirBench.Factory + alias ElixirBench.Benchmarks + + setup do + Enum.each(0..4, fn _ -> insert(:job) end) + end + + describe "list_jobs/2" do + test "returns 2 jobs for page 1" do + jobs = Benchmarks.list_jobs(1, 2) + assert length(jobs) == 2 + end + + test "returns 2 jobs for page 2" do + jobs = Benchmarks.list_jobs(2, 2) + assert length(jobs) == 2 + end + + test "returns 1 job for page 3" do + jobs = Benchmarks.list_jobs(3, 2) + assert length(jobs) == 1 + end + + test "without page returns all 5 jobs" do + jobs = Benchmarks.list_jobs() + assert length(jobs) == 5 + end + end +end From e39dbe355e12160a4212f3aa78045b467e6c4881 Mon Sep 17 00:00:00 2001 From: Marcos Oliveira Date: Sat, 20 Oct 2018 00:57:29 -0700 Subject: [PATCH 2/4] limit jobs page size to jobs_max_limit --- lib/elixir_bench/benchmarks/benchmarks.ex | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/elixir_bench/benchmarks/benchmarks.ex b/lib/elixir_bench/benchmarks/benchmarks.ex index 0f800b1..c71c988 100644 --- a/lib/elixir_bench/benchmarks/benchmarks.ex +++ b/lib/elixir_bench/benchmarks/benchmarks.ex @@ -86,6 +86,8 @@ defmodule ElixirBench.Benchmarks do end def list_jobs(page, size) do + size = if size < @jobs_max_limit, do: size, else: @jobs_max_limit + Job |> paginate(page, size) |> Repo.all() From 0f52f4d8ed81fb54dd6590f4fba900ecef015ab8 Mon Sep 17 00:00:00 2001 From: Marcos Oliveira Date: Sat, 20 Oct 2018 01:28:15 -0700 Subject: [PATCH 3/4] fix formatting --- lib/elixir_bench/benchmarks/benchmarks.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/elixir_bench/benchmarks/benchmarks.ex b/lib/elixir_bench/benchmarks/benchmarks.ex index c71c988..a4748e7 100644 --- a/lib/elixir_bench/benchmarks/benchmarks.ex +++ b/lib/elixir_bench/benchmarks/benchmarks.ex @@ -73,7 +73,8 @@ defmodule ElixirBench.Benchmarks do end def paginate(query, page, size) do - from(query, + from( + query, limit: ^size, offset: ^((page - 1) * size) ) From cc56d9e7a078c7aa66514d56a82a4f3956749a29 Mon Sep 17 00:00:00 2001 From: Marcos Oliveira Date: Sat, 20 Oct 2018 23:28:01 -0700 Subject: [PATCH 4/4] add page number and page size to the schema --- lib/elixir_bench_web/schema.ex | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/elixir_bench_web/schema.ex b/lib/elixir_bench_web/schema.ex index b74c2ee..a75e371 100644 --- a/lib/elixir_bench_web/schema.ex +++ b/lib/elixir_bench_web/schema.ex @@ -43,8 +43,11 @@ defmodule ElixirBenchWeb.Schema do end field :jobs, list_of(:job) do - resolve(fn _, _ -> - {:ok, Benchmarks.list_jobs()} + arg(:page, :integer, default_value: 1) + arg(:size, :integer, default_value: 10) + + resolve(fn %{page: page_num, size: page_size}, _ -> + {:ok, Benchmarks.list_jobs(page_num, page_size)} end) end