Skip to content

Commit af900bc

Browse files
committed
Add configuration for thresholds and runtime
Add PR changes
1 parent 4bb5049 commit af900bc

26 files changed

+185
-101
lines changed

api/management/commands/cron_job_monitor.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from django.conf import settings
66
from django.core.management.base import BaseCommand
77

8-
from main.sentry import SentryMonitor
8+
from main.sentry import SentryMonitor, SentryMonitorConfig
99
from main.settings import SENTRY_DSN
1010

1111
logger = logging.getLogger(__name__)
@@ -48,6 +48,10 @@ def handle(self, *args, **options):
4848
"type": "crontab",
4949
"value": str(schedule),
5050
},
51+
"tz": settings.TIME_ZONE,
52+
"checkin_margin": SentryMonitorConfig.get_checkin_margin(cronjob),
53+
"failure_issue_threshold": SentryMonitorConfig.get_failure_issue_threshold(cronjob),
54+
"recovery_threshold": SentryMonitorConfig.get_recovery_threshold(cronjob),
5155
},
5256
"environment": settings.GO_ENVIRONMENT,
5357
"status": "ok",

api/management/commands/index_and_notify.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767
}
6868

6969

70-
@monitor(monitor_slug=SentryMonitor.INDEX_AND_NOTIFY)
7170
class Command(BaseCommand):
7271
help = "Index and send notifications about new/changed records"
7372

@@ -920,6 +919,7 @@ def check_ingest_issues(self, having_ingest_issue):
920919
+ ", notification sent to IM team"
921920
)
922921

922+
@monitor(monitor_slug=SentryMonitor.INDEX_AND_NOTIFY)
923923
def handle(self, *args, **options):
924924
if self.is_digest_mode():
925925
time_diff = self.diff_1_week() # in digest mode (once a week, for new_entities only) we use a bigger interval

api/management/commands/ingest_appeals.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
GEC_CODES = GECCode.objects.select_related("country").all()
3434

3535

36-
@monitor(monitor_slug=SentryMonitor.INGEST_APPEALS)
3736
class Command(BaseCommand):
3837
help = "Add new entries from Access database file"
3938

@@ -292,6 +291,7 @@ def parse_appeal_record(self, r, **options):
292291

293292
return fields
294293

294+
@monitor(monitor_slug=SentryMonitor.INGEST_APPEALS)
295295
def handle(self, *args, **options):
296296
logger.info("Starting appeals ingest")
297297
start_appeals_count = Appeal.objects.all().count()

api/management/commands/ingest_disaster_law.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
from main.sentry import SentryMonitor
99

1010

11-
@monitor(monitor_slug=SentryMonitor.INGEST_DISASTER_LAW)
1211
class Command(BaseCommand):
1312
help = "Add ICRC data"
1413

14+
@monitor(monitor_slug=SentryMonitor.INGEST_DISASTER_LAW)
1515
def handle(self, *args, **kwargs):
1616
logger.info("Starting Disaster Law data")
1717
home_url = "https://disasterlaw.ifrc.org/"

api/management/commands/ingest_icrc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
from main.sentry import SentryMonitor
99

1010

11-
@monitor(monitor_slug=SentryMonitor.INGEST_ICRC)
1211
class Command(BaseCommand):
1312
help = "Add ICRC data"
1413

14+
@monitor(monitor_slug=SentryMonitor.INGEST_ICRC)
1515
def handle(self, *args, **kwargs):
1616
logger.info("Strating ICRC data ingest")
1717
HEADERS = {

api/management/commands/ingest_ns_capacity.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
from main.sentry import SentryMonitor
99

1010

11-
@monitor(monitor_slug=SentryMonitor.INGEST_NS_CAPACITY)
1211
class Command(BaseCommand):
1312
help = "Add ns contact details"
1413

14+
@monitor(monitor_slug=SentryMonitor.INGEST_NS_CAPACITY)
1515
def handle(self, *args, **kwargs):
1616
logger.info("Starting NS Contacts")
1717

api/management/commands/ingest_ns_contact.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import xmltodict
66
from django.conf import settings
77
from django.core.management.base import BaseCommand
8+
from django.db import transaction
89
from requests.auth import HTTPBasicAuth
910
from sentry_sdk.crons import monitor
1011

@@ -13,10 +14,11 @@
1314
from main.sentry import SentryMonitor
1415

1516

16-
@monitor(monitor_slug=SentryMonitor.INGEST_NS_CONTACT)
1717
class Command(BaseCommand):
1818
help = "Add ns contact details"
1919

20+
@monitor(monitor_slug=SentryMonitor.INGEST_NS_CONTACT)
21+
@transaction.atomic
2022
def handle(self, *args, **kwargs):
2123
logger.info("Starting NS Contacts")
2224
url = "https://go-api.ifrc.org/"

api/management/commands/ingest_ns_directory.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import xmltodict
33
from django.conf import settings
44
from django.core.management.base import BaseCommand
5+
from django.db import transaction
56
from requests.auth import HTTPBasicAuth
67
from sentry_sdk.crons import monitor
78

@@ -10,10 +11,11 @@
1011
from main.sentry import SentryMonitor
1112

1213

13-
@monitor(monitor_slug=SentryMonitor.INGEST_NS_DIRECTORY)
1414
class Command(BaseCommand):
1515
help = "Add ns contact details"
1616

17+
@monitor(monitor_slug=SentryMonitor.INGEST_NS_DIRECTORY)
18+
@transaction.atomic
1719
def handle(self, *args, **kwargs):
1820
def postprocessor(path, key, value):
1921
if key == "@i:nil":
@@ -41,6 +43,7 @@ def postprocessor(path, key, value):
4143
raise Exception("Error querying NationalSocietiesContacts")
4244

4345
added = 0
46+
created_country_directory_ids = []
4447
dict_data = xmltodict.parse(response.content, postprocessor=postprocessor)
4548
for data in dict_data["ArrayOfNationalSocietiesContacts"]["NationalSocietiesContacts"]:
4649
country_name = data["CON_country"] if isinstance(data["CON_country"], str) else None
@@ -62,14 +65,15 @@ def postprocessor(path, key, value):
6265
"position": data["CON_title"],
6366
"country": country,
6467
}
65-
existing_qs = CountryDirectory.objects.filter(
68+
country_directory, _ = CountryDirectory.objects.get_or_create(
6669
country=country,
6770
first_name__iexact=data["first_name"],
6871
last_name__iexact=data["last_name"],
6972
position__iexact=data["position"],
7073
)
71-
if not existing_qs.exists():
72-
CountryDirectory.objects.create(**data)
74+
created_country_directory_ids.append(country_directory.pk)
75+
# NOTE: Deleting the country directory which are not available in the source
76+
CountryDirectory.objects.exclude(id__in=created_country_directory_ids).delete()
7377
text_to_log = "%s Ns Directory added" % added
7478
logger.info(text_to_log)
7579
body = {"name": "ingest_ns_directory", "message": text_to_log, "num_result": added, "status": CronJobStatus.SUCCESSFUL}

api/management/commands/ingest_ns_document.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@
55
import requests
66
from django.conf import settings
77
from django.core.management.base import BaseCommand
8+
from django.db import transaction
89
from sentry_sdk.crons import monitor
910

1011
from api.logger import logger
1112
from api.models import Country, CountryKeyDocument, CronJob, CronJobStatus
1213
from main.sentry import SentryMonitor
1314

1415

15-
@monitor(monitor_slug=SentryMonitor.INGEST_NS_DOCUMENT)
1616
class Command(BaseCommand):
1717
help = "Add ns documents"
1818

19+
@monitor(monitor_slug=SentryMonitor.INGEST_NS_DOCUMENT)
20+
@transaction.atomic
1921
def handle(self, *args, **kwargs):
2022
logger.info("Starting NS Key Documents")
2123

@@ -98,6 +100,7 @@ def fetch_all_country_documents(self, api_key, country_table):
98100

99101
def save_documents_to_database(self, result):
100102
added = 0
103+
created_country_key_document_ids = []
101104
for document in result:
102105
country = Country.objects.filter(fdrs=document["country_code"]).first()
103106
if country is None:
@@ -122,8 +125,20 @@ def save_documents_to_database(self, result):
122125
country_key_document.year = document["year"]
123126
country_key_document.end_year = document["end_year"]
124127
country_key_document.year_text = document["year_text"]
125-
country_key_document.save(update_fields=["name", "thumbnail", "document_type", "year", "end_year", "year_text"])
128+
country_key_document.save(
129+
update_fields=[
130+
"name",
131+
"thumbnail",
132+
"document_type",
133+
"year",
134+
"end_year",
135+
"year_text",
136+
]
137+
)
138+
created_country_key_document_ids.append(country_key_document.pk)
126139
added += 1
140+
# NOTE: Deleting the CountryKeyDocument that are not in the source
141+
CountryKeyDocument.objects.exclude(id__in=created_country_key_document_ids).delete()
127142
return added
128143

129144
def sync_cron_success(self, text_to_log, added):

api/management/commands/ingest_ns_initiatives.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@
33
import requests
44
from django.conf import settings
55
from django.core.management.base import BaseCommand
6+
from django.db import transaction
67
from sentry_sdk.crons import monitor
78

89
from api.logger import logger
910
from api.models import Country, CronJob, CronJobStatus, NSDInitiatives
1011
from main.sentry import SentryMonitor
1112

1213

13-
@monitor(monitor_slug=SentryMonitor.INGEST_NS_INITIATIVES)
1414
class Command(BaseCommand):
1515
help = "Add ns initiatives"
1616

17+
@monitor(monitor_slug=SentryMonitor.INGEST_NS_INITIATIVES)
18+
@transaction.atomic
1719
def handle(self, *args, **kwargs):
1820
logger.info("Starting NS Inititatives")
1921
api_key = settings.NS_INITIATIVES_API_KEY
@@ -43,6 +45,7 @@ def handle(self, *args, **kwargs):
4345
],
4446
)
4547
funding_data = funding_data.replace({np.nan: None})
48+
created_ns_initiatives_pk = []
4649
for data in funding_data.values.tolist():
4750
# TODO: Filter not by society name
4851
country = Country.objects.filter(society_name__iexact=data[0]).first()
@@ -60,11 +63,14 @@ def handle(self, *args, **kwargs):
6063
)
6164
if not created:
6265
nsd_initiatives.title = data[3]
66+
nsd_initiatives.categories = data[4]
6367
nsd_initiatives.allocation = data[5]
6468
nsd_initiatives.funding_period = data[6]
65-
nsd_initiatives.categories = data[4]
66-
nsd_initiatives.save(update_fields=["title", "allocation", "funding_period", "categories"])
69+
nsd_initiatives.save(update_fields=["title", "categories", "allocation", "funding_period"])
70+
created_ns_initiatives_pk.append(nsd_initiatives.pk)
6771
added += 1
72+
# NOTE: Delete the NSDInitiatives that are not in the source
73+
NSDInitiatives.objects.exclude(id__in=created_ns_initiatives_pk).delete()
6874

6975
text_to_log = "%s Ns initiatives added" % added
7076
logger.info(text_to_log)

0 commit comments

Comments
 (0)