diff --git a/smart_service/apis/update_validation.py b/smart_service/apis/update_validation.py index b4e1fba..5d79fc5 100644 --- a/smart_service/apis/update_validation.py +++ b/smart_service/apis/update_validation.py @@ -2,19 +2,22 @@ import frappe from frappe.model.document import Document import json import os -from frappe.utils import cstr + import time from smart_service.apis.app_user_login import input_validation -current_db_name = frappe.conf.get("db_name") + +from frappe.utils import cstr site_name = cstr(frappe.local.site) base_url = os.path.expanduser( "~") + "/frappe-bench/sites/" + site_name + "/public" - +current_db_name = frappe.conf.get("db_name") # Constants JSON_EXT = ".json" JSON_INT_PATH = "/files/json_files/internal/" +JSON_FULL_INT_PATH = "/files/json_files/full_update/internal/" JSON_GLOABL_PATH = "/files/json_files/global/" JSON_FULL_UPDATE_PATH = "/files/json_files/full_update/" +JSON_FULL_GLOBAL_PATH = "/files/json_files/full_update/" STATUS = "status" ERROR = "error" UPDATE_AVAILABLE = 'IsUpdateAvailable' @@ -22,10 +25,12 @@ UPDATE_FAILED = "Update checking Failed" PARAM_MISSING = "Parameter missing :" VERSION = 'Version' CUR_VERSION = 'CurrentVersion' +DIS = 'Description' FILE_ERROR = 'File failed to load' LANGUAGE = 'Language' FILE_SIZE = 'FileSize' JSON_URL = 'JsonURL' +REMARKS = 'remarks' def custom_rate_limit(limit, seconds): @@ -63,8 +68,8 @@ def get_parent_map(input_list, parameter): return list1, steps_length -@frappe.whitelist() -def check_all_vehicle_updates(vehicle_list=None): +@frappe.whitelist(allow_guest=1) +def check_all_vehicle_updates_old(vehicle_list=None): rate_res = custom_rate_limit(limit=5, seconds=15) if rate_res != 1: return rate_res @@ -84,50 +89,109 @@ def check_all_vehicle_updates(vehicle_list=None): vehicle_data = v_list['VehicleReqList'] publish_type = frappe.db.sql( '''SELECT publish_type FROM `tabApp Device` where name='{}';'''.format(iid), as_list=1) - + # frappe.log_error('request' + str(vehicle_list)) if publish_type[0][0] is not None: if vehicle_data: for v in vehicle_data: v_id = v['Vehicle'] current_version = float(v[CUR_VERSION]) - data1_global = frappe.db.sql('''SELECT name,format(max(version),2) as version,vehicle_id,language FROM `tabPublish` where vehicle='{}' and language='{}' - and publish_status='Published' and publish_type='Global' and vehicle_status='Active' - order by version ASC;'''.format(v_id, lang), as_dict=1) - data1 = frappe.db.sql('''SELECT name,format(max(version),2) as version,vehicle_id,language FROM `tabPublish` where vehicle='{}' and language='{}' - and publish_status='Published' and publish_type='{}' and vehicle_status='Active' - order by version ASC;'''.format(v_id, lang, publish_type[0][0]), as_dict=1) + # frappe.log_error('cur', str(current_version)) + data1_global = frappe.db.sql('''SELECT name,format(version,2) as version,vehicle_id,language,release_description as description + FROM `tabPublish` where vehicle='{}' and language='{}' and publish_status='Published' + and publish_type='Global' + and vehicle_status='Active' and version = (select max(version) as version + from `tabPublish` where vehicle='{}' and publish_type='Global' and language='{}');'''.format(v_id, lang, v_id, lang), as_dict=1) + # frappe.log_error( + # 'first_data1_global' + str(data1_global)) + data1 = frappe.db.sql('''SELECT name,format(max(version),2) as version,vehicle_id,language,release_description as description + FROM `tabPublish` where vehicle='{}' and language='{}' + and publish_status='Published' and vehicle_status='Active' + and vehicle='{}' and language='{}' + ;'''.format(v_id, lang, v_id, lang), as_dict=1) + # frappe.log_error('data1' + str(data1)) if data1[0]['version'] is not None: if current_version == float(data1[0]['version']): + # frappe.log_error('con1') + data1[0][CUR_VERSION] = current_version data1[0][UPDATE_AVAILABLE] = "false" vehicle_req_list.append(data1) + elif len(data1_global) == 0: + # frappe.log_error('con5') + if current_version < float(data1[0]['version']): + data = frappe.db.sql('''SELECT name,format(version,2) as version,vehicle_id,language,release_description as description + FROM `tabPublish` where vehicle='{}' and language='{}' and publish_status='Published' and + publish_type='{}' and vehicle_status='Active' and + version = (select max(version) as version from `tabPublish` where vehicle='{}' and language='{}')'''.format(v_id, lang, publish_type[0][0], v_id, lang), as_dict=1) + data_append = [] + rd = [] + try: + for d in data: + d[UPDATE_AVAILABLE] = 'true' + + d[CUR_VERSION] = current_version + data_append.append(d) + vehicle_req_list.append(data_append) + + except: + pass else: if current_version < float(data1_global[0]['version']): - data = frappe.db.sql('''SELECT name,format(max(version),2) as version,vehicle_id,language FROM `tabPublish` where vehicle='{}' and language='{}' - and publish_status='Published' and publish_type='Global' and vehicle_status='Active' and version > '{}' - order by version ASC;'''.format(v_id, lang, current_version), as_dict=1) + # frappe.log_error('con2') + data = frappe.db.sql('''SELECT name,format(version,2) as version,vehicle_id,language,release_description as description + FROM `tabPublish` where vehicle='{}' and language='{}' + and publish_status='Published' and publish_type='Global' and vehicle_status='Active' + + and version = (select max(version) as version from `tabPublish` + where vehicle='{}' and language='{}')'''.format(v_id, lang, v_id, lang), as_dict=1) + # frappe.log_error( + # 'data_internal' + str(data)) elif current_version == float(data1_global[0]['version']): + # frappe.log_error('con3') + if current_version < float(data1[0]['version']): + data = frappe.db.sql('''SELECT name,format(version,2) as version,vehicle_id,language,release_description as description + FROM `tabPublish` where vehicle='{}' and language='{}' and publish_status='Published' and publish_type='{}' + and vehicle_status='Active' + and version = (select max(version) as version from `tabPublish` where vehicle='{}' and language='{}')'''.format(v_id, lang, publish_type[0][0], v_id, lang), as_dict=1) + # frappe.log_error('data'+str(data)) + + elif current_version > float(data1_global[0]['version']): + # frappe.log_error('con4') if current_version < float(data1[0]['version']): - data = frappe.db.sql('''SELECT name,format(max(version),2) as version,vehicle_id,language FROM `tabPublish` where vehicle='{}' and - language='{}' and publish_status='Published' and publish_type='{}' and vehicle_status='Active' and version > '{}' - order by version ASC;'''.format(v_id, lang, publish_type[0][0], current_version), as_dict=1) + data = frappe.db.sql('''SELECT name,format(version,2) as version,vehicle_id,language,release_description as description + FROM `tabPublish` where vehicle='{}' and language='{}' and publish_status='Published' and + publish_type='{}' and vehicle_status='Active' and + version = (select max(version) as version from `tabPublish` where vehicle='{}' and language='{}')'''.format(v_id, lang, publish_type[0][0], v_id, lang), as_dict=1) + # return data data_append = [] + rd = [] try: for d in data: d[UPDATE_AVAILABLE] = 'true' - d[CUR_VERSION] = float( - current_version) + + d[CUR_VERSION] = current_version data_append.append(d) vehicle_req_list.append(data_append) + + # for note in publish_dec: + # rd.append(note) + # vehicle_req_list.append(rd) + # for n in publish_dec: + # rd.append(n['release_description']) + # vehicle_req_list.append(rd) except: pass response['LanguageID'] = lang if len(vehicle_req_list) != 0: - response['VehicleReqList'] = vehicle_req_list[0] + data = [vu[0] for vu in vehicle_req_list] + # frappe.log_error(str(data)) + # publish_dec_txt = [pd[0] for pd in publish_dec] + response['VehicleReqList'] = data + # response['release_description'] = publish_dec_txt['release_description'] return response else: return {STATUS: 0, ERROR: "No Vehicles in criteria"} @@ -137,25 +201,564 @@ def check_all_vehicle_updates(vehicle_list=None): return {STATUS: 0, ERROR: "Invalid Publish Details"} except Exception as e: - return {STATUS: 0, ERROR: "Failed to fetch updates"} + # frappe.log_error(str(e)) + return {STATUS: 0, ERROR: str(e)} else: return {STATUS: 0, ERROR: "Check argument: vehicle list"} -@frappe.whitelist() +@frappe.whitelist(allow_guest=1) +def check_all_vehicle_updates(vehicle_list=None): + """ Validate Input's """ + val = input_validation(vehicle_list=vehicle_list) + + """ Error response based on missing Input arguements """ + if val != '': + return {"LanguageID": None, + "VehicleReqList": [], STATUS: 0, ERROR: PARAM_MISSING+val} + + if vehicle_list: + """ Validate Input keys """ + try: + vehicle_req_list = [] + v_list = json.loads(vehicle_list) + language = v_list['LanguageID'] + iid = v_list['InstallationId'] + vehicle_data = v_list['VehicleReqList'] + publish_type = frappe.db.sql( + '''SELECT publish_type FROM `tabApp Device` where name='{}';'''.format(iid), as_list=1) + if not publish_type: + """ Error Response for Invalid Publish Type """ + return {"LanguageID": None, + "VehicleReqList": [], STATUS: 0, ERROR: "Publish Type not set "} + + for v in vehicle_data: + vehicle_id = get_vehicle_id(v['Vehicle']) + publish_response = get_all_vehicle_publish_data( + v['Vehicle'], language, float(v['CurrentVersion']), publish_type[0][0], vehicle_id) + + vehicle_req_list.append(publish_response) + + """ Vehicle List Response """ + return {"LanguageID": language, + "VehicleReqList": vehicle_req_list, STATUS: 1, ERROR: 'No Error'} + + except Exception as e: + """ Error Response for Input key's validation """ + return {"LanguageID": None, + "VehicleReqList": [], STATUS: 0, ERROR: str(e)} + + else: + """ Error Response for Empty Vehicle List """ + return {"LanguageID": None, + "VehicleReqList": [], STATUS: 0, ERROR: "Check Arugument: vehicle list"} + + +def get_vehicle_id(vehicle): + vehicle_id = frappe.db.sql( + '''select myid as vehicle_id from `tabVehicle` where vehicle ='{0}'; '''.format(vehicle), as_dict=1) + if vehicle_id: + return vehicle_id[0]['vehicle_id'] + else: + False + + +def get_all_vehicle_publish_data(vehicle, l_id, current_version, publish_type, vehicle_id): + """ Response Structure """ + vehicle_response_data = { + "name": None, + "version": 0, + "vehicle_id": vehicle_id, + "language": l_id, + "description": None, + ERROR: None, + UPDATE_AVAILABLE: False, + "CurrentVersion": current_version + } + latest_global_version = frappe.db.sql('''SELECT name,format(version,2) as version,vehicle_id,language,release_description as description + FROM `tabPublish` where vehicle='{0}' and language='{1}' and publish_status='Published' and vehicle_status='Active' + and version = (select max(version) as version from `tabPublish` + where vehicle='{0}' and publish_type='Global' and language='{1}'); + '''.format(vehicle, l_id), as_dict=1) + + latest_publish_version = frappe.db.sql('''SELECT name,format(version,2) as version,vehicle_id,language,release_description as description + FROM `tabPublish` where vehicle='{0}' and language='{1}' and publish_status='Published' and vehicle_status='Active' + and version = (select max(version) as version from `tabPublish` + where vehicle='{0}' and language='{1}' and version > {2}); + '''.format(vehicle, l_id, current_version), as_dict=1) + + if not latest_publish_version: + """ Latest File already downloaded Global/Internal """ + + vehicle_response_data[UPDATE_AVAILABLE] = False + vehicle_response_data[REMARKS] = "Latest Version" + + else: + if not latest_global_version or (current_version >= float(latest_global_version[0]['version'])): + """ Only First Internal available """ + + if publish_type == 'Internal': + value = round( + float(latest_publish_version[0]['version']) - current_version, 2) + + if value == 0.01: + # frappe.log_error('Inside 1 Internal') + """ 1 Internal File """ + data = frappe.db.sql('''SELECT name, format(version,2) as version,vehicle_id, language, + release_description as description FROM `tabPublish` where vehicle='{0}' and language='{1}' and + publish_status='Published' and vehicle_status='Active' + and version = (select max(version) as version from `tabPublish` + where publish_type='Internal' + and vehicle='{0}' and language='{1}'); + '''.format(vehicle, l_id), as_dict=1) + + if data: + file_name = JSON_INT_PATH+vehicle+"/"+vehicle + \ + "-" + l_id + "_v" + \ + str(data[0]['version']) + JSON_EXT + + if os.path.exists(base_url + file_name): + vehicle_response_data['name'] = data[0]['name'] + + vehicle_response_data['version'] = data[0]['version'] + # vehicle_response_data['vehicle_id'] = data[0]['vehicle_id'] + vehicle_response_data['language'] = data[0]['language'] + vehicle_response_data['description'] = data[0]['description'] + # vehicle_response_data['FileSize'] = get_file_size( + # file_name) + # vehicle_response_data['JsonURL'] = file_name + vehicle_response_data[UPDATE_AVAILABLE] = True + vehicle_response_data[REMARKS] = "Internal available" + + else: + vehicle_response_data[UPDATE_AVAILABLE] = False + vehicle_response_data[REMARKS] = "Internal available" + vehicle_response_data[ERROR] = "Failed to read file" + else: + vehicle_response_data[UPDATE_AVAILABLE] = False + vehicle_response_data[REMARKS] = "Internal available" + vehicle_response_data[ERROR] = "Failed to get Publish version" + + else: + """ Full Internal File """ + data = frappe.db.sql('''SELECT name, format(version,2) as version,vehicle_id, language, + release_description as description FROM `tabPublish` where vehicle='{0}' and language='{1}' and + publish_status='Published' and vehicle_status='Active' + and version = (select max(version) as version from `tabPublish` + where publish_type='Internal' + and vehicle='{0}' and language='{1}' ); + '''.format(vehicle, l_id), as_dict=1) + + if data: + file_name = JSON_FULL_INT_PATH+vehicle+"/"+vehicle + \ + "-" + l_id + "-"+"full_v" + \ + str(data[0]['version'] + ) + JSON_EXT + if os.path.exists(base_url + file_name): + vehicle_response_data['name'] = data[0]['name'] + vehicle_response_data['version'] = data[0]['version'] + # vehicle_response_data['vehicle_id'] = data[0]['vehicle_id'] + vehicle_response_data['language'] = data[0]['language'] + vehicle_response_data['description'] = data[0]['description'] + # vehicle_response_data['FileSize'] = get_file_size( + # file_name) + # vehicle_response_data['JsonURL'] = file_name + + vehicle_response_data[UPDATE_AVAILABLE] = True + vehicle_response_data[REMARKS] = "Internal Full Update available" + + else: + vehicle_response_data[UPDATE_AVAILABLE] = False + vehicle_response_data[REMARKS] = "Internal Full Update available" + vehicle_response_data[ERROR] = "Failed to read file" + else: + vehicle_response_data[UPDATE_AVAILABLE] = False + vehicle_response_data[REMARKS] = "Internal Full Update available" + vehicle_response_data[ERROR] = "Failed to get Publish version" + else: + vehicle_response_data[UPDATE_AVAILABLE] = False + vehicle_response_data[REMARKS] = "No Update available" + + elif current_version < float(latest_global_version[0]['version']): + """ Only Global available """ + + if (float(latest_global_version[0]['version']) - int(current_version)) > 1: + """ Full Global File """ + data = frappe.db.sql('''SELECT name,format(version,2) as version,vehicle_id, language, + release_description as description FROM `tabPublish` where vehicle='{0}' and language='{1}' and + publish_status='Published' and vehicle_status='Active' + and version = (select max(version) as version from `tabPublish` + where vehicle='{0}' and language='{1}' and publish_type ='Global'); + '''.format(vehicle, l_id), as_dict=1) + if data: + file_name = JSON_FULL_GLOBAL_PATH + vehicle + "/" + vehicle + \ + "-" + l_id + "-"+"full_v" + \ + str(data[0]['version'] + ) + JSON_EXT + + if os.path.exists(base_url + file_name): + + vehicle_response_data['name'] = data[0]['name'] + vehicle_response_data['version'] = data[0]['version'] + # vehicle_response_data['vehicle_id'] = data[0]['vehicle_id'] + vehicle_response_data['language'] = data[0]['language'] + vehicle_response_data['description'] = data[0]['description'] + + # vehicle_response_data['FileSize'] = get_file_size( + # file_name) + # vehicle_response_data['JsonURL'] = file_name + + vehicle_response_data[UPDATE_AVAILABLE] = True + vehicle_response_data[REMARKS] = "Global Full Update available" + + else: + vehicle_response_data[UPDATE_AVAILABLE] = False + vehicle_response_data[REMARKS] = "Global Full Update available" + vehicle_response_data[ERROR] = "Failed to read file" + else: + vehicle_response_data[UPDATE_AVAILABLE] = False + vehicle_response_data[REMARKS] = "Global Full Update available" + vehicle_response_data[ERROR] = "Failed to get Publish version" + + # vehicle_response_data[UPDATE_AVAILABLE] = True + # vehicle_response_data[REMARKS] = "Full Update Global available" + else: + + """ Single Global File """ + # vehicle_response_data[UPDATE_AVAILABLE] = True + # vehicle_response_data[REMARKS] = "Global available" + + data = frappe.db.sql('''SELECT name,format(version,2) as version,vehicle_id, language, + release_description as description FROM `tabPublish` where vehicle='{0}' and language='{1}' and + publish_status='Published' and vehicle_status='Active' + and version = (select max(version) as version from `tabPublish` + where vehicle='{0}' and language='{1}' and publish_type ='Global'); + '''.format(vehicle, l_id), as_dict=1) + + if data: + file_name = JSON_GLOABL_PATH+vehicle+"/"+vehicle + \ + "-" + l_id + "_v" + \ + str(data[0]['version']) + JSON_EXT + + if os.path.exists(base_url + file_name): + + vehicle_response_data['name'] = data[0]['name'] + vehicle_response_data['version'] = data[0]['version'] + # vehicle_response_data['vehicle_id'] = data[0]['vehicle_id'] + vehicle_response_data['language'] = data[0]['language'] + vehicle_response_data['description'] = data[0]['description'] + + # vehicle_response_data['FileSize'] = get_file_size( + # file_name) + # vehicle_response_data['JsonURL'] = file_name + + vehicle_response_data[UPDATE_AVAILABLE] = True + vehicle_response_data[REMARKS] = "Global available" + + else: + vehicle_response_data[UPDATE_AVAILABLE] = False + vehicle_response_data[REMARKS] = "Global available" + vehicle_response_data[ERROR] = "Failed to read file" + else: + vehicle_response_data[UPDATE_AVAILABLE] = False + vehicle_response_data[REMARKS] = "Global available" + vehicle_response_data[ERROR] = "Failed to get Publish version" + + else: + """ Unknow condition Dont Know Yet """ + value = {'latest_publish_version': latest_publish_version[0]['version'], + " latest_global_version": latest_global_version[0]['version']} + frappe.log_error('New check Update', str( + f'Unknow Condition{value}')) + vehicle_response_data[UPDATE_AVAILABLE] = True + vehicle_response_data[REMARKS] = "New Condition" + + return vehicle_response_data + + +@frappe.whitelist(allow_guest=1) def check_vehicle_update(vehicle_list=None): + """ Rate Limiting """ rate_res = custom_rate_limit(limit=5, seconds=15) if rate_res != 1: return rate_res - # if vehicle_list == None: - # return {STATUS: 0, ERROR: "Parameter missing: Vehicle List"} + + """ Validate Inputs """ val = input_validation(vehicle_list=vehicle_list) + + """ Validate Input response """ if val != '': - return {STATUS: 0, ERROR: PARAM_MISSING+val} + return {"Vehicle": None, + "VehicleReqList": [], + STATUS: 0, ERROR: PARAM_MISSING+val} + response = {} + """ Iterate Vehicle List """ + if vehicle_list: + try: + """ Read Request Body """ + req_list = json.loads(vehicle_list) + response['Vehicle'] = req_list['Vehicle'] + vehicle = req_list['Vehicle'] + iid = req_list['InstallationId'] + vehicle_data = req_list['VehicleReqList'] + + """ Get Publish Type based on IID """ + publish_type = frappe.db.get_list( + 'App Device', filters={'name': iid}, fields='publish_type') + + if publish_type: + publish_type = publish_type[0]['publish_type'] + vehicle_req_list = [] + for v in vehicle_data: + l_id = v['LanguageID'] + current_version = float(v[CUR_VERSION]) + + """ New function to validate publish version and get file path """ + pulish_response = get_vehicle_publish_data(vehicle=vehicle, + l_id=l_id, current_version=current_version, publish_type=publish_type) + + """ Append to Main Vehicle Request Response List """ + vehicle_req_list.append(pulish_response) + + return { + "Vehicle": vehicle, "VehicleReqList": vehicle_req_list, STATUS: 1, ERROR: "No error"} + except Exception as e: + return {"Vehicle": None, "VehicleReqList": [], STATUS: 0, ERROR: str(e)} + else: + return {"Vehicle": None, "VehicleReqList": [], STATUS: 0, ERROR: "Check Parameter: vehicle list"} + + +def get_file_size(file_name): + return os.path.getsize(base_url + file_name) + + +def get_vehicle_publish_data(vehicle, l_id, current_version, publish_type): + """ Response Structure """ + vehicle_response_data = { + "Name": None, + "Version": 0, + "Vehicle": None, + "Language": l_id, + "Description": None, + "JsonURL": None, + "FileSize": None, + "Error": None, + "IsUpdateAvailable": False, + "CurrentVersion": current_version + } + latest_global_version = frappe.db.sql('''SELECT name,format(version,2) as version,vehicle_id,language,release_description as Description + FROM `tabPublish` where vehicle='{0}' and language='{1}' and publish_status='Published' and vehicle_status='Active' + and version = (select max(version) as version from `tabPublish` + where vehicle='{0}' and publish_type='Global' and language='{1}'); + '''.format(vehicle, l_id), as_dict=1) + + latest_publish_version = frappe.db.sql('''SELECT name,format(version,2) as version,vehicle_id,language,release_description as Description + FROM `tabPublish` where vehicle='{0}' and language='{1}' and publish_status='Published' and vehicle_status='Active' + and version = (select max(version) as version from `tabPublish` + where vehicle='{0}' and language='{1}' and version > {2}); + '''.format(vehicle, l_id, current_version), as_dict=1) + + if not latest_publish_version: + """ Latest File already downloaded Global/Internal """ + + vehicle_response_data['IsUpdateAvailable'] = False + vehicle_response_data[REMARKS] = "Latest Version" + + else: + if not latest_global_version or (current_version >= float(latest_global_version[0]['version'])): + """ Only First Internal available """ + + if publish_type == 'Internal': + value = round( + float(latest_publish_version[0]['version']) - current_version, 2) + + if value == 0.01: + # frappe.log_error('Inside 1 Internal') + """ 1 Internal File """ + data = frappe.db.sql('''SELECT name as Name,format(version,2) as Version,vehicle_id as Vehicle,language as Language, + release_description as Description FROM `tabPublish` where vehicle='{0}' and language='{1}' and + publish_status='Published' and vehicle_status='Active' + and version = (select max(version) as version from `tabPublish` + where publish_type='Internal' + and vehicle='{0}' and language='{1}'); + '''.format(vehicle, l_id), as_dict=1) + + if data: + file_name = JSON_INT_PATH+vehicle+"/"+vehicle + \ + "-" + l_id + "_v" + \ + str(data[0][VERSION]) + JSON_EXT + + if os.path.exists(base_url + file_name): + vehicle_response_data['Name'] = data[0]['Name'] + vehicle_response_data['Vehicle'] = data[0]['Vehicle'] + vehicle_response_data['Version'] = data[0]['Version'] + vehicle_response_data['Language'] = data[0]['Language'] + vehicle_response_data['Description'] = data[0]['Description'] + vehicle_response_data['FileSize'] = get_file_size( + file_name) + vehicle_response_data['JsonURL'] = file_name + vehicle_response_data['IsUpdateAvailable'] = True + vehicle_response_data[REMARKS] = "Internal available" + + else: + vehicle_response_data['IsUpdateAvailable'] = False + vehicle_response_data[REMARKS] = "Internal available" + vehicle_response_data['Error'] = "Failed to read file" + else: + vehicle_response_data['IsUpdateAvailable'] = False + vehicle_response_data[REMARKS] = "Internal available" + vehicle_response_data['Error'] = "Failed to get Publish version" + + else: + """ Full Internal File """ + data = frappe.db.sql('''SELECT name as Name,format(version,2) as Version,vehicle_id as Vehicle,language as Language, + release_description as Description FROM `tabPublish` where vehicle='{0}' and language='{1}' and + publish_status='Published' and vehicle_status='Active' + and version = (select max(version) as version from `tabPublish` + where publish_type='Internal' + and vehicle='{0}' and language='{1}' ); + '''.format(vehicle, l_id), as_dict=1) + + if data: + file_name = JSON_FULL_INT_PATH+vehicle+"/"+vehicle + \ + "-" + l_id + "-"+"full_v" + \ + str(data[0][VERSION] + ) + JSON_EXT + if os.path.exists(base_url + file_name): + vehicle_response_data['Name'] = data[0]['Name'] + vehicle_response_data['Vehicle'] = data[0]['Vehicle'] + vehicle_response_data['Version'] = data[0]['Version'] + vehicle_response_data['Language'] = data[0]['Language'] + vehicle_response_data['Description'] = data[0]['Description'] + vehicle_response_data['FileSize'] = get_file_size( + file_name) + vehicle_response_data['JsonURL'] = file_name + + vehicle_response_data['IsUpdateAvailable'] = True + vehicle_response_data[REMARKS] = "Internal Full Update available" + + else: + vehicle_response_data['IsUpdateAvailable'] = False + vehicle_response_data[REMARKS] = "Internal Full Update available" + vehicle_response_data['Error'] = "Failed to read file" + else: + vehicle_response_data['IsUpdateAvailable'] = False + vehicle_response_data[REMARKS] = "Internal Full Update available" + vehicle_response_data['Error'] = "Failed to get Publish version" + else: + vehicle_response_data['IsUpdateAvailable'] = False + vehicle_response_data[REMARKS] = "No Update available" + + elif current_version < float(latest_global_version[0]['version']): + """ Only Global available """ + + if (float(latest_global_version[0]['version']) - int(current_version)) > 1: + """ Full Global File """ + data = frappe.db.sql('''SELECT name as Name,format(version,2) as Version,vehicle_id as Vehicle,language as Language, + release_description as Description FROM `tabPublish` where vehicle='{0}' and language='{1}' and + publish_status='Published' and vehicle_status='Active' + and version = (select max(version) as version from `tabPublish` + where vehicle='{0}' and language='{1}' and publish_type ='Global'); + '''.format(vehicle, l_id), as_dict=1) + if data: + file_name = JSON_FULL_GLOBAL_PATH + vehicle + "/" + vehicle + \ + "-" + l_id + "-"+"full_v" + \ + str(data[0][VERSION] + ) + JSON_EXT + + if os.path.exists(base_url + file_name): + + vehicle_response_data['Name'] = data[0]['Name'] + vehicle_response_data['Vehicle'] = data[0]['Vehicle'] + vehicle_response_data['Version'] = data[0]['Version'] + vehicle_response_data['Language'] = data[0]['Language'] + vehicle_response_data['Description'] = data[0]['Description'] + + vehicle_response_data['FileSize'] = get_file_size( + file_name) + vehicle_response_data['JsonURL'] = file_name + + vehicle_response_data['IsUpdateAvailable'] = True + vehicle_response_data[REMARKS] = "Global Full Update available" + + else: + vehicle_response_data['IsUpdateAvailable'] = False + vehicle_response_data[REMARKS] = "Global Full Update available" + vehicle_response_data['Error'] = "Failed to read file" + else: + vehicle_response_data['IsUpdateAvailable'] = False + vehicle_response_data[REMARKS] = "Global Full Update available" + vehicle_response_data['Error'] = "Failed to get Publish version" + + # vehicle_response_data['IsUpdateAvailable'] = True + # vehicle_response_data[REMARKS] = "Full Update Global available" + else: + + """ Single Global File """ + # vehicle_response_data['IsUpdateAvailable'] = True + # vehicle_response_data[REMARKS] = "Global available" + + data = frappe.db.sql('''SELECT name as Name,format(version,2) as Version,vehicle_id as Vehicle,language as Language, + release_description as Description FROM `tabPublish` where vehicle='{0}' and language='{1}' and + publish_status='Published' and vehicle_status='Active' + and version = (select max(version) as version from `tabPublish` + where vehicle='{0}' and language='{1}' and publish_type ='Global'); + '''.format(vehicle, l_id), as_dict=1) + + if data: + file_name = JSON_GLOABL_PATH+vehicle+"/"+vehicle + \ + "-" + l_id + "_v" + \ + str(data[0][VERSION]) + JSON_EXT + + if os.path.exists(base_url + file_name): + + vehicle_response_data['Name'] = data[0]['Name'] + vehicle_response_data['Vehicle'] = data[0]['Vehicle'] + vehicle_response_data['Version'] = data[0]['Version'] + vehicle_response_data['Language'] = data[0]['Language'] + vehicle_response_data['Description'] = data[0]['Description'] + + vehicle_response_data['FileSize'] = get_file_size( + file_name) + vehicle_response_data['JsonURL'] = file_name + + vehicle_response_data['IsUpdateAvailable'] = True + vehicle_response_data[REMARKS] = "Global available" + + else: + vehicle_response_data['IsUpdateAvailable'] = False + vehicle_response_data[REMARKS] = "Global available" + vehicle_response_data['Error'] = "Failed to read file" + else: + vehicle_response_data['IsUpdateAvailable'] = False + vehicle_response_data[REMARKS] = "Global available" + vehicle_response_data['Error'] = "Failed to get Publish version" + + else: + """ Unknow condition Dont Know Yet """ + value = {'latest_publish_version': latest_publish_version[0]['version'], + " latest_global_version": latest_global_version[0]['version']} + frappe.log_error('New check Update', str( + f'Unknow Condition{value}')) + vehicle_response_data['IsUpdateAvailable'] = True + vehicle_response_data[REMARKS] = "New Condition" + + return vehicle_response_data + + +@frappe.whitelist(allow_guest=1) +def check_vehicle_update_old(vehicle_list=None): + rate_res = custom_rate_limit(limit=5, seconds=15) + if rate_res != 1: + return rate_res + + val = input_validation(vehicle_list=vehicle_list) + if val != '': + return {STATUS: 0, ERROR: PARAM_MISSING+val} + response = {} if vehicle_list: try: - response = {} req_list = json.loads(vehicle_list) response['Vehicle'] = req_list['Vehicle'] vehicle = req_list['Vehicle'] @@ -165,20 +768,29 @@ def check_vehicle_update(vehicle_list=None): 'App Device', filters={'name': iid}, fields='publish_type') if len(publish_type) > 0: + publish_type = publish_type[0]['publish_type'] vehicle_req_list = [] + # Iterate on language for v in vehicle_data: l_id = v['LanguageID'] current_version = float(v[CUR_VERSION]) - data1_global = frappe.db.sql('''SELECT name,format(max(version),2) as version,vehicle_id,language FROM `tabPublish` where vehicle='{}' and language='{}' - and publish_status='Published' and publish_type='Global' and vehicle_status='Active' - order by version ASC;'''.format(vehicle, l_id), as_dict=1) - - data1 = frappe.db.sql('''SELECT name,format(max(version),2) as version,vehicle_id,language FROM `tabPublish` where vehicle='{}' and language='{}' and - publish_status='Published' and vehicle_status='Active' - order by version ASC;'''.format(vehicle, l_id), as_dict=1) - if data1_global[0]['version']: + data1_global = frappe.db.sql('''SELECT name,format(version,2) as version,vehicle_id,language,release_description as Description FROM `tabPublish` where vehicle='{}' + and language='{}' and publish_status='Published' and vehicle_status='Active' + and version = (select max(version) as version from `tabPublish` where vehicle='{}' + and publish_type='Global' and language='{}'); + '''.format(vehicle, l_id, vehicle, l_id), as_dict=1) + + # frappe.log_error('glbal'+str(data1_global)) + + data1 = frappe.db.sql('''SELECT name,format(version,2) as version,vehicle_id,language,release_description as Description FROM `tabPublish` where vehicle='{}' and language='{}' and + publish_status='Published' and vehicle_status='Active' + and version = (select max(version) as version from `tabPublish` where vehicle='{}' and language='{}'); '''.format(vehicle, l_id, vehicle, l_id), as_dict=1) + + # frappe.log_error('data1' + str(data1)) + # if len(data1_global) > 0 and len(data1) > 0: + if len(data1_global) > 0: if data1[0]['version'] is not None: if current_version == float(data1[0]['version']): data1[0][UPDATE_AVAILABLE] = "false" @@ -190,16 +802,22 @@ def check_vehicle_update(vehicle_list=None): else: if current_version < (float(data1_global[0]['version'])-1.00): - data = frappe.db.sql('''SELECT name as Name,format(max(version),2) as Version,vehicle_id as Vehicle,language as Language FROM `tabPublish` - where vehicle='{}' and language='{}' and publish_status='Published' and publish_type='Global' and - vehicle_status='Active' and Version > '{}' order by version ASC;'''.format(vehicle, l_id, current_version), as_dict=1) + # frappe.log_error(str('global_test4')) + data = frappe.db.sql('''SELECT name as Name,format(version,2) as Version,vehicle_id as Vehicle,language as Language, + release_description as Description FROM `tabPublish` where vehicle='{}' and language='{}' and + publish_status='Published' and vehicle_status='Active' + and version = (select max(version) as version from `tabPublish` + where publish_type='Global' + and vehicle='{}' and language='{}')'''.format(vehicle, l_id, vehicle, l_id), as_dict=1) + # frappe.log_error('file'+str(data)) try: for d in data: try: file_name = JSON_FULL_UPDATE_PATH+vehicle+"/"+vehicle + \ "-" + \ d[LANGUAGE]+"-"+"full_v" + \ - str(d[VERSION])+JSON_EXT + str(d[VERSION]) + \ + JSON_EXT file_size = os.path.getsize( base_url + file_name) d[JSON_URL] = file_name @@ -208,6 +826,8 @@ def check_vehicle_update(vehicle_list=None): d[UPDATE_AVAILABLE] = 'true' except Exception as e: + # frappe.log_error( + # 'error1', str(e)) file_name = None file_size = None d[JSON_URL] = file_name @@ -219,13 +839,17 @@ def check_vehicle_update(vehicle_list=None): current_version) vehicle_req_list.append(d) except Exception as e: + # frappe.log_error('error 2', str(e)) vehicle_req_list.append( {ERROR: UPDATE_FAILED}) elif current_version == (float(data1_global[0]['version'])-1.00): - data = frappe.db.sql('''SELECT name as Name,format(max(version),2) as Version,vehicle_id as Vehicle,language as Language FROM `tabPublish` - where vehicle='{}' and language='{}' and publish_status='Published' and publish_type='Global' and vehicle_status='Active' - and Version > '{}' order by version ASC;'''.format(vehicle, l_id, current_version), as_dict=1) + # frappe.log_error(str('global_test3')) + data = frappe.db.sql('''SELECT name as Name,format(version,2) as Version,vehicle_id as Vehicle,language as Language, + release_description as Description FROM `tabPublish` where vehicle='{}' and language='{}' and + publish_status='Published' and vehicle_status='Active' + and version = (select max(version) as version from `tabPublish` where + publish_type='Global' and vehicle='{}' and language='{}')'''.format(vehicle, l_id, vehicle, l_id), as_dict=1) try: for d in data: file_name = None @@ -233,7 +857,8 @@ def check_vehicle_update(vehicle_list=None): file_name = JSON_GLOABL_PATH+vehicle+"/" + \ vehicle+"-" + \ d[LANGUAGE]+"_v" + \ - str(d[VERSION])+JSON_EXT + str(d[VERSION]) + \ + JSON_EXT file_size = os.path.getsize( base_url + file_name) d[JSON_URL] = file_name @@ -242,6 +867,8 @@ def check_vehicle_update(vehicle_list=None): d[UPDATE_AVAILABLE] = 'true' except Exception as e: + # frappe.log_error( + # 'error 3', str(e)) # file_name= None file_size = None d[JSON_URL] = file_name @@ -253,30 +880,55 @@ def check_vehicle_update(vehicle_list=None): current_version) vehicle_req_list.append(d) except Exception as e1: + # frappe.log_error('error 4', str(e)) vehicle_req_list.append( {ERROR: UPDATE_FAILED}) elif current_version == float(data1_global[0]['version']): + """ Check For only internal """ + # frappe.log_error(str('global_test2')) + if current_version < float(data1[0]['version']): - data = frappe.db.sql('''SELECT name as Name,format(max(version),2) as Version,vehicle_id as Vehicle,language as Language - FROM `tabPublish` where vehicle='{}' and language='{}' and publish_status='Published' and publish_type='{}' and - vehicle_status='Active' and version > '{}' order by version ASC;'''.format(vehicle, l_id, publish_type, current_version), as_dict=1) + data = frappe.db.sql('''SELECT name as Name,format(version,2) as Version,vehicle_id as Vehicle,language as Language,release_description as Description + FROM `tabPublish` where vehicle='{}' and language='{}' and publish_status='Published' and + vehicle_status='Active' + and version = (select max(version) as version from `tabPublish` where + publish_type='{}' and vehicle='{}' and language='{}')'''.format(vehicle, l_id, publish_type, vehicle, l_id), as_dict=1) + try: for d in data: try: - file_name = JSON_INT_PATH+vehicle+"/" + \ - vehicle+"-" + \ - d[LANGUAGE]+"_v" + \ - str(d[VERSION]) + \ - JSON_EXT - file_size = os.path.getsize( - base_url + file_name) - d[JSON_URL] = file_name - d[FILE_SIZE] = file_size - d['Error'] = None - d[UPDATE_AVAILABLE] = 'true' + if float(data1[0]['version']) - current_version > 0.01: + + file_name = JSON_FULL_INT_PATH + vehicle+"/" + \ + vehicle+"-" + \ + d[LANGUAGE]+"-full_v" + \ + str(d[VERSION]) + \ + JSON_EXT + # frappe.log_error( + # 'File 1 ', file_name) + file_size = os.path.getsize( + base_url + file_name) + d[JSON_URL] = file_name + d[FILE_SIZE] = file_size + d['Error'] = None + d[UPDATE_AVAILABLE] = 'true' + else: + file_name = JSON_INT_PATH+vehicle+"/" + \ + vehicle+"-" + \ + d[LANGUAGE]+"_v" + \ + str(d[VERSION]) + \ + JSON_EXT + file_size = os.path.getsize( + base_url + file_name) + d[JSON_URL] = file_name + d[FILE_SIZE] = file_size + d['Error'] = None + d[UPDATE_AVAILABLE] = 'true' except Exception as e: + # frappe.log_error( + # 'error 5', str(e)) file_name = None file_size = None d[JSON_URL] = file_name @@ -288,31 +940,53 @@ def check_vehicle_update(vehicle_list=None): current_version) vehicle_req_list.append(d) except Exception as e2: + # frappe.log_error( + # 'error 6', str(e)) vehicle_req_list.append( {ERROR: UPDATE_FAILED}) - elif (float(data1_global[0]['version'])-1.00) < current_version < float(data1_global[0]['version']): + elif current_version > float(data1_global[0]['version']): + # frappe.log_error(str('global_test6')) if current_version < float(data1[0]['version']): - data = frappe.db.sql('''SELECT name as Name,format(max(version),2) as Version,vehicle_id as Vehicle,language as Language FROM `tabPublish` - where vehicle='{}' and language='{}' and publish_status='Published' and publish_type='Global' and vehicle_status='Active' - and version > '{}' order by version ASC;'''.format(vehicle, l_id, current_version), as_dict=1) + data = frappe.db.sql('''SELECT name as Name,format(version,2) as Version,vehicle_id as Vehicle,language as Language ,release_description as Description + FROM `tabPublish` where vehicle='{}' and language='{}' and publish_status='Published' and + vehicle_status='Active' and version = (select max(version) as + version from `tabPublish` where publish_type ='{}' and + vehicle='{}' and language='{}' )'''.format(vehicle, l_id, publish_type, vehicle, l_id), as_dict=1) try: for d in data: try: - file_name = JSON_GLOABL_PATH+vehicle+"/" + \ - vehicle+"-" + \ - d[LANGUAGE]+"_v" + \ - str(d[VERSION]) + \ - JSON_EXT - file_size = os.path.getsize( - base_url + file_name) - d[JSON_URL] = file_name - d[FILE_SIZE] = file_size - d['Error'] = None - d[UPDATE_AVAILABLE] = 'true' + if float(data1[0]['version']) - current_version > 0.01: + file_name = JSON_FULL_INT_PATH + vehicle+"/" + \ + vehicle+"-" + \ + d[LANGUAGE]+"-full_v" + \ + str(d[VERSION]) + \ + JSON_EXT + # frappe.log_error( + # 'File 2 ', file_name) + file_size = os.path.getsize( + base_url + file_name) + d[JSON_URL] = file_name + d[FILE_SIZE] = file_size + d['Error'] = None + d[UPDATE_AVAILABLE] = 'true' + else: + file_name = JSON_INT_PATH+vehicle+"/" + \ + vehicle+"-" + \ + d[LANGUAGE]+"_v" + \ + str(d[VERSION]) + \ + JSON_EXT + file_size = os.path.getsize( + base_url + file_name) + d[JSON_URL] = file_name + d[FILE_SIZE] = file_size + d['Error'] = None + d[UPDATE_AVAILABLE] = 'true' except Exception as e: - file_name = JSON_GLOABL_PATH+vehicle+"/" + \ + # frappe.log_error( + # 'error 9', str(e)) + file_name = JSON_INT_PATH+vehicle+"/" + \ vehicle+"-" + \ d[LANGUAGE]+"_v" + \ str(d[VERSION]) + \ @@ -326,19 +1000,31 @@ def check_vehicle_update(vehicle_list=None): d[CUR_VERSION] = float( current_version) vehicle_req_list.append(d) - except Exception as e2: + except Exception as e3: + # frappe.log_error( + # 'error 10', str(e)) vehicle_req_list.append( - {ERROR: UPDATE_FAILED}) + {'Error': UPDATE_FAILED}) + # else: + # vehicle_req_list.append({'Error':"Else 3"}) - elif current_version > float(data1_global[0]['version']): + elif (float(data1_global[0]['version'])-1.00) < current_version < float(data1_global[0]['version']): + # frappe.log_error(str('global_test1')) if current_version < float(data1[0]['version']): - data = frappe.db.sql('''SELECT name as Name,format(max(version),2) as Version,vehicle_id as Vehicle,language as Language FROM `tabPublish` - where vehicle='{}' and language='{}' and publish_status='Published' and publish_type='{}' and vehicle_status='Active' and version > '{}' - order by version ASC;'''.format(vehicle, l_id, publish_type, current_version), as_dict=1) + # frappe.log_error( + # str('global_test')) + data = frappe.db.sql('''SELECT name as Name,format(version,2) as Version,vehicle_id as Vehicle,language as Language, + release_description as Description FROM `tabPublish` where vehicle='{}' and language='{}' + and publish_status='Published' and vehicle_status='Active' + and version = (select max(version) as version from `tabPublish` + where vehicle='{}' and publish_type='Global' and language='{}' ); + '''.format(vehicle, l_id, vehicle, l_id), as_dict=1) + # frappe.log_error( + # 'error 7 data', str(data)) try: for d in data: try: - file_name = JSON_INT_PATH+vehicle+"/" + \ + file_name = JSON_GLOABL_PATH+vehicle+"/" + \ vehicle+"-" + \ d[LANGUAGE]+"_v" + \ str(d[VERSION]) + \ @@ -351,7 +1037,9 @@ def check_vehicle_update(vehicle_list=None): d[UPDATE_AVAILABLE] = 'true' except Exception as e: - file_name = JSON_INT_PATH+vehicle+"/" + \ + # frappe.log_error( + # 'error 7', str(e)) + file_name = JSON_GLOABL_PATH+vehicle+"/" + \ vehicle+"-" + \ d[LANGUAGE]+"_v" + \ str(d[VERSION]) + \ @@ -365,48 +1053,58 @@ def check_vehicle_update(vehicle_list=None): d[CUR_VERSION] = float( current_version) vehicle_req_list.append(d) - except Exception as e3: + except Exception as e2: + # frappe.log_error( + # 'error 8', str(e)) vehicle_req_list.append( - {'Error': UPDATE_FAILED}) - # else: - # vehicle_req_list.append({'Error':"Else 3"}) + {ERROR: UPDATE_FAILED}) else: vehicle_req_list.append( {LANGUAGE: l_id, UPDATE_AVAILABLE: 'false', CUR_VERSION: float(current_version)}) else: - data = frappe.db.sql('''SELECT name as Name,format(max(version),2) as Version,vehicle_id as Vehicle,language as Language FROM `tabPublish` - where vehicle='{}' and language='{}' and publish_status='Published' and publish_type='{}' and vehicle_status='Active' - and version > '{}' order by version ASC;'''.format(vehicle, l_id, publish_type, current_version), as_dict=1) - try: - for d in data: - try: - file_name = JSON_INT_PATH+vehicle+"/" + \ - vehicle+"-"+d[LANGUAGE] + \ - "_v"+str(d[VERSION])+JSON_EXT - file_size = os.path.getsize( - base_url + file_name) - d[JSON_URL] = file_name - d[FILE_SIZE] = file_size - d['Error'] = None - d[UPDATE_AVAILABLE] = 'true' + # frappe.log_error('null') + data = frappe.db.sql('''SELECT name as Name,format(version,2) as Version,vehicle_id as Vehicle,language as Language,release_description as Description FROM `tabPublish` + where vehicle='{}' and language='{}' and publish_status='Published' and vehicle_status='Active' + and version = + (select max(version) as version from `tabPublish` where + publish_type='{}' and vehicle='{}' and language='{}');'''.format(vehicle, l_id, publish_type, vehicle, l_id), as_dict=1) + if data: + try: + for d in data: + try: + file_name = JSON_INT_PATH+vehicle+"/" + \ + vehicle+"-"+d[LANGUAGE] + \ + "_v"+str(d[VERSION])+JSON_EXT + file_size = os.path.getsize( + base_url + file_name) + d[JSON_URL] = file_name + d[FILE_SIZE] = file_size + d['Error'] = None + d[UPDATE_AVAILABLE] = 'true' - except: - file_name = None - file_size = None - d[JSON_URL] = file_name - d[FILE_SIZE] = file_size - d['Error'] = FILE_ERROR - d[UPDATE_AVAILABLE] = 'false' - - d[CUR_VERSION] = float(current_version) - vehicle_req_list.append(d) - except Exception as e2: - vehicle_req_list.append( - {ERROR: UPDATE_FAILED}) + except: + # frappe.log_error( + # 'error 11', str(e)) + file_name = None + file_size = None + d[JSON_URL] = file_name + d[FILE_SIZE] = file_size + d['Error'] = FILE_ERROR + d[UPDATE_AVAILABLE] = 'false' + + d[CUR_VERSION] = float(current_version) + vehicle_req_list.append(d) + except Exception as e2: + # frappe.log_error('error 12', str(e)) + vehicle_req_list.append( + {ERROR: UPDATE_FAILED}) + if len(vehicle_req_list) > 0: + response['Vehicle'] = vehicle + response['VehicleReqList'] = vehicle_req_list + else: + return {STATUS: 0, ERROR: "No Vehicles in criteria"} - response['Vehicle'] = vehicle - response['VehicleReqList'] = vehicle_req_list return response else: response['JSON'] = {STATUS: 0, ERROR: "Data Not available"} @@ -478,7 +1176,8 @@ def calculate_step(data): def get_publish_ver(vehicle, l_id): return frappe.db.sql('''SELECT format(max(version),2) as version,publish_type as version,vehicle_id,language FROM `tabPublish` where vehicle='{}' and language='{}' and - publish_status='Published' and vehicle_status='Active' ;'''.format(vehicle, l_id), as_dict=1) + publish_status='Published' and vehicle_status='Active' + ;'''.format(vehicle, l_id), as_dict=1) @frappe.whitelist()