Compare commits
No commits in common. "9fdbef91ad6571ddb253a737a63aa57be5db9bf2" and "c92c55da7686d16a194bc0943ad3f5b370805e7c" have entirely different histories.
9fdbef91ad
...
c92c55da76
5 changed files with 20 additions and 99 deletions
|
|
@ -56,7 +56,6 @@ APP_CODES = {
|
||||||
"librewolf": 5
|
"librewolf": 5
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def call_legacy_insert_function(key: str, new_data: dict):
|
def call_legacy_insert_function(key: str, new_data: dict):
|
||||||
func = LEGACY_SQL_FUNCT_DICT.get(key)
|
func = LEGACY_SQL_FUNCT_DICT.get(key)
|
||||||
if func is None:
|
if func is None:
|
||||||
|
|
@ -74,26 +73,14 @@ def get_orm_model(key) -> bool:
|
||||||
return ORM_TABLES[key]
|
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 ..")
|
client_observer.notify('synchronizing', "Fetching list of new data ..")
|
||||||
logger.info("Starting sync")
|
|
||||||
|
|
||||||
####################################
|
####################################
|
||||||
# METADATA. Should we even sync?
|
# METADATA. Should we even sync?
|
||||||
####################################
|
####################################
|
||||||
metadata_result = coordinate_cache_sync(
|
metadata_result = coordinate_cache_sync(client_observer, connection_observer)
|
||||||
client_observer=client_observer,
|
|
||||||
connection_observer=connection_observer,
|
|
||||||
force_all=force_all
|
|
||||||
)
|
|
||||||
|
|
||||||
# Outright Error:
|
# Outright Error:
|
||||||
if not metadata_result["success"]:
|
if not metadata_result["success"]:
|
||||||
|
|
@ -195,15 +182,13 @@ def new_sync(client_observer: ClientObserver, connection_observer: ConnectionObs
|
||||||
|
|
||||||
if total_skipped == 0:
|
if total_skipped == 0:
|
||||||
client_observer.notify('synchronized', "Fetch & Save Complete!")
|
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)
|
save_successful = save_metadata(filtered_metadata)
|
||||||
if save_successful:
|
if save_successful:
|
||||||
return _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)
|
||||||
else:
|
else:
|
||||||
error_msg = "Finshed sync, but had issues with the saving of metadata for next time."
|
error_msg = "Finshed sync, but had issues with the saving of metadata for next time."
|
||||||
logger.error(error_msg)
|
logger.error(error_msg)
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
from core.observers.ObserverStash import ObserverStash
|
|
||||||
from core.controllers.SyncController import new_sync
|
|
||||||
from core.controllers.ProfileController import ProfileController
|
from core.controllers.ProfileController import ProfileController
|
||||||
from core.services.payment_phase.payment_state import PaymentData
|
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_models.Plan import Plan
|
||||||
from core.models.orm_calls.model_neutral_tools import get_all_for_model
|
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
|
from core.models.orm_calls.location_calls import get_profile_location_data, get_profile_location_data_by_id
|
||||||
|
|
@ -85,30 +83,6 @@ def get_plans_for_profile(profile_id: int) -> Result:
|
||||||
billing_data.protocol_choice = chosen_protocol
|
billing_data.protocol_choice = chosen_protocol
|
||||||
|
|
||||||
# Return plans for that 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)
|
return get_all_plans_by_protocol(chosen_protocol)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,16 +20,7 @@ from core.models.orm_models.Dependency import Dependency
|
||||||
|
|
||||||
from core.models.orm_models.EncryptedProxy import EncryptedProxy
|
from core.models.orm_models.EncryptedProxy import EncryptedProxy
|
||||||
|
|
||||||
from sqlalchemy import exists
|
# This is the public interface,
|
||||||
|
|
||||||
|
|
||||||
@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:
|
def insert_into_model(model_class: Type, all_data: dict | list, override=False) -> DatabaseOperation:
|
||||||
"""
|
"""
|
||||||
Purpose:
|
Purpose:
|
||||||
|
|
@ -47,20 +38,7 @@ 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]
|
data_list = all_data if isinstance(all_data, list) else [all_data]
|
||||||
|
|
||||||
# Call the wrapped function with normalized data
|
# Call the wrapped function with normalized data
|
||||||
inserted = _wrapped_insert(model_class=model_class, data_list=data_list, override=override)
|
return _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
|
@safe_db_operation
|
||||||
|
|
@ -84,6 +62,7 @@ def _wrapped_insert(model_class: Type, data_list: dict | list, session: Session,
|
||||||
|
|
||||||
# Get valid field names upfront
|
# Get valid field names upfront
|
||||||
valid_fields = {col.name for col in model_class.__table__.columns}
|
valid_fields = {col.name for col in model_class.__table__.columns}
|
||||||
|
logger.info(f"valid_fields are {valid_fields}")
|
||||||
|
|
||||||
# Filter data proactively
|
# Filter data proactively
|
||||||
filtered_data_list = []
|
filtered_data_list = []
|
||||||
|
|
@ -103,10 +82,7 @@ def _wrapped_insert(model_class: Type, data_list: dict | list, session: Session,
|
||||||
|
|
||||||
try:
|
try:
|
||||||
for filtered_json in filtered_data_list:
|
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)
|
instance = model_class(**filtered_json)
|
||||||
session.add(instance)
|
session.add(instance)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
@ -127,8 +103,6 @@ def _wrapped_insert(model_class: Type, data_list: dict | list, session: Session,
|
||||||
message=f"Wrapped Insert Failed: {str(e)}"
|
message=f"Wrapped Insert Failed: {str(e)}"
|
||||||
))
|
))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# =================== Isolation Test ======================
|
# =================== Isolation Test ======================
|
||||||
"""
|
"""
|
||||||
Spoof Failure of an operational error to trigger/test a migration
|
Spoof Failure of an operational error to trigger/test a migration
|
||||||
|
|
|
||||||
|
|
@ -91,3 +91,10 @@ def change_location_server(
|
||||||
ticket_observer=observer_stash.ticket_observer,
|
ticket_observer=observer_stash.ticket_observer,
|
||||||
connection_observer=observer_stash.connection_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)
|
||||||
|
|
|
||||||
|
|
@ -144,25 +144,10 @@ def _log_what_changed(new_model_types: list, changed_tables: list):
|
||||||
logger.info("No models changed between versions.")
|
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:
|
# Highest Level in this Flow:
|
||||||
def coordinate_cache_sync(
|
def coordinate_cache_sync(
|
||||||
client_observer: Optional[ClientObserver] = None,
|
client_observer: Optional[ClientObserver] = None,
|
||||||
connection_observer: Optional[ConnectionObserver] = None,
|
connection_observer: Optional[ConnectionObserver] = None
|
||||||
force_all: bool = False
|
|
||||||
) -> dict:
|
) -> dict:
|
||||||
"""
|
"""
|
||||||
Fetch version metadata from API and return changed/new model keys.
|
Fetch version metadata from API and return changed/new model keys.
|
||||||
|
|
@ -233,10 +218,6 @@ def coordinate_cache_sync(
|
||||||
new_data = original_data.get('data', original_data)
|
new_data = original_data.get('data', original_data)
|
||||||
print(f"new_data is {new_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:
|
if "version" not in new_data:
|
||||||
error_msg = f"API returned invalid metadata: missing 'version' key. The data is {new_data}"
|
error_msg = f"API returned invalid metadata: missing 'version' key. The data is {new_data}"
|
||||||
logger.error(error_msg)
|
logger.error(error_msg)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue