Skip to content

Commit

Permalink
Add sentry statistics in app refs #107
Browse files Browse the repository at this point in the history
  • Loading branch information
thorion21 committed Aug 29, 2019
1 parent 2e571a4 commit 3e3c7c7
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
13 changes: 12 additions & 1 deletion copernicus/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"""

import os

import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration
from getenv import env

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
Expand All @@ -31,6 +32,16 @@
ALLOWED_HOSTS = ['localhost', '127.0.0.1', env('ALLOWED_HOSTS')]
CSRF_TRUSTED_ORIGINS = env('ALLOWED_HOSTS')

# Sentry
SENTRY_DSN = env('SENTRY_DSN', '')
sentry_sdk.init(
dsn=SENTRY_DSN,
integrations=[DjangoIntegration()]
)
SENTRY_ORG_SLUG = env('SENTRY_ORG_SLUG', '')
SENTRY_PROJ_SLUG = env('SENTRY_PROJ_SLUG', '')
SENTRY_AUTH_TOKEN = env('SENTRY_AUTH_TOKEN', '')
SENTRY_BASE_URL = f"https://sentry.io/api/0/projects/{SENTRY_ORG_SLUG}/{SENTRY_PROJ_SLUG}/"

# Application definition

Expand Down
3 changes: 3 additions & 0 deletions docker/app.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ ELASTICSEARCH_TEST_HOST=elasticsearch_test
ELASTICSEARCH_TEST_AUTH=user:password
SENTRY_DSN=
SENTRY_PUBLIC_DSN=
SENTRY_ORG_SLUG=
SENTRY_PROJ_SLUG=
SENTRY_AUTH_TOKEN=
TIMEOUT=60
MATOMO=False

Expand Down
44 changes: 44 additions & 0 deletions insitu/views/management.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import re
import json
import requests
from datetime import datetime
from django.urls import reverse_lazy
from django.conf import settings

Expand Down Expand Up @@ -54,3 +58,43 @@ class AboutView(ProtectedTemplateView):
permission_classes = (IsAuthenticated,)
permission_denied_redirect = reverse_lazy('auth:login')

@staticmethod
def get_issues(status='unresolved'):
base_url = settings.SENTRY_BASE_URL
endpoint = 'issues/?query=is:' + status

url = ''.join((base_url, endpoint))
r = requests.get(
url,
headers={
"Authorization": f"Bearer {settings.SENTRY_AUTH_TOKEN}"
}
)

response = json.loads(r.text)
issues = list()
for message in response:
issue = dict()

issue['title'] = message['title']
issue['resolved'] = (message['status'] == 'resolved')
parsed_date = re.sub('[A-Z]', '', message['lastSeen'])
issue['timestamp'] = datetime.strptime(parsed_date, '%Y-%m-%d%H:%M:%S.%f')

issues.append(issue)

return issues

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)

resolved = AboutView.get_issues(status='resolved')
unresolved = AboutView.get_issues(status='unresolved')

issues = resolved + unresolved
issues.sort(key=lambda i: i['timestamp'], reverse=True)

context['issues'] = issues

return context

1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ django-debug-toolbar==1.10.1
factory-boy==2.8.1
coverage==4.4.1
coveralls==1.2.0
sentry-sdk==0.11.1

0 comments on commit 3e3c7c7

Please sign in to comment.