import json
import math
import random
import requests

url = 'http://localhost:3000/xychart'

def r():
    #return random.randrange(-1000, 1000)
    magnitude = random.randrange(0, 1000)
    magnitude = (magnitude * magnitude) / 1000
    sign = random.choice([1, -1])
    return magnitude * sign

colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']

def rc():
    return random.choice(colors)

fn = ['alice', 'bob', 'charlie', 'dave', 'ed', 'fred', 'george']
ln = ['zulu', 'yankee', 'xray', 'whiskey', 'victor', 'uniform']

def rn():
    return random.choice(fn) + '.' + random.choice(ln) + '.' + str(random.randrange(1000,9999))

num_points = 500
points = []
for i in range(num_points):
    new_point = {'name': rn(), 'x': r(), 'y': r(), 'z': r(), 'color': rc()}
    print('New point:', new_point)
    points.append(new_point)

num_connections = 50
connections = []
def rp():
    return int(random.random() * len(points))

for i in range(num_connections):
    new_connection = [rp(), rp()]
    connections.append(new_connection)

chart_spec = {
    'title': 'Test data generated by testbig.py',
    'banner': 'job:joel54 iteration:32 chi^2: 332.5',
    'points': points,
    'connections': connections
}

payload = json.dumps(chart_spec)
print('Data length:', len(payload))
#print 'Posting:', data

status = requests.post(url,
                        data=payload,
                        headers={'Content-Type': 'application/json'});
print('Response:', status)
