def add_traceroutes (conn, new_traceroutes_db, be_verbose=False):
    lock=MutexFile(LOCK_FILE)
    if not lock.acquire ():
        return False

    if not conn:
        conn = ixmaps.DBConnect.getConnection()
    
    new_traceroutes = new_traceroutes_db.dictresult()

    insert_into_traits = ""
    insert_into_countries = ""

    for traceroute in new_traceroutes:

        traceroute_id = traceroute['id']

        if be_verbose:
            print traceroute_id

        qres = conn.query ("select * from tr_item where traceroute_id=%i" 
                            % traceroute_id)

        tr_body = qres.dictresult()

        if not tr_body: continue

        (nhops, nattempts) = get_tr_items_dim (tr_body)

        ipaddrs = [None] * nhops
        # ip_info = [None] * nhops

        # print "tr_body:" , tr_body

        for probe in tr_body:
            # print "    probe['hop']:", probe['hop']
            hop = probe['hop']-1

            # --- There are multiple attempts for each hop, but we
            #     only need one, so, after you aquire an IP, ignore
            #     further attempts ---
            if (ipaddrs[hop]):
                continue     
            ipaddrs[hop] = probe['ip_addr']

        # for hop in range (nhops):
            # ip_info[hop] = ixmaps.get_ip_addr_info (ipaddrs[hop], conn)

        ip_info = get_ip_addr_info_list (ipaddrs, conn)

        number_of_ips = len(ip_info)

        is_nsa = False
        # print "ip_info:", ip_info
        # print "len(ip_info):", len (ip_info)
        # print "len(ipaddrs):", len(ipaddrs)
        # print "ipaddrs:", ipaddrs
        # print "number_of_ips:", number_of_ips

        # print "for hop in range (number_of_ips):"
        for hop in range (number_of_ips):
            # print '    ipaddrs[' + str(hop) + ']:', ipaddrs[hop]
            pr_addr = ipaddrs[hop]
            # print (ip_info[hop])
            is_nsa = ixmaps.is_nsa(ip_info[hop])
            # print "    is_nsa:", is_nsa
            if is_nsa == True:
                break

        is_chotel = False
        # print "for hop in range (number_of_ips):"
        for hop in range (number_of_ips):
            pr_addr = ipaddrs[hop]
            # print "    ip_info[" + str(hop) + "]: ", ip_info[hop]
            is_chotel = ixmaps.is_chotel(ip_info[hop])
            if is_chotel == True:
                break

        canada_flag = False
        us_flag = False
        for hop in range (number_of_ips):
            pr_addr = ipaddrs[hop]
            country = ixmaps.get_country (ip_info[hop])
            if country == "CA":
                canada_flag = True
            if country == "US":
                us_flag = True
            if (canada_flag == True) and (us_flag == True):
                break

        # --- Remove, so as not to get an SQL error, just in case some
        #     traits-data for this traceroute already exists ---
        conn.query ("delete from traceroute_traits where id = %i" % traceroute_id)
        conn.query ("""insert into traceroute_traits (id, nsa, hotel)
        values (%i, %s, %s)""" % (traceroute_id, is_nsa, is_chotel))

        # --- Remove, so as not to get an SQL error, just in case some
        #     country-data for this traceroute already exists ---
        if canada_flag or us_flag:
            conn.query("delete from traceroute_countries where traceroute_id = %s" \
                       % (traceroute_id) )

            if canada_flag:
                conn.query ("""insert into traceroute_countries (country_code, traceroute_id)
                values ('CA', %i)""" % traceroute_id)
            if us_flag:
                conn.query ("""insert into traceroute_countries (country_code, traceroute_id)
                values ('US', %i)""" % traceroute_id)

    return True
Example #2
0
def html_traceroute_details(field_names, orig_result_list, link_to,
                            generic_headers_link_to, specific_headers_link_to):
    html = xml_utils()
    doc = ''

    conn = ixmaps.DBConnect.getConnection()
    chotels = ixmaps.CHotels(conn=conn)

    # --- Note: there should be a function to auto-generate these values (es) ---
    attempt_col = 0
    hop_col = 1
    ip_col = 2
    country_col = 3
    rtt_col = 4
    city_col = 6
    override_col = 7
    lat_col = 9
    long_col = 10
    region_col = 11

    result_list = convert_attempts_to_hops_no_rtt(orig_result_list)

    nattempts = len(orig_result_list)
    nhops = result_list[-1][hop_col]
    # nhops = len(result_list)
    rtt = array_2d(nhops, 4)
    ipaddrs = array_2d(nhops, nattempts)
    for probe in orig_result_list:
        hop = probe[hop_col] - 1
        attempt = probe[attempt_col] - 1
        rtt[hop][attempt] = probe[rtt_col]
        ipaddrs[hop][attempt] = probe[ip_col]

    doc += html.tag('table')
    doc += html.tag("tr")

    for field in field_names[1:lat_col]:
        if (field in specific_headers_link_to):
            if specific_headers_link_to[field]:
                field = ("<a href='" + specific_headers_link_to[field] + "'>" +
                         field + "</a>")

        elif generic_headers_link_to:
            field = ("<a href='" + generic_headers_link_to +
                     urllib.quote_plus(str(field)) + "'>" + field + "</a>")

        doc += html.tagged_text('th', field)

    doc += html.end_tag("/tr")

    for hop in range(len(result_list)):
        record = result_list[hop]
        doc += html.tag('tr')
        min_latency = get_min_latency(hop, rtt, nhops)

        country_img = ICON_PREFIX + "clear" + ICON_SUFFIX
        chotel_img = ICON_PREFIX + "clear" + ICON_SUFFIX
        nsa_img = ICON_PREFIX + "clear" + ICON_SUFFIX

        is_chotel = ixmaps.is_chotel(conn=conn,
                                     long_lat=(record[long_col],
                                               record[lat_col]))

        # --- Assign row-icons ---
        if record[country_col]:
            country_img = ICON_PREFIX + record[country_col] + ICON_SUFFIX
        if is_chotel: chotel_img = ICON_PREFIX + "carrierhotel" + ICON_SUFFIX
        nsa_img = ICON_PREFIX + get_nsa_flag(
            conn=conn,
            long_lat=(record[long_col], record[lat_col])) + ICON_SUFFIX

        for item_num in range(1, len(record)):
            if item_num == country_col:
                doc += html.tag('td align="center" style="white-space:nowrap"')

                doc += html.empty_tag(
                    'img width="10" src="' + country_img + '"',
                    'img width="10" src="' + chotel_img + '"',
                    'img width="10" src="' + nsa_img + '"')
                doc += html.end_tag("/td")

            elif item_num == rtt_col:
                doc += html.tag('td')
                doc += html.indent(min_latency)
                doc += html.end_tag("/td")

            elif item_num == city_col:
                doc += html.tag('td')
                doc += html.text(record[city_col])
                doc += html.text(record[region_col])
                doc += html.end_tag("/td")

            elif item_num == override_col:
                geo_precision = get_geo_precision(record[override_col],
                                                  record[lat_col],
                                                  record[long_col])
                doc += html.tag('td')
                doc += html.indent(geo_precision)
                # doc += html.indent (record[override_col] )
                # doc += html.indent (record[lat_col] )
                # doc += html.indent (record[long_col] )
                doc += html.end_tag("/td")

            elif (item_num >= lat_col):
                pass

            else:
                doc += html.tagged_text('td', record[item_num])
        doc += html.end_tag("/tr")

    doc += html.end_tag("/table")

    return doc
def html_traceroute_details (field_names, orig_result_list, link_to,
                             generic_headers_link_to, specific_headers_link_to):
    html = xml_utils ( )
    doc = ''

    conn = ixmaps.DBConnect.getConnection ( )
    chotels = ixmaps.CHotels (conn=conn)

    # --- Note: there should be a function to auto-generate these values (es) ---
    attempt_col  = 0
    hop_col      = 1
    ip_col       = 2
    country_col  = 3
    rtt_col      = 4
    city_col     = 6
    override_col = 7
    lat_col      = 9
    long_col     = 10
    region_col   = 11

    result_list = convert_attempts_to_hops_no_rtt (orig_result_list)

    nattempts = len(orig_result_list)
    nhops = result_list[-1][hop_col]
    # nhops = len(result_list)
    rtt = array_2d(nhops,4)
    ipaddrs = array_2d(nhops, nattempts)
    for probe in orig_result_list:
        hop = probe[hop_col]-1
        attempt = probe[attempt_col]-1
        rtt[hop][attempt] = probe[rtt_col]
        ipaddrs[hop][attempt] = probe[ip_col]

    doc += html.tag ("table")
    doc += html.tag ("tr")

    for field in field_names[1:lat_col]:
        if (field in specific_headers_link_to):
            if specific_headers_link_to[field]:
                field = ("<a href='" + specific_headers_link_to[field]
                         + "'>" + field + "</a>")

        elif generic_headers_link_to:
            field = ("<a href='" + generic_headers_link_to +
                     urllib.quote_plus ( str(field) )
                     + "'>" + field + "</a>")

        doc += html.tagged_text ("th", field)

    doc += html.end_tag ("/tr")

    for hop in range ( len (result_list) ):
        record = result_list[hop]
        doc += html.tag ("tr")
        min_latency = get_min_latency(hop, rtt, nhops)

        country_img = ICON_PREFIX + "clear" + ICON_SUFFIX
        chotel_img = ICON_PREFIX + "clear" + ICON_SUFFIX
        nsa_img = ICON_PREFIX + "clear" + ICON_SUFFIX

        is_chotel = ixmaps.is_chotel (
            conn=conn, long_lat=(record[long_col],record[lat_col] ) )

        # --- Assign row-icons ---
        if record[country_col]: country_img = ICON_PREFIX + record[country_col] + ICON_SUFFIX
        if is_chotel: chotel_img = ICON_PREFIX + "carrierhotel" + ICON_SUFFIX
        nsa_img = ICON_PREFIX + get_nsa_flag (
            conn=conn, long_lat=(record[long_col],record[lat_col] ) ) + ICON_SUFFIX

        for item_num in range (1, len(record) ):
            if item_num == country_col:
                doc += html.tag ('td align="center" style="white-space:nowrap"')

                doc += html.empty_tag ('img width="10" src="' + country_img + '"', 
                               'img width="10" src="' + chotel_img + '"',
                               'img width="10" src="' + nsa_img + '"')
                doc += html.end_tag ("/td")

            elif item_num == rtt_col:
                doc += html.tag ("td")
                doc += html.indent (min_latency)
                doc += html.end_tag ("/td")

            elif item_num == city_col:
                doc += html.tag ("td")
                doc += html.text ( record[city_col] )
                doc += html.text ( record[region_col] )
                doc += html.end_tag ("/td")

            elif item_num == override_col:
                geo_precision = get_geo_precision (
                    record[override_col], record[lat_col], record[long_col] )
                doc += html.tag ("td")
                doc += html.indent (geo_precision)
                # doc += html.indent (record[override_col] )
                # doc += html.indent (record[lat_col] )
                # doc += html.indent (record[long_col] )
                doc += html.end_tag ("/td")

            elif (item_num >= lat_col):
                pass

            else:
                doc += html.tagged_text ("td", record[item_num])
        doc += html.end_tag ("/tr")

    doc += html.end_tag ("/table")

    return doc