Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ defmodule ExCubicIngestion.Schema.CubicLoad do
From a list of S3 objects defined as maps, filter in only new objects since last load and
insert them in database.
"""
@spec insert_new_from_objects_with_table([map()], CubicTable.t()) ::
@spec insert_new_from_objects_with_table([map()], CubicTable.t(), integer()) ::
{:ok,
{CubicOdsTableSnapshot.t() | nil,
[
{t(), CubicOdsLoadSnapshot.t() | nil, CubicTable.t(),
CubicOdsTableSnapshot.t() | nil}
]}}
| {:error, term()}
def insert_new_from_objects_with_table(objects, table) do
def insert_new_from_objects_with_table(objects, table, limit \\ 500) do
# get ODS snapshot if available
ods_table_snapshot = CubicOdsTableSnapshot.get_by(table_id: table.id)

Expand All @@ -122,12 +122,14 @@ defmodule ExCubicIngestion.Schema.CubicLoad do
# create a list of objects that have a last modified that is later than
# the last object we have in database
new_objects =
Enum.filter(objects, fn object ->
objects
|> Enum.filter(fn object ->
last_modified = parse_and_drop_msec(object.last_modified)

is_nil(last_inserted_load) or
DateTime.compare(last_modified, last_inserted_load.s3_modified) == :gt
end)
|> Enum.take(limit)

if Enum.empty?(new_objects) do
{:ok, {ods_table_snapshot, []}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,22 @@ defmodule ExCubicIngestion.Schema.CubicLoadTest do
]}} = CubicLoad.insert_new_from_objects_with_table(ods_load_objects, ods_table)
end

test "limiting list of ODS objects", %{
ods_table: ods_table,
ods_load_objects: ods_load_objects
} do
{:ok, {_last_ods_table_snapshot, new_ods_load_recs}} =
CubicLoad.insert_new_from_objects_with_table(ods_load_objects, ods_table, 1)
Comment thread
ealexa05 marked this conversation as resolved.

assert [List.first(ods_load_objects).key] ==
new_ods_load_recs
|> Enum.reverse()
|> Enum.map(fn {%CubicLoad{status: "ready", s3_key: s3_key}, _ods_load_snapshot,
_table, _ods_table_snapshot} ->
s3_key
end)
end

test "providing an empty list of objects", %{
dmap_table: dmap_table
} do
Expand Down