Skip to content

Commit

Permalink
Track feed id in sentry
Browse files Browse the repository at this point in the history
- Remove inject - TODO: Add as a management command
- N+1 fix for /
- Update config for isort
  • Loading branch information
thenav56 committed Apr 26, 2024
1 parent 2386678 commit 2b2722e
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 17 deletions.
2 changes: 2 additions & 0 deletions apps/cap_feed/formats/format_handler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from apps.cap_feed.models import Alert
from main.sentry import SentryTag

from .atom import get_alerts_atom
from .nws_us import get_alerts_nws_us
Expand All @@ -15,6 +16,7 @@ def get_alerts(feed, all_alert_urls=set()):

print(f'Processing feed: {feed}')

SentryTag.set_tags({SentryTag.Tag.FEED: feed.pk})
try:
ns = {'atom': 'http://www.w3.org/2005/Atom', 'cap': 'urn:oasis:names:tc:emergency:cap:1.2'}
match feed.format:
Expand Down
3 changes: 2 additions & 1 deletion apps/cap_feed/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# Generated by Django 5.0.3 on 2024-03-11 12:14

import apps.cap_feed.models
import django.core.validators
import django.db.models.deletion
import django.utils.timezone
from django.db import migrations, models

import apps.cap_feed.models


class Migration(migrations.Migration):

Expand Down
2 changes: 1 addition & 1 deletion apps/cap_feed/migrations/0003_alter_country_continent.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Generated by Django 4.2.11 on 2024-04-21 07:52

from django.db import migrations, models
import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):
Expand Down
1 change: 1 addition & 0 deletions apps/cap_feed/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def remove_expired_alert_records():
return "removed records of expired alerts"


# TODO: Add this to management command
@shared_task
def inject_data():
inject_geographical_data()
Expand Down
6 changes: 3 additions & 3 deletions apps/cap_feed/templates/cap_feed/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ <h2><a href="/admin">LINK: /admin</a></h2>
{% if latest_alert_list %}
<ul>
{% for alert in latest_alert_list %}
<h3>{{ alert.country }}</h3>
<h4>{{ alert.feed.url }}</h4>
<h3>{{ alert.country_iso3 }} {{ alert.country_name }}</h3>
<h4>{{ alert.feed_url }}</h4>
<p>identifier: {{ alert.identifier }}</p>
<p>sender: {{ alert.sender }}</p>
<p>sent: {{ alert.sent }}</p>
Expand All @@ -17,4 +17,4 @@ <h4>{{ alert.feed.url }}</h4>
</ul>
{% else %}
<p>No alerts are available.</p>
{% endif %}
{% endif %}
1 change: 0 additions & 1 deletion apps/cap_feed/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@

urlpatterns = [
path('', views.index, name='index'),
path('inject/', views.inject, name='inject'),
]
22 changes: 11 additions & 11 deletions apps/cap_feed/views.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
from django.db import models
from django.http import HttpResponse
from django.template import loader

from apps.cap_feed.tasks import inject_data

from .models import Alert


def index(request):
latest_alert_list = Alert.objects.order_by("-sent")[:10]
latest_alert_list = Alert.objects.order_by("-sent").values(
'identifier',
'sender',
'sent',
'status',
'msg_type',
country_name=models.F('country__name'),
country_iso3=models.F('country__iso3'),
feed_url=models.F('feed__url'),
)[:10]
template = loader.get_template("cap_feed/index.html")
context = {
"latest_alert_list": latest_alert_list,
}
return HttpResponse(template.render(context, request))


def inject(request):
try:
inject_data.apply_async(args=(), kwargs={}, queue='inject')
except Exception:
print('Celery not running')
return HttpResponse("Done")
13 changes: 13 additions & 0 deletions main/sentry.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from enum import Enum

import sentry_sdk
from sentry_sdk.integrations.celery import CeleryIntegration
from sentry_sdk.integrations.django import DjangoIntegration
Expand Down Expand Up @@ -34,3 +36,14 @@ def init_sentry(app_type, tags={}, **config):
scope.set_tag("app_type", app_type)
for tag, value in tags.items():
scope.set_tag(tag, value)


class SentryTag:
class Tag(str, Enum):
_BASE = 'alert-hub.'
FEED = _BASE + 'feed'

@staticmethod
def set_tags(kwargs: dict[Tag, int | str]):
for key, value in kwargs.items():
sentry_sdk.set_tag(key, value)
8 changes: 8 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ extend-exclude = "(__pycache__|.*snap_test_.*\\.py|.+\\/.+\\/migrations\\/.*)"

[tool.isort]
profile = "black"
multi_line_output = 3
skip = [
"**/__pycache__",
"**/snap_test_*.py",
".venv/",
"legacy/",
"**/migrations/*.py",
]

[tool.pyright]
exclude = [
Expand Down

0 comments on commit 2b2722e

Please sign in to comment.