-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from egze/refactor
Refactor
- Loading branch information
Showing
7 changed files
with
180 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import Config | ||
|
||
config :logger, level: :warning |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
defmodule Obanalyze.NavItem do | ||
defstruct [:name, :label, :timestamp_field] | ||
|
||
def new(state, count, timestamp_field) do | ||
%__MODULE__{ | ||
name: state, | ||
label: "#{Phoenix.Naming.humanize(state)} (#{count})", | ||
timestamp_field: timestamp_field | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
defmodule Obanalyze.ObanJobs do | ||
import Ecto.Query, only: [group_by: 3, order_by: 2, order_by: 3, select: 3, limit: 2, where: 3] | ||
|
||
def get_oban_job(id) do | ||
Oban.Repo.get(Oban.config(), Oban.Job, id) | ||
end | ||
|
||
def list_jobs_with_total(params, job_state) do | ||
total_jobs = Oban.Repo.aggregate(Oban.config(), jobs_count_query(job_state), :count) | ||
|
||
jobs = | ||
Oban.Repo.all(Oban.config(), jobs_query(params, job_state)) |> Enum.map(&Map.from_struct/1) | ||
|
||
{jobs, total_jobs} | ||
end | ||
|
||
defp jobs_query(%{sort_by: sort_by, sort_dir: sort_dir, limit: limit}, job_state) do | ||
Oban.Job | ||
|> limit(^limit) | ||
|> where([job], job.state == ^job_state) | ||
|> order_by({^sort_dir, ^sort_by}) | ||
end | ||
|
||
defp jobs_count_query(job_state) do | ||
Oban.Job | ||
|> where([job], job.state == ^job_state) | ||
end | ||
|
||
def job_states_with_count do | ||
Oban.Repo.all( | ||
Oban.config(), | ||
Oban.Job | ||
|> group_by([j], [j.state]) | ||
|> order_by([j], [j.state]) | ||
|> select([j], {j.state, count(j.id)}) | ||
) | ||
|> Enum.into(%{}) | ||
end | ||
|
||
def timestamp_field_for_job_state(job_state, default \\ :attempted_at) do | ||
case job_state do | ||
"available" -> :scheduled_at | ||
"cancelled" -> :cancelled_at | ||
"completed" -> :completed_at | ||
"discarded" -> :discarded_at | ||
"executing" -> :attempted_at | ||
"retryable" -> :scheduled_at | ||
"scheduled" -> :scheduled_at | ||
_ -> default | ||
end | ||
end | ||
end |
Oops, something went wrong.