Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 1 | """REST API for accessing the crashreports statistics.""" |
| 2 | import zipfile |
Mitja Nikolaus | 24f4d12 | 2018-08-24 16:32:58 +0200 | [diff] [blame^] | 3 | from collections import OrderedDict |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 4 | |
Mitja Nikolaus | 24f4d12 | 2018-08-24 16:32:58 +0200 | [diff] [blame^] | 5 | from drf_yasg import openapi |
| 6 | from drf_yasg.utils import swagger_auto_schema |
| 7 | from rest_framework import generics, status |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 8 | from rest_framework import serializers |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 9 | from rest_framework.exceptions import NotFound |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 10 | from rest_framework.response import Response |
| 11 | from rest_framework.views import APIView |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 12 | |
| 13 | from django.core.exceptions import ObjectDoesNotExist |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 14 | from django.db import connection |
Dirk Vogt | 1accb67 | 2017-05-10 14:07:42 +0200 | [diff] [blame] | 15 | from django.db.models.expressions import F |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 16 | |
| 17 | from django_filters.rest_framework import ( |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 18 | DjangoFilterBackend, |
| 19 | DateFilter, |
| 20 | FilterSet, |
| 21 | CharFilter, |
| 22 | BooleanFilter, |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 23 | ) |
| 24 | |
| 25 | from crashreport_stats.models import ( |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 26 | Version, |
| 27 | VersionDaily, |
| 28 | RadioVersion, |
| 29 | RadioVersionDaily, |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 30 | ) |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 31 | from crashreports.models import Device, Crashreport, HeartBeat, LogFile |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 32 | from crashreports.permissions import ( |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 33 | HasRightsOrIsDeviceOwnerDeviceCreation, |
| 34 | HasStatsAccess, |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 35 | ) |
Mitja Nikolaus | 24f4d12 | 2018-08-24 16:32:58 +0200 | [diff] [blame^] | 36 | from crashreports.response_descriptions import default_desc |
| 37 | |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 38 | from . import raw_querys |
| 39 | |
Mitja Nikolaus | 24f4d12 | 2018-08-24 16:32:58 +0200 | [diff] [blame^] | 40 | _RESPONSE_STATUS_200_DESCRIPTION = "OK" |
| 41 | |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 42 | |
| 43 | def dictfetchall(cursor): |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 44 | """Return all rows from a cursor as a dict.""" |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 45 | desc = cursor.description |
| 46 | return [ |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 47 | dict(zip([col[0] for col in desc], row)) for row in cursor.fetchall() |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 48 | ] |
| 49 | |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 50 | |
Mitja Nikolaus | 24f4d12 | 2018-08-24 16:32:58 +0200 | [diff] [blame^] | 51 | _DEVICE_UPDATE_HISTORY_SCHEMA = openapi.Schema( |
| 52 | type=openapi.TYPE_ARRAY, |
| 53 | items=openapi.Schema( |
| 54 | type=openapi.TYPE_OBJECT, |
| 55 | title="DeviceUpdateHistoryEntry", |
| 56 | properties=OrderedDict( |
| 57 | [ |
| 58 | ("build_fingerprint", openapi.Schema(type=openapi.TYPE_STRING)), |
| 59 | ("heartbeats", openapi.Schema(type=openapi.TYPE_INTEGER)), |
| 60 | ("max", openapi.Schema(type=openapi.TYPE_INTEGER)), |
| 61 | ("other", openapi.Schema(type=openapi.TYPE_INTEGER)), |
| 62 | ("prob_crashes", openapi.Schema(type=openapi.TYPE_INTEGER)), |
| 63 | ("smpl", openapi.Schema(type=openapi.TYPE_INTEGER)), |
| 64 | ("update_date", openapi.Schema(type=openapi.TYPE_STRING)), |
| 65 | ] |
| 66 | ), |
| 67 | ), |
| 68 | ) |
| 69 | |
| 70 | |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 71 | class DeviceUpdateHistory(APIView): |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 72 | """View the update history of a specific device.""" |
| 73 | |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 74 | permission_classes = (HasRightsOrIsDeviceOwnerDeviceCreation,) |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 75 | |
Mitja Nikolaus | 24f4d12 | 2018-08-24 16:32:58 +0200 | [diff] [blame^] | 76 | @swagger_auto_schema( |
| 77 | operation_description="Get the update history of a device", |
| 78 | responses=dict( |
| 79 | [ |
| 80 | default_desc(NotFound), |
| 81 | ( |
| 82 | status.HTTP_200_OK, |
| 83 | openapi.Response( |
| 84 | _RESPONSE_STATUS_200_DESCRIPTION, |
| 85 | _DEVICE_UPDATE_HISTORY_SCHEMA, |
| 86 | ), |
| 87 | ), |
| 88 | ] |
| 89 | ), |
| 90 | ) |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 91 | def get(self, request, uuid, format=None): |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 92 | """Get the update history of a device. |
| 93 | |
| 94 | Args: |
| 95 | request: Http request |
| 96 | uuid: The UUID of the device |
| 97 | format: Optional response format parameter |
| 98 | |
| 99 | Returns: The update history of the requested device. |
| 100 | |
| 101 | """ |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 102 | cursor = connection.cursor() |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 103 | raw_querys.execute_device_update_history_query(cursor, {"uuid": uuid}) |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 104 | res = dictfetchall(cursor) |
| 105 | return Response(res) |
| 106 | |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 107 | |
Mitja Nikolaus | 24f4d12 | 2018-08-24 16:32:58 +0200 | [diff] [blame^] | 108 | _DEVICE_REPORT_HISTORY_SCHEMA = openapi.Schema( |
| 109 | type=openapi.TYPE_ARRAY, |
| 110 | items=openapi.Schema( |
| 111 | type=openapi.TYPE_OBJECT, |
| 112 | title="DeviceReportHistoryEntry", |
| 113 | properties=OrderedDict( |
| 114 | [ |
| 115 | ("date", openapi.Schema(type=openapi.TYPE_STRING)), |
| 116 | ("heartbeats", openapi.Schema(type=openapi.TYPE_INTEGER)), |
| 117 | ("other", openapi.Schema(type=openapi.TYPE_INTEGER)), |
| 118 | ("prob_crashes", openapi.Schema(type=openapi.TYPE_INTEGER)), |
| 119 | ("smpl", openapi.Schema(type=openapi.TYPE_INTEGER)), |
| 120 | ] |
| 121 | ), |
| 122 | ), |
| 123 | ) |
| 124 | |
| 125 | |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 126 | class DeviceReportHistory(APIView): |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 127 | """View the report history of a specific device.""" |
| 128 | |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 129 | permission_classes = (HasRightsOrIsDeviceOwnerDeviceCreation,) |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 130 | |
Mitja Nikolaus | 24f4d12 | 2018-08-24 16:32:58 +0200 | [diff] [blame^] | 131 | @swagger_auto_schema( |
| 132 | operation_description="Get the report history of a device", |
| 133 | responses=dict( |
| 134 | [ |
| 135 | default_desc(NotFound), |
| 136 | ( |
| 137 | status.HTTP_200_OK, |
| 138 | openapi.Response( |
| 139 | _RESPONSE_STATUS_200_DESCRIPTION, |
| 140 | _DEVICE_REPORT_HISTORY_SCHEMA, |
| 141 | ), |
| 142 | ), |
| 143 | ] |
| 144 | ), |
| 145 | ) |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 146 | def get(self, request, uuid, format=None): |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 147 | """Get the report history of a device. |
| 148 | |
| 149 | Args: |
| 150 | request: Http request |
| 151 | uuid: The UUID of the device |
| 152 | format: Optional response format parameter |
| 153 | |
| 154 | Returns: The report history of the requested device. |
| 155 | |
| 156 | """ |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 157 | cursor = connection.cursor() |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 158 | raw_querys.execute_device_report_history(cursor, {"uuid": uuid}) |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 159 | res = dictfetchall(cursor) |
| 160 | return Response(res) |
| 161 | |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 162 | |
Mitja Nikolaus | 24f4d12 | 2018-08-24 16:32:58 +0200 | [diff] [blame^] | 163 | _STATUS_RESPONSE_SCHEMA = openapi.Schema( |
| 164 | title="Status", |
| 165 | type=openapi.TYPE_OBJECT, |
| 166 | properties=OrderedDict( |
| 167 | [ |
| 168 | ("devices", openapi.Schema(type=openapi.TYPE_INTEGER)), |
| 169 | ("crashreports", openapi.Schema(type=openapi.TYPE_INTEGER)), |
| 170 | ("heartbeats", openapi.Schema(type=openapi.TYPE_INTEGER)), |
| 171 | ] |
| 172 | ), |
| 173 | ) |
| 174 | |
| 175 | |
Dirk Vogt | 571168c | 2017-12-08 16:54:12 +0100 | [diff] [blame] | 176 | class Status(APIView): |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 177 | """View the number of devices, crashreports and heartbeats.""" |
| 178 | |
Borjan Tchakaloff | fa134bd | 2018-04-09 16:16:11 +0200 | [diff] [blame] | 179 | permission_classes = (HasStatsAccess,) |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 180 | |
Mitja Nikolaus | 24f4d12 | 2018-08-24 16:32:58 +0200 | [diff] [blame^] | 181 | @swagger_auto_schema( |
| 182 | operation_description="Get the number of devices, crashreports and " |
| 183 | "heartbeats", |
| 184 | responses=dict( |
| 185 | [ |
| 186 | ( |
| 187 | status.HTTP_200_OK, |
| 188 | openapi.Response( |
| 189 | _RESPONSE_STATUS_200_DESCRIPTION, |
| 190 | _STATUS_RESPONSE_SCHEMA, |
| 191 | ), |
| 192 | ) |
| 193 | ] |
| 194 | ), |
| 195 | ) |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 196 | def get(self, request, format=None): |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 197 | """Get the number of devices, crashreports and heartbeats. |
| 198 | |
| 199 | Args: |
| 200 | request: Http request |
| 201 | format: Optional response format parameter |
| 202 | |
| 203 | Returns: The number of devices, crashreports and heartbeats. |
| 204 | |
| 205 | """ |
Dirk Vogt | 571168c | 2017-12-08 16:54:12 +0100 | [diff] [blame] | 206 | num_devices = Device.objects.count() |
| 207 | num_crashreports = Crashreport.objects.count() |
| 208 | num_heartbeats = HeartBeat.objects.count() |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 209 | return Response( |
| 210 | { |
| 211 | "devices": num_devices, |
| 212 | "crashreports": num_crashreports, |
| 213 | "heartbeats": num_heartbeats, |
| 214 | } |
| 215 | ) |
Dirk Vogt | 571168c | 2017-12-08 16:54:12 +0100 | [diff] [blame] | 216 | |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 217 | |
Mitja Nikolaus | 24f4d12 | 2018-08-24 16:32:58 +0200 | [diff] [blame^] | 218 | _DEVICE_STAT_OVERVIEW_SCHEMA = openapi.Schema( |
| 219 | title="DeviceStatOverview", |
| 220 | type=openapi.TYPE_OBJECT, |
| 221 | properties=OrderedDict( |
| 222 | [ |
| 223 | ("board_date", openapi.Schema(type=openapi.TYPE_STRING)), |
| 224 | ("crashes_per_day", openapi.Schema(type=openapi.TYPE_NUMBER)), |
| 225 | ("crashreports", openapi.Schema(type=openapi.TYPE_INTEGER)), |
| 226 | ("heartbeats", openapi.Schema(type=openapi.TYPE_INTEGER)), |
| 227 | ("last_active", openapi.Schema(type=openapi.TYPE_STRING)), |
| 228 | ("smpl_per_day", openapi.Schema(type=openapi.TYPE_NUMBER)), |
| 229 | ("smpls", openapi.Schema(type=openapi.TYPE_INTEGER)), |
| 230 | ("uuid", openapi.Schema(type=openapi.TYPE_STRING)), |
| 231 | ] |
| 232 | ), |
| 233 | ) |
| 234 | |
| 235 | |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 236 | class DeviceStat(APIView): |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 237 | """View an overview of the statistics of a device.""" |
| 238 | |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 239 | permission_classes = (HasRightsOrIsDeviceOwnerDeviceCreation,) |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 240 | |
Mitja Nikolaus | 24f4d12 | 2018-08-24 16:32:58 +0200 | [diff] [blame^] | 241 | @swagger_auto_schema( |
| 242 | operation_description="Get some general statistics for a device.", |
| 243 | responses=dict( |
| 244 | [ |
| 245 | default_desc(NotFound), |
| 246 | ( |
| 247 | status.HTTP_200_OK, |
| 248 | openapi.Response( |
| 249 | _RESPONSE_STATUS_200_DESCRIPTION, |
| 250 | _DEVICE_STAT_OVERVIEW_SCHEMA, |
| 251 | ), |
| 252 | ), |
| 253 | ] |
| 254 | ), |
| 255 | ) |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 256 | def get(self, request, uuid, format=None): |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 257 | """Get some general statistics for a device. |
| 258 | |
| 259 | Args: |
| 260 | request: Http request |
| 261 | uuid: The UUID of the device |
| 262 | format: Optional response format parameter |
| 263 | |
| 264 | Returns: Some general information of the device in a dictionary. |
| 265 | |
| 266 | """ |
| 267 | device = Device.objects.filter(uuid=uuid) |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 268 | last_active = ( |
| 269 | HeartBeat.objects.filter(device=device).order_by("-date")[0].date |
| 270 | ) |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 271 | heartbeats = HeartBeat.objects.filter(device=device).count() |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 272 | crashreports = ( |
| 273 | Crashreport.objects.filter(device=device) |
| 274 | .filter(boot_reason__in=Crashreport.CRASH_BOOT_REASONS) |
| 275 | .count() |
| 276 | ) |
| 277 | crashes_per_day = ( |
| 278 | crashreports * 1.0 / heartbeats if heartbeats > 0 else 0 |
| 279 | ) |
| 280 | smpls = ( |
| 281 | Crashreport.objects.filter(device=device) |
| 282 | .filter(boot_reason__in=Crashreport.SMPL_BOOT_REASONS) |
| 283 | .count() |
| 284 | ) |
| 285 | smpl_per_day = smpls * 1.0 / heartbeats if heartbeats > 0 else 0 |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 286 | return Response( |
| 287 | { |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 288 | "uuid": uuid, |
| 289 | "last_active": last_active, |
| 290 | "heartbeats": heartbeats, |
| 291 | "crashreports": crashreports, |
| 292 | "crashes_per_day": crashes_per_day, |
| 293 | "smpls": smpls, |
| 294 | "smpl_per_day": smpl_per_day, |
| 295 | "board_date": device[0].board_date, |
| 296 | } |
| 297 | ) |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 298 | |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 299 | |
Mitja Nikolaus | 24f4d12 | 2018-08-24 16:32:58 +0200 | [diff] [blame^] | 300 | _LOG_FILE_SCHEMA = openapi.Schema(title="LogFile", type=openapi.TYPE_FILE) |
| 301 | |
| 302 | |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 303 | class LogFileDownload(APIView): |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 304 | """View for downloading log files.""" |
| 305 | |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 306 | permission_classes = (HasRightsOrIsDeviceOwnerDeviceCreation,) |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 307 | |
Mitja Nikolaus | 24f4d12 | 2018-08-24 16:32:58 +0200 | [diff] [blame^] | 308 | @swagger_auto_schema( |
| 309 | operation_description="Get a log file.", |
| 310 | responses=dict( |
| 311 | [ |
| 312 | default_desc(NotFound), |
| 313 | ( |
| 314 | status.HTTP_200_OK, |
| 315 | openapi.Response( |
| 316 | _RESPONSE_STATUS_200_DESCRIPTION, _LOG_FILE_SCHEMA |
| 317 | ), |
| 318 | ), |
| 319 | ] |
| 320 | ), |
| 321 | ) |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 322 | def get(self, request, id_logfile, format=None): |
| 323 | """Get a logfile. |
| 324 | |
| 325 | Args: |
| 326 | request: Http request |
| 327 | id_logfile: The id of the log file |
| 328 | format: Optional response format parameter |
| 329 | |
| 330 | Returns: The log file with the corresponding id. |
| 331 | |
| 332 | """ |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 333 | try: |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 334 | logfile = LogFile.objects.get(id=id_logfile) |
| 335 | except ObjectDoesNotExist: |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 336 | raise NotFound(detail="Logfile does not exist.") |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 337 | zip_file = zipfile.ZipFile(logfile.logfile.path) |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 338 | ret = {} |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 339 | for file in zip_file.filelist: |
| 340 | file_open = zip_file.open(file) |
| 341 | ret[file.filename] = file_open.read() |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 342 | return Response(ret) |
Dirk Vogt | 1accb67 | 2017-05-10 14:07:42 +0200 | [diff] [blame] | 343 | |
Borjan Tchakaloff | 227b431 | 2018-02-23 17:02:16 +0400 | [diff] [blame] | 344 | |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 345 | class _VersionStatsFilter(FilterSet): |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 346 | first_seen_before = DateFilter( |
| 347 | field_name="first_seen_on", lookup_expr="lte" |
| 348 | ) |
| 349 | first_seen_after = DateFilter(field_name="first_seen_on", lookup_expr="gte") |
| 350 | released_before = DateFilter(field_name="released_on", lookup_expr="lte") |
| 351 | released_after = DateFilter(field_name="released_on", lookup_expr="gte") |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 352 | |
Dirk Vogt | 1accb67 | 2017-05-10 14:07:42 +0200 | [diff] [blame] | 353 | |
Borjan Tchakaloff | 227b431 | 2018-02-23 17:02:16 +0400 | [diff] [blame] | 354 | class _VersionStatsSerializer(serializers.ModelSerializer): |
Borjan Tchakaloff | fa134bd | 2018-04-09 16:16:11 +0200 | [diff] [blame] | 355 | permission_classes = (HasStatsAccess,) |
Dirk Vogt | 1accb67 | 2017-05-10 14:07:42 +0200 | [diff] [blame] | 356 | |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 357 | |
Borjan Tchakaloff | 227b431 | 2018-02-23 17:02:16 +0400 | [diff] [blame] | 358 | class _VersionStatsListView(generics.ListAPIView): |
Borjan Tchakaloff | fa134bd | 2018-04-09 16:16:11 +0200 | [diff] [blame] | 359 | permission_classes = (HasStatsAccess,) |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 360 | filter_backends = (DjangoFilterBackend,) |
Dirk Vogt | 1accb67 | 2017-05-10 14:07:42 +0200 | [diff] [blame] | 361 | |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 362 | |
| 363 | class _DailyVersionStatsFilter(FilterSet): |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 364 | date_start = DateFilter(field_name="date", lookup_expr="gte") |
| 365 | date_end = DateFilter(field_name="date", lookup_expr="lte") |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 366 | |
Borjan Tchakaloff | 227b431 | 2018-02-23 17:02:16 +0400 | [diff] [blame] | 367 | |
| 368 | class _DailyVersionStatsSerializer(serializers.ModelSerializer): |
Borjan Tchakaloff | fa134bd | 2018-04-09 16:16:11 +0200 | [diff] [blame] | 369 | permission_classes = (HasStatsAccess,) |
Borjan Tchakaloff | 227b431 | 2018-02-23 17:02:16 +0400 | [diff] [blame] | 370 | |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 371 | |
Borjan Tchakaloff | 227b431 | 2018-02-23 17:02:16 +0400 | [diff] [blame] | 372 | class _DailyVersionStatsListView(generics.ListAPIView): |
Borjan Tchakaloff | fa134bd | 2018-04-09 16:16:11 +0200 | [diff] [blame] | 373 | permission_classes = (HasStatsAccess,) |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 374 | filter_backends = (DjangoFilterBackend,) |
Borjan Tchakaloff | 227b431 | 2018-02-23 17:02:16 +0400 | [diff] [blame] | 375 | |
| 376 | |
| 377 | class VersionSerializer(_VersionStatsSerializer): |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 378 | """Serializer for the Version class.""" |
| 379 | |
| 380 | class Meta: # noqa: D106 |
Borjan Tchakaloff | 227b431 | 2018-02-23 17:02:16 +0400 | [diff] [blame] | 381 | model = Version |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 382 | fields = "__all__" |
Borjan Tchakaloff | 227b431 | 2018-02-23 17:02:16 +0400 | [diff] [blame] | 383 | |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 384 | |
Borjan Tchakaloff | 227b431 | 2018-02-23 17:02:16 +0400 | [diff] [blame] | 385 | class VersionFilter(_VersionStatsFilter): |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 386 | """Filter for Version instances.""" |
| 387 | |
| 388 | class Meta: # noqa: D106 |
Borjan Tchakaloff | 227b431 | 2018-02-23 17:02:16 +0400 | [diff] [blame] | 389 | model = Version |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 390 | fields = "__all__" |
Borjan Tchakaloff | 227b431 | 2018-02-23 17:02:16 +0400 | [diff] [blame] | 391 | |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 392 | |
Borjan Tchakaloff | 227b431 | 2018-02-23 17:02:16 +0400 | [diff] [blame] | 393 | class VersionListView(_VersionStatsListView): |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 394 | """View for listing versions.""" |
| 395 | |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 396 | queryset = Version.objects.all().order_by("-heartbeats") |
Borjan Tchakaloff | 227b431 | 2018-02-23 17:02:16 +0400 | [diff] [blame] | 397 | filter_class = VersionFilter |
| 398 | serializer_class = VersionSerializer |
| 399 | |
Borjan Tchakaloff | 227b431 | 2018-02-23 17:02:16 +0400 | [diff] [blame] | 400 | |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 401 | class VersionDailyFilter(_DailyVersionStatsFilter): |
| 402 | """Filter for VersionDaily instances.""" |
| 403 | |
| 404 | version__build_fingerprint = CharFilter() |
| 405 | version__is_official_release = BooleanFilter() |
| 406 | version__is_beta_release = BooleanFilter() |
| 407 | |
| 408 | class Meta: # noqa: D106 |
Dirk Vogt | 1accb67 | 2017-05-10 14:07:42 +0200 | [diff] [blame] | 409 | model = VersionDaily |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 410 | fields = "__all__" |
Dirk Vogt | 1accb67 | 2017-05-10 14:07:42 +0200 | [diff] [blame] | 411 | |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 412 | |
Borjan Tchakaloff | 227b431 | 2018-02-23 17:02:16 +0400 | [diff] [blame] | 413 | class VersionDailySerializer(_DailyVersionStatsSerializer): |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 414 | """Serializer for VersionDaily instances.""" |
| 415 | |
Borjan Tchakaloff | 227b431 | 2018-02-23 17:02:16 +0400 | [diff] [blame] | 416 | build_fingerprint = serializers.CharField() |
| 417 | |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 418 | class Meta: # noqa: D106 |
Dirk Vogt | 1accb67 | 2017-05-10 14:07:42 +0200 | [diff] [blame] | 419 | model = VersionDaily |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 420 | fields = "__all__" |
Dirk Vogt | 1accb67 | 2017-05-10 14:07:42 +0200 | [diff] [blame] | 421 | |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 422 | |
Borjan Tchakaloff | 227b431 | 2018-02-23 17:02:16 +0400 | [diff] [blame] | 423 | class VersionDailyListView(_DailyVersionStatsListView): |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 424 | """View for listing VersionDaily instances.""" |
| 425 | |
Borjan Tchakaloff | 227b431 | 2018-02-23 17:02:16 +0400 | [diff] [blame] | 426 | queryset = ( |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 427 | VersionDaily.objects.annotate( |
| 428 | build_fingerprint=F("version__build_fingerprint") |
| 429 | ) |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 430 | .all() |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 431 | .order_by("date") |
Borjan Tchakaloff | 227b431 | 2018-02-23 17:02:16 +0400 | [diff] [blame] | 432 | ) |
| 433 | filter_class = VersionDailyFilter |
| 434 | filter_fields = ( |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 435 | "version__build_fingerprint", |
| 436 | "version__is_official_release", |
| 437 | "version__is_beta_release", |
Borjan Tchakaloff | 227b431 | 2018-02-23 17:02:16 +0400 | [diff] [blame] | 438 | ) |
Dirk Vogt | 1accb67 | 2017-05-10 14:07:42 +0200 | [diff] [blame] | 439 | serializer_class = VersionDailySerializer |
Borjan Tchakaloff | 1db45c7 | 2018-02-23 17:03:49 +0400 | [diff] [blame] | 440 | |
| 441 | |
| 442 | class RadioVersionSerializer(_VersionStatsSerializer): |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 443 | """Serializer for RadioVersion instances.""" |
| 444 | |
| 445 | class Meta: # noqa: D106 |
Borjan Tchakaloff | 1db45c7 | 2018-02-23 17:03:49 +0400 | [diff] [blame] | 446 | model = RadioVersion |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 447 | fields = "__all__" |
Borjan Tchakaloff | 1db45c7 | 2018-02-23 17:03:49 +0400 | [diff] [blame] | 448 | |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 449 | |
Borjan Tchakaloff | 1db45c7 | 2018-02-23 17:03:49 +0400 | [diff] [blame] | 450 | class RadioVersionFilter(_VersionStatsFilter): |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 451 | """Filter for RadioVersion instances.""" |
| 452 | |
| 453 | class Meta: # noqa: D106 |
Borjan Tchakaloff | 1db45c7 | 2018-02-23 17:03:49 +0400 | [diff] [blame] | 454 | model = RadioVersion |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 455 | fields = "__all__" |
Borjan Tchakaloff | 1db45c7 | 2018-02-23 17:03:49 +0400 | [diff] [blame] | 456 | |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 457 | |
Borjan Tchakaloff | 1db45c7 | 2018-02-23 17:03:49 +0400 | [diff] [blame] | 458 | class RadioVersionListView(_VersionStatsListView): |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 459 | """View for listing RadioVersion instances.""" |
| 460 | |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 461 | queryset = RadioVersion.objects.all().order_by("-heartbeats") |
Borjan Tchakaloff | 1db45c7 | 2018-02-23 17:03:49 +0400 | [diff] [blame] | 462 | serializer_class = RadioVersionSerializer |
| 463 | filter_class = RadioVersionFilter |
| 464 | |
Borjan Tchakaloff | 1db45c7 | 2018-02-23 17:03:49 +0400 | [diff] [blame] | 465 | |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 466 | class RadioVersionDailyFilter(_DailyVersionStatsFilter): |
| 467 | """Filter for RadioVersionDaily instances.""" |
| 468 | |
| 469 | version__radio_version = CharFilter() |
| 470 | version__is_official_release = BooleanFilter() |
| 471 | version__is_beta_release = BooleanFilter() |
| 472 | |
| 473 | class Meta: # noqa: D106 |
Borjan Tchakaloff | 1db45c7 | 2018-02-23 17:03:49 +0400 | [diff] [blame] | 474 | model = RadioVersionDaily |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 475 | fields = "__all__" |
Borjan Tchakaloff | 1db45c7 | 2018-02-23 17:03:49 +0400 | [diff] [blame] | 476 | |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 477 | |
Borjan Tchakaloff | 1db45c7 | 2018-02-23 17:03:49 +0400 | [diff] [blame] | 478 | class RadioVersionDailySerializer(_DailyVersionStatsSerializer): |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 479 | """Serializer for RadioVersionDaily instances.""" |
| 480 | |
Borjan Tchakaloff | 1db45c7 | 2018-02-23 17:03:49 +0400 | [diff] [blame] | 481 | radio_version = serializers.CharField() |
| 482 | |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 483 | class Meta: # noqa: D106 |
Borjan Tchakaloff | 1db45c7 | 2018-02-23 17:03:49 +0400 | [diff] [blame] | 484 | model = RadioVersionDaily |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 485 | fields = "__all__" |
Borjan Tchakaloff | 1db45c7 | 2018-02-23 17:03:49 +0400 | [diff] [blame] | 486 | |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 487 | |
Borjan Tchakaloff | 1db45c7 | 2018-02-23 17:03:49 +0400 | [diff] [blame] | 488 | class RadioVersionDailyListView(_DailyVersionStatsListView): |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 489 | """View for listing RadioVersionDaily instances.""" |
| 490 | |
Borjan Tchakaloff | 1db45c7 | 2018-02-23 17:03:49 +0400 | [diff] [blame] | 491 | queryset = ( |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 492 | RadioVersionDaily.objects.annotate( |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 493 | radio_version=F("version__radio_version") |
| 494 | ) |
Mitja Nikolaus | 5c3e057 | 2018-07-30 13:36:14 +0200 | [diff] [blame] | 495 | .all() |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 496 | .order_by("date") |
Borjan Tchakaloff | 1db45c7 | 2018-02-23 17:03:49 +0400 | [diff] [blame] | 497 | ) |
| 498 | filter_class = RadioVersionDailyFilter |
| 499 | filter_fields = ( |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 500 | "version__radio_version", |
| 501 | "version__is_official_release", |
| 502 | "version__is_beta_release", |
Borjan Tchakaloff | 1db45c7 | 2018-02-23 17:03:49 +0400 | [diff] [blame] | 503 | ) |
| 504 | serializer_class = RadioVersionDailySerializer |