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