-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Import des stats mensuelles data.gouv.fr pour les ressources
- Loading branch information
1 parent
c471d43
commit 3e46684
Showing
8 changed files
with
362 additions
and
39 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,26 @@ | ||
defmodule DB.ResourceMonthlyMetric do | ||
@moduledoc """ | ||
Monthly metrics related to resources as given by the data.gouv.fr | ||
API. | ||
Example: https://metric-api.data.gouv.fr/api/resources/data/?metric_month__sort=desc&resource_id__exact=e0dbd217-15cd-4e28-9459-211a27511a34&page_size=50 | ||
""" | ||
use Ecto.Schema | ||
use TypedEctoSchema | ||
import Ecto.Changeset | ||
|
||
typed_schema "resource_monthly_metrics" do | ||
belongs_to(:resource, DB.Resource, foreign_key: :resource_datagouv_id, references: :datagouv_id, type: :string) | ||
field(:year_month, :string) | ||
field(:metric_name, Ecto.Enum, values: [:downloads]) | ||
field(:count, :integer) | ||
timestamps(type: :utc_datetime_usec) | ||
end | ||
|
||
def changeset(struct, attrs \\ %{}) do | ||
struct | ||
|> cast(attrs, [:resource_datagouv_id, :year_month, :metric_name, :count]) | ||
|> validate_required([:resource_datagouv_id, :year_month, :metric_name, :count]) | ||
|> validate_format(:year_month, ~r/\A2\d{3}-(0[1-9]|1[012])\z/) | ||
|> validate_number(:count, greater_than_or_equal_to: 0) | ||
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
29 changes: 29 additions & 0 deletions
29
apps/transport/lib/jobs/import_resource_monthly_metrics_job.ex
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,29 @@ | ||
defmodule Transport.Jobs.ImportResourceMonthlyMetricsJob do | ||
@moduledoc """ | ||
Import monthly metrics related to resources coming from the data.gouv.fr's API. | ||
This job is executed daily and imports metrics for all resources for the last 2 years. | ||
Records are not supposed to change in the past, except for the current month. | ||
""" | ||
use Oban.Worker, max_attempts: 3 | ||
import Ecto.Query | ||
|
||
# The number of workers to run in parallel when importing metrics | ||
@task_concurrency 5 | ||
|
||
@impl Oban.Worker | ||
def perform(%Oban.Job{}) do | ||
resource_datagouv_ids() | ||
|> Task.async_stream( | ||
fn datagouv_id -> Transport.Jobs.ImportMonthlyMetrics.import_metrics(:resource, datagouv_id) end, | ||
max_concurrency: @task_concurrency, | ||
on_timeout: :kill_task, | ||
timeout: 10_000 | ||
) | ||
|> Stream.run() | ||
end | ||
|
||
def resource_datagouv_ids do | ||
DB.Resource.base_query() |> select([resource: r], r.datagouv_id) |> DB.Repo.all() | ||
end | ||
end |
16 changes: 16 additions & 0 deletions
16
apps/transport/priv/repo/migrations/20240117075117_resource_monthly_metrics.exs
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,16 @@ | ||
defmodule DB.Repo.Migrations.ResourceMonthlyMetrics do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:resource_monthly_metrics) do | ||
add(:resource_datagouv_id, :string, null: false, size: 50) | ||
# Example: 2023-12 | ||
add(:year_month, :string, null: false, size: 7) | ||
add(:metric_name, :string, null: false, size: 50) | ||
add(:count, :integer, null: false) | ||
timestamps(type: :utc_datetime_usec) | ||
end | ||
|
||
create(unique_index(:resource_monthly_metrics, [:resource_datagouv_id, :year_month, :metric_name])) | ||
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,42 @@ | ||
defmodule DB.ResourceMonthlyMetricTest do | ||
use ExUnit.Case, async: true | ||
import DB.Factory | ||
|
||
setup do | ||
Ecto.Adapters.SQL.Sandbox.checkout(DB.Repo) | ||
end | ||
|
||
describe "changeset" do | ||
test "can insert a record" do | ||
resource = insert(:resource) | ||
|
||
assert %Ecto.Changeset{valid?: true} = | ||
changeset = | ||
DB.ResourceMonthlyMetric.changeset(%DB.ResourceMonthlyMetric{}, %{ | ||
resource_datagouv_id: resource.datagouv_id, | ||
metric_name: :downloads, | ||
count: 42, | ||
year_month: "2023-12" | ||
}) | ||
|
||
DB.Repo.insert!(changeset) | ||
end | ||
|
||
test "identifies errors" do | ||
assert %Ecto.Changeset{ | ||
valid?: false, | ||
errors: [ | ||
{:count, _}, | ||
{:year_month, _}, | ||
{:metric_name, _} | ||
] | ||
} = | ||
DB.ResourceMonthlyMetric.changeset(%DB.ResourceMonthlyMetric{}, %{ | ||
resource_datagouv_id: Ecto.UUID.generate(), | ||
metric_name: :foo, | ||
count: -1, | ||
year_month: "bar" | ||
}) | ||
end | ||
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
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
Oops, something went wrong.