import argparse
import csv

from postgresql import get_postgres_connection

parser = argparse.ArgumentParser(
    description="Insert IPs + times from one of Sam's DNS csv files")
parser.add_argument("config", type=str, help="DB config file")
parser.add_argument("csv", type=str, help="Input CSV file")
args = parser.parse_args()

with open(args.config, "r") as db_config:
    connection = get_postgres_connection(db_config)
cursor = connection.cursor()

hops = []
with open(args.csv, "r") as file:
    reader = csv.reader(file)
    for line in reader:
        hops.append((line[1], line[3], float(line[4])))

args_str = ",".join(("('%s', '%s', %f)" % hop) for hop in hops)
cursor.execute("INSERT INTO hops VALUES " + args_str)
cursor.commit()
connection.close()
parser.add_argument("--max-lng", type=float, default=180, help="Maximum longitude.")
parser.add_argument("--max-lat", type=float, default=90, help="Maximum latitude.")
parser.add_argument("--min-lng", type=float, default=-180, help="Minimum longitude.")
parser.add_argument("--min-lat", type=float, default=-90, help="Minimum latitude.")
parser.add_argument("-p", "--dpi", type=int, default=300, help="Chart DPI")
parser.add_argument("-e", "--exponent", type=float, default=0.25, help="Exponent used for data processing, improves graph"
																	" contrast at cost of accuracy.")
parser.add_argument("--min-connectivity", type=float, default=0.003, help="Minimum ms/km required for display on"
																		  " map. Useful for removing outliers.")
parser.add_argument("--max-connectivity", type=float, default=0.03, help="Maximum ms/km required for display on"
																		 " map. Useful for removing outliers.")
args = parser.parse_args()
vprint = print if args.verbose else lambda *x, **y: None

with open(args.dbconfig, "r") as dbconfig:
	connection = postgresql.get_postgres_connection(dbconfig)
cursor = connection.cursor()

vprint("Grabbing connectivity data")
cursor.execute("SELECT quad, center(quad), connect_avg FROM quads"
						 " WHERE box(point(?, ?), point(?, ?)) @> quad",
						 (args.min_lat, args.min_lng, args.max_lat, args.max_lng))
results = cursor.fetchall()
connection.close()

vprint("Loaded {} data points".format(len(results)))
# Convert from string quads to numbers
data = []
for result in results:
	quad = list(map(lambda x: float(x), result[0].replace("(", "").replace(")", "").split(',')))
	quad.extend(list(map(lambda x: float(x), result[1].replace("(", "").replace(")", "").split(','))))