Skip to content

Commit bef0e3f

Browse files
committed
Fixup the denormalizing PR
- Refunding flights triggers recalc of totals - Handle case where a flight is slightly overfilled - Run denormalized calcs every 5m - Don't bother with "error counts" in denormalized calcs. It will either succeed or error and alert us.
1 parent 554d07e commit bef0e3f

File tree

4 files changed

+19
-31
lines changed

4 files changed

+19
-31
lines changed

adserver/admin.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,9 +928,14 @@ def refund_impressions(self, request, queryset):
928928
)
929929

930930
count = 0
931+
flights = set()
931932
for impression in queryset:
932933
if impression.refund():
933934
count += 1
935+
flights.add(impression.advertisement.flight)
936+
937+
for flight in flights:
938+
flight.refresh_denormalized_totals()
934939

935940
messages.add_message(
936941
request,

adserver/models.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,6 +1506,12 @@ def refresh_denormalized_totals(self):
15061506

15071507
self.total_views = aggregation["total_views"] or 0
15081508
self.total_clicks = aggregation["total_clicks"] or 0
1509+
1510+
if self.sold_clicks > 0 and self.total_clicks > self.sold_clicks:
1511+
self.total_clicks = self.sold_clicks
1512+
if self.sold_impressions > 0 and self.total_views > self.sold_impressions:
1513+
self.total_views = self.sold_impressions
1514+
15091515
self.save(update_fields=["total_views", "total_clicks"])
15101516

15111517
def copy_niche_targeting_urls(self, other_flight):

adserver/tasks.py

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -939,30 +939,9 @@ def refresh_flight_denormalized_totals():
939939
# Only refresh active flights to avoid unnecessary work
940940
flights = Flight.objects.filter(live=True)
941941
total_flights = flights.count()
942-
success_count = 0
943-
error_count = 0
944942

945943
for flight in flights:
946-
try:
947-
flight.refresh_denormalized_totals()
948-
success_count += 1
949-
except Exception as e:
950-
error_count += 1
951-
log.error(
952-
"Failed to refresh denormalized totals for flight %s: %s",
953-
flight.slug,
954-
e,
955-
exc_info=True,
956-
)
957-
958-
duration = (timezone.now() - start_time).total_seconds()
959-
log.info(
960-
"Finished refreshing denormalized totals: %d/%d succeeded, %d failed, took %.2fs",
961-
success_count,
962-
total_flights,
963-
error_count,
964-
duration,
965-
)
944+
flight.refresh_denormalized_totals()
966945

967946
# Update cache with last successful run timestamp
968947
cache.set(
@@ -971,14 +950,12 @@ def refresh_flight_denormalized_totals():
971950
timeout=None, # Never expire
972951
)
973952

974-
# Alert if there are significant failures
975-
if error_count > 0 and error_count / max(total_flights, 1) > 0.1:
976-
slack_message(
977-
"adserver/slack/generic-message.slack",
978-
{
979-
"text": f"⚠️ Flight denormalized total refresh had {error_count}/{total_flights} failures"
980-
},
981-
)
953+
duration = (timezone.now() - start_time).total_seconds()
954+
log.info(
955+
"Finished refreshing denormalized totals: %d flights, took %.2fs",
956+
total_flights,
957+
duration,
958+
)
982959

983960

984961
@app.task()

config/settings/production.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@
204204
},
205205
"frequent-refresh-flight-totals": {
206206
"task": "adserver.tasks.refresh_flight_denormalized_totals",
207-
"schedule": crontab(minute="*/10"), # Every 10 minutes
207+
"schedule": crontab(minute="*/5"), # Every 5 minutes
208208
},
209209
# Run publisher importers daily
210210
"every-day-sync-publisher-data": {

0 commit comments

Comments
 (0)