Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Periodic task #690

Merged
merged 4 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions app/apps/itinerary/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from datetime import timedelta

from apps.itinerary.models import (
Itinerary,
ItineraryItem,
Expand All @@ -10,7 +12,6 @@
from django.contrib import admin
from django.http import JsonResponse
from django.utils import timezone
from datetime import timedelta


class PostalCodeSettingsInline(admin.StackedInline):
Expand Down Expand Up @@ -41,33 +42,37 @@ class ItineraryItemInline(admin.StackedInline):


class CreatedAtFilter(admin.SimpleListFilter):
title = 'created_at'
parameter_name = 'created_at'
title = "created_at"
parameter_name = "created_at"

def lookups(self, request, model_admin):
return (
('today', 'Today'),
('past_7_days', 'Past 7 days'),
('this_month', 'This month'),
('longer_than_a_month', 'Longer than a month ago'),
("today", "Today"),
("past_7_days", "Past 7 days"),
("this_month", "This month"),
("longer_than_a_month", "Longer than a month ago"),
)

def queryset(self, request, queryset):
if self.value() == 'today':
today_start = timezone.now().replace(hour=0, minute=0, second=0, microsecond=0)
if self.value() == "today":
today_start = timezone.now().replace(
hour=0, minute=0, second=0, microsecond=0
)
today_end = today_start + timedelta(days=1)
return queryset.filter(created_at__range=(today_start, today_end))

elif self.value() == 'past_7_days':
elif self.value() == "past_7_days":
past_7_days_start = timezone.now() - timedelta(days=7)
return queryset.filter(created_at__gte=past_7_days_start)

elif self.value() == 'this_month':
first_day_of_month = timezone.now().replace(day=1, hour=0, minute=0, second=0, microsecond=0)
elif self.value() == "this_month":
first_day_of_month = timezone.now().replace(
day=1, hour=0, minute=0, second=0, microsecond=0
)
next_month = first_day_of_month.replace(month=first_day_of_month.month + 1)
return queryset.filter(created_at__range=(first_day_of_month, next_month))

elif self.value() == 'longer_than_a_month':
elif self.value() == "longer_than_a_month":
one_month_ago = timezone.now() - timedelta(days=30)
return queryset.filter(created_at__lt=one_month_ago)

Expand All @@ -82,9 +87,7 @@ class ItineraryAdmin(admin.ModelAdmin):
"start_case",
)
search_fields = ["team_members__user__email"]
list_filter = (
CreatedAtFilter,
)
list_filter = (CreatedAtFilter,)

inlines = [
ItineraryTeamMemberInline,
Expand Down Expand Up @@ -117,6 +120,7 @@ class ItineraryItemAdmin(admin.ModelAdmin):
"external_state_id",
)
list_display = (
"case", "itinerary",
"case",
"itinerary",
)
search_fields = ["case__case_id"]
30 changes: 30 additions & 0 deletions app/apps/itinerary/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import logging
from datetime import timedelta

from apps.itinerary.models import Itinerary
from celery import shared_task
from django.utils import timezone

logger = logging.getLogger("celery")

DEFAULT_RETRY_DELAY = 10
DAYS_UNTIL_DELETION = 30


@shared_task(bind=True, default_retry_delay=DEFAULT_RETRY_DELAY)
def clean_up_itineraries_task(self):
"""
Clean up itineraries older than 30 days.
"""
logger.info("Started cleanup of itineraries")

try:
one_month_ago = timezone.now() - timedelta(days=DAYS_UNTIL_DELETION)
deleted_count, _ = Itinerary.objects.filter(
created_at__lt=one_month_ago
).delete()
logger.info(f"Ended itineraries cleanup, deleted {deleted_count} itineraries")

except Exception as exception:
logger.error(f"Exception occurred during itineraries cleanup: {exception}")
self.retry(exc=exception)
48 changes: 29 additions & 19 deletions app/apps/visits/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from datetime import timedelta

from apps.visits.models import (
Observation,
Situation,
Expand All @@ -7,50 +9,58 @@
)
from django.contrib import admin
from django.utils import timezone
from datetime import timedelta


class StartTimeFilter(admin.SimpleListFilter):
title = 'start_time'
parameter_name = 'start_time'
title = "start_time"
parameter_name = "start_time"

def lookups(self, request, model_admin):
return (
('today', 'Today'),
('past_7_days', 'Past 7 days'),
('this_month', 'This month'),
('longer_than_a_month', 'Longer than a month ago'),
("today", "Today"),
("past_7_days", "Past 7 days"),
("this_month", "This month"),
("longer_than_a_month", "Longer than a month ago"),
)

def queryset(self, request, queryset):
if self.value() == 'today':
today_start = timezone.now().replace(hour=0, minute=0, second=0, microsecond=0)
if self.value() == "today":
today_start = timezone.now().replace(
hour=0, minute=0, second=0, microsecond=0
)
today_end = today_start + timedelta(days=1)
return queryset.filter(start_time__range=(today_start, today_end))

elif self.value() == 'past_7_days':
elif self.value() == "past_7_days":
past_7_days_start = timezone.now() - timedelta(days=7)
return queryset.filter(start_time__gte=past_7_days_start)

elif self.value() == 'this_month':
first_day_of_month = timezone.now().replace(day=1, hour=0, minute=0, second=0, microsecond=0)
elif self.value() == "this_month":
first_day_of_month = timezone.now().replace(
day=1, hour=0, minute=0, second=0, microsecond=0
)
next_month = first_day_of_month.replace(month=first_day_of_month.month + 1)
return queryset.filter(start_time__range=(first_day_of_month, next_month))

elif self.value() == 'longer_than_a_month':
elif self.value() == "longer_than_a_month":
one_month_ago = timezone.now() - timedelta(days=30)
return queryset.filter(start_time__lt=one_month_ago)

return queryset



@admin.register(Visit)
class VisitAdmin(admin.ModelAdmin):
list_display = ("id", "case_id", "author", "start_time", "completed", "situation", )
search_fields = ("case_id__case_id",)
list_filter = (
StartTimeFilter,
"completed"
list_display = (
"id",
"case_id",
"author",
"start_time",
"completed",
"situation",
)
search_fields = ("case_id__case_id",)
list_filter = (StartTimeFilter, "completed")


@admin.register(VisitMetaData)
Expand Down
20 changes: 20 additions & 0 deletions app/apps/visits/tasks.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import logging
from datetime import timedelta

import requests
from celery import shared_task
from django.conf import settings
from django.utils import timezone
from utils.queries_zaken_api import get_headers

from .models import Visit
Expand All @@ -12,6 +14,7 @@
DEFAULT_RETRY_DELAY = 10
CONNECT_TIMEOUT = 10
READ_TIMEOUT = 60
DAYS_UNTIL_DELETION = 30


def get_serialized_visit(visit_id):
Expand Down Expand Up @@ -57,3 +60,20 @@ def push_visit(self, visit_id, auth_header=None, task_name_ids=[]):
self.retry(exc=exception)

return f"visit_id: {visit_id}"


@shared_task(bind=True, default_retry_delay=DEFAULT_RETRY_DELAY)
def clean_up_visits_task(self):
"""
Clean up visits older than 30 days.
"""
logger.info("Started cleanup of visits")

try:
one_month_ago = timezone.now() - timedelta(days=DAYS_UNTIL_DELETION)
deleted_count, _ = Visit.objects.filter(start_time__lt=one_month_ago).delete()
logger.info(f"Ended visits cleanup, deleted {deleted_count} visits")

except Exception as exception:
logger.error(f"Exception occurred during visits cleanup: {exception}")
self.retry(exc=exception)
Loading