-
Notifications
You must be signed in to change notification settings - Fork 1
Auto-trigger data sync when archive contains newer data than deployed #537
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
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
c4714ce
Initial plan
Copilot 1d7d392
Add data outdated check and workflow trigger
Copilot 161a8a0
Refactor check_data_outdated.sh based on code review
Copilot 3707619
Fix indentation in check_data_outdated.sh
Copilot 473c20c
Parse XML to extract last departure timestamp
Copilot 8b52913
Improve error messages and validation
Copilot 8ef1c17
Remove --ref and --repo from workflow trigger
Copilot 563b996
Remove unnecessary file existence check
Copilot 4a06947
Merge data check and workflow trigger into single step
Copilot ef429af
Add check_data_outdated.sh to MT.gitignore
Copilot 89e7cfe
Merge branch 'master' into copilot/update-data-trigger-workflow
mmathieum 21b6305
wip
mmathieum a05593d
Merge branch 'copilot/update-data-trigger-workflow' of github.com:mtr…
mmathieum 16aac5e
Return exit 0 when data freshness is uncertain
Copilot 572566b
try
mmathieum 149f1c7
new grep
mmathieum bebf662
clean
mmathieum dfcb661
clean
mmathieum bce7663
cleanup
mmathieum a6fa781
cleanup
mmathieum 6f6ae81
Try gemini suggestion
mmathieum 1b2ba8d
cleanup
mmathieum e38e082
fix
mmathieum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| #!/bin/bash | ||
| SCRIPT_DIR="$(dirname "$0")"; | ||
| source ${SCRIPT_DIR}/../commons/commons.sh | ||
|
|
||
| echo ">> Checking if data is outdated..." | ||
|
|
||
| # Check if app-android directory exists | ||
| APP_ANDROID_DIR="${SCRIPT_DIR}/../app-android/src/main"; | ||
| if [[ ! -d "${APP_ANDROID_DIR}" ]]; then | ||
| echo ">> No app-android directory found. Data check not applicable."; | ||
| exit 0; | ||
| fi | ||
|
|
||
| # Get current timestamp in seconds | ||
| NOW_TIMESTAMP_SEC=$(date +%s); | ||
| echo "> Current timestamp: '$NOW_TIMESTAMP_SEC'"; | ||
|
|
||
| # Check current and next data files | ||
| CURRENT_VALUES="${APP_ANDROID_DIR}/res-current/values/current_gtfs_rts_values_gen.xml"; | ||
| NEXT_VALUES="${APP_ANDROID_DIR}/res-next/values/next_gtfs_rts_values_gen.xml"; | ||
|
|
||
| # Function to extract last departure timestamp from XML file | ||
| get_last_departure_from_xml() { | ||
| local FILE=$1; | ||
| if [[ ! -f "$FILE" ]]; then | ||
| echo ""; | ||
| return; | ||
| fi | ||
| # Extract the last_departure_in_sec value from XML | ||
| # Pattern matches lines like: | ||
| # <integer name="current_gtfs_rts_last_departure_in_sec">1782094500</integer> | ||
| # <integer name="next_gtfs_rts_last_departure_in_sec">1782094500</integer> <!-- with comments --> | ||
| grep -E "<integer name=\"[^\"]*_gtfs_rts_last_departure_in_sec\">[0-9]+</integer>" "$FILE" 2>/dev/null | sed 's/[^[:digit:]]*\([[:digit:]]\+\).*/\1/' || echo ""; | ||
| } | ||
|
|
||
| # Prefer "next" file if available, otherwise fallback to "current" | ||
| DEPLOYED_LAST_DEPARTURE_SEC=""; | ||
| DATA_FILE_USED=""; | ||
|
|
||
| if [[ -f "$NEXT_VALUES" ]]; then | ||
| DEPLOYED_LAST_DEPARTURE_SEC=$(get_last_departure_from_xml "$NEXT_VALUES"); | ||
| DATA_FILE_USED="next"; | ||
| if [[ -n "$DEPLOYED_LAST_DEPARTURE_SEC" ]]; then | ||
| echo "> Using next data file."; | ||
| else | ||
| echo "> Next data file found but timestamp not found."; | ||
| fi | ||
| elif [[ -f "$CURRENT_VALUES" ]]; then | ||
| DEPLOYED_LAST_DEPARTURE_SEC=$(get_last_departure_from_xml "$CURRENT_VALUES"); | ||
| DATA_FILE_USED="current"; | ||
| if [[ -n "$DEPLOYED_LAST_DEPARTURE_SEC" ]]; then | ||
| echo "> Using current data file."; | ||
| else | ||
| echo "> Current data file found but timestamp not found."; | ||
| fi | ||
| fi | ||
|
|
||
| if [[ -z "$DEPLOYED_LAST_DEPARTURE_SEC" ]]; then | ||
| if [[ ! -f "$NEXT_VALUES" ]] && [[ ! -f "$CURRENT_VALUES" ]]; then | ||
| echo ">> No data files found. Cannot determine if data is outdated."; | ||
| else | ||
| echo ">> Data files found but last departure timestamp not found. Cannot determine if data is outdated."; | ||
| fi | ||
| exit 0; # Exit code 0 - avoid triggering sync when uncertain | ||
| fi | ||
|
|
||
| echo "> Deployed last departure timestamp: '$DEPLOYED_LAST_DEPARTURE_SEC' (from $DATA_FILE_USED)"; | ||
|
|
||
| # Check if deployed data is outdated (last departure is in the past) | ||
| if [[ "$DEPLOYED_LAST_DEPARTURE_SEC" -lt "$NOW_TIMESTAMP_SEC" ]]; then | ||
| DIFF_SEC=$((NOW_TIMESTAMP_SEC - DEPLOYED_LAST_DEPARTURE_SEC)); | ||
| DIFF_DAYS=$((DIFF_SEC / 86400)); | ||
| echo ">> Deployed data has expired! Last departure was $DIFF_DAYS days ago."; | ||
| echo ">> Data is OUTDATED. Sync recommended."; | ||
| exit 1; # Exit code 1 indicates data is outdated | ||
| fi | ||
|
|
||
| # Check archive directory for available data | ||
| ARCHIVE_DIR="${SCRIPT_DIR}/archive"; | ||
| echo "> Archive dir: '$ARCHIVE_DIR'"; | ||
|
|
||
| if [[ ! -d "$ARCHIVE_DIR" ]]; then | ||
| echo ">> No archive directory found. Cannot check for newer data."; | ||
| echo ">> Data is up-to-date (based on deployed data only)."; | ||
| exit 0; # No archive, so we can't determine if newer data available | ||
| fi | ||
|
|
||
| # Find archives | ||
| mapfile -t ARCHIVES < <(find "$ARCHIVE_DIR" -name "*.zip" -type f 2>/dev/null | sort) | ||
| echo "> Archives found: ${#ARCHIVES[@]}"; | ||
|
|
||
| if [[ "${#ARCHIVES[@]}" -eq 0 ]]; then | ||
| echo ">> No archives available. Data cannot be updated."; | ||
| echo ">> Data is up-to-date (based on deployed data only)."; | ||
| exit 0; | ||
| fi | ||
|
|
||
| # Check if any archive has data that extends beyond the deployed last departure | ||
| ARCHIVE_HAS_NEWER_DATA=false; | ||
|
|
||
| # Find archives and check them | ||
| for ARCHIVE in "${ARCHIVES[@]}" ; do | ||
| ARCHIVE_BASENAME=$(basename "$ARCHIVE"); | ||
| ARCHIVE_BASENAME_NO_EXT="${ARCHIVE_BASENAME%.*}"; | ||
|
|
||
| # Validate archive date format | ||
| if ! [[ "$ARCHIVE_BASENAME_NO_EXT" =~ ^[0-9]{8}-[0-9]{8}$ ]]; then | ||
| echo "> Archive: $ARCHIVE_BASENAME"; | ||
| echo " - WARNING: Archive filename doesn't match expected format (YYYYMMDD-YYYYMMDD.zip)"; | ||
| continue; | ||
| fi | ||
|
|
||
| ARCHIVE_START_DATE=${ARCHIVE_BASENAME_NO_EXT:0:8} | ||
| ARCHIVE_END_DATE=${ARCHIVE_BASENAME_NO_EXT:9:8} | ||
| echo "> Archive: $ARCHIVE_BASENAME (${ARCHIVE_START_DATE} to ${ARCHIVE_END_DATE})"; | ||
|
|
||
| # Convert archive end date (YYYYMMDD) to timestamp at end of day (23:59:59) | ||
| ARCHIVE_END_TIMESTAMP=$(date -d "${ARCHIVE_END_DATE} 23:59:59" +%s 2>/dev/null); | ||
|
|
||
| if [[ -n "$ARCHIVE_END_TIMESTAMP" ]]; then | ||
| echo " - Archive end timestamp: $ARCHIVE_END_TIMESTAMP"; | ||
| # If archive has data beyond what's currently deployed, we need to sync | ||
| if [[ "$ARCHIVE_END_TIMESTAMP" -gt "$DEPLOYED_LAST_DEPARTURE_SEC" ]]; then | ||
| echo " - Archive has newer data than deployed!"; | ||
| ARCHIVE_HAS_NEWER_DATA=true; | ||
| break; # Found newer data, no need to check other archives | ||
| fi | ||
| fi | ||
| done | ||
|
|
||
| # Determine if data is outdated based on archive availability | ||
| if [[ "$ARCHIVE_HAS_NEWER_DATA" == true ]]; then | ||
| echo ">> Archive contains newer data. Data is OUTDATED. Sync recommended."; | ||
| exit 1; # Exit code 1 indicates data is outdated | ||
| else | ||
| echo ">> Data is up-to-date."; | ||
| exit 0; # Exit code 0 indicates data is current | ||
| fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.