'''
Simple statistics plugin

To use it, go to http://127.0.0.1:8000/stat
'''
from modules.PluginInterface import VPProxyPlugin


class Stat(VPProxyPlugin):
    handlers = ('stat', )

    def __init__(self, VPConfig, VPStuff):
        self.config = VPConfig
        self.stuff = VPStuff

    def handle(self, connection):
        connection.send_response(200)
        connection.send_header('Content-type', 'text/html')
        connection.end_headers()
        connection.wfile.write(
            '<html><body><h4>Connected clients: ' + str(self.stuff.clientcounter.total) + '</h4>')
        connection.wfile.write(
            '<h5>Concurrent connections limit: ' + str(self.config.maxconns) + '</h5>')
        connection.wfile.write('<table border="1"><tr><th>url</th><th>count</th><th>clients</th></tr>')
        for i in self.stuff.clientcounter.clients:
          connection.wfile.write('<tr>')
          connection.wfile.write('<td>' + str(i) + ' </td><td> ' + str(self.stuff.clientcounter.clients[i][0]) + '</td><td>')
          for client in self.stuff.clientcounter.clients[i][1]:
            connection.wfile.write(str(client) + '<br>')
          connection.wfile.write('</td></tr>')
        connection.wfile.write('</table>')
        connection.wfile.write('</body></html>')
