Skip to content

Commit

Permalink
Add deduplication of overlapping Dropmate objects
Browse files Browse the repository at this point in the history
Closes: #2
  • Loading branch information
sco1 committed Aug 12, 2023
1 parent d2ca94c commit d94d6c7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
24 changes: 12 additions & 12 deletions dropmate_py/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from sco1_misc.prompts import prompt_for_dir, prompt_for_file

from dropmate_py.audits import audit_pipeline
from dropmate_py.parser import log_parse_pipeline
from dropmate_py.parser import log_parse_pipeline, merge_dropmates

MIN_ALT_LOSS = 200 # feet
MIN_FIRMWARE = 5
Expand Down Expand Up @@ -75,17 +75,17 @@ def audit_bulk(
log_files = list(log_dir.glob(log_pattern))
print(f"Found {len(log_files)} log files to process.")

found_errs = []
for log_filepath in log_files:
conslidated_log = log_parse_pipeline(log_filepath)
found_errs.extend(
audit_pipeline(
consolidated_log=conslidated_log,
min_alt_loss_ft=min_alt_loss_ft,
min_firmware=min_firmware,
max_scanned_time_delta_sec=time_delta_minutes * 60,
)
)
compiled_logs = log_parse_pipeline(log_files[0])
for log_filepath in log_files[1:]:
compiled_logs.extend(log_parse_pipeline(log_filepath))

compiled_logs = merge_dropmates(compiled_logs)
found_errs = audit_pipeline(
consolidated_log=compiled_logs,
min_alt_loss_ft=min_alt_loss_ft,
min_firmware=min_firmware,
max_scanned_time_delta_sec=time_delta_minutes * 60,
)

print(f"Found {len(found_errs)} errors.")
if found_errs:
Expand Down
11 changes: 10 additions & 1 deletion dropmate_py/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def __str__(self) -> str: # pragma: no cover
return f"UID: {self.uid}, FW: {self.firmware_version}, {len(self.drops)} drops, Scanned: {scanned_pretty} UTC" # noqa: E501


def _group_by_uid(drop_logs: abc.Sequence[DropRecord]) -> list[Dropmate]:
def _group_by_uid(drop_logs: abc.Collection[DropRecord]) -> list[Dropmate]:
# Groupby assumes logs are sorted, so we need to sort them to avoid duplicate devices
sorted_logs = sorted(drop_logs, key=operator.attrgetter("uid", "flight_index"))
dropmates = []
Expand Down Expand Up @@ -213,3 +213,12 @@ def log_parse_pipeline(log_filepath: Path) -> list[Dropmate]:
parsed_records = _parse_raw_log(log_lines)

return _group_by_uid(parsed_records)


def merge_dropmates(dropmates: abc.Sequence[Dropmate]) -> list[Dropmate]:
"""Merge a collection of potentially overlapping `Dropmate` devices into a new list."""
all_drops = set()
for dropmate in dropmates:
all_drops.update(dropmate.drops)

return _group_by_uid(all_drops)
12 changes: 12 additions & 0 deletions tests/test_log_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,15 @@ def test_file_parse_pipeline(tmp_path: Path) -> None:
grouped_records = parser.log_parse_pipeline(log_file)
assert len(grouped_records) == 3
assert [rec.uid for rec in grouped_records] == ["A1", "A2", "A3"]


def test_merge_dropomates() -> None:
faux_batch_dropmates = parser._group_by_uid(
parser._parse_raw_log(SAMPLE_CONSOLIDATED_LOG.splitlines())
)
faux_batch_dropmates.extend(
parser._group_by_uid(parser._parse_raw_log(SAMPLE_CONSOLIDATED_LOG.splitlines()))
)

merged = parser.merge_dropmates(faux_batch_dropmates)
assert len(merged) == 3

0 comments on commit d94d6c7

Please sign in to comment.