def delete_event(event_id): """ Remove event from the database. :param event_id: Identifier of the event to remove. :return: """ collection = yield db.get_event_collection() return_value = yield collection.delete_many({'event_id': event_id}) defer.returnValue(return_value)
def get_distinct_users_from_events(campaign_id, timestamp): """ Fetch distinct user identifiers for this campaign, for this timestamp. :param campaign_id: Campaign identifier. :param timestamp: Time in seconds since the epoch, used for getting the full hour timestamp. :return: List of distinct users ids. """ timestamp = common_utils.timestamp2hour(timestamp) collection = yield db.get_event_collection() return_values = yield collection.distinct(key='user_id', filter={'timestamp': timestamp, 'campaign_id': campaign_id}) defer.returnValue(return_values)
def get_banner_events_iter(banner_id, timestamp): """ Fetch all banner documents from the database. :param banner_id: Banner identifier. :param timestamp: Time in seconds since the epoch, used for getting the full hour timestamp. :return: Iterable events collection (QueryIterator) """ timestamp = common_utils.timestamp2hour(timestamp) collection = yield db.get_event_collection() defer.returnValue(QueryIterator(collection.find({ 'banner_id': banner_id, 'timestamp': common_utils.timestamp2hour(timestamp) }, cursor=True)))
def update_event(event_doc): """ Create or update an event if it doesn't exist. :param event_doc: Event document :return: """ event_doc['timestamp'] = common_utils.timestamp2hour(event_doc['timestamp']) collection = yield db.get_event_collection() return_value = yield collection.replace_one({'event_id': event_doc['event_id']}, event_doc, upsert=True) defer.returnValue(return_value)
def get_events_per_user_iter(campaign_id, timestamp, uid): """ Fetch all events for this campaign, for this timestamp, for this uid. :param campaign_id: Campaign identifier. :param timestamp: Time in seconds since the epoch, used for getting the full hour timestamp. :param uid: User identifier. :return: Iterable events for the user. """ timestamp = common_utils.timestamp2hour(timestamp) collection = yield db.get_event_collection() defer.returnValue(QueryIterator(collection.find({ 'user_id': uid, 'campaign_id': campaign_id, 'timestamp': timestamp }, cursor=True)))
def delete_events(timestamp): collection = yield db.get_event_collection() return_value = yield collection.delete_many({'timestamp': {'$lt': timestamp}}) defer.returnValue(return_value)