-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
277 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
from datetime import timedelta | ||
|
||
from django.db.models import F | ||
from django.db.models.functions import Now | ||
|
||
from stats.models import PlayerEvent | ||
|
||
|
||
def post_process_player_events(database="default"): | ||
################################################################### | ||
# first pass: get unprocessed (no time_end) events where | ||
# time + max_duration is in the past and older than 24 hours | ||
################################################################### | ||
qs = ( | ||
PlayerEvent.objects.using(database) | ||
.annotate( | ||
annotated_time_end=F("time") + F("max_duration"), | ||
) | ||
.filter( | ||
time_end__isnull=True, | ||
annotated_time_end__lt=Now(), | ||
state=PlayerEvent.State.PLAYING, | ||
time__lt=Now() - timedelta(days=1), | ||
) | ||
) | ||
|
||
# and set the time_end field | ||
# qs.update( | ||
|
||
print("pass 1", qs.count()) | ||
|
||
################################################################### | ||
# second pass: get unprocessed (no time_end) and update the | ||
# time_end to the calculated / annotated time | ||
################################################################### | ||
qs = ( | ||
PlayerEvent.objects.using(database) | ||
.annotate_times_and_durations() | ||
.annotate( | ||
annotated_max_time_end=F("time") + F("max_duration"), | ||
) | ||
.filter( | ||
time_end__isnull=True, | ||
state__in=[ | ||
PlayerEvent.State.PLAYING, | ||
PlayerEvent.State.PAUSED, | ||
PlayerEvent.State.BUFFERING, | ||
], | ||
) | ||
.exclude( | ||
time__lte=Now() - timedelta(days=10000), | ||
annotated_time_end=None, | ||
) | ||
) | ||
|
||
# and set the time_end field | ||
# qs.update does not work here (in combination with window) | ||
print("###") | ||
# for event in [e for e in qs if e.annotated_time_end]: | ||
for event in qs: | ||
if ( | ||
event.state == PlayerEvent.State.PLAYING | ||
and event.annotated_time_end | ||
and event.annotated_max_time_end | ||
): | ||
time_end = min(event.annotated_time_end, event.annotated_max_time_end) | ||
print("TE", time_end) | ||
else: | ||
time_end = event.annotated_time_end | ||
|
||
PlayerEvent.objects.using(database).filter( | ||
pk=event.pk, | ||
).update( | ||
time_end=time_end, | ||
) | ||
|
||
print("pass 2", qs.count()) | ||
|
||
return qs.count() | ||
|
||
# | ||
# | ||
# # Window is disallowed in the filter clause | ||
# # so filtering has to be done in python | ||
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
obr_core/stats/management/commands/stats_postprocess_events.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from django.core.management.base import BaseCommand | ||
|
||
from stats import events | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Post-process events (adding end-times, etc.)" | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"--database", | ||
type=str, | ||
default="default", | ||
) | ||
|
||
def handle(self, *args, **options): | ||
num_processed = events.post_process_player_events(database=options["database"]) | ||
self.stdout.write(f"processed {num_processed} events") |
61 changes: 61 additions & 0 deletions
61
obr_core/stats/migrations/0015_playerevent_max_duration_playerevent_time_end_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Generated by Django 4.2.13 on 2024-06-26 14:44 | ||
|
||
import datetime | ||
|
||
from django.db import migrations, models | ||
|
||
import django_countries.fields | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("stats", "0014_alter_streamevent_geoip_city_and_more"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="playerevent", | ||
name="max_duration", | ||
field=models.DurationField( | ||
blank=True, db_index=True, default=datetime.timedelta(0) | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="playerevent", | ||
name="time_end", | ||
field=models.DateTimeField(blank=True, db_index=True, null=True), | ||
), | ||
migrations.AlterField( | ||
model_name="streamevent", | ||
name="geoip_city", | ||
field=models.CharField( | ||
blank=True, | ||
db_index=True, | ||
default="", | ||
max_length=128, | ||
verbose_name="city", | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name="streamevent", | ||
name="geoip_country", | ||
field=django_countries.fields.CountryField( | ||
blank=True, | ||
db_index=True, | ||
default="", | ||
max_length=2, | ||
verbose_name="country", | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name="streamevent", | ||
name="geoip_region", | ||
field=models.CharField( | ||
blank=True, | ||
db_index=True, | ||
default="", | ||
max_length=128, | ||
verbose_name="region", | ||
), | ||
), | ||
] |
17 changes: 17 additions & 0 deletions
17
obr_core/stats/migrations/0016_alter_playerevent_max_duration.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Generated by Django 4.2.13 on 2024-06-26 14:45 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("stats", "0015_playerevent_max_duration_playerevent_time_end_and_more"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="playerevent", | ||
name="max_duration", | ||
field=models.DurationField(blank=True, db_index=True, null=True), | ||
), | ||
] |
Oops, something went wrong.