def update_operation_pickup_time(self, operation_id, agent_id, operation, conn=None): keys_to_update = ({ OperationPerAgentKey.Status: PICKEDUP, OperationPerAgentKey.PickedUpTime: self.db_time, }) try: (r.table(OperationsPerAgentCollection).get_all( [operation_id, agent_id], index=OperationPerAgentIndexes.OperationIdAndAgentId).update( keys_to_update).run(conn)) (r.table(OperationsCollection).get(operation_id).update({ OperationKey.AgentsPendingPickUpCount: (r.branch( r.row[OperationKey.AgentsPendingPickUpCount] > 0, r.row[OperationKey.AgentsPendingPickUpCount] - 1, r.row[OperationKey.AgentsPendingPickUpCount], )), OperationKey.AgentsPendingResultsCount: (r.branch( r.row[OperationKey.AgentsPendingResultsCount] < r.row[OperationKey.AgentsTotalCount], r.row[OperationKey.AgentsPendingResultsCount] + 1, r.row[OperationKey.AgentsPendingResultsCount])), OperationKey.UpdatedTime: self.db_time }).run(conn)) results = (OperationResults( self.username, self.uri, self.method).operation_updated(operation_id)) logger.info(results) except Exception as e: results = (GenericResults(self.username, self.uri, self.method).something_broke( operation_id, operation, e)) logger.exception(results) return (results)
def update_hidden_status(username, customer_name, uri, method, app_ids, hidden='yes', table=AppsCollection, conn=None): if table == AppsCollection: CurrentAppsKey = AppsKey elif table == CustomAppsCollection: CurrentAppsKey = CustomAppsKey elif table == SupportedAppsCollection: CurrentAppsKey = SupportedAppsKey elif table == AgentAppsPerAgentCollection: CurrentAppsKey = AgentAppsKey try: if hidden == YES or hidden == NO: (r.expr(app_ids).for_each(lambda app_id: r.table(table).get( app_id).update({CurrentAppsKey.Hidden: hidden})).run(conn)) elif hidden == 'toggle': for app_id in app_ids: toggled = (r.table(table).get(app_id).update({ CurrentAppsKey.Hidden: (r.branch(r.row[CurrentAppsKey.Hidden] == YES, NO, YES)) }).run(conn)) results = (PackageResults(username, uri, method).toggle_hidden(app_ids, hidden)) except Exception as e: logger.exception(e) results = (GenericResults(username, uri, method).something_broke( app_ids, 'toggle hidden on os_apps', e)) return (results)
def _update_agent_stats(self, operation_id, agent_id, conn=None): completed = True try: agent_operation = (r.table(OperationsPerAgentCollection).get_all( [operation_id, agent_id], index=OperationPerAgentIndexes.OperationIdAndAgentId).run(conn) ) if agent_operation: for oper in agent_operation: if (oper[OperationPerAgentKey.Status] == OperationCodes.ResultsReceived): (r.table(OperationsCollection).get( operation_id ).update({ OperationKey.AgentsCompletedCount: r.branch( r.row[OperationKey.AgentsCompletedCount] < r.row[OperationKey.AgentsTotalCount], r.row[OperationKey.AgentsCompletedCount] + 1, r.row[OperationKey.AgentsCompletedCount]), OperationKey.AgentsPendingResultsCount: r.branch( r.row[OperationKey.AgentsPendingResultsCount] > 0, r.row[OperationKey.AgentsPendingResultsCount] - 1, r.row[OperationKey.AgentsPendingResultsCount]), OperationKey.UpdatedTime: self.db_time, OperationKey.CompletedTime: self.db_time }).run(conn)) elif (oper[OperationPerAgentKey.Status] == OperationCodes.ResultsReceivedWithErrors): (r.table(OperationsCollection).get( operation_id ).update({ OperationKey.AgentsFailedCount: r.branch( r.row[OperationKey.AgentsFailedCount] < r.row[OperationKey.AgentsTotalCount], r.row[OperationKey.AgentsFailedCount] + 1, r.row[OperationKey.AgentsFailedCount]), OperationKey.AgentsPendingResultsCount: r.branch( r.row[OperationKey.AgentsPendingResultsCount] > 0, r.row[OperationKey.AgentsPendingResultsCount] - 1, r.row[OperationKey.AgentsPendingResultsCount]), OperationKey.UpdatedTime: self.db_time, OperationKey.CompletedTime: self.db_time }).run(conn)) except Exception as e: results = (GenericResults(self.username, self.uri, self.method).something_broke( operation_id, agent_id, e)) logger.exception(results) completed = False return (completed)
def _update_agent_stats_by_app_stats(self, operation_id, agent_id, completed_count, failed_count, pending_count, conn=None): completed = True try: total_count = completed_count + failed_count + pending_count if total_count == completed_count: (r.table(OperationsCollection).get(operation_id).update({ OperationKey.AgentsCompletedCount: r.branch( r.row[OperationKey.AgentsCompletedCount] < r.row[OperationKey.AgentsTotalCount], r.row[OperationKey.AgentsCompletedCount] + 1, r.row[OperationKey.AgentsCompletedCount]), OperationKey.AgentsPendingResultsCount: r.branch(r.row[OperationKey.AgentsPendingResultsCount] > 0, r.row[OperationKey.AgentsPendingResultsCount] - 1, r.row[OperationKey.AgentsPendingResultsCount]), OperationKey.UpdatedTime: self.db_time, OperationKey.CompletedTime: self.db_time }).run(conn)) self._update_completed_time_on_agents(operation_id, agent_id) self._update_operation_status_code(operation_id) elif total_count == failed_count: (r.table(OperationsCollection).get(operation_id).update({ OperationKey.AgentsFailedCount: r.branch( r.row[OperationKey.AgentsFailedCount] < r.row[OperationKey.AgentsTotalCount], r.row[OperationKey.AgentsFailedCount] + 1, r.row[OperationKey.AgentsFailedCount]), OperationKey.AgentsPendingResultsCount: r.branch(r.row[OperationKey.AgentsPendingResultsCount] > 0, r.row[OperationKey.AgentsPendingResultsCount] - 1, r.row[OperationKey.AgentsPendingResultsCount]), OperationKey.UpdatedTime: self.db_time, OperationKey.CompletedTime: self.db_time }).run(conn)) self._update_completed_time_on_agents(operation_id, agent_id) self._update_operation_status_code(operation_id) elif total_count == (failed_count + completed_count): (r.table(OperationsCollection).get(operation_id).update({ OperationKey.AgentsCompletedWithErrorsCount: r.branch( r.row[OperationKey.AgentsCompletedWithErrorsCount] < r.row[OperationKey.AgentsTotalCount], r.row[OperationKey.AgentsCompletedWithErrorsCount] + 1, r.row[OperationKey.AgentsCompletedWithErrorsCount]), OperationKey.AgentsPendingResultsCount: r.branch(r.row[OperationKey.AgentsPendingResultsCount] > 0, r.row[OperationKey.AgentsPendingResultsCount] - 1, r.row[OperationKey.AgentsPendingResultsCount]), OperationKey.UpdatedTime: self.db_time, OperationKey.CompletedTime: self.db_time }).run(conn)) self._update_completed_time_on_agents(operation_id, agent_id) self._update_operation_status_code(operation_id) except Exception as e: results = (GenericResults(self.username, self.uri, self.method).something_broke( operation_id, agent_id, e)) logger.exception(results) completed = False return (completed)
def _update_agent_stats(self, operation_id, agent_id, conn=None): completed = True try: agent_operation = ( r .table(OperationsPerAgentCollection) .get_all( [operation_id, agent_id], index=OperationPerAgentIndexes.OperationIdAndAgentId ) .run(conn) ) if agent_operation: for oper in agent_operation: if (oper[OperationPerAgentKey.Status] == OperationCodes.ResultsReceived): ( r .table(OperationsCollection) .get(operation_id) .update( { OperationKey.AgentsCompletedCount: r.branch( r.row[OperationKey.AgentsCompletedCount] < r.row[OperationKey.AgentsTotalCount], r.row[OperationKey.AgentsCompletedCount] + 1, r.row[OperationKey.AgentsCompletedCount] ), OperationKey.AgentsPendingResultsCount: r.branch( r.row[OperationKey.AgentsPendingResultsCount] > 0, r.row[OperationKey.AgentsPendingResultsCount] - 1, r.row[OperationKey.AgentsPendingResultsCount] ), OperationKey.UpdatedTime: self.db_time, OperationKey.CompletedTime: self.db_time } ) .run(conn) ) elif (oper[OperationPerAgentKey.Status] == OperationCodes.ResultsReceivedWithErrors): ( r .table(OperationsCollection) .get(operation_id) .update( { OperationKey.AgentsFailedCount: r.branch( r.row[OperationKey.AgentsFailedCount] < r.row[OperationKey.AgentsTotalCount], r.row[OperationKey.AgentsFailedCount] + 1, r.row[OperationKey.AgentsFailedCount] ), OperationKey.AgentsPendingResultsCount: r.branch( r.row[OperationKey.AgentsPendingResultsCount] > 0, r.row[OperationKey.AgentsPendingResultsCount] - 1, r.row[OperationKey.AgentsPendingResultsCount] ), OperationKey.UpdatedTime: self.db_time, OperationKey.CompletedTime: self.db_time } ) .run(conn) ) except Exception as e: results = ( GenericResults( self.username, self.uri, self.method ).something_broke(operation_id, agent_id, e) ) logger.exception(results) completed = False return(completed)
def _update_agent_stats_by_app_stats(self, operation_id, agent_id, completed_count, failed_count, pending_count, conn=None): completed = True try: total_count = completed_count + failed_count + pending_count if total_count == completed_count: ( r .table(OperationsCollection) .get(operation_id) .update( { OperationKey.AgentsCompletedCount: r.branch( r.row[OperationKey.AgentsCompletedCount] < r.row[OperationKey.AgentsTotalCount], r.row[OperationKey.AgentsCompletedCount] + 1, r.row[OperationKey.AgentsCompletedCount] ), OperationKey.AgentsPendingResultsCount: r.branch( r.row[OperationKey.AgentsPendingResultsCount] > 0, r.row[OperationKey.AgentsPendingResultsCount] - 1, r.row[OperationKey.AgentsPendingResultsCount] ), OperationKey.UpdatedTime: self.db_time, OperationKey.CompletedTime: self.db_time } ) .run(conn) ) self._update_completed_time_on_agents(operation_id, agent_id) self._update_operation_status_code(operation_id) elif total_count == failed_count: ( r .table(OperationsCollection) .get(operation_id) .update( { OperationKey.AgentsFailedCount: r.branch( r.row[OperationKey.AgentsFailedCount] < r.row[OperationKey.AgentsTotalCount], r.row[OperationKey.AgentsFailedCount] + 1, r.row[OperationKey.AgentsFailedCount] ), OperationKey.AgentsPendingResultsCount: r.branch( r.row[OperationKey.AgentsPendingResultsCount] > 0, r.row[OperationKey.AgentsPendingResultsCount] - 1, r.row[OperationKey.AgentsPendingResultsCount] ), OperationKey.UpdatedTime: self.db_time, OperationKey.CompletedTime: self.db_time } ) .run(conn) ) self._update_completed_time_on_agents(operation_id, agent_id) self._update_operation_status_code(operation_id) elif total_count == (failed_count + completed_count): ( r .table(OperationsCollection) .get(operation_id) .update( { OperationKey.AgentsCompletedWithErrorsCount: r.branch( r.row[OperationKey.AgentsCompletedWithErrorsCount] < r.row[OperationKey.AgentsTotalCount], r.row[OperationKey.AgentsCompletedWithErrorsCount] + 1, r.row[OperationKey.AgentsCompletedWithErrorsCount] ), OperationKey.AgentsPendingResultsCount: r.branch( r.row[OperationKey.AgentsPendingResultsCount] > 0, r.row[OperationKey.AgentsPendingResultsCount] - 1, r.row[OperationKey.AgentsPendingResultsCount] ), OperationKey.UpdatedTime: self.db_time, OperationKey.CompletedTime: self.db_time } ) .run(conn) ) self._update_completed_time_on_agents(operation_id, agent_id) self._update_operation_status_code(operation_id) except Exception as e: results = ( GenericResults( self.username, self.uri, self.method ).something_broke(operation_id, agent_id, e) ) logger.exception(results) completed = False return(completed)
def update_operation_pickup_time(self, operation_id, agent_id, operation, conn=None): keys_to_update = ( { OperationPerAgentKey.Status: PICKEDUP, OperationPerAgentKey.PickedUpTime: self.db_time, } ) try: ( r .table(OperationsPerAgentCollection) .get_all( [operation_id, agent_id], index=OperationPerAgentIndexes.OperationIdAndAgentId ) .update(keys_to_update) .run(conn) ) ( r .table(OperationsCollection) .get(operation_id) .update( { OperationKey.AgentsPendingPickUpCount: ( r.branch( r.row[OperationKey.AgentsPendingPickUpCount] > 0, r.row[OperationKey.AgentsPendingPickUpCount] - 1, r.row[OperationKey.AgentsPendingPickUpCount], ) ), OperationKey.AgentsPendingResultsCount: ( r.branch( r.row[OperationKey.AgentsPendingResultsCount] < r.row[OperationKey.AgentsTotalCount], r.row[OperationKey.AgentsPendingResultsCount] + 1, r.row[OperationKey.AgentsPendingResultsCount] ) ), OperationKey.UpdatedTime: self.db_time } ) .run(conn) ) results = ( OperationResults( self.username, self.uri, self.method ).operation_updated(operation_id) ) logger.info(results) except Exception as e: results = ( GenericResults( self.username, self.uri, self.method ).something_broke(operation_id, operation, e) ) logger.exception(results) return(results)
def update_hidden_status(username, customer_name, uri, method, app_ids, hidden='yes', table=AppsCollection, conn=None): if table == AppsCollection: CurrentAppsKey = AppsKey elif table == CustomAppsCollection: CurrentAppsKey = CustomAppsKey elif table == SupportedAppsCollection: CurrentAppsKey = SupportedAppsKey elif table == AgentAppsPerAgentCollection: CurrentAppsKey = AgentAppsKey try: if hidden == YES or hidden == NO: ( r .expr(app_ids) .for_each( lambda app_id: r .table(table) .get(app_id) .update( { CurrentAppsKey.Hidden: hidden } ) ) .run(conn) ) elif hidden == 'toggle': for app_id in app_ids: toggled = ( r .table(table) .get(app_id) .update( { CurrentAppsKey.Hidden: ( r.branch( r.row[CurrentAppsKey.Hidden] == YES, NO, YES ) ) } ) .run(conn) ) results = ( PackageResults( username, uri, method ).toggle_hidden(app_ids, hidden) ) except Exception as e: logger.exception(e) results = ( GenericResults( username, uri, method ).something_broke(app_ids, 'toggle hidden on os_apps', e) ) return(results)