<gh_stars>100-1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author  : jeffzhang
# @Time    : 2019/9/5
# @File    : http_log_orm.py
# @Desc    : ""

import time
from fuxi.core.databases.db_error import DatabaseError
from fuxi.core.databases.orm.database_base import DatabaseBase
from fuxi.core.databases.db_mongo import mongo, T_HTTP_REQUEST_LOG
from fuxi.common.utils.logger import logger


class _DBHttpRequestLog(DatabaseBase):
    def __init__(self):
        DatabaseBase.__init__(self)
        self.table = T_HTTP_REQUEST_LOG

    def get_list(self, keyword=None):
        date = int(time.time()) - 24*60*60*2
        if keyword:
            keyword = keyword.lower()
            return mongo[self.table].find({
                "$or": [
                    {"ip": {'$regex': keyword}}, {"data": {'$regex': keyword}},
                    {"referrer": {'$regex': keyword}}
                ],
                "date": {"$gt": date}
            }).sort("date", -1).limit(1000)
        return mongo[self.table].find({"date": {"$gt": date}}).sort("date", -1).limit(1000)

    def add(self, ip, referrer, data):
        if ip and data and referrer:
            inserted_id = mongo[self.table].insert_one({
                "ip": ip.strip(), "data": data, "referrer": referrer,
                "date": int(time.time())
            }).inserted_id
            return str(inserted_id)
        else:
            logger.warning("http request log insert failed: invalid data")
            raise DatabaseError("invalid data")

    def verify(self, data):
        return mongo[self.table].find_one({"data": data})


DBHttpRequestLog = _DBHttpRequestLog()

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author  : jeffzhang
# @Time    : 2019/9/5
# @File    : http_log_orm.py
# @Desc    : ""

import time
from fuxi.core.databases.db_error import DatabaseError
from fuxi.core.databases.orm.database_base import DatabaseBase
from fuxi.core.databases.db_mongo import mongo, T_HTTP_REQUEST_LOG
from fuxi.common.utils.logger import logger


class _DBHttpRequestLog(DatabaseBase):
    def __init__(self):
        DatabaseBase.__init__(self)
        self.table = T_HTTP_REQUEST_LOG

    def get_list(self, keyword=None):
        date = int(time.time()) - 24*60*60*2
        if keyword:
            keyword = keyword.lower()
            return mongo[self.table].find({
                "$or": [
                    {"ip": {'$regex': keyword}}, {"data": {'$regex': keyword}},
                    {"referrer": {'$regex': keyword}}
                ],
                "date": {"$gt": date}
            }).sort("date", -1).limit(1000)
        return mongo[self.table].find({"date": {"$gt": date}}).sort("date", -1).limit(1000)

    def add(self, ip, referrer, data):
        if ip and data and referrer:
            inserted_id = mongo[self.table].insert_one({
                "ip": ip.strip(), "data": data, "referrer": referrer,
                "date": int(time.time())
            }).inserted_id
            return str(inserted_id)
        else:
            logger.warning("http request log insert failed: invalid data")
            raise DatabaseError("invalid data")

    def verify(self, data):
        return mongo[self.table].find_one({"data": data})


DBHttpRequestLog = _DBHttpRequestLog()
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Author  : jeffzhang # @Time    : 2019/9/5 # @File    : http_log_orm.py # @Desc    : ""