-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Copied get_expired_items to new Events async service
- Loading branch information
1 parent
6e60d55
commit c131916
Showing
2 changed files
with
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,50 @@ | ||
from eve.utils import date_to_str | ||
|
||
from planning.types import EventResourceModel | ||
from planning.common import get_max_recurrent_events, WORKFLOW_STATE | ||
from planning.core.service import PlanningAsyncResourceService | ||
|
||
|
||
class EventsAsyncService(PlanningAsyncResourceService[EventResourceModel]): | ||
resource_name = "events" | ||
|
||
async def get_expired_items(self, expiry_datetime, spiked_events_only=False): | ||
"""Get the expired items | ||
Where end date is in the past | ||
""" | ||
query = { | ||
"query": {"bool": {"must_not": [{"term": {"expired": True}}]}}, | ||
"filter": {"range": {"dates.end": {"lte": date_to_str(expiry_datetime)}}}, | ||
"sort": [{"dates.start": "asc"}], | ||
"size": get_max_recurrent_events(), | ||
} | ||
|
||
if spiked_events_only: | ||
query["query"] = {"bool": {"must": [{"term": {"state": WORKFLOW_STATE.SPIKED}}]}} | ||
|
||
total_received = 0 | ||
total_events = -1 | ||
|
||
while True: | ||
query["from"] = total_received | ||
|
||
results = self.search(query) | ||
|
||
# If the total_events has not been set, then this is the first query | ||
# In which case we need to store the total hits from the search | ||
if total_events < 0: | ||
total_events = results.count() | ||
|
||
# If the search doesn't contain any results, return here | ||
if total_events < 1: | ||
break | ||
|
||
# If the last query doesn't contain any results, return here | ||
if not len(results.docs): | ||
break | ||
|
||
total_received += len(results.docs) | ||
|
||
# Yield the results for iteration by the callee | ||
yield list(results.docs) |