from datetime import datetime
from functools import reduce
from operator import add
from socket import error as SocketError

from django.db import connection
from django.utils.deprecation import MiddlewareMixin

__author__ = 'Shafikur Rahman'

import time


class StatsMiddleware(MiddlewareMixin):

    def process_request(self, request):
        request.start_time = time.time()

    def process_response(self, request, response):
        try:
            duration = time.time() - request.start_time
            db_queries = len(connection.queries)
            if db_queries:
                db_time = reduce(add, [float(q['time']) for q in connection.queries[:]])
            else:
                db_time = 0.0

            db_time = float(db_time * 1000)
            total_time = float(duration * 1000)
            python_time = float(total_time - db_time)

            db_queries = db_queries

            response["X-Total-Time"] = round(total_time, 2)
            response["X-Python-Time"] = round(python_time, 2)
            response["X-DB-Time"] = round(db_time, 2)
            response["X-DB-Queries"] = db_queries
        except SocketError as e:
            pass
        print('--------------------------')
        print(datetime.now())
        print('Total time: %s' % response["X-Total-Time"])
        print('Python time: %s' % response["X-Python-Time"])
        print('Total DB time: %s' % response["X-DB-Time"])
        print('Total queries: %s\n' % response["X-DB-Queries"])
        return response

77553618327f9c3e5b8e9b889a04a8bbebec557b
<mask token>


class StatsMiddleware(MiddlewareMixin):
    <mask token>

    def process_response(self, request, response):
        try:
            duration = time.time() - request.start_time
            db_queries = len(connection.queries)
            if db_queries:
                db_time = reduce(add, [float(q['time']) for q in connection
                    .queries[:]])
            else:
                db_time = 0.0
            db_time = float(db_time * 1000)
            total_time = float(duration * 1000)
            python_time = float(total_time - db_time)
            db_queries = db_queries
            response['X-Total-Time'] = round(total_time, 2)
            response['X-Python-Time'] = round(python_time, 2)
            response['X-DB-Time'] = round(db_time, 2)
            response['X-DB-Queries'] = db_queries
        except SocketError as e:
            pass
        print('--------------------------')
        print(datetime.now())
        print('Total time: %s' % response['X-Total-Time'])
        print('Python time: %s' % response['X-Python-Time'])
        print('Total DB time: %s' % response['X-DB-Time'])
        print('Total queries: %s\n' % response['X-DB-Queries'])
        return response

<mask token>


class StatsMiddleware(MiddlewareMixin):

    def process_request(self, request):
        request.start_time = time.time()

    def process_response(self, request, response):
        try:
            duration = time.time() - request.start_time
            db_queries = len(connection.queries)
            if db_queries:
                db_time = reduce(add, [float(q['time']) for q in connection
                    .queries[:]])
            else:
                db_time = 0.0
            db_time = float(db_time * 1000)
            total_time = float(duration * 1000)
            python_time = float(total_time - db_time)
            db_queries = db_queries
            response['X-Total-Time'] = round(total_time, 2)
            response['X-Python-Time'] = round(python_time, 2)
            response['X-DB-Time'] = round(db_time, 2)
            response['X-DB-Queries'] = db_queries
        except SocketError as e:
            pass
        print('--------------------------')
        print(datetime.now())
        print('Total time: %s' % response['X-Total-Time'])
        print('Python time: %s' % response['X-Python-Time'])
        print('Total DB time: %s' % response['X-DB-Time'])
        print('Total queries: %s\n' % response['X-DB-Queries'])
        return response

<mask token>
__author__ = 'Shafikur Rahman'
<mask token>


class StatsMiddleware(MiddlewareMixin):

    def process_request(self, request):
        request.start_time = time.time()

    def process_response(self, request, response):
        try:
            duration = time.time() - request.start_time
            db_queries = len(connection.queries)
            if db_queries:
                db_time = reduce(add, [float(q['time']) for q in connection
                    .queries[:]])
            else:
                db_time = 0.0
            db_time = float(db_time * 1000)
            total_time = float(duration * 1000)
            python_time = float(total_time - db_time)
            db_queries = db_queries
            response['X-Total-Time'] = round(total_time, 2)
            response['X-Python-Time'] = round(python_time, 2)
            response['X-DB-Time'] = round(db_time, 2)
            response['X-DB-Queries'] = db_queries
        except SocketError as e:
            pass
        print('--------------------------')
        print(datetime.now())
        print('Total time: %s' % response['X-Total-Time'])
        print('Python time: %s' % response['X-Python-Time'])
        print('Total DB time: %s' % response['X-DB-Time'])
        print('Total queries: %s\n' % response['X-DB-Queries'])
        return response

from datetime import datetime
from functools import reduce
from operator import add
from socket import error as SocketError
from django.db import connection
from django.utils.deprecation import MiddlewareMixin
__author__ = 'Shafikur Rahman'
import time


class StatsMiddleware(MiddlewareMixin):

    def process_request(self, request):
        request.start_time = time.time()

    def process_response(self, request, response):
        try:
            duration = time.time() - request.start_time
            db_queries = len(connection.queries)
            if db_queries:
                db_time = reduce(add, [float(q['time']) for q in connection
                    .queries[:]])
            else:
                db_time = 0.0
            db_time = float(db_time * 1000)
            total_time = float(duration * 1000)
            python_time = float(total_time - db_time)
            db_queries = db_queries
            response['X-Total-Time'] = round(total_time, 2)
            response['X-Python-Time'] = round(python_time, 2)
            response['X-DB-Time'] = round(db_time, 2)
            response['X-DB-Queries'] = db_queries
        except SocketError as e:
            pass
        print('--------------------------')
        print(datetime.now())
        print('Total time: %s' % response['X-Total-Time'])
        print('Python time: %s' % response['X-Python-Time'])
        print('Total DB time: %s' % response['X-DB-Time'])
        print('Total queries: %s\n' % response['X-DB-Queries'])
        return response

from datetime import datetime
from functools import reduce
from operator import add
from socket import error as SocketError

from django.db import connection
from django.utils.deprecation import MiddlewareMixin

__author__ = 'Shafikur Rahman'

import time


class StatsMiddleware(MiddlewareMixin):

    def process_request(self, request):
        request.start_time = time.time()

    def process_response(self, request, response):
        try:
            duration = time.time() - request.start_time
            db_queries = len(connection.queries)
            if db_queries:
                db_time = reduce(add, [float(q['time']) for q in connection.queries[:]])
            else:
                db_time = 0.0

            db_time = float(db_time * 1000)
            total_time = float(duration * 1000)
            python_time = float(total_time - db_time)

            db_queries = db_queries

            response["X-Total-Time"] = round(total_time, 2)
            response["X-Python-Time"] = round(python_time, 2)
            response["X-DB-Time"] = round(db_time, 2)
            response["X-DB-Queries"] = db_queries
        except SocketError as e:
            pass
        print('--------------------------')
        print(datetime.now())
        print('Total time: %s' % response["X-Total-Time"])
        print('Python time: %s' % response["X-Python-Time"])
        print('Total DB time: %s' % response["X-DB-Time"])
        print('Total queries: %s\n' % response["X-DB-Queries"])
        return response
