|
| 1 | +import logging |
| 2 | +from typing import List |
| 3 | + |
| 4 | +from aw_core.models import Event |
| 5 | +from aw_client import ActivityWatchClient |
| 6 | + |
| 7 | +# Function was moved into aw_transform |
| 8 | +from aw_transform import filter_keyvals, filter_period_intersect |
| 9 | + |
| 10 | + |
| 11 | +logger = logging.getLogger(__name__) |
| 12 | + |
| 13 | + |
| 14 | +def filter_short(events, threshold: float = 1): |
| 15 | + # TODO: Try to fill hole in timeline where events have been removed |
| 16 | + # (if events before and after where are the same) |
| 17 | + # Useful for filtering AFK data and to make data look "smoother". |
| 18 | + # Might be something for another function |
| 19 | + return [e for e in events if e.duration.total_seconds() > threshold] |
| 20 | + |
| 21 | + |
| 22 | +def filter_datafields(events: List[Event], fields: List[str]): |
| 23 | + """Filters away specific datafield from every event in a list""" |
| 24 | + for e in events: |
| 25 | + for field in fields: |
| 26 | + if field in e.data: |
| 27 | + e.data.pop(field) |
| 28 | + return events |
| 29 | + |
| 30 | + |
| 31 | +def test_filter_data(): |
| 32 | + awapi = ActivityWatchClient("cleaner", testing=True) |
| 33 | + events = awapi.get_events("aw-watcher-web-test", limit=-1) |
| 34 | + events = filter_datafields(events, "title") |
| 35 | + assert "title" not in events[0].data |
| 36 | + |
| 37 | + |
| 38 | +def test_filter_short(): |
| 39 | + # TODO: This was used in dev and does not work. |
| 40 | + awapi = ActivityWatchClient("cleaner", testing=True) |
| 41 | + events = awapi.get_events("aw-watcher-web-test", limit=-1) |
| 42 | + filter_short(events, threshold=1) |
| 43 | + |
| 44 | + events = awapi.get_events("aw-watcher-window-testing_erb-main2-arch", limit=-1) |
| 45 | + filter_short(events, threshold=1) |
| 46 | + |
| 47 | + events = awapi.get_events("aw-watcher-afk-testing_erb-main2-arch", limit=-1) |
| 48 | + filter_short(events, threshold=30) |
| 49 | + |
| 50 | + |
| 51 | +if __name__ == "__main__": |
| 52 | + test_filter_data() |
| 53 | + test_filter_short() |
0 commit comments