Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 1 | """Manage Hiccup stats. |
| 2 | |
| 3 | This module provides a command to compute statistics of |
| 4 | heartbeats, crashes, and versions sent from Hiccup clients. |
| 5 | """ |
| 6 | import datetime |
Mitja Nikolaus | 9c3b29e | 2018-08-22 11:17:50 +0200 | [diff] [blame^] | 7 | from typing import Dict, List, Optional |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 8 | |
| 9 | from django.core.management.base import BaseCommand |
| 10 | from django.db import transaction |
Mitja Nikolaus | 9c3b29e | 2018-08-22 11:17:50 +0200 | [diff] [blame^] | 11 | from django.db.models import Count, F, Model, Q, QuerySet |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 12 | from django.db.models.functions import TruncDate |
| 13 | import pytz |
| 14 | |
| 15 | from crashreport_stats.models import ( |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 16 | RadioVersion, |
| 17 | RadioVersionDaily, |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 18 | StatsMetadata, |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 19 | Version, |
| 20 | VersionDaily, |
Mitja Nikolaus | 9c3b29e | 2018-08-22 11:17:50 +0200 | [diff] [blame^] | 21 | _VersionStats, |
| 22 | _DailyVersionStats, |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 23 | ) |
| 24 | from crashreports.models import Crashreport, HeartBeat |
| 25 | |
| 26 | |
| 27 | # pylint: disable=too-few-public-methods |
| 28 | # Classes in this file inherit from each other and are not method containers. |
| 29 | |
| 30 | |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 31 | class _ReportCounterFilter: |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 32 | """Filter reports matching a report counter requirements. |
| 33 | |
| 34 | Attributes: |
Mitja Nikolaus | 9c3b29e | 2018-08-22 11:17:50 +0200 | [diff] [blame^] | 35 | model: The report model. |
| 36 | name: The human-readable report counter name. |
| 37 | field_name: |
| 38 | The counter name as defined in the stats model where it is a field. |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 39 | |
| 40 | """ |
| 41 | |
Mitja Nikolaus | 9c3b29e | 2018-08-22 11:17:50 +0200 | [diff] [blame^] | 42 | def __init__(self, model: Model, name: str, field_name: str) -> None: |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 43 | """Initialise the filter. |
| 44 | |
| 45 | Args: |
Mitja Nikolaus | 9c3b29e | 2018-08-22 11:17:50 +0200 | [diff] [blame^] | 46 | model: The report model. |
| 47 | name: The human-readable report counter name. |
| 48 | field_name: |
| 49 | The counter name as defined in the stats model where it is a |
| 50 | field. |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 51 | |
| 52 | """ |
| 53 | self.model = model |
| 54 | self.name = name |
| 55 | self.field_name = field_name |
| 56 | |
Mitja Nikolaus | 9c3b29e | 2018-08-22 11:17:50 +0200 | [diff] [blame^] | 57 | def filter(self, query_objects: QuerySet) -> QuerySet: |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 58 | """Filter the reports. |
| 59 | |
| 60 | Args: |
Mitja Nikolaus | 9c3b29e | 2018-08-22 11:17:50 +0200 | [diff] [blame^] | 61 | query_objects: The reports to filter. |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 62 | Returns: |
Mitja Nikolaus | 9c3b29e | 2018-08-22 11:17:50 +0200 | [diff] [blame^] | 63 | The reports matching this report counter requirements. |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 64 | |
| 65 | """ |
| 66 | # pylint: disable=no-self-use |
| 67 | # self is potentially used by subclasses. |
| 68 | return query_objects |
| 69 | |
| 70 | |
| 71 | class HeartBeatCounterFilter(_ReportCounterFilter): |
| 72 | """The heartbeats counter filter.""" |
| 73 | |
| 74 | def __init__(self): |
| 75 | """Initialise the filter.""" |
| 76 | super(HeartBeatCounterFilter, self).__init__( |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 77 | model=HeartBeat, name="heartbeats", field_name="heartbeats" |
| 78 | ) |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 79 | |
| 80 | |
| 81 | class CrashreportCounterFilter(_ReportCounterFilter): |
| 82 | """The crashreports counter filter. |
| 83 | |
| 84 | Attributes: |
Mitja Nikolaus | 9c3b29e | 2018-08-22 11:17:50 +0200 | [diff] [blame^] | 85 | include_boot_reasons: |
| 86 | The boot reasons assumed to characterise this crashreport ("OR"ed). |
| 87 | exclude_boot_reasons: |
| 88 | The boot reasons assumed to *not* characterise this crashreport ( |
| 89 | "AND"ed). |
| 90 | inclusive_filter: |
| 91 | The boot reasons filter for filtering reports that should be |
| 92 | included. |
| 93 | exclusive_filter: |
| 94 | The boot reasons filter for filtering reports that should *not* |
| 95 | be included. |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 96 | |
| 97 | """ |
| 98 | |
| 99 | def __init__( |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 100 | self, |
Mitja Nikolaus | 9c3b29e | 2018-08-22 11:17:50 +0200 | [diff] [blame^] | 101 | name: str, |
| 102 | field_name: str, |
| 103 | include_boot_reasons: Optional[List[str]] = None, |
| 104 | exclude_boot_reasons: Optional[List[str]] = None, |
| 105 | ) -> None: |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 106 | """Initialise the filter. |
| 107 | |
| 108 | One or both of `include_boot_reasons` and `exclude_boot_reasons` must |
| 109 | be specified. |
| 110 | |
| 111 | Args: |
Mitja Nikolaus | 9c3b29e | 2018-08-22 11:17:50 +0200 | [diff] [blame^] | 112 | name: The human-readable report counter name. |
| 113 | field_name: |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 114 | The counter name as defined in the stats model where it is a |
| 115 | field. |
Mitja Nikolaus | 9c3b29e | 2018-08-22 11:17:50 +0200 | [diff] [blame^] | 116 | include_boot_reasons: |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 117 | The boot reasons assumed to characterise this crashreport |
| 118 | ("OR"ed). |
Mitja Nikolaus | 9c3b29e | 2018-08-22 11:17:50 +0200 | [diff] [blame^] | 119 | exclude_boot_reasons: |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 120 | The boot reasons assumed to *not* characterise this |
| 121 | crashreport ("AND"ed). |
| 122 | Raises: |
| 123 | ValueError: |
| 124 | None of `include_boot_reasons` and `exclude_boot_reasons` have |
| 125 | been supplied. |
| 126 | |
| 127 | """ |
| 128 | if not include_boot_reasons and not exclude_boot_reasons: |
| 129 | raise ValueError( |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 130 | "One or both of `include_boot_reasons` and " |
| 131 | "`exclude_boot_reasons` must be specified." |
| 132 | ) |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 133 | |
| 134 | super(CrashreportCounterFilter, self).__init__( |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 135 | model=Crashreport, name=name, field_name=field_name |
| 136 | ) |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 137 | |
| 138 | # Cache the boot reasons inclusive filter |
| 139 | self.include_boot_reasons = include_boot_reasons |
| 140 | self.inclusive_filter = self._create_query_filter(include_boot_reasons) |
| 141 | |
| 142 | # Cache the boot reasons exclusive filter |
| 143 | self.exclude_boot_reasons = exclude_boot_reasons |
| 144 | self.exclusive_filter = self._create_query_filter(exclude_boot_reasons) |
| 145 | |
| 146 | @staticmethod |
Mitja Nikolaus | 9c3b29e | 2018-08-22 11:17:50 +0200 | [diff] [blame^] | 147 | def _create_query_filter(reasons: Optional[List[str]]) -> Q: |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 148 | """Combine boot reasons into one filter. |
| 149 | |
| 150 | Args: |
Mitja Nikolaus | 9c3b29e | 2018-08-22 11:17:50 +0200 | [diff] [blame^] | 151 | reasons: List of boot reasons to include in filter. |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 152 | Returns: |
Mitja Nikolaus | 9c3b29e | 2018-08-22 11:17:50 +0200 | [diff] [blame^] | 153 | Query that matches either of reasons as boot_reason if list is |
| 154 | not empty, otherwise None. |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 155 | |
| 156 | """ |
| 157 | if not reasons: |
| 158 | return None |
| 159 | |
| 160 | query = Q(boot_reason=reasons[0]) |
| 161 | for reason in reasons[1:]: |
| 162 | query = query | Q(boot_reason=reason) |
| 163 | return query |
| 164 | |
Mitja Nikolaus | 9c3b29e | 2018-08-22 11:17:50 +0200 | [diff] [blame^] | 165 | def filter(self, query_objects: QuerySet) -> QuerySet: |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 166 | """Filter the reports according to the inclusive and exclusive fitlers. |
| 167 | |
| 168 | Args: |
Mitja Nikolaus | 9c3b29e | 2018-08-22 11:17:50 +0200 | [diff] [blame^] | 169 | query_objects: The reports to filter. |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 170 | Returns: |
Mitja Nikolaus | 9c3b29e | 2018-08-22 11:17:50 +0200 | [diff] [blame^] | 171 | The reports matching this report counter requirements. |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 172 | |
| 173 | """ |
| 174 | if self.inclusive_filter: |
| 175 | query_objects = query_objects.filter(self.inclusive_filter) |
| 176 | if self.exclusive_filter: |
| 177 | query_objects = query_objects.exclude(self.exclusive_filter) |
| 178 | |
| 179 | return query_objects |
| 180 | |
| 181 | |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 182 | class _StatsModelsEngine: |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 183 | """Stats models engine. |
| 184 | |
| 185 | An engine to update general stats (_VersionStats) and their daily |
| 186 | counterparts (_DailyVersionStats). |
| 187 | """ |
| 188 | |
Mitja Nikolaus | 9c3b29e | 2018-08-22 11:17:50 +0200 | [diff] [blame^] | 189 | def __init__( |
| 190 | self, |
| 191 | stats_model: _VersionStats, |
| 192 | daily_stats_model: _DailyVersionStats, |
| 193 | version_field_name: str, |
| 194 | ) -> None: |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 195 | """Initialise the engine. |
| 196 | |
| 197 | Args: |
Mitja Nikolaus | 9c3b29e | 2018-08-22 11:17:50 +0200 | [diff] [blame^] | 198 | stats_model: The _VersionStats model to update stats for. |
| 199 | daily_stats_model: The _DailyVersionStats model to update stats for. |
| 200 | version_field_name: |
| 201 | The version field name as specified in the stats models. |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 202 | |
| 203 | """ |
| 204 | self.stats_model = stats_model |
| 205 | self.daily_stats_model = daily_stats_model |
| 206 | self.version_field_name = version_field_name |
| 207 | |
Mitja Nikolaus | 9c3b29e | 2018-08-22 11:17:50 +0200 | [diff] [blame^] | 208 | def _valid_objects(self, query_objects: QuerySet) -> QuerySet: |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 209 | """Filter out invalid reports. |
| 210 | |
| 211 | Returns: |
Mitja Nikolaus | 9c3b29e | 2018-08-22 11:17:50 +0200 | [diff] [blame^] | 212 | All the valid reports. |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 213 | |
| 214 | """ |
| 215 | # pylint: disable=no-self-use |
| 216 | # self is potentially used by subclasses. |
| 217 | return query_objects |
| 218 | |
Mitja Nikolaus | 9c3b29e | 2018-08-22 11:17:50 +0200 | [diff] [blame^] | 219 | def _objects_within_period( |
| 220 | self, |
| 221 | query_objects: QuerySet, |
| 222 | up_to: datetime.datetime, |
| 223 | starting_from: Optional[datetime.datetime] = None, |
| 224 | ) -> QuerySet: |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 225 | """Retrieve the reports within a specific period of time. |
| 226 | |
| 227 | The objects are filtered considering a specific period of time to allow |
| 228 | for comparable results between subclasses. The lower bound should be |
| 229 | omitted for the first update but always set for later calls. The upper |
| 230 | bound must be specified to avoid race conditions. |
| 231 | |
| 232 | Args: |
Mitja Nikolaus | 9c3b29e | 2018-08-22 11:17:50 +0200 | [diff] [blame^] | 233 | query_objects: The reports to filter. |
| 234 | up_to: The maximum timestamp to consider (inclusive). |
| 235 | starting_from: The minimum timestamp to consider (exclusive). |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 236 | Returns: |
Mitja Nikolaus | 9c3b29e | 2018-08-22 11:17:50 +0200 | [diff] [blame^] | 237 | The reports received within a specific period of time. |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 238 | |
| 239 | """ |
| 240 | # pylint: disable=no-self-use |
| 241 | # self might be used by subclasses. |
| 242 | query_objects = query_objects.filter(created_at__lte=up_to) |
| 243 | if starting_from: |
| 244 | query_objects = query_objects.filter(created_at__gt=starting_from) |
| 245 | |
| 246 | return query_objects |
| 247 | |
Mitja Nikolaus | 9c3b29e | 2018-08-22 11:17:50 +0200 | [diff] [blame^] | 248 | def _unique_objects_per_day(self, query_objects: QuerySet) -> QuerySet: |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 249 | """Count the unique reports per version per day. |
| 250 | |
| 251 | Args: |
Mitja Nikolaus | 9c3b29e | 2018-08-22 11:17:50 +0200 | [diff] [blame^] | 252 | query_objects: The reports to count. |
| 253 | Returns: The unique reports grouped per version per day. |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 254 | |
| 255 | """ |
| 256 | return ( |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 257 | query_objects.annotate(_report_day=TruncDate("date")).values( |
| 258 | self.version_field_name, "_report_day" |
| 259 | ) |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 260 | # FIXME Agressively drop duplicates |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 261 | .annotate(count=Count("date", distinct=True)) |
| 262 | ) |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 263 | |
Mitja Nikolaus | 9c3b29e | 2018-08-22 11:17:50 +0200 | [diff] [blame^] | 264 | def delete_stats(self) -> Dict[str, int]: |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 265 | """Delete the general and daily stats instances. |
| 266 | |
Mitja Nikolaus | 9c3b29e | 2018-08-22 11:17:50 +0200 | [diff] [blame^] | 267 | Returns: The count of deleted entries per model name. |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 268 | |
| 269 | """ |
| 270 | # Clear the general stats, the daily stats will be deleted by cascading |
| 271 | # effect |
| 272 | _, count_per_model = self.stats_model.objects.all().delete() |
| 273 | return count_per_model |
| 274 | |
Mitja Nikolaus | 9c3b29e | 2018-08-22 11:17:50 +0200 | [diff] [blame^] | 275 | def update_stats( |
| 276 | self, |
| 277 | report_counter: _ReportCounterFilter, |
| 278 | up_to: datetime.datetime, |
| 279 | starting_from: Optional[datetime.datetime] = None, |
| 280 | ) -> Dict[Model, Dict[str, int]]: |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 281 | """Update the statistics of the general and daily stats entries. |
| 282 | |
| 283 | The algorithm works as follow: |
| 284 | 1. The reports are filtered considering a specific period of time to |
| 285 | allow for comparable results between subclasses. The lower bound |
| 286 | should be omitted for the first update but always set for later |
| 287 | calls. The upper bound must be specified to avoid race conditions. |
| 288 | 2. The report counter requirements are applied to the reports. |
| 289 | 3. The reports are grouped per day and per version, a counter is |
| 290 | generated. |
| 291 | 4. Each report group count is used to update specific daily stats, |
| 292 | while the sum of them per version updates the general stats. |
| 293 | |
| 294 | Args: |
Mitja Nikolaus | 9c3b29e | 2018-08-22 11:17:50 +0200 | [diff] [blame^] | 295 | report_counter: The report counter to update the stats with. |
| 296 | up_to: The maximum timestamp to consider (inclusive). |
| 297 | starting_from: The minimum timestamp to consider (exclusive). |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 298 | Returns: |
Mitja Nikolaus | 9c3b29e | 2018-08-22 11:17:50 +0200 | [diff] [blame^] | 299 | The number of added entries and the number of updated entries |
| 300 | bundled in a dict, respectively hashed with the keys 'created' |
| 301 | and 'updated', per model name. |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 302 | |
| 303 | """ |
| 304 | counts_per_model = { |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 305 | self.stats_model: {"created": 0, "updated": 0}, |
| 306 | self.daily_stats_model: {"created": 0, "updated": 0}, |
| 307 | } |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 308 | |
| 309 | query_objects = self._valid_objects(report_counter.model.objects.all()) |
| 310 | # Only include reports from the interesting period of time |
| 311 | query_objects = self._objects_within_period( |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 312 | query_objects, up_to, starting_from |
| 313 | ) |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 314 | # Apply the report counter requirements |
| 315 | query_objects = report_counter.filter(query_objects) |
| 316 | # Chain our own filters |
| 317 | query_objects = self._unique_objects_per_day(query_objects) |
| 318 | |
| 319 | # Explicitly use the iterator() method to avoid caching as we will |
| 320 | # not re-use the QuerySet |
| 321 | for query_object in query_objects.iterator(): |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 322 | report_day = query_object["_report_day"] |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 323 | # Use a dict to be able to dereference the field name |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 324 | stats, created = self.stats_model.objects.get_or_create( |
| 325 | **{ |
| 326 | self.version_field_name: query_object[ |
| 327 | self.version_field_name |
| 328 | ], |
| 329 | "defaults": { |
| 330 | "first_seen_on": report_day, |
| 331 | "released_on": report_day, |
| 332 | }, |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 333 | } |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 334 | ) |
| 335 | counts_per_model[self.stats_model][ |
| 336 | ("created" if created else "updated") |
| 337 | ] += 1 |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 338 | |
| 339 | # Reports are coming in an unordered manner, a late report can |
| 340 | # be older (device time wise). Make sure that the current reports |
| 341 | # creation date is taken into account in the version history. |
| 342 | if not created and stats.first_seen_on > report_day: |
| 343 | # Avoid changing the released_on field if it is different than |
| 344 | # the default value (i.e. equals to the value of first_seen_on) |
| 345 | # since it indicates that it was manually changed. |
| 346 | if stats.released_on == stats.first_seen_on: |
| 347 | stats.released_on = report_day |
| 348 | stats.first_seen_on = report_day |
| 349 | |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 350 | daily_stats, created = self.daily_stats_model.objects.get_or_create( |
| 351 | version=stats, date=report_day |
| 352 | ) |
| 353 | counts_per_model[self.daily_stats_model][ |
| 354 | ("created" if created else "updated") |
| 355 | ] += 1 |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 356 | |
| 357 | setattr( |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 358 | stats, |
| 359 | report_counter.field_name, |
| 360 | F(report_counter.field_name) + query_object["count"], |
| 361 | ) |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 362 | setattr( |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 363 | daily_stats, |
| 364 | report_counter.field_name, |
| 365 | F(report_counter.field_name) + query_object["count"], |
| 366 | ) |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 367 | |
| 368 | stats.save() |
| 369 | daily_stats.save() |
| 370 | |
| 371 | return counts_per_model |
| 372 | |
| 373 | |
| 374 | class VersionStatsEngine(_StatsModelsEngine): |
| 375 | """Version stats engine. |
| 376 | |
| 377 | An engine to update a counter of general stats (Version) and their daily |
| 378 | counterparts (VersionDaily). |
| 379 | """ |
| 380 | |
| 381 | def __init__(self): |
| 382 | """Initialise the engine.""" |
| 383 | super(VersionStatsEngine, self).__init__( |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 384 | stats_model=Version, |
| 385 | daily_stats_model=VersionDaily, |
| 386 | version_field_name="build_fingerprint", |
| 387 | ) |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 388 | |
| 389 | |
| 390 | class RadioVersionStatsEngine(_StatsModelsEngine): |
| 391 | """Radio version stats engine. |
| 392 | |
| 393 | An engine to update a counter of general stats (RadioVersion) and their |
| 394 | daily counterparts (RadioVersionDaily). |
| 395 | """ |
| 396 | |
| 397 | def __init__(self): |
| 398 | """Initialise the engine.""" |
| 399 | super(RadioVersionStatsEngine, self).__init__( |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 400 | stats_model=RadioVersion, |
| 401 | daily_stats_model=RadioVersionDaily, |
| 402 | version_field_name="radio_version", |
| 403 | ) |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 404 | |
| 405 | def _valid_objects(self, query_objects): |
| 406 | # For legacy reasons, the version field might be null |
| 407 | return query_objects.filter(radio_version__isnull=False) |
| 408 | |
| 409 | |
| 410 | class Command(BaseCommand): |
| 411 | """Management command to compute Hiccup statistics.""" |
| 412 | |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 413 | _STATS_MODELS_ENGINES = [VersionStatsEngine(), RadioVersionStatsEngine()] |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 414 | |
| 415 | # All the report counters that are listed in the stats models |
| 416 | _REPORT_COUNTER_FILTERS = [ |
| 417 | HeartBeatCounterFilter(), |
| 418 | CrashreportCounterFilter( |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 419 | name="crashes", |
| 420 | field_name="prob_crashes", |
| 421 | include_boot_reasons=Crashreport.CRASH_BOOT_REASONS, |
| 422 | ), |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 423 | CrashreportCounterFilter( |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 424 | name="smpl", |
| 425 | field_name="smpl", |
| 426 | include_boot_reasons=Crashreport.SMPL_BOOT_REASONS, |
| 427 | ), |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 428 | CrashreportCounterFilter( |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 429 | name="other", |
| 430 | field_name="other", |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 431 | exclude_boot_reasons=( |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 432 | Crashreport.SMPL_BOOT_REASONS + Crashreport.CRASH_BOOT_REASONS |
| 433 | ), |
| 434 | ), |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 435 | ] |
| 436 | |
| 437 | help = __doc__ |
| 438 | |
| 439 | def add_arguments(self, parser): |
| 440 | """Add custom arguments to the command.""" |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 441 | parser.add_argument("action", choices=["reset", "update"]) |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 442 | |
| 443 | def handle(self, *args, **options): |
| 444 | """Carry out the command executive logic.""" |
| 445 | # pylint: disable=attribute-defined-outside-init |
| 446 | # self.debug is only ever read through calls of handle(). |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 447 | self.debug = int(options["verbosity"]) >= 2 |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 448 | |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 449 | if options["action"] == "reset": |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 450 | self.delete_all_stats() |
| 451 | self.update_all_stats() |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 452 | elif options["action"] == "update": |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 453 | self.update_all_stats() |
| 454 | |
| 455 | def _success(self, msg, *args, **kwargs): |
| 456 | # pylint: disable=no-member |
| 457 | # Members of Style are generated and cannot be statically inferred. |
| 458 | self.stdout.write(self.style.SUCCESS(msg), *args, **kwargs) |
| 459 | |
| 460 | def delete_all_stats(self): |
| 461 | """Delete the statistics from all stats models.""" |
| 462 | with transaction.atomic(): |
| 463 | for engine in self._STATS_MODELS_ENGINES: |
| 464 | counts_per_model = engine.delete_stats() |
| 465 | if self.debug: |
| 466 | # Default the count of deleted models to 0 if missing |
| 467 | if not counts_per_model: |
| 468 | counts_per_model = { |
Franz-Xaver Geiger | cc1e04d | 2018-08-07 11:51:51 +0200 | [diff] [blame] | 469 | engine.stats_model._meta.label: 0, |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 470 | engine.daily_stats_model._meta.label: 0, |
| 471 | } |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 472 | for model, count in counts_per_model.items(): |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 473 | name = model.split(".")[-1] |
| 474 | self._success("{} {} deleted".format(count, name)) |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 475 | |
| 476 | # Reset the metadata |
| 477 | count, _ = StatsMetadata.objects.all().delete() |
| 478 | if self.debug: |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 479 | self._success("{} StatsMetadata deleted".format(count)) |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 480 | |
| 481 | def update_all_stats(self): |
| 482 | """Update the statistics from all stats models.""" |
| 483 | try: |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 484 | previous_update = StatsMetadata.objects.latest("updated_at") |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 485 | starting_from = previous_update.updated_at |
| 486 | except StatsMetadata.DoesNotExist: |
| 487 | starting_from = None |
| 488 | # Fix the upper limit to avoid race conditions with new reports sent |
| 489 | # while we are updating the different statistics |
| 490 | up_to = datetime.datetime.now(tz=pytz.utc) |
| 491 | |
| 492 | for engine in self._STATS_MODELS_ENGINES: |
| 493 | with transaction.atomic(): |
| 494 | for filter_ in self._REPORT_COUNTER_FILTERS: |
| 495 | counts_per_model = engine.update_stats( |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 496 | filter_, up_to, starting_from |
| 497 | ) |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 498 | if self.debug: |
| 499 | for model, counts in counts_per_model.items(): |
| 500 | for action, count in counts.items(): |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 501 | msg = "{} {} {} for counter {}".format( |
| 502 | count, model.__name__, action, filter_.name |
| 503 | ) |
Borjan Tchakaloff | b98dba7 | 2018-03-16 11:04:47 +0400 | [diff] [blame] | 504 | self._success(msg) |
| 505 | |
| 506 | StatsMetadata(updated_at=up_to).save() |