Skip to content

Commit

Permalink
fix(dashboard): nigth passenger currency depends on day currency
Browse files Browse the repository at this point in the history
  • Loading branch information
zyv committed May 26, 2024
1 parent fec392f commit 27bca55
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 42 deletions.
10 changes: 7 additions & 3 deletions logbook/models/aircraft.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class SpeedUnit(models.TextChoices):


class Aircraft(models.Model):
CURRENCY_REQUIRED_LANDINGS = 3

type = models.CharField(max_length=3, choices=AircraftType.choices)
maker = models.CharField(max_length=64)
model = models.CharField(max_length=64)
Expand Down Expand Up @@ -71,6 +73,8 @@ def __str__(self):

@property
def currency_status(self) -> Optional[NinetyDaysCurrency]:
from .log_entry import LogEntry

return get_ninety_days_currency(LogEntry.objects.filter(aircraft=self)) if self.currency_required else None
return (
get_ninety_days_currency(self.logentry_set.all(), self.CURRENCY_REQUIRED_LANDINGS)
if self.currency_required
else None
)
13 changes: 12 additions & 1 deletion logbook/statistics/currency.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def expires_on(self) -> date:

def get_ninety_days_currency(
queryset: QuerySet["LogEntry"],
required_landings: int = CURRENCY_REQUIRED_LANDINGS_PASSENGER,
required_landings: int,
) -> NinetyDaysCurrency:
eligible_entries = queryset.filter(arrival_time__gte=datetime.now(tz=UTC) - timedelta(days=CURRENCY_DAYS_RANGE))

Expand Down Expand Up @@ -91,3 +91,14 @@ def get_ninety_days_currency(
expires_in=time_to_expiry,
landings_to_renew=landings_to_renew,
)


def get_passenger_currency(entries: QuerySet["LogEntry"]) -> (CurrencyStatus, CurrencyStatus):
day_currency = get_ninety_days_currency(entries, CURRENCY_REQUIRED_LANDINGS_PASSENGER)

night_currency = get_ninety_days_currency(entries.filter(night=True), CURRENCY_REQUIRED_LANDINGS_NIGHT)

if day_currency.status == CurrencyStatus.NOT_CURRENT:
night_currency = day_currency

return day_currency, night_currency
60 changes: 22 additions & 38 deletions logbook/views/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from ..models.aircraft import Aircraft, AircraftType
from ..models.log_entry import FunctionType, LogEntry
from ..statistics.currency import CURRENCY_REQUIRED_LANDINGS_NIGHT, get_ninety_days_currency
from ..statistics.currency import get_passenger_currency
from ..statistics.experience import compute_totals
from .utils import (
AuthenticatedTemplateView,
Expand All @@ -30,69 +30,53 @@ def totals_per_function(log_entries: QuerySet[LogEntry]):
"1Y": timedelta(days=365),
"2Y": timedelta(days=365 * 2),
}

all_entries = LogEntry.objects.all()

day_sep_currency, night_sep_currency = get_passenger_currency(
all_entries.filter(aircraft__type=AircraftType.SEP)
)
day_tmg_currency, night_tmg_currency = get_passenger_currency(
all_entries.filter(aircraft__type=AircraftType.TMG),
)

return super().get_context_data(*args, **kwargs) | {
"passenger_currency": {
"sep": {
"day": get_ninety_days_currency(
LogEntry.objects.filter(
aircraft__type=AircraftType.SEP,
),
),
"night": get_ninety_days_currency(
LogEntry.objects.filter(
aircraft__type=AircraftType.SEP,
night=True,
),
required_landings=CURRENCY_REQUIRED_LANDINGS_NIGHT,
),
"day": day_sep_currency,
"night": night_sep_currency,
},
"tmg": {
"day": get_ninety_days_currency(
LogEntry.objects.filter(
aircraft__type=AircraftType.TMG,
),
),
"night": get_ninety_days_currency(
LogEntry.objects.filter(
aircraft__type=AircraftType.TMG,
night=True,
),
required_landings=CURRENCY_REQUIRED_LANDINGS_NIGHT,
),
"day": day_tmg_currency,
"night": night_tmg_currency,
},
},
"totals_per_type": {
aircraft_type: {
"grand": compute_totals(LogEntry.objects.filter(aircraft__type=aircraft_type)),
"per_function": totals_per_function(LogEntry.objects.filter(aircraft__type=aircraft_type)),
"grand": compute_totals(all_entries.filter(aircraft__type=aircraft_type)),
"per_function": totals_per_function(all_entries.filter(aircraft__type=aircraft_type)),
"per_aircraft": [
(
aircraft,
totals_per_function(LogEntry.objects.filter(aircraft=aircraft)),
compute_totals(LogEntry.objects.filter(aircraft=aircraft)),
totals_per_function(all_entries.filter(aircraft=aircraft)),
compute_totals(all_entries.filter(aircraft=aircraft)),
)
for aircraft in Aircraft.objects.filter(type=aircraft_type)
],
"per_period": [
{
"per_function": totals_per_function(
LogEntry.objects.filter(
aircraft__type=aircraft_type,
departure_time__gt=now - period_delta,
),
all_entries.filter(aircraft__type=aircraft_type, departure_time__gt=now - period_delta),
),
"grand": compute_totals(
LogEntry.objects.filter(
aircraft__type=aircraft_type,
departure_time__gt=now - period_delta,
),
all_entries.filter(aircraft__type=aircraft_type, departure_time__gt=now - period_delta),
),
}
for period_delta in periods.values()
],
}
for aircraft_type in reversed(AircraftType)
},
"grand_total": compute_totals(LogEntry.objects.all()),
"grand_total": compute_totals(all_entries),
"period_labels": list(periods.keys()),
}

0 comments on commit 27bca55

Please sign in to comment.