SciPost Code Repository
Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
SciPost
Manage
Activity
Members
Labels
Plan
Issues
118
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SciPost
SciPost
Commits
81b2884c
Commit
81b2884c
authored
4 years ago
by
Jean-Sébastien Caux
Browse files
Options
Downloads
Patches
Plain Diff
Add nr_minutes argument to mailgun_get_events command
parent
ddd0cbe3
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
apimail/management/commands/mailgun_get_events.py
+30
-7
30 additions, 7 deletions
apimail/management/commands/mailgun_get_events.py
with
30 additions
and
7 deletions
apimail/management/commands/mailgun_get_events.py
+
30
−
7
View file @
81b2884c
...
@@ -2,6 +2,7 @@ __copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)"
...
@@ -2,6 +2,7 @@ __copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)"
__license__
=
"
AGPL v3
"
__license__
=
"
AGPL v3
"
import
time
import
requests
import
requests
from
django.conf
import
settings
from
django.conf
import
settings
...
@@ -11,7 +12,7 @@ from ...exceptions import APIMailError
...
@@ -11,7 +12,7 @@ from ...exceptions import APIMailError
from
...models
import
Domain
,
Event
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.
For the given domain, get events from Mailgun Events API.
...
@@ -19,14 +20,29 @@ def get_and_save_events(url=None, domain_name=None):
...
@@ -19,14 +20,29 @@ def get_and_save_events(url=None, domain_name=None):
If no url is given, get the first page.
If no url is given, get the first page.
Returns the paging JSON, if present, so traversing can be performed.
Returns the paging JSON, if present, so traversing can be performed.
"""
"""
response
=
{}
if
url
is
None
and
domain_name
is
None
:
if
url
is
None
and
domain_name
is
None
:
raise
APIMailError
(
'
Please provide either a url or domain_name to get_and_save_events.
'
)
raise
APIMailError
(
'
Please provide either a url or domain_name to get_and_save_events.
'
)
response
=
requests
.
get
(
elif
url
:
url
if
url
else
"
https://api.eu.mailgun.net/v3/%s/events
"
%
domain_name
,
response
=
requests
.
get
(
auth
=
(
"
api
"
,
settings
.
MAILGUN_API_KEY
)
url
,
).
json
()
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
:
try
:
events
=
response
[
'
items
'
]
events
=
response
[
'
items
'
]
print
(
"
Retrieved %d events
"
%
len
(
response
[
'
items
'
]))
for
item
in
events
:
for
item
in
events
:
if
not
Event
.
objects
.
filter
(
data__timestamp
=
item
[
'
timestamp
'
],
if
not
Event
.
objects
.
filter
(
data__timestamp
=
item
[
'
timestamp
'
],
data__id
=
item
[
'
id
'
]).
exists
():
data__id
=
item
[
'
id
'
]).
exists
():
...
@@ -44,12 +60,19 @@ class Command(BaseCommand):
...
@@ -44,12 +60,19 @@ class Command(BaseCommand):
"""
"""
Perform a GET request to harvest Events from the Mailgun API, saving them to the DB.
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.
'
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
):
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
():
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
ctr
=
1
# Safety: ensure no runaway requests
while
ctr
<
100
and
info
[
'
nitems
'
]
>
0
:
while
ctr
<
100
and
info
[
'
nitems
'
]
>
0
:
info
=
get_and_save_events
(
url
=
info
[
'
paging
'
][
'
next
'
])
info
=
get_and_save_events
(
url
=
info
[
'
paging
'
][
'
next
'
])
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment