|
| 1 | +# Copyright 2023 UW-IT, University of Washington |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +""" |
| 5 | +Clean up the entries no longer useful |
| 6 | +""" |
| 7 | + |
| 8 | +import logging |
| 9 | +import time |
| 10 | +from datetime import timedelta |
| 11 | +from django.db import connection |
| 12 | +from django.core.management.base import BaseCommand, CommandError |
| 13 | +from uw_sws import sws_now, SWS_TIMEZONE |
| 14 | +from myuw.models import ( |
| 15 | + VisitedLinkNew, SeenRegistration, UserNotices, UserCourseDisplay) |
| 16 | +from myuw.logger.timer import Timer |
| 17 | + |
| 18 | +logger = logging.getLogger(__name__) |
| 19 | +batch_size = 1000 |
| 20 | + |
| 21 | + |
| 22 | +class Command(BaseCommand): |
| 23 | + |
| 24 | + def add_arguments(self, parser): |
| 25 | + parser.add_argument('name', choices=[ |
| 26 | + 'course', 'notice', 'seenreg', 'linkvisit'], |
| 27 | + help="The table to check ") |
| 28 | + |
| 29 | + def handle(self, *args, **options): |
| 30 | + self.action = options['name'] |
| 31 | + |
| 32 | + if self.action == 'course': |
| 33 | + self.course_display() |
| 34 | + if self.action == 'notice': |
| 35 | + self.notice_read() |
| 36 | + if self.action == 'seenreg': |
| 37 | + self.registration_seen() |
| 38 | + if self.action == 'linkvisit': |
| 39 | + self.link_visited() |
| 40 | + |
| 41 | + def get_cut_off_date(self, days_delta=364): |
| 42 | + # default is 52 weeks (364 days) |
| 43 | + now = SWS_TIMEZONE.localize(sws_now()) |
| 44 | + return now - timedelta(days=days_delta) |
| 45 | + |
| 46 | + def deletion(self, ids_to_delete, queryf): |
| 47 | + try: |
| 48 | + while ids_to_delete and len(ids_to_delete) > 0: |
| 49 | + batch_ids = ids_to_delete[:batch_size] |
| 50 | + with connection.cursor() as cursor: |
| 51 | + placeholders = ','.join( |
| 52 | + str(id) for id in batch_ids) |
| 53 | + cursor.execute( |
| 54 | + queryf.format(placeholders)) |
| 55 | + time.sleep(2) |
| 56 | + ids_to_delete = ids_to_delete[batch_size:] |
| 57 | + except Exception as ex: |
| 58 | + logger.error("{} {}\n".format(queryf, ex)) |
| 59 | + raise CommandError(ex) |
| 60 | + |
| 61 | + def course_display(self): |
| 62 | + # clean up after one year |
| 63 | + timer = Timer() |
| 64 | + queryf = "DELETE FROM user_course_display_pref WHERE id IN ({})" |
| 65 | + for y in range(2000, 2023): |
| 66 | + for q in ["winter", "spring", "summer", "autumn"]: |
| 67 | + if y == 2022 and q == "autumn": |
| 68 | + break |
| 69 | + for c in range(1, 9): |
| 70 | + qset = UserCourseDisplay.objects.filter( |
| 71 | + year=y, quarter=q, color_id=c) |
| 72 | + if qset.exists(): |
| 73 | + ids_to_delete = qset.values_list('id', flat=True) |
| 74 | + self.deletion(ids_to_delete, queryf) |
| 75 | + logger.info( |
| 76 | + "Delete UserCourseDisplay {} {}, Time: {} sec\n".format( |
| 77 | + y, q, timer.get_elapsed())) |
| 78 | + |
| 79 | + def notice_read(self): |
| 80 | + # clean up after 180 days |
| 81 | + timer = Timer() |
| 82 | + queryf = "DELETE FROM myuw_mobile_usernotices WHERE id IN ({})" |
| 83 | + cut_off_dt = self.get_cut_off_date(180) |
| 84 | + qset = UserNotices.objects.filter(first_viewed__lt=cut_off_dt) |
| 85 | + if qset.exists(): |
| 86 | + ids_to_delete = qset.values_list('id', flat=True) |
| 87 | + self.deletion(ids_to_delete, queryf) |
| 88 | + logger.info( |
| 89 | + "Delete UserNotices viewed before {} Time: {} sec\n".format( |
| 90 | + cut_off_dt, timer.get_elapsed())) |
| 91 | + |
| 92 | + def registration_seen(self): |
| 93 | + # clean up after the quarter ends |
| 94 | + timer = Timer() |
| 95 | + queryf = "DELETE FROM myuw_mobile_seenregistration WHERE id IN ({})" |
| 96 | + for y in range(2013, 2024): |
| 97 | + for q in ["winter", "spring", "summer", "autumn"]: |
| 98 | + if y == 2023 and q == "autumn": |
| 99 | + break |
| 100 | + qset = SeenRegistration.objects.filter(year=y, quarter=q) |
| 101 | + if qset.exists(): |
| 102 | + ids_to_delete = qset.values_list('id', flat=True) |
| 103 | + self.deletion(ids_to_delete, queryf) |
| 104 | + logger.info( |
| 105 | + "Delete SeenRegistration {} {} Time: {}\n".format( |
| 106 | + y, q, timer.get_elapsed())) |
| 107 | + |
| 108 | + def link_visited(self): |
| 109 | + # clean up after one year |
| 110 | + timer = Timer() |
| 111 | + queryf = "DELETE FROM myuw_visitedlinknew WHERE id IN ({})" |
| 112 | + cut_off_dt = self.get_cut_off_date() |
| 113 | + qset = VisitedLinkNew.objects.filter(visit_date__lt=cut_off_dt) |
| 114 | + if qset.exists(): |
| 115 | + ids_to_delete = qset.values_list('id', flat=True) |
| 116 | + self.deletion(ids_to_delete, queryf) |
| 117 | + logger.info( |
| 118 | + "Delete VisitedLinkNew viewed before {} Time: {}\n".format( |
| 119 | + cut_off_dt, timer.get_elapsed())) |
0 commit comments