Skip to content

Commit

Permalink
Merge branch 'main' into untrack-html-with-citations
Browse files Browse the repository at this point in the history
  • Loading branch information
quevon24 authored Mar 11, 2025
2 parents 7d9f431 + eccc7b2 commit d00a88a
Show file tree
Hide file tree
Showing 82 changed files with 9,094 additions and 3,833 deletions.
1 change: 1 addition & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ ff2bcc3ea7dd467e2b9fc276a086f7953958511b
ee43b40ac13e0db8275018cb43380aeb24751795
30099991642874440a818585a44193d75db27309
d37a81620f9ed2eaa542dff8389806f2df6aa0ac
9f16741ec680b53463443ad2cd1e25e8ca8494b4

# Eslint
e0d8f6cbca7455e63c9b0234f22d30d5abf56b94
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ jobs:
cl/users/management/commands/cl_retry_failed_email.py \
cl/users/tasks.py \
cl/recap/management/commands/remove_appellate_entries_with_long_numbers.py \
cl/recap/utils.py \
cl/search/management/commands/cl_index_parent_and_child_docs.py \
cl/search/management/commands/sweep_indexer.py
cl/search/management/commands/sweep_indexer.py \
cl/search/management/commands/pacer_bulk_fetch.py
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ repos:
args: [--line-length=79, --transform-concats]

- repo: https://github.com/psf/black
rev: 24.4.2
rev: 25.1.0
hooks:
- id: black

Expand Down
96 changes: 68 additions & 28 deletions cl/alerts/management/commands/cl_send_alerts.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import calendar
import traceback
from datetime import date, timedelta
from urllib.parse import urlencode

from asgiref.sync import async_to_sync
Expand Down Expand Up @@ -37,32 +38,68 @@
MAX_RT_ITEM_QUERY = 1000


def get_cut_off_date(rate, d=datetime.date.today()):
"""Given a rate of dly, wly or mly and a date, returns the date after which
new results should be considered a hit for an cl.
DAYS_WEEK = 7
DAYS_MONTH = 28


def get_cut_off_start_date(rate: str, d: date) -> date:
"""Calculate the cut-off start date based on the given rate and date.
:param rate: The alert rate type.
:param d: The reference date from which the cut-off start date is calculated.
:return: The cut-off start date after which new results should be
considered a hit for an alert.
"""
match rate:
case Alert.REAL_TIME:
# use a couple of days ago to limit results without risk of leaving out
# important items (this will be filtered further later).
return d - timedelta(days=10)
case Alert.DAILY:
# For daily alerts: Set cut_off_date to the previous day since the
# cron job runs early in the morning.
return d - timedelta(days=1)
case Alert.WEEKLY:
return d - timedelta(days=DAYS_WEEK)
case Alert.MONTHLY:
if date.today().day > DAYS_MONTH:
raise InvalidDateError(
"Monthly alerts cannot be run on the 29th, 30th, or 31st."
)
# Get the first day of the previous month, regardless of the
# current date.
early_last_month = d - timedelta(days=DAYS_MONTH)
return date(early_last_month.year, early_last_month.month, 1)

case _:
raise NotImplementedError("Unsupported rate type: %s", rate)


def get_cut_off_end_date(rate: str, cutoff_start_date: date) -> date | None:
"""Given a rate of dly, wly, or mly and the cutoff_start_date, returns
the cut-off end date to set the upper limit for the date range query.
:param rate: The alert rate type.
:param cutoff_start_date: The start date from which the cut-off end
date is calculated.
:return: The cut-off end date that serves as the upper limit for the date
range query, or None if the rate is unsupported
"""
cut_off_date = None
if rate == Alert.REAL_TIME:
# use a couple days ago to limit results without risk of leaving out
# important items (this will be filtered further later).
cut_off_date = d - datetime.timedelta(days=10)
elif rate == Alert.DAILY:
cut_off_date = d
elif rate == Alert.WEEKLY:
cut_off_date = d - datetime.timedelta(days=7)
elif rate == Alert.MONTHLY:
if datetime.date.today().day > 28:
raise InvalidDateError(
"Monthly alerts cannot be run on the 29th, 30th or 31st."
)

# Get the first of the month of the previous month regardless of the
# current date
early_last_month = d - datetime.timedelta(days=28)
cut_off_date = datetime.datetime(
early_last_month.year, early_last_month.month, 1
)
return cut_off_date
match rate:
case Alert.DAILY:
return cutoff_start_date
case Alert.WEEKLY:
return cutoff_start_date + timedelta(days=DAYS_WEEK - 1)
case Alert.MONTHLY:
last_day = calendar.monthrange(
cutoff_start_date.year, cutoff_start_date.month
)[1]
return date(
cutoff_start_date.year, cutoff_start_date.month, last_day
)
case _:
return None


def send_alert(user_profile, hits):
Expand Down Expand Up @@ -166,6 +203,7 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.options = {}
self.valid_ids = []
self.date_today = date.today()

def add_arguments(self, parser):
parser.add_argument(
Expand Down Expand Up @@ -198,10 +236,13 @@ def run_query(self, alert, rate, v1_webhook=False):
except KeyError:
pass
qd["order_by"] = "score desc"
cut_off_date = get_cut_off_date(rate)
cut_off_date = get_cut_off_start_date(rate, self.date_today)
# Default to 'o', if not available, according to the front end.
query_type = qd.get("type", SEARCH_TYPES.OPINION)
qd["filed_after"] = cut_off_date
cut_off_end_date = get_cut_off_end_date(rate, cut_off_date)
if cut_off_end_date:
qd["filed_before"] = cut_off_end_date
if query_type != SEARCH_TYPES.OPINION:
# This command now only serves OPINION search alerts.
return query_type, results, v1_results
Expand All @@ -210,7 +251,6 @@ def run_query(self, alert, rate, v1_webhook=False):
search_form = SearchForm(qd)
if search_form.is_valid():
cd = search_form.cleaned_data

if rate == Alert.REAL_TIME and len(self.valid_ids) == 0:
# Bail out. No results will be found if no valid_ids.
return query_type, results, v1_results
Expand Down Expand Up @@ -299,7 +339,7 @@ def remove_stale_rt_items(self, age=2):
them?
"""
RealTimeQueue.objects.filter(
date_modified__lt=now() - datetime.timedelta(days=age),
date_modified__lt=now() - timedelta(days=age),
).delete()

def get_new_ids(self):
Expand Down
Loading

0 comments on commit d00a88a

Please sign in to comment.