# Path: umit/icm/agent/logger.py
# LOGLEVEL = 'DEBUG'
# 
# Path: umit/icm/agent/utils/Network.py
# def convert_ip(ip):
#     """Receives the IP as a list of ints:
#     [192, 168, 0, 0] representing "192.168.0.0"
#     
#     Or, receives as a string, and converts it for internal use:
#     "192.168.0.0" to [192, 168, 0, 0] and then uses it
#     """
#     ip = sanitize_ip(ip)
#     int_ip = 0
#     int_ip |= (ip[0] << 24)
#     int_ip |= (ip[1] << 16)
#     int_ip |= (ip[2] << 8)
#     int_ip |= ip[3]
#     
#     return int_ip


import os
import time
import base64
import cPickle
import sqlite3
from types import StringTypes
from umit.icm.agent.logger import g_logger
from umit.icm.agent.utils.Network import convert_ip
        from umit.icm.agent.gui.dashboard.DashboardListBase import TASK_ALL,TASK_SUCCESSED,TASK_FAILED
        from umit.icm.agent.test import TASK_STATUS_DONE,TASK_STATUS_FAILED
        from umit.icm.agent.gui.dashboard.DashboardListBase import REPORT,REPORT_SENT,REPORT_UNSENT,REPORT_RECEIVED
        from umit.icm.agent.gui.dashboard.DashboardListBase import CAPA_THROTTLED,CAPACITY,CAPA_SERVICE
        from umit.icm.agent.test import TASK_STATUS_DONE,TASK_STATUS_FAILED
        from umit.icm.agent.gui.dashboard.DashboardListBase import CAPA_THROTTLED,CAPACITY,CAPA_SERVICE
        from umit.icm.agent.gui.dashboard.DashboardListBase import REPORT,REPORT_SENT,REPORT_UNSENT,REPORT_RECEIVED
        from umit.icm.agent.gui.dashboard.DashboardListBase import TASK_SUCCESSED, TASK_FAILED,TASK_ALL,TASK
    from umit.icm.agent.BasePaths import *

    def get_status(self,key,default = "0"):
        """
        """
        try:
            result = self.db_conn.select("select value from stats where key=?",(key,))
            return result[0]
        except:
            g_logger.warning("No value found for key '%s' in SET stats." % key)
            return default
    
    #########################
    #Get test sets by version
    def get_tests_by_version(self,test_version):
        """
        """
        test_version = int(test_version)
        test_sets = []
        try:
            rs = self.db_conn.select("select * from tasks where test_id =?",(test_version,))
            for record in rs:
                test_result_dict = {}
                test_result_dict["test_id"]         = record[1]
                test_result_dict["website_url"]     = record[2]
                test_result_dict["test_type"]       = record[3]
                test_result_dict["service_name"]    = record[4]
                test_result_dict["service_port"]    = record[5]
                test_result_dict["service_ip"]      = record[6]
                test_sets.append(test_result_dict)
            return test_sets
        except Exception,data:

# Path: umit/icm/agent/logger.py
# LOGLEVEL = 'DEBUG'
# 
# Path: umit/icm/agent/utils/Network.py
# def convert_ip(ip):
#     """Receives the IP as a list of ints:
#     [192, 168, 0, 0] representing "192.168.0.0"
#     
#     Or, receives as a string, and converts it for internal use:
#     "192.168.0.0" to [192, 168, 0, 0] and then uses it
#     """
#     ip = sanitize_ip(ip)
#     int_ip = 0
#     int_ip |= (ip[0] << 24)
#     int_ip |= (ip[1] << 16)
#     int_ip |= (ip[2] << 8)
#     int_ip |= ip[3]
#     
#     return int_ip


# Path: umit/icm/agent/db.py
import os
import time
import base64
import cPickle
import sqlite3
from types import StringTypes
from umit.icm.agent.logger import g_logger
from umit.icm.agent.utils.Network import convert_ip
        from umit.icm.agent.gui.dashboard.DashboardListBase import TASK_ALL,TASK_SUCCESSED,TASK_FAILED
        from umit.icm.agent.test import TASK_STATUS_DONE,TASK_STATUS_FAILED
        from umit.icm.agent.gui.dashboard.DashboardListBase import REPORT,REPORT_SENT,REPORT_UNSENT,REPORT_RECEIVED
        from umit.icm.agent.gui.dashboard.DashboardListBase import CAPA_THROTTLED,CAPACITY,CAPA_SERVICE
        from umit.icm.agent.test import TASK_STATUS_DONE,TASK_STATUS_FAILED
        from umit.icm.agent.gui.dashboard.DashboardListBase import CAPA_THROTTLED,CAPACITY,CAPA_SERVICE
        from umit.icm.agent.gui.dashboard.DashboardListBase import REPORT,REPORT_SENT,REPORT_UNSENT,REPORT_RECEIVED
        from umit.icm.agent.gui.dashboard.DashboardListBase import TASK_SUCCESSED, TASK_FAILED,TASK_ALL,TASK
    from umit.icm.agent.BasePaths import *

    def get_status(self,key,default = "0"):
        """
        """
        try:
            result = self.db_conn.select("select value from stats where key=?",(key,))
            return result[0]
        except:
            g_logger.warning("No value found for key '%s' in SET stats." % key)
            return default
    
    #########################
    #Get test sets by version
    def get_tests_by_version(self,test_version):
        """
        """
        test_version = int(test_version)
        test_sets = []
        try:
            rs = self.db_conn.select("select * from tasks where test_id =?",(test_version,))
            for record in rs:
                test_result_dict = {}
                test_result_dict["test_id"]         = record[1]
                test_result_dict["website_url"]     = record[2]
                test_result_dict["test_type"]       = record[3]
                test_result_dict["service_name"]    = record[4]
                test_result_dict["service_port"]    = record[5]
                test_result_dict["service_ip"]      = record[6]
                test_sets.append(test_result_dict)
            return test_sets
        except Exception,data:

            g_logger.warning("Wrong in get Test sets from database %s,%s" % (Exception,data))
