def query2(fr, to, typ):

    return list(
        logs_col.aggregate([{
            '$match': {
                'timestamp': {
                    '$gte': fr,
                    '$lte': to
                },
                'type': typ
            }
        }, {
            '$group': {
                '_id': {
                    '$dateToString': {
                        'format': '%d-%m-%Y',
                        'date': '$timestamp'
                    }
                },
                'count': {
                    '$sum': 1
                }
            }
        }, {
            '$project': {
                '_id': 0,
                'day': '$_id',
                'count': 1
            }
        }]))
def query7(fr, to):

    return list(
        logs_col.aggregate([{
            '$match': {
                'timestamp': {
                    '$gte': fr,
                    '$lte': to
                }
            }
        }, {
            '$lookup': {
                'from': 'admins',
                'localField': '_id',
                'foreignField': 'upvotes',
                'as': 'upvotes'
            }
        }, {
            '$project': {
                'votes': {
                    '$size': '$upvotes'
                },
                '_id': 0,
                'logid': '$_id'
            }
        }, {
            '$sort': SON({'votes': -1})
        }, {
            '$limit': 50
        }]))
def query4(fr, to):

    return list(
        logs_col.aggregate([{
            '$match': {
                'timestamp': {
                    '$gte': fr,
                    '$lte': to
                },
                'http_method': {
                    '$exists': True
                }
            }
        }, {
            '$group': {
                '_id': '$http_method',
                'count': {
                    '$sum': 1
                }
            }
        }, {
            '$sort': SON({'count': 1})
        }, {
            '$limit': 2
        }, {
            '$project': {
                '_id': 0,
                'http_method': '$_id'
            }
        }]))
def query1(fr, to):

    return list(
        logs_col.aggregate([{
            '$match': {
                'timestamp': {
                    '$gte': fr,
                    '$lte': to
                }
            }
        }, {
            '$group': {
                '_id': '$type',
                'count': {
                    '$sum': 1
                }
            }
        }, {
            '$sort': SON({'count': -1})
        }, {
            '$project': {
                '_id': 0,
                'type': '$_id',
                'count': 1
            }
        }]))
def query6():

    return list(
        logs_col.aggregate([{
            '$match': {
                '$or': [{
                    'type': 'replicate'
                }, {
                    'type': 'served'
                }]
            }
        }, {
            '$project': {
                'timestamp': 1,
                'type': 1,
                'block_ids': 1
            }
        }, {
            '$unwind': '$block_ids'
        }, {
            '$group': {
                '_id': {
                    'day': {
                        '$dateToString': {
                            'format': '%d-%m-%Y',
                            'date': '$timestamp'
                        }
                    },
                    'block': '$block_ids',
                    'type': '$type'
                }
            }
        }, {
            '$project': {
                'day': '$_id.day',
                'block': '$_id.block',
                'type': '$_id.type'
            }
        }, {
            '$group': {
                '_id': {
                    'day': '$day',
                    'block': '$block'
                },
                'count': {
                    '$sum': 1
                }
            }
        }, {
            '$match': {
                'count': 2
            }
        }, {
            '$project': {
                'block': '$_id.block',
                '_id': 0
            }
        }]))
def query5():

    return list(
        logs_col.aggregate([{
            '$match': {
                'referer': {
                    '$exists': True
                },
                'resource': {
                    '$exists': True
                }
            }
        }, {
            '$group': {
                '_id': '$referer',
                'resources': {
                    '$addToSet': '$resource'
                }
            }
        }, {
            '$project': {
                '_id': 0,
                'referer': '$_id',
                'arrsize': {
                    '$size': '$resources'
                }
            }
        }, {
            '$match': {
                'arrsize': {
                    '$gte': 2
                }
            }
        }, {
            '$project': {
                'referer': 1
            }
        }]))
def query3(fr, to):

    return list(
        logs_col.aggregate([{
            '$match': {
                'timestamp': {
                    '$gte': fr,
                    '$lte': to
                }
            }
        }, {
            '$group': {
                '_id': {
                    'source_ip': '$source_ip',
                    'type': '$type'
                },
                'numtypes': {
                    '$sum': 1
                }
            }
        }, {
            '$project': {
                '_id': 0,
                'source_ip': '$_id.source_ip',
                'type': '$_id.type',
                'numtypes': 1
            }
        }, {
            '$group': {
                '_id': '$source_ip',
                'countpertype': {
                    '$push': {
                        'type': '$type',
                        'count': '$numtypes'
                    }
                }
            }
        }, {
            '$unwind': '$countpertype'
        }, {
            '$sort': {
                'countpertype.count': -1
            }
        }, {
            '$group': {
                '_id': '$_id',
                'sortedArr': {
                    '$push': {
                        'type': '$countpertype.type',
                        'count': '$countpertype.count'
                    }
                }
            }
        }, {
            '$project': {
                '_id': 0,
                'source_ip': '$_id',
                'types': {
                    '$slice': ['$sortedArr', 3]
                }
            }
        }]))