示例#1
0
def _status2Kml(db):
    #prepare kml
    import kml as kml_lib
    kml = kml_lib.cmip5()



    #sets
    institutes = set()
    datanodes = set()
    gateways = set()
    inst2node = set()
    node2gate = set()

    #add all relations
    for institute, model, count, size, filecount, datanode, type, gateway in getCurrentStatus(db):
        institutes.add(institute)
        datanodes.add(datanode)
        gateways.add(gateway)
        inst2node.add((institute,datanode))
        node2gate.add((datanode,gateway))

    #get geo objects
    import geo_utils
    #*** We asume geo table is in the same DB   ***
    db_geo = geo_utils.GeoDB(str(db._engine.url))    
    db_geo.open()
    point = {}
#    for item in institutes.union(datanodes).union(gateways):
#        point[item] = geo_utils.GeoDAO(name=item)
    t = lambda **dic: dic
    for geo_item in db_geo.addAll([geo_utils.GeoDAO(name=item) for item in institutes.union(datanodes).union(gateways)]):
        point[geo_item.name] = geo_item
        if not geo_item.lat:
            log.info('No geo coordinates for %s', geo_item)
            continue
        #put placemark in place
        if geo_item in gateways: style = 'gateway'
        elif geo_item in datanodes: style = 'datanode'
        elif geo_item in institutes: style = 'institute'
        #Add placemarks
        kml['placemarks'].append(t(name=geo_item.name,description=style,style=style,longitude=geo_item.lon, latitude=geo_item.lat))
    
    #now add relations
    for institute,datanode in inst2node:
        if not point[institute].lat or not point[datanode].lat: continue
            
        kml['lines'].append(t(name='{0}->{1}'.format(institute, datanode),style='institute2datanode',\
points=[t(longitude=point[institute].lon, latitude=point[institute].lat), t(longitude=point[datanode].lon, latitude=point[datanode].lat)]))
    
    for datanode, gateway in node2gate:
        if not point[gateway].lat or not point[datanode].lat: continue

        kml['lines'].append(t(name='{0}->{1}'.format(datanode, gateway),style='datanode2gateway',\
points=[t(longitude=point[datanode].lon, latitude=point[datanode].lat), t(longitude=point[gateway].lon, latitude=point[gateway].lat)]))

    return  kml.getXML().toprettyxml('','')
示例#2
0
def createKML():
    # *** This is broken as of now. ****
    raise Exception('Method is broken')

    import kml as kml_lib
    kml = kml_lib.cmip5()

    set_gateways = set(['BADC', 'PCMDI'])
    set_institutes = set()
    set_servers = set()
    set_inst_to_server = set()
    set_server_to_gateway = set()
    
    for gateway in set_gateways:
        glog.debug('Analyzing gateway: %s', gateway)
        for collection in GW[gateway]['collections']:
            glog.debug('Collection: %s', collection)
            cmd = '-g {0} -od --parent {1}'.format(gateway, collection)
            glog.debug('Running cmd: %s', cmd)
            res = main(cmd.split(' '))
            all_drs = {}
            from drs import DRS
            for r in res:
                drs = DRS(id=r['id'])
                if drs.institute not in all_drs:
                    glog.debug('New Institute: %s', drs.institute)
                    set_institutes.add(drs.institute)
                    cmd = '-g {0} -of --parent {1}'.format(gateway, drs.getId())
                    glog.debug('Running cmd: %s', cmd)
                    res = main(cmd.split(' '))
                    #representative file
                    servers = []
                    all_drs[drs.institute] = {'file': res[0], 'drs':drs, 'server':servers}
                    #get al known servers for this file
                    for endpoint in res[0]['endpoints']:
                        if endpoint['server'] not in servers:
                            servers.append(endpoint)
                            set_servers.add(endpoint['server'])
                            set_inst_to_server.add((drs.institute, endpoint['server']))
                            set_server_to_gateway.add((endpoint['server'], gateway))


    print set_gateways, set_institutes, set_servers, set_inst_to_server, set_server_to_gateway
    from geo_utils import GeoDB
    import re
    pat = re.compile('^[^/]*//([^/]*)(/.*)?$')
    g = GeoDB('sqlite:///geo.db')
    t = lambda **dic: dic

#    for gateway in set_gateways:
#        url = pat.match(GW[gateway]['url']).group(1)
#        geo = GeoIPDAO(name=url)
#        geo = g.addAll([geo])[0]
#        kml['placemarks'].append(t(name='ESG-' + gateway,description=url,style='gateway',longitude=geo.lon, latitude=geo.lat))
#
#    for server in set_servers:
#        url = server
#        geo = GeoIPDAO(name=url)
#        geo = g.addAll([geo])[0]
#        kml['placemarks'].append(t(name='DN@' + server,description=url,style='node',longitude=geo.lon, latitude=geo.lat))
#
#    for server, gateway in set_server_to_gateway:
#        url_server = server
#        geo_server = g.addAll([GeoIPDAO(name=url_server)])[0]
#        url_gw = pat.match(GW[gateway]['url']).group(1)
#        geo_gw = g.addAll([GeoIPDAO(name=url_gw)])[0]
#        kml['lines'].append(t(name='DN@{0} -> ESG-{1}'.format(server, gateway),style='nodeToGateway',points=[t(longitude=geo_server.lon, latitude=geo_server.lat), t(longitude=geo_gw.lon, latitude=geo_gw.lat)]))

    print kml.getXML().toprettyxml('','')