|
15 | 15 | Sum,
|
16 | 16 | CharField,
|
17 | 17 | DecimalField,
|
| 18 | + IntegerField, |
18 | 19 | Func,
|
19 | 20 | Value,
|
20 | 21 | Q,
|
@@ -248,111 +249,171 @@ def annotate_main_activity_details_id(self):
|
248 | 249 | )
|
249 | 250 | )
|
250 | 251 |
|
251 |
| - def appropriations_for_dst_payload(self, from_date=None, to_date=None): |
| 252 | + def appropriations_for_dst_payload( |
| 253 | + self, from_date=None, to_date=None, initial_load=False |
| 254 | + ): |
252 | 255 | """Filter appropriations for a Danmarks Statistik payload.
|
253 | 256 |
|
254 |
| - We annotate a report_type based on whether an Appropriation |
255 |
| - has "changed" or not which is based on: |
256 |
| - - changed acting municipality . |
257 |
| - - appropriation_date for activities |
258 |
| -
|
259 |
| - appropriation contains only granted main activities appropriated |
260 |
| - before 'from_date' |
261 |
| - -> exclude (as they have been included in a previous payload) |
262 |
| -
|
263 |
| - appropriation contains only granted main activities appropriated |
264 |
| - after 'from_date' |
265 |
| - -> status NEW |
266 |
| -
|
267 |
| - appropriation contains granted main activities appropriated both |
268 |
| - before and after 'from_date' |
269 |
| - -> status CHANGED |
| 257 | + This involves filtering and annotating an appropriate dst_report_type. |
270 | 258 | """
|
271 | 259 | from core.models import (
|
272 | 260 | MAIN_ACTIVITY,
|
273 | 261 | STATUS_GRANTED,
|
274 | 262 | Case as CaseModel,
|
| 263 | + Activity, |
275 | 264 | )
|
276 | 265 |
|
277 |
| - queryset = self.filter( |
278 |
| - activities__status=STATUS_GRANTED, |
279 |
| - activities__activity_type=MAIN_ACTIVITY, |
280 |
| - ) |
281 |
| - # If to_date is set we cut off appropriations |
282 |
| - # with activities with a newer appropriation_date. |
283 |
| - if to_date: |
284 |
| - queryset = queryset.filter( |
285 |
| - activities__appropriation_date__lte=to_date |
286 |
| - ) |
| 266 | + if not from_date: |
| 267 | + from_date = datetime.date(year=1970, month=1, day=1) |
| 268 | + if not to_date: |
| 269 | + to_date = timezone.now().date() |
287 | 270 |
|
288 | 271 | report_types = {
|
289 | 272 | "NEW": "Ny",
|
290 | 273 | "CHANGED": "Ændring",
|
291 | 274 | "CANCELLED": "Annullering",
|
292 | 275 | }
|
293 | 276 |
|
294 |
| - if from_date: |
| 277 | + queryset = self |
| 278 | + |
| 279 | + if initial_load: |
| 280 | + # Full/initial load where all appropriations are marked "NEW". |
| 281 | + |
| 282 | + # Cut off appropriations appropriated outside the |
| 283 | + # from_date->to_date range. |
| 284 | + queryset = ( |
| 285 | + queryset.filter( |
| 286 | + activities__status=STATUS_GRANTED, |
| 287 | + activities__activity_type=MAIN_ACTIVITY, |
| 288 | + activities__appropriation_date__gte=from_date, |
| 289 | + activities__appropriation_date__lte=to_date, |
| 290 | + ) |
| 291 | + .annotate( |
| 292 | + dst_report_type=Value( |
| 293 | + report_types["NEW"], output_field=CharField() |
| 294 | + ) |
| 295 | + ) |
| 296 | + .distinct() |
| 297 | + ) |
| 298 | + |
| 299 | + # Delta load where appropriations are marked "NEW" and "CHANGED" |
| 300 | + # based on different criteria. |
| 301 | + else: |
295 | 302 | cases = CaseModel.objects.filter(appropriations__in=queryset)
|
296 | 303 | changed_cases = cases.filter_changed_cases_for_dst_payload(
|
297 | 304 | from_date, to_date
|
298 | 305 | )
|
299 |
| - |
300 |
| - main_activities_q = Q(activities__activity_type=MAIN_ACTIVITY) |
301 |
| - main_activities_appropriated_after_from_date_q = Q( |
| 306 | + # Cut off appropriations appropriated outside the |
| 307 | + # from_date->to_date range. |
| 308 | + queryset = queryset.filter( |
| 309 | + Q( |
| 310 | + Q(activities__appropriation_date__gte=from_date) |
| 311 | + & Q(activities__appropriation_date__lte=to_date) |
| 312 | + ) |
| 313 | + | Q(case__in=changed_cases), |
| 314 | + activities__status=STATUS_GRANTED, |
302 | 315 | activities__activity_type=MAIN_ACTIVITY,
|
303 |
| - activities__appropriation_date__gte=from_date, |
304 | 316 | )
|
305 |
| - |
306 |
| - main_activities_appropriated_before_from_date_q = Q( |
307 |
| - activities__activity_type=MAIN_ACTIVITY, |
308 |
| - activities__appropriation_date__lte=from_date, |
| 317 | + # Use subqueries as multiple annotations will yield wrong results: |
| 318 | + # https://docs.djangoproject.com/en/2.2/topics/db/aggregation/#combining-multiple-aggregations |
| 319 | + # https://code.djangoproject.com/ticket/10060 |
| 320 | + main_acts = ( |
| 321 | + Activity.objects.filter(appropriation=OuterRef("id")) |
| 322 | + .order_by() |
| 323 | + .filter( |
| 324 | + activity_type=MAIN_ACTIVITY, |
| 325 | + status=STATUS_GRANTED, |
| 326 | + ) |
| 327 | + .values("appropriation") |
| 328 | + .annotate(c=Count("appropriation")) |
| 329 | + .values("c") |
| 330 | + ) |
| 331 | + main_acts_before_from_date = ( |
| 332 | + Activity.objects.filter(appropriation=OuterRef("id")) |
| 333 | + .order_by() |
| 334 | + .filter( |
| 335 | + activity_type=MAIN_ACTIVITY, |
| 336 | + status=STATUS_GRANTED, |
| 337 | + appropriation_date__lt=from_date, |
| 338 | + ) |
| 339 | + .values("appropriation") |
| 340 | + .annotate(c=Count("appropriation")) |
| 341 | + .values("c") |
| 342 | + ) |
| 343 | + main_acts_after_from_date = ( |
| 344 | + Activity.objects.filter(appropriation=OuterRef("id")) |
| 345 | + .order_by() |
| 346 | + .filter( |
| 347 | + activity_type=MAIN_ACTIVITY, |
| 348 | + status=STATUS_GRANTED, |
| 349 | + appropriation_date__gte=from_date, |
| 350 | + ) |
| 351 | + .values("appropriation") |
| 352 | + .annotate(c=Count("appropriation")) |
| 353 | + .values("c") |
309 | 354 | )
|
310 | 355 |
|
311 |
| - queryset = queryset.annotate( |
312 |
| - main_acts_count=Count("activities", filter=main_activities_q), |
313 |
| - main_acts_appropriated_after_from_date_count=Count( |
314 |
| - "activities", |
315 |
| - filter=main_activities_appropriated_after_from_date_q, |
316 |
| - ), |
317 |
| - main_acts_appropriated_before_from_date_count=Count( |
318 |
| - "activities", |
319 |
| - filter=main_activities_appropriated_before_from_date_q, |
320 |
| - ), |
321 |
| - dst_report_type=Case( |
322 |
| - When( |
323 |
| - case__in=changed_cases, |
324 |
| - then=Value(report_types["CHANGED"]), |
| 356 | + queryset = ( |
| 357 | + # annotate fields we need to determine dst_report_type. |
| 358 | + queryset.annotate( |
| 359 | + main_acts_count=Subquery( |
| 360 | + main_acts, output_field=IntegerField() |
325 | 361 | ),
|
326 |
| - When( |
327 |
| - main_acts_count=F( |
328 |
| - "main_acts_appropriated_before_from_date_count" |
329 |
| - ), |
330 |
| - then=Value(""), |
| 362 | + main_acts_after_from_date_count=Subquery( |
| 363 | + main_acts_after_from_date, output_field=IntegerField() |
331 | 364 | ),
|
332 |
| - When( |
333 |
| - main_acts_count=F( |
334 |
| - "main_acts_appropriated_after_from_date_count" |
335 |
| - ), |
336 |
| - then=Value(report_types["NEW"]), |
| 365 | + main_acts_before_from_date_count=Subquery( |
| 366 | + main_acts_before_from_date, output_field=IntegerField() |
337 | 367 | ),
|
338 |
| - When( |
339 |
| - main_acts_count__gt=F( |
340 |
| - "main_acts_appropriated_after_from_date_count" |
| 368 | + dst_report_type=Case( |
| 369 | + # If an appropriation has a case with a changed |
| 370 | + # acting municipality we mark it "changed". |
| 371 | + When( |
| 372 | + case__in=changed_cases, |
| 373 | + then=Value(report_types["CHANGED"]), |
| 374 | + ), |
| 375 | + # If an appropriation has main activities appropriated |
| 376 | + # after the from date, and the total number of |
| 377 | + # main activities is higher (and thus has |
| 378 | + # appropriated main activities before the from_date) |
| 379 | + # we consider it "changed". |
| 380 | + When( |
| 381 | + Q(main_acts_after_from_date_count__gt=0) |
| 382 | + & Q( |
| 383 | + main_acts_count__gt=F( |
| 384 | + "main_acts_after_from_date_count" |
| 385 | + ) |
| 386 | + ), |
| 387 | + then=Value(report_types["CHANGED"]), |
341 | 388 | ),
|
342 |
| - then=Value(report_types["CHANGED"]), |
| 389 | + # If appropriation has a total number of main |
| 390 | + # activities equal to the number of main activities |
| 391 | + # appropriated after the from_date we consider it "new" |
| 392 | + When( |
| 393 | + Q( |
| 394 | + main_acts_count=F( |
| 395 | + "main_acts_after_from_date_count" |
| 396 | + ) |
| 397 | + ), |
| 398 | + then=Value(report_types["NEW"]), |
| 399 | + ), |
| 400 | + # Lastly if an appropriation only has main activities |
| 401 | + # appropriated in the past we do not include them. |
| 402 | + When( |
| 403 | + main_acts_count=F( |
| 404 | + "main_acts_before_from_date_count" |
| 405 | + ), |
| 406 | + then=Value(""), |
| 407 | + ), |
| 408 | + default=Value(""), |
| 409 | + output_field=CharField(), |
343 | 410 | ),
|
344 |
| - default=Value(""), |
345 |
| - output_field=CharField(), |
346 |
| - ), |
347 |
| - ).exclude(dst_report_type="") |
348 |
| - else: |
349 |
| - queryset = queryset.annotate( |
350 |
| - dst_report_type=Value( |
351 |
| - report_types["NEW"], output_field=CharField() |
352 | 411 | )
|
| 412 | + .exclude(dst_report_type="") |
| 413 | + .distinct() |
353 | 414 | )
|
354 | 415 |
|
355 |
| - return queryset.distinct() |
| 416 | + return queryset |
356 | 417 |
|
357 | 418 | def get_duplicate_sbsys_id_appropriations_for_dst(self):
|
358 | 419 | """
|
@@ -392,7 +453,7 @@ def get_duplicate_sbsys_id_appropriations_for_dst(self):
|
392 | 453 | )
|
393 | 454 | .exclude(sbsys_common=None)
|
394 | 455 | .values("sbsys_common")
|
395 |
| - .annotate(ids=ArrayAgg("id")) |
| 456 | + .annotate(ids=ArrayAgg("id", distinct=True)) |
396 | 457 | .annotate(id_count=Func("ids", Value(1), function="array_length"))
|
397 | 458 | .filter(id_count__gt=1)
|
398 | 459 | )
|
|
0 commit comments