From 81b2884c401a5b2321abd3f39572babf540065cb Mon Sep 17 00:00:00 2001
From: "J.-S. Caux" <J.S.Caux@uva.nl>
Date: Sun, 18 Oct 2020 09:28:54 +0200
Subject: [PATCH] Add nr_minutes argument to mailgun_get_events command

---
 .../management/commands/mailgun_get_events.py | 37 +++++++++++++++----
 1 file changed, 30 insertions(+), 7 deletions(-)

diff --git a/apimail/management/commands/mailgun_get_events.py b/apimail/management/commands/mailgun_get_events.py
index 1b9a37e7c..9440010a0 100644
--- a/apimail/management/commands/mailgun_get_events.py
+++ b/apimail/management/commands/mailgun_get_events.py
@@ -2,6 +2,7 @@ __copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)"
 __license__ = "AGPL v3"
 
 
+import time
 import requests
 
 from django.conf import settings
@@ -11,7 +12,7 @@ from ...exceptions import APIMailError
 from ...models import Domain, Event
 
 
-def get_and_save_events(url=None, domain_name=None):
+def get_and_save_events(url=None, domain_name=None, nr_minutes=2):
     """
     For the given domain, get events from Mailgun Events API.
 
@@ -19,14 +20,29 @@ def get_and_save_events(url=None, domain_name=None):
     If no url is given, get the first page.
     Returns the paging JSON, if present, so traversing can be performed.
     """
+    response = {}
     if url is None and domain_name is None:
         raise APIMailError('Please provide either a url or domain_name to get_and_save_events.')
-    response = requests.get(
-        url if url else "https://api.eu.mailgun.net/v3/%s/events" % domain_name,
-        auth=("api", settings.MAILGUN_API_KEY)
-    ).json()
+    elif url:
+        response = requests.get(
+            url,
+            auth=("api", settings.MAILGUN_API_KEY)
+        ).json()
+    else:
+        print("Fetching items for the last %d minutes" % nr_minutes)
+        begin_time = int(time.time()) - 60*nr_minutes
+        response = requests.get(
+            "https://api.eu.mailgun.net/v3/%s/events" % domain_name,
+            auth=("api", settings.MAILGUN_API_KEY),
+            params={
+                "begin": begin_time,
+                "ascending": "yes"
+            }
+        ).json()
+        print(response)
     try:
         events = response['items']
+        print("Retrieved %d events" % len(response['items']))
         for item in events:
             if not Event.objects.filter(data__timestamp=item['timestamp'],
                                         data__id=item['id']).exists():
@@ -44,12 +60,19 @@ class Command(BaseCommand):
     """
     Perform a GET request to harvest Events from the Mailgun API, saving them to the DB.
     """
-
     help = 'Gets Events from the Mailgun Events API and saves them to the DB.'
 
+    def add_arguments(self, parser):
+        parser.add_argument(
+            '--nr_minutes', action='store', dest='nr_minutes', type=int,
+            help='number of minutes in the past where events are to be retrieved'
+        )
+
     def handle(self, *args, **kwargs):
+        nr_minutes = kwargs.get('nr_minutes', 2)
+        print("Getting events for the last %d minutes" % nr_minutes)
         for domain in Domain.objects.active():
-            info = get_and_save_events(domain_name=domain.name)
+            info = get_and_save_events(domain_name=domain.name, nr_minutes=nr_minutes)
             ctr = 1 # Safety: ensure no runaway requests
             while ctr < 100 and info['nitems'] > 0:
                 info = get_and_save_events(url=info['paging']['next'])
-- 
GitLab