#!/usr/bin/env python
from django.core.management import setup_environ
import settings
setup_environ(settings)

from monitor.models import *

from collections_24 import defaultdict
import sys

from logging import getLogger, FileHandler, INFO, StreamHandler
file_log_name = __name__
getLogger(file_log_name).setLevel(INFO)
getLogger(file_log_name).addHandler(FileHandler("%s.log" % 'hydra'))

def log():
    return getLogger(file_log_name)

def screen(string):
    print string 
    log().debug(string)


import cmd
from texttable import Texttable

class HydraDebug(cmd.Cmd, object):
    def __init__(self):
        super(HydraDebug, self).__init__()
        self.prompt = "Hydra> "

    def precmd(self, line):
        log().debug("> %s" % line)
        return line

    def do_EOF(self, line):
        raise KeyboardInterrupt()

    def __volume_row(self, vol, table):
        table.add_row([
            vol.id,
            vol.role(),
            vol.host.address,
            vol.name,
            AuditMountable.mountable_status_string(vol)
            ])

    def __filesystem_title(self, filesystem):
        screen(filesystem.name)
        screen("=" * len(filesystem.name))

    def __list_volumes_filesystem(self, filesystem):
        table = Texttable()
        table.header(['id', 'kind', 'host', 'name', 'status'])

        try:
            mgs = ManagementTarget.objects.get(filesystems = filesystem)
            self.__volume_row(mgs, table)
        except ManagementTarget.DoesNotExist:
            pass

        try:
            mdt = MetadataTarget.objects.get(filesystem = filesystem)
            self.__volume_row(mdt, table)
        except MetadataTarget.DoesNotExist:
            pass

        osts = ObjectStoreTarget.objects.filter(filesystem = filesystem)
        for ost in osts:
            self.__volume_row(ost, table)
        
        self.__filesystem_title(filesystem)
        screen(table.draw())
        screen("\n")


    def do_volume_list(self, filesystem_name):
        """volume_list [filesystem name]
        Show all volumes in filesystem, or all filesystem if no filesystem is given."""
        if filesystem_name:
            filesystem = Filesystem.objects.get(name = filesystem_name)
            self.__list_volumes_filesystem(filesystem)
        else:
            for filesystem in Filesystem.objects.all():
                self.__list_volumes_filesystem(filesystem)

    def __list_clients_filesystem(self, filesystem):
        table = Texttable()
        table.header(['id', 'host', 'mount point', 'status'])
        clients = Client.objects.filter(filesystem = filesystem)
        for client in clients:
            table.add_row([client.id, client.host.address, client.mount_point, self.__mountable_audit_status(client)])

        self.__filesystem_title(filesystem)
        screen(table.draw())
        screen("\n")

    def do_client_list(self, filesystem_name):
        """client_list [filesystem]
        Display clients of filesystem, or all clients if no filesystem is given."""
        if filesystem_name:
            filesystem = Filesystem.objects.get(name = filesystem_name)
            self.__list_clients_filesystem(filesystem)
        else:
            for filesystem in Filesystem.objects.all():
                self.__list_clients_filesystem(filesystem)

    def do_server_list(self, line):
        """server_list
        Display all lustre server hosts"""
        table = Texttable()
        table.header(['id', 'address', 'kind', 'lnet status'])
        for host in Host.objects.all():
            if host.mountable_set.count() > 0:
                table.add_row([host.id, host.address, host.role(), AuditHost.lnet_status_string(host)])

        screen(table.draw())

    def do_add_host(self, line):
        """add_host [user@]<hostname>[:port]
        Add a host to be monitored"""
        host, ssh_monitor = SshMonitor.from_string(line)
        host.save()
        ssh_monitor.host = host
        ssh_monitor.save()

    def do_host_list(self, line):
        """host_list
        Display all known hosts"""
        table = Texttable()
        table.header(['id', 'name'])
        for host in Host.objects.all():
            table.add_row([host.id, host.address])
        screen(table.draw())

    def do_test_fake_events(self, line):
        from random import randint
        count = int(line)
        tms = list(TargetMount.objects.all())
        for i in range(0, count):
            tm = tms[randint(0, len(tms))]
            TargetOnlineEvent(target_mount = tm, state = False).save()
            TargetOnlineEvent(target_mount = tm, state = True).save()

    def do_audit_list(self, line):
        for m in Monitor.objects.all():
            from celery.result import AsyncResult
            if m.task_id:
                task_state = AsyncResult(m.task_id).state
            else:
                task_state = ""
            print "%s %s %s %s" % (m.host, m.state, m.task_id, task_state)
    def do_audit_clear(self, line):
        for m in Monitor.objects.all():
            m.update(state = 'idle', task_id = None)



if __name__ == '__main__':
    cmdline = HydraDebug

    if len(sys.argv) == 1:
        try:
            cmdline().cmdloop()
        except KeyboardInterrupt:
            screen("Exiting...")
    else:
        cmdline().onecmd(" ".join(sys.argv[1:]))


@@ -951,81 +951,30 @@
 vol.
-host.address,%0A            vol.name,%0A            AuditMountable.mountable_
+name,%0A            vol.
 stat
@@ -983,19 +983,16 @@
 _string(
-vol
 )%0A      
@@ -1239,24 +1239,16 @@
  'kind',
- 'host',
  'name',
@@ -1277,81 +1277,8 @@
 ry:%0A
-            mgs = ManagementTarget.objects.get(filesystems = filesystem)%0A
     
@@ -1303,16 +1303,27 @@
 ume_row(
+filesystem.
 mgs, tab
@@ -3540,23 +3540,13 @@
 (), 
-AuditHost.lnet_
+host.
 stat
@@ -3555,20 +3555,16 @@
 _string(
-host
 )%5D)%0A%0A   
@@ -3995,16 +3995,26 @@
 , 'name'
+, 'status'
 %5D)%0A     
@@ -4096,16 +4096,38 @@
 .address
+, host.status_string()
 %5D)%0A     

Update debug functions