def run(self): """ Goes through each subscription and deletes items older than expiration_time + clean_up_time """ LOGGER.debug("Running Subscription Clean Up") subscriptions = KVStoreCollectionAccessObject( collection=SUBSCRIPTIONS_COLLECTION_NAME, session_key=self.session_key) searches = KVStoreCollectionAccessObject( collection=SEARCHES_COLLECTION_NAME, session_key=self.session_key) search_updates = KVStoreCollectionAccessObject( collection=SEARCH_UPDATES_COLLECTION_NAME, session_key=self.session_key) # clean all expired subscriptions self._clean_expired_subscriptions(subscriptions) # Build kvstore query to return any records not in valid subscription list # All subscriptions should be valid after above cleaning not_keys_query = not_subscription_keys_query(subscriptions) # Delete any search_updates, subscription_credentials that don't belong to valid subscriptions search_updates.delete_items_by_query(not_keys_query) self._clean_user_namespaced_items(not_keys_query) # Clean up any searches that have not been updated in a multiple of the clean_up time self._clean_expired_searches(searches) LOGGER.debug("Completed Subscription Clean up")
def run(self): """ Goes through each alerts related collection and deletes items older than ttl_days """ LOGGER.info("Running alerts ttl utility") try: for kvstore_ttl_resource in self.ttl_resource_list: timestamp_attribute_name = kvstore_ttl_resource.time_attribute_name collection = kvstore_ttl_resource.collection_name ttl_num_seconds = day_to_seconds( kvstore_ttl_resource.ttl_num_days) collection_handler = KVStoreCollectionAccessObject( collection=kvstore_ttl_resource.collection_name, session_key=self.session_key, timestamp_attribute_name=timestamp_attribute_name) self.delete_invalid_entries(collection, collection_handler, timestamp_attribute_name) try: resp = collection_handler.delete_expired_items( expired_time=ttl_num_seconds) LOGGER.info( "Successfully performed TTL for collection=%s with response=%s" % (collection, str(resp))) except: LOGGER.exception( "Exception performing TTL for collection=%s" % collection) except: LOGGER.exception("Failure encountered during Alerts TTL ")
def calculate(self): kvstore_client = KVStoreCollectionAccessObject( MOBILE_ALERTS_COLLECTION_NAME, self.session_token, owner=NOBODY) r, jsn = kvstore_client.get_collection_keys() collection_keys = json.loads(jsn) collection_size = len(collection_keys) return {self.METRIC_NAME: collection_size}
def _clean_user_namespaced_items(self): users = get_all_mobile_users(self.session_key) timestamp_before = time_utils.get_current_timestamp() - self.clean_up_time LOGGER.debug('Deleting credentials older than last_update_time=%s, users=%s', timestamp_before, len(users)) for owner in users: credentials = KVStoreCollectionAccessObject(collection=SUBSCRIPTION_CREDENTIALS_COLLECTION_NAME, owner=owner, session_key=self.session_key) credentials.delete_expired_items(expired_time=timestamp_before, expiration_attribute_name=LAST_UPDATE_TIME)
def calculate(self): kvstore_client = KVStoreCollectionAccessObject(APPLICATION_TYPES_COLLECTION_NAME, self.session_token, owner=NOBODY) r, app_states = kvstore_client.get_all_items() metrics = {} for app_state in json.loads(app_states): metrics[app_state["application_name"]] = app_state["application_enabled"] return {self.METRIC_NAME: metrics}
def get(self, request): """ get registered companion apps """ system_authtoken = request['system_authtoken'] kvstore_client = KVStoreCollectionAccessObject(collection=self.COMPANION_APPS_COLLECTION_NAME, session_key=system_authtoken) r, content = kvstore_client.get_all_items() payload = json.loads(content.decode('utf-8')) if r.status == HTTPStatus.OK else content.decode('utf-8') return { 'payload': payload, 'status': r.status }
def delete(self, request): """ delete a companion app using comma separated list of app_ids. e.g. app_ids=id1,id2,id3 """ session_token = request['session']['authtoken'] app_ids = extract_parameter(request['query'], self.APP_IDS_LABEL, "query") app_ids_lst = app_ids.strip().split(',') query = {constants.OR_OPERATOR: [{constants.KEY: app_id} for app_id in app_ids_lst]} kvstore_client = KVStoreCollectionAccessObject(collection=self.COMPANION_APPS_COLLECTION_NAME, session_key=session_token) r, content = kvstore_client.delete_items_by_query(query) return { 'payload': content.decode('utf-8'), 'status': r.status }
def post(self, request): """ register a new companion app. Must have valid signature""" app_bundle = json.loads(request['payload']) session_token = request['session']['authtoken'] # Validate if provided bundle has all the required fields self.validate_app_bundle(app_bundle) # Validate provided signature self.validate_signature(app_bundle) # Write bundle to KV Store kvstore_payload = self.build_kvstore_payload(app_bundle) kvstore_client = KVStoreCollectionAccessObject(collection=self.COMPANION_APPS_COLLECTION_NAME, session_key=session_token) r, content = kvstore_client.insert_or_update_item_containing_key(kvstore_payload) payload = json.loads(content.decode('utf-8')) if r.status == HTTPStatus.OK else content.decode('utf-8') return { 'payload': payload, 'status': r.status }