Compare commits

...

2 commits

5 changed files with 99 additions and 20 deletions

View file

@ -56,6 +56,7 @@ APP_CODES = {
"librewolf": 5
}
def call_legacy_insert_function(key: str, new_data: dict):
func = LEGACY_SQL_FUNCT_DICT.get(key)
if func is None:
@ -73,14 +74,26 @@ def get_orm_model(key) -> bool:
return ORM_TABLES[key]
def _sync_success():
notification = "Finshed sync."
Notifications.emit(EventType.STATUS_UPDATE, notification)
Notifications.emit(EventType.SYNC_COMPLETE, notification)
global_state_marks_sync_completed()
return Result(valid=True, message=notification)
def new_sync(client_observer: ClientObserver, connection_observer: ConnectionObserver) -> Result:
def new_sync(client_observer: ClientObserver, connection_observer: ConnectionObserver, force_all: bool = False) -> Result:
client_observer.notify('synchronizing', "Fetching list of new data ..")
logger.info("Starting sync")
####################################
# METADATA. Should we even sync?
####################################
metadata_result = coordinate_cache_sync(client_observer, connection_observer)
metadata_result = coordinate_cache_sync(
client_observer=client_observer,
connection_observer=connection_observer,
force_all=force_all
)
# Outright Error:
if not metadata_result["success"]:
@ -182,13 +195,15 @@ def new_sync(client_observer: ClientObserver, connection_observer: ConnectionObs
if total_skipped == 0:
client_observer.notify('synchronized', "Fetch & Save Complete!")
# forced_sync being true, forces this to none:
if filtered_metadata is None:
return _sync_success()
# otherwise evaluate saving of metadata:
save_successful = save_metadata(filtered_metadata)
if save_successful:
notification = "Finshed sync."
Notifications.emit(EventType.STATUS_UPDATE, notification)
Notifications.emit(EventType.SYNC_COMPLETE, notification)
global_state_marks_sync_completed()
return Result(valid=True, message=notification)
return _sync_success()
else:
error_msg = "Finshed sync, but had issues with the saving of metadata for next time."
logger.error(error_msg)

View file

@ -1,6 +1,8 @@
from core.observers.ObserverStash import ObserverStash
from core.controllers.SyncController import new_sync
from core.controllers.ProfileController import ProfileController
from core.services.payment_phase.payment_state import PaymentData
from core.models.BaseProfile import BaseProfile as Profile
# from core.models.BaseProfile import BaseProfile as Profile
from core.models.orm_models.Plan import Plan
from core.models.orm_calls.model_neutral_tools import get_all_for_model
from core.models.orm_calls.location_calls import get_profile_location_data, get_profile_location_data_by_id
@ -83,6 +85,30 @@ def get_plans_for_profile(profile_id: int) -> Result:
billing_data.protocol_choice = chosen_protocol
# Return plans for that protocol:
all_plans_result = get_all_plans_by_protocol(chosen_protocol)
if all_plans_result.valid:
return all_plans_result
if all_plans_result.error_type == ResultError.MISSING_DATA:
return force_sync_then_try_again(chosen_protocol)
else:
return all_plans_result
def force_sync_then_try_again(chosen_protocol: ProtocolChoice) -> Result:
doc_string = "Sync with ALL keys forced to download. Then try fetching the plan data again"
logger.info(doc_string)
observer_stash = ObserverStash()
result = new_sync(
client_observer=observer_stash.client_observer,
connection_observer=observer_stash.connection_observer,
force_all=True)
if not result.valid:
return result.wrap("couldn't force sync all keys, to get the SQL data for plans.")
return get_all_plans_by_protocol(chosen_protocol)

View file

@ -20,7 +20,16 @@ from core.models.orm_models.Dependency import Dependency
from core.models.orm_models.EncryptedProxy import EncryptedProxy
# This is the public interface,
from sqlalchemy import exists
@safe_db_operation
def verify_table_not_empty(model_class: Type, session: Session) -> DatabaseOperation:
"""Checks if the table contains at least one record."""
return session.query(exists().select_from(model_class)).scalar()
# This is the main public interface,
def insert_into_model(model_class: Type, all_data: dict | list, override=False) -> DatabaseOperation:
"""
Purpose:
@ -38,7 +47,20 @@ def insert_into_model(model_class: Type, all_data: dict | list, override=False)
data_list = all_data if isinstance(all_data, list) else [all_data]
# Call the wrapped function with normalized data
return _wrapped_insert(model_class=model_class, data_list=data_list, override=override)
inserted = _wrapped_insert(model_class=model_class, data_list=data_list, override=override)
if not inserted.valid:
return inserted
not_blank = verify_table_not_empty(model_class)
if not not_blank:
error_type = "Although it initally inserted, it returned blank."
logger.error(error_type)
return DatabaseOperation(valid=False, error_type=DBErrorType.UNKNOWN, message=error_type)
logger.info("Confirmed it's not blank")
return inserted
@safe_db_operation
@ -62,7 +84,6 @@ def _wrapped_insert(model_class: Type, data_list: dict | list, session: Session,
# Get valid field names upfront
valid_fields = {col.name for col in model_class.__table__.columns}
logger.info(f"valid_fields are {valid_fields}")
# Filter data proactively
filtered_data_list = []
@ -82,7 +103,10 @@ def _wrapped_insert(model_class: Type, data_list: dict | list, session: Session,
try:
for filtered_json in filtered_data_list:
logger.info(f"inserting {filtered_json}")
if filtered_json is None:
logger.error(f"Critical error of filtered_json being None for {model_class}")
instance = model_class(**filtered_json)
session.add(instance)
session.commit()
@ -103,6 +127,8 @@ def _wrapped_insert(model_class: Type, data_list: dict | list, session: Session,
message=f"Wrapped Insert Failed: {str(e)}"
))
# =================== Isolation Test ======================
"""
Spoof Failure of an operational error to trigger/test a migration

View file

@ -91,10 +91,3 @@ def change_location_server(
ticket_observer=observer_stash.ticket_observer,
connection_observer=observer_stash.connection_observer
)
# if not server_side_changed.valid:
# return server_side_changed.wrap("server-side failed")
# return Result(valid=True)

View file

@ -144,10 +144,25 @@ def _log_what_changed(new_model_types: list, changed_tables: list):
logger.info("No models changed between versions.")
def _force_all_keys(new_data: dict) -> dict:
error_msg = "Forcing a sync of all keys"
logger.info(error_msg)
changed_tables = [key for key in new_data]
return {
"success": True,
"changed_tables": changed_tables,
"new_model_types": [],
"error": error_msg,
"filtered_metadata": None
}
# Highest Level in this Flow:
def coordinate_cache_sync(
client_observer: Optional[ClientObserver] = None,
connection_observer: Optional[ConnectionObserver] = None
connection_observer: Optional[ConnectionObserver] = None,
force_all: bool = False
) -> dict:
"""
Fetch version metadata from API and return changed/new model keys.
@ -218,6 +233,10 @@ def coordinate_cache_sync(
new_data = original_data.get('data', original_data)
print(f"new_data is {new_data}")
# proceed with force all:
if force_all:
return _force_all_keys(new_data)
if "version" not in new_data:
error_msg = f"API returned invalid metadata: missing 'version' key. The data is {new_data}"
logger.error(error_msg)