|
28 | 28 | from io import BytesIO, StringIO |
29 | 29 | from shapely.geometry import shape, mapping |
30 | 30 | from shapely.ops import unary_union |
| 31 | +from pathlib import Path as osPath |
31 | 32 |
|
32 | 33 | import distinctipy |
33 | 34 | import flask_monitoringdashboard as dashboard |
|
221 | 222 | delete_trip, |
222 | 223 | update_trip_type, |
223 | 224 | attach_ticket_to_trips, |
224 | | - delete_ticket_from_db |
| 225 | + delete_ticket_from_db, |
225 | 226 | fetch_trips_paths |
226 | 227 | ) |
227 | 228 | from src.paths import Path |
@@ -591,9 +592,14 @@ def sort_key(item): |
591 | 592 | sorted_country_codes = dict(sorted(country_codes.items(), key=sort_key)) |
592 | 593 | return sorted_country_codes |
593 | 594 |
|
| 595 | +def db_lock_state(): |
| 596 | + """Check if database sync is currently in progress""" |
| 597 | + lock_file = osPath("db_sync.lock") |
| 598 | + return lock_file.exists() |
594 | 599 |
|
595 | | -app.jinja_env.globals.update(get_country_codes_from_files=get_country_codes_from_files) |
596 | 600 |
|
| 601 | +app.jinja_env.globals.update(get_country_codes_from_files=get_country_codes_from_files) |
| 602 | +app.jinja_env.globals.update(db_lock_state=db_lock_state) |
597 | 603 |
|
598 | 604 | @app.route("/api/localtime", methods=["GET"]) |
599 | 605 | def get_local_time(): |
@@ -2514,22 +2520,25 @@ def update_user_count(): |
2514 | 2520 | User.last_login >= twenty_four_hours_ago |
2515 | 2521 | ).count() |
2516 | 2522 | today = datetime.utcnow().date() |
2517 | | - with managed_cursor(mainConn) as cursor: |
2518 | | - cursor.execute("SELECT number FROM daily_active_users WHERE date = ?", (today,)) |
2519 | | - result = cursor.fetchone() |
2520 | | - if result: |
2521 | | - current_count = result["number"] |
2522 | | - if active_users_count > current_count: |
| 2523 | + try: |
| 2524 | + with managed_cursor(mainConn) as cursor: |
| 2525 | + cursor.execute("SELECT number FROM daily_active_users WHERE date = ?", (today,)) |
| 2526 | + result = cursor.fetchone() |
| 2527 | + if result: |
| 2528 | + current_count = result["number"] |
| 2529 | + if active_users_count > current_count: |
| 2530 | + cursor.execute( |
| 2531 | + "UPDATE daily_active_users SET number = ? WHERE date = ?", |
| 2532 | + (active_users_count, today), |
| 2533 | + ) |
| 2534 | + else: |
2523 | 2535 | cursor.execute( |
2524 | | - "UPDATE daily_active_users SET number = ? WHERE date = ?", |
2525 | | - (active_users_count, today), |
| 2536 | + "INSERT INTO daily_active_users (date, number) VALUES (?, ?)", |
| 2537 | + (today, active_users_count), |
2526 | 2538 | ) |
2527 | | - else: |
2528 | | - cursor.execute( |
2529 | | - "INSERT INTO daily_active_users (date, number) VALUES (?, ?)", |
2530 | | - (today, active_users_count), |
2531 | | - ) |
2532 | | - mainConn.commit() |
| 2539 | + mainConn.commit() |
| 2540 | + except: |
| 2541 | + pass |
2533 | 2542 |
|
2534 | 2543 |
|
2535 | 2544 | @app.route("/", methods=["GET", "POST"]) |
|
0 commit comments