def node_info_set_timeline(request, parameter):
    '''
     Parameter received as (metric/node_name.network.interface_name.attribute/start_time0=...)
    '''
    server_ip = util.SERVER_IP
    server_port = util.SERVER_PORT

    (metric_nodeid, resource, resource_spec1, attribute_arguments) = string.split(parameter, '.')

    metric, node_id = metric_nodeid.split("/")
    attribute, arguments = attribute_arguments.split("/")

    parameter = metric_nodeid+'.'+resource+'.'+resource_spec1+'.'+attribute

    value_type= resource+"."+resource_spec1+"."+attribute

    document = fetchdocument.fetch_most_recent_document(node_id)
    name = documentparser.get_value(document, "name")


    arg_dict = util.split_arguments_return_dict(arguments)

    values_graph = getview.get_view_node_id_attribute_timeline(node_id, value_type, start_time =arg_dict['start_time_epoch'],
                                                                end_time=arg_dict['end_time_epoch'], limit=arg_dict['limit'])


    #values_graph = json.dumps(values)
    return render_to_response('node_info_set_timeline.html',{ 'name':name,'nodeid':node_id, 'value': resource_spec1+" "+attribute,
                                                          'server_ip': server_ip, 'server_port': server_port,
                                                          'parameter':parameter,  'arguments':arg_dict, 'values_graph':values_graph},context_instance=RequestContext(request))
def async_node_attribute(request, parameter):
    '''
       Parameter of form metric/node/
    '''
    (metric,node_id,time) =parameter.split('/')
    document = fetchdocument.fetch_most_recent_document(node_id)
    name = documentparser.get_value(document, "name")


    server_ip = util.SERVER_IP
    server_port = util.SERVER_PORT

    return render_to_response('async_node_info_timeline.html',{'server_ip': server_ip, 'server_port': server_port,'name':name, 'nodeid':node_id, 'metric':metric})
def async_aggr_set_node_attribute(request, parameter):
    '''
       Parameter of form set//node//interface//metric//
    '''
    (set,node_id,interface,metric,time) =parameter.split('//')

    document = fetchdocument.fetch_most_recent_document(node_id)
    name = documentparser.get_value(document, "name")


    server_ip = util.SERVER_IP
    server_port = util.SERVER_PORT

    return render_to_response('async_aggregate_set.html',{'server_ip': server_ip, 'server_port': server_port,'name':name, 'nodeid':node_id, 'metric':metric, 'interface': interface, 'set':set})
def get_view_sliver_most_recent_attribute_treemap( node_id, value_type):
    '''
    Document returned from view sliver-timestamp/get_sliver-timestamp
    key = [sliver_id, server_timestamp],  value= {'sliver': sliverinfo,'nodeid':nodeid,'server_timestamp': server_timestamp}

    returns
    '''

    log.debug("Get most recent sliver attributes for Node: %s" %node_id)
    all_values = [['Id', 'parent', 'metricvalue'], [value_type, '', 0]]

    document = fetchdocument.fetch_most_recent_document(node_id)
    
    if(document):

        slivers = document['slivers']

        for container in slivers:
            sliver = slivers[container]
            sliver_id = documentparser.get_value(sliver, 'sliver_name')
            value = documentparser.get_value(sliver, value_type)
            all_values.append([sliver_id, value_type, value])

    return all_values
def node_slivers (request, parameter):

    server_ip = util.SERVER_IP
    server_port = util.SERVER_PORT

    all_values = []
    values_graph = []
    network_values = []
    disk_values = []
    memory_values=[]
    node_in_db = False
    name= parameter

    node_id = parameter
    document = fetchdocument.fetch_most_recent_document(node_id)
    slivers = documentparser.get_value(document, 'slivers')
    print " NODE ID: ", node_id


    count = 0
    if(document):
        node_in_db = True
        if slivers:
            for container in slivers:
                sliver= slivers[container]
                count +=1
                sliver_name = documentparser.get_value(sliver, 'sliver_name')
                sliver_cpu_usage = documentparser.get_value(sliver,'sliver_cpu_usage')
                sliver_slice_name = documentparser.get_value(sliver, 'sliver_slice_name')
                sliver_total_memory = documentparser.get_value(sliver, 'sliver_total_memory')
                sliver_total_memory_free = documentparser.get_value(sliver, 'sliver_total_memory_free')
                sliver_total_memory_percent_used = documentparser.get_value(sliver, 'sliver_total_memory_percent_used')
                sliver_ip = documentparser.get_value(sliver, 'sliver_ip')
                sliver_state = documentparser.get_value(sliver, 'sliver_state')

                sliver_total_memory, sliver_total_memory_free = util.convert_bytes_to_human_readable([sliver_total_memory, sliver_total_memory_free])

                all_values.append({'sliver_name': sliver_name, 'sliver_cpu_usage':sliver_cpu_usage, 'sliver_slice_name':sliver_slice_name,
                               'sliver_total_memory':sliver_total_memory, 'sliver_total_memory_free': sliver_total_memory_free,
                               'sliver_total_memory_percent_used':sliver_total_memory_percent_used, 'sliver_ip':sliver_ip,
                               'sliver_state': sliver_state,'serial':count})


                 # Populate Treemap graph
                values = getview.get_view_sliver_most_recent_attribute_treemap( node_id, 'sliver_cpu_usage')
                values_graph = json.dumps(values)


        #Network Values
        name = documentparser.get_value(document, "name")
        nodeid = documentparser.get_value(document, "nodeid")
        network_values= documentparser.get_set(document, "network")

        #Disk Values
        disk_values= documentparser.get_set(document, "disk")

        #Disk Values
        memory_values= documentparser.get_set(document, "memory")

    return render_to_response('node_slivers.html',{'disk_values':disk_values, 'all_values':all_values,
                                                   'values_graph':values_graph, 'network_values':network_values,'nodeid':node_id,
                                                   'name':name, 'server_ip': server_ip, 'server_port': server_port,
                                                   'numberslivers': count, 'node_in_db':node_in_db, 'memory_values': memory_values},
                                                    context_instance=RequestContext(request))
def get_view_inter_node_trace(node_id):
    document = fetchdocument.fetch_most_recent_document(node_id)
    if(document):
        return document