-
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.
add ProductAvailabilitySnapshot purging job (#72)
- Loading branch information
1 parent
34c0e90
commit f9744f7
Showing
4 changed files
with
76 additions
and
3 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
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
47 changes: 47 additions & 0 deletions
47
lib/nerves_metal_detector/jobs/product_availability_snapshots_purge.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,47 @@ | ||
defmodule NervesMetalDetector.Jobs.ProductAvailabilitySnapshotsPurge do | ||
use Oban.Worker, | ||
queue: :product_availability_snapshots_purge, | ||
max_attempts: 3 | ||
|
||
import Ecto.Query, only: [dynamic: 2] | ||
|
||
alias NervesMetalDetector.Inventory | ||
alias NervesMetalDetector.Inventory.ProductAvailabilitySnapshot | ||
alias NervesMetalDetector.TimeSeries | ||
alias NervesMetalDetector.Repo | ||
|
||
@impl Oban.Worker | ||
def perform(%Oban.Job{args: %{"vendor" => vendor, "sku" => sku}}) do | ||
snapshots = list_snapshots(vendor, sku) | ||
|
||
{_, to_delete} = | ||
TimeSeries.consecutive_dedup(snapshots, fn %ProductAvailabilitySnapshot{} = snapshot, | ||
_index, | ||
_total_count -> | ||
{DateTime.to_date(snapshot.fetched_at), snapshot.price, snapshot.items_in_stock, | ||
snapshot.in_stock} | ||
end) | ||
|
||
for snapshot <- to_delete do | ||
Process.sleep(50) | ||
Repo.delete(snapshot) | ||
end | ||
|
||
:ok | ||
end | ||
|
||
defp list_snapshots(vendor, sku) do | ||
two_weeks_ago = | ||
DateTime.now!("Etc/UTC") | ||
|> DateTime.to_date() | ||
|> Date.add(-13) | ||
|> DateTime.new!(Time.new!(0, 0, 0), "Etc/UTC") | ||
|
||
Inventory.list_product_availability_snapshots( | ||
dynamic( | ||
[s], | ||
s.vendor == ^vendor and s.sku == ^sku and s.fetched_at < ^two_weeks_ago | ||
) | ||
) | ||
end | ||
end |
25 changes: 25 additions & 0 deletions
25
lib/nerves_metal_detector/jobs/schedule_product_availability_snapshots_purges.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,25 @@ | ||
defmodule NervesMetalDetector.Jobs.ScheduleProductAvailabilitySnapshotsPurges do | ||
use Oban.Worker, | ||
queue: :product_availability_snapshots_purge, | ||
max_attempts: 1 | ||
|
||
alias NervesMetalDetector.Vendors | ||
alias NervesMetalDetector.Inventory.Data.ProductUpdateItems | ||
alias NervesMetalDetector.Jobs.ProductAvailabilitySnapshotsPurge | ||
|
||
@impl Oban.Worker | ||
def perform(_job) do | ||
vendors = Vendors.all() | ||
|
||
for vendor <- vendors do | ||
{:ok, product_update_items} = ProductUpdateItems.get_by_vendor(vendor) | ||
|
||
for item <- product_update_items do | ||
ProductAvailabilitySnapshotsPurge.new(%{vendor: vendor.id, sku: item.sku}) | ||
|> Oban.insert() | ||
end | ||
end | ||
|
||
:ok | ||
end | ||
end |