#!/usr/bin/env python
# encoding: utf-8
"""
idcview.py

Created by QingFeng on 2008-03-17.
Copyright (c) 2007 xBayDNS Team. All rights reserved.
"""

from decimal import Decimal
from operator import itemgetter

def pingtype_weight(pingtype,v,data_value=-1):
    if data_value == -1:data_value=v
    pingtype=pingtype.upper()
    rule={
        'OUT_OF_REACH':0,
        'other':(data_value+v)/2,
    }
    return rule.get(pingtype,rule.get('other'))
    
def convfiles(files):
    data={}
    for f in files:
        for r in open(f):
            #OUT_OF_REACH,PING_GATEWAY,-1
            if r.strip()=='':
                continue
            ip,pingtype,pingavg,pingtime=map(lambda x:x.strip(),r.split(','))
            if ip not in data:
                data[ip]=pingtype_weight(pingtype,Decimal(pingavg),-1 )
            else:
                data[ip]=pingtype_weight(pingtype,Decimal(pingavg),data[ip] )
    data=sorted(data.items(),key=itemgetter(1))
    return data
    

@@ -202,16 +202,27 @@
 emgetter
+%0Aimport sys
 %0A%0Adef pi
@@ -762,32 +762,33 @@
        data%5Bip%5D=
+%5B
 pingtype_weight(
@@ -816,13 +816,10 @@
 avg)
-,-1 
 )
+%5D
 %0A   
@@ -857,17 +857,24 @@
 data%5Bip%5D
-=
+.append(
 pingtype
@@ -910,17 +910,54 @@
 avg)
-,
+))%0A    for ip in data.keys():%0A        
 data%5Bip%5D
  )%0A 
@@ -952,17 +952,22 @@
 data%5Bip%5D
- 
+.sort(
 )%0A    da
@@ -1025,12 +1025,314 @@
 n data%0A    %0A
+agentfiles = sys.argv%5B1:%5D%0Adatafile = %22idcview_out.txt%22%0Adatafile_obj = open(datafile, %22w%22)%0Adata = convfiles(agentfiles)%0Afor ip, latencies in data:%0A    datafile_obj.write(%22%25s%22%25ip)%0A    for latency in latencies:%0A        datafile_obj.write(%22,%25.3f%22%25latency)%0A    datafile_obj.write('%5Cn')%0Adatafile_obj.close()%0A

convert to the data structure qingfeng required.