Exemplo n.º 1
0
def mongo_compute_state_count(state, update_time):
    logger.info("Computing aggregates for state %s" % state)
    collection = get_collection("votes")
    pipeline = [
        {"$match": {"state": state, "vote_timestamp": {"$lt": update_time}}},
        {"$group": {"_id": {"state": '$state', "vote_timestamp": '$vote_timestamp', "vote_result": "$vote_result"},
                    "result": {"$sum": 1}}}
    ]

    aggregates_list = list(collection.aggregate(pipeline, allowDiskUse=True))
    aggregates_list = clean_bson_to_json(aggregates_list)
    if len(aggregates_list) == 0:
        logger.info("No aggregate for state %s" % state)
        return False

    # Unnest id properties
    for item in aggregates_list:
        item["state"] = item["_id"]["state"]
        item["vote_timestamp"] = item["_id"]["vote_timestamp"]
        item["vote_result"] = item["_id"]["vote_result"]
        del item["_id"]

    # Save it in aggregates
    logger.info("Saving aggregates")
    collection = get_collection("aggregates")
    collection.insert_many(aggregates_list)
    return aggregates_list
Exemplo n.º 2
0
def mongo_query_aggregates_state(state):
    """
    Checks if state aggregates are available
    """
    collection = get_collection("aggregates")
    find_query = {"state": state}
    result = list(collection.find(find_query))
    if len(result) > 0:
        return clean_bson_to_json(result)
    else:
        # If no aggregates
        return False
Exemplo n.º 3
0
def mongo_query_states_with_info(update_time):
    collection = get_collection("votes")
    query = {"vote_timestamp": {"$lt": update_time}}
    states = list(collection.distinct("state", query))
    return states
Exemplo n.º 4
0
def does_index_exists_votes(column):
    collection = get_collection("votes")
    indexes = collection.index_information()
    logger.debug("Indexes %s" % indexes)
    potential_index = column + "_1"
    return potential_index in indexes
Exemplo n.º 5
0
def create_index_votes(column):
    collection = get_collection("votes")
    collection.create_index(column)
Exemplo n.º 6
0
def mongo_query_aggregates_all(update_time):
    collection = get_collection("aggregates")
    find_query = {"vote_timestamp": {"$lte": update_time}}
    aggregates = list(collection.find(find_query))
    clean_aggregates = clean_bson_to_json(aggregates)
    return clean_aggregates