|
1 | 1 | import logging |
2 | 2 | import functions_framework |
3 | | -from datetime import datetime, timezone |
4 | 3 | from shared.helpers.logger import Logger |
| 4 | +from shared.helpers.feed_status import update_feed_statuses_query |
5 | 5 | from typing import TYPE_CHECKING |
6 | | -from sqlalchemy import text |
7 | | -from shared.database_gen.sqlacodegen_models import Gtfsdataset, Feed, t_feedsearch |
8 | | -from shared.database.database import refresh_materialized_view, with_db_session |
| 6 | +from shared.database.database import with_db_session |
9 | 7 |
|
10 | 8 | if TYPE_CHECKING: |
11 | 9 | from sqlalchemy.orm import Session |
12 | 10 |
|
13 | 11 | logging.basicConfig(level=logging.INFO) |
14 | 12 |
|
15 | | - |
16 | | -# query to update the status of the feeds based on the service date range of the latest dataset |
17 | | -def update_feed_statuses_query(session: "Session"): |
18 | | - today_utc = datetime.now(timezone.utc).date() |
19 | | - |
20 | | - latest_dataset_subq = ( |
21 | | - session.query( |
22 | | - Gtfsdataset.feed_id, |
23 | | - Gtfsdataset.service_date_range_start, |
24 | | - Gtfsdataset.service_date_range_end, |
25 | | - ) |
26 | | - .filter( |
27 | | - Gtfsdataset.latest.is_(True), |
28 | | - Gtfsdataset.service_date_range_start.isnot(None), |
29 | | - Gtfsdataset.service_date_range_end.isnot(None), |
30 | | - ) |
31 | | - .subquery() |
32 | | - ) |
33 | | - |
34 | | - status_conditions = [ |
35 | | - ( |
36 | | - latest_dataset_subq.c.service_date_range_end < today_utc, |
37 | | - "inactive", |
38 | | - ), |
39 | | - ( |
40 | | - latest_dataset_subq.c.service_date_range_start > today_utc, |
41 | | - "future", |
42 | | - ), |
43 | | - ( |
44 | | - (latest_dataset_subq.c.service_date_range_start <= today_utc) |
45 | | - & (latest_dataset_subq.c.service_date_range_end >= today_utc), |
46 | | - "active", |
47 | | - ), |
48 | | - ] |
49 | | - |
50 | | - try: |
51 | | - diff_counts: dict[str, int] = {} |
52 | | - |
53 | | - for service_date_conditions, status in status_conditions: |
54 | | - diff_counts[status] = ( |
55 | | - session.query(Feed) |
56 | | - .filter( |
57 | | - Feed.id == latest_dataset_subq.c.feed_id, |
58 | | - Feed.status != text("'deprecated'::status"), |
59 | | - Feed.status != text("'development'::status"), |
60 | | - # We filter out feeds that already have the status so that the |
61 | | - # update count reflects the number of feeds that actually |
62 | | - # changed status. |
63 | | - Feed.status != text("'%s'::status" % status), |
64 | | - service_date_conditions, |
65 | | - ) |
66 | | - .update({Feed.status: status}, synchronize_session=False) |
67 | | - ) |
68 | | - except Exception as e: |
69 | | - logging.error(f"Error updating feed statuses: {e}") |
70 | | - raise Exception(f"Error updating feed statuses: {e}") |
71 | | - |
72 | | - try: |
73 | | - session.commit() |
74 | | - refresh_materialized_view(session, t_feedsearch.name) |
75 | | - logging.info("Feed Database changes committed.") |
76 | | - session.close() |
77 | | - return diff_counts |
78 | | - except Exception as e: |
79 | | - logging.error("Error committing changes:", e) |
80 | | - session.rollback() |
81 | | - session.close() |
82 | | - raise Exception(f"Error creating dataset: {e}") |
83 | | - |
84 | | - |
85 | 13 | @with_db_session |
86 | 14 | @functions_framework.http |
87 | 15 | def update_feed_status(_, db_session): |
88 | 16 | """Updates the Feed status based on the latets dataset service date range.""" |
89 | 17 | Logger.init_logger() |
90 | 18 | try: |
91 | 19 | logging.info("Database session started.") |
92 | | - diff_counts = update_feed_statuses_query(db_session) |
| 20 | + diff_counts = update_feed_statuses_query(db_session, []) |
93 | 21 | return diff_counts, 200 |
94 | 22 |
|
95 | 23 | except Exception as error: |
|
0 commit comments