Skip to content
This repository has been archived by the owner on Aug 23, 2022. It is now read-only.

Commit

Permalink
some small dedup function refactor to make it simple (#38)
Browse files Browse the repository at this point in the history
* small refactor

* linter love

* bump version
  • Loading branch information
Anton Sapozhnikov authored Mar 26, 2020
1 parent 9a938af commit 40a8bb1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 21 deletions.
28 changes: 8 additions & 20 deletions comet_core/data_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"""Data Store module - interface to database."""

from datetime import datetime, timedelta
from itertools import tee, islice, chain

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
Expand All @@ -26,20 +25,6 @@
Session = sessionmaker(autocommit=True) # pylint: disable=invalid-name


def now_and_next(some_iterable):
"""
get an iterator in a nice itertools way so we can act on the list as we remove args
Args:
some_iterable (iterable): any iterable list
Returns:
tuple: tuple of two items in a row
"""
items, nexts = tee(some_iterable, 2)
nexts = chain(islice(nexts, 1, None), [None])
return zip(items, nexts)


def remove_duplicate_events(event_record_list):
"""
This removes duplicates based on fingerprint and chooses the newest issue
Expand All @@ -48,11 +33,14 @@ def remove_duplicate_events(event_record_list):
Returns:
list: of EventRecords with extra fingerprints removed
"""
event_record_list = sorted(event_record_list, key=lambda x: (x.fingerprint, x.received_at))
for issue_1, issue_2 in now_and_next(event_record_list):
if issue_1 and issue_2 and issue_1.fingerprint == issue_2.fingerprint:
event_record_list.remove(issue_1)
return event_record_list
events_hash_table = {}
for e in event_record_list:
if e.fingerprint in events_hash_table:
if events_hash_table[e.fingerprint].received_at < e.received_at:
events_hash_table[e.fingerprint] = e
else:
events_hash_table[e.fingerprint] = e
return list(events_hash_table.values())


class DataStore:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

setuptools.setup(
name="comet-core",
version="2.5.0",
version="2.5.1",
url="https://github.com/spotify/comet-core",
author="Spotify Platform Security",
author_email="[email protected]",
Expand Down

0 comments on commit 40a8bb1

Please sign in to comment.