def getjson(request, datatype): req = request.REQUEST start = req.get('start', None) end = req.get('end', None) daysofweek = req.get('daysofweek', '') reply = req.get('reply', None) lat = bool(req.get('lat', False)) email = req.get('email', None) granularity = req.get('granularity', None) if start: start = parse(start) if end: end = parse(end) if daysofweek: daysofweek = daysofweek.split(",") if reply is not None: reply = bool(reply) curruser = User.objects.get(username=request.user) #ATTENTION: going to need to change the host once we deploy live #conn_string = "host=localhost dbname=liamg user=liamg password=liamg" #connect to db to get the data #conn = psycopg2.connect(conn_string) conn = connection #DEPRECATED: for the sqlite prototype database #conn = sqlite3.connect(curruser.dbname, detect_types=sqlite3.PARSE_DECLTYPES) #get the curruser id so that you can pass it to the functions below curridsql = "select id from accounts where user_id = %s" c = conn.cursor() c.execute(curridsql, (request.user.pk,)) currid = c.fetchone()[0] #get the top people who respond to the user if datatype == 'topsenders': req = request.REQUEST start = req.get('start', None) end = req.get ('end', None) top = 10 email = curruser.username data = topsenders.get_top_senders(top, start, end, email, conn) #get the sent top ten people who the user contacts elif datatype == "topsent": req = request.REQUEST start = req.get('start', None) end = req.get('end', None) top = 10 email = curruser.username data = topsent.get_top_sent(top, start, end, email, conn) #get the rate for a specifc user (filtered) and for the general population elif datatype == "getrate": req = request.REQUEST start = req.get('start', None) end = req.get('end', None) emailAddy = curruser.username replyAddy = req.get('email', None) mode = req.get('mode', None) data = responseRateByTime.get_response_rate(mode, start, end, emailAddy, replyAddy, conn) #use this to get the count in the first graph and maybe the second graph? elif datatype == "byhour": ebh = RepliesByHour() queries = [] queries.append(('y', ebh.get_sql(lat=lat, reply=reply, start=start, end=end, daysofweek=daysofweek, email=email, currid = currid))) ld = LineData() data = ld.get_data(queries, conn) #not sure what this does yet? elif datatype == "contacts": contacts = Contacts() data = contacts.get_data(conn) #get the second graph that shows the response time elif datatype == "getlatency": bd = ByDay() queries = [] queries.append(('y', bd.get_sql(lat=lat, reply=reply, start=start, end=end, granularity=granularity, email=email))) ld = BDLineData() chartdata, maxval = ld.get_data(queries, conn, 0, granularity=granularity, start=start, end=end) data = [chartdata, maxval] #use this to get the small graph next to the top ten elif datatype == "getcount": bd = ByDayNorm() queries = [] #get the queries for the line charts in the top ten queries.append(('y', bd.get_sql(lat=lat, reply=reply, start=start, end=end, granularity=granularity, email=email, currid=currid))) ld = BDLineData() chartdata, maxval = ld.get_data(queries, conn, 1, granularity=granularity, start=start, end=end) data = [chartdata, maxval] else: return HttpResponse('json call not recognized') # return data as json return HttpResponse(json.dumps(data), mimetype="application/json")
def getjson(request, datatype): req = request.REQUEST start = req.get('start', None) end = req.get('end', None) daysofweek = req.get('daysofweek', '') reply = req.get('reply', None) lat = bool(req.get('lat', False)) email = req.get('email', None) granularity = req.get('granularity', None) if start: start = parse(start) if end: end = parse(end) if daysofweek: daysofweek = daysofweek.split(",") if reply is not None: reply = bool(reply) curruser = Userdbs.objects.get(username=request.user) print request.user print curruser.dbname conn = sqlite3.connect(curruser.dbname, detect_types=sqlite3.PARSE_DECLTYPES) # db call to get data if datatype == 'topsenders': req = request.REQUEST start = req.get('start', None) end = req.get ('end', None) top = 10 data = topsenders.get_top_senders(top, start, end, conn) elif datatype == "getrate": req = request.REQUEST start = req.get('start', None) end = req.get('end', None) emailAddy = curruser.username replyAddy = req.get('replyAddy', None) mode = req.get('mode', None) data = responseRateByTime.get_response_rate(mode, start, end, emailAddy, replyAddy, conn) elif datatype == "byhour": ebh = RepliesByHour() queries = [] print "EMAIL", email queries.append(('y', ebh.get_sql(lat=lat, reply=reply, start=start, end=end, daysofweek=daysofweek, email=email))) ld = LineData() data = ld.get_data(queries, conn) elif datatype == "contacts": contacts = Contacts() data = contacts.get_data(conn) elif datatype == "getlatency": bd = ByDay() queries = [] queries.append(('y', bd.get_sql(lat=lat, reply=reply, start=start, end=end, granularity=granularity, email=email))) ld = BDLineData() data = ld.get_data(queries, conn, 0, granularity=granularity, start=start, end=end) elif datatype == "getcount": bd = ByDayNorm() queries = [] queries.append(('y', bd.get_sql(lat=lat, reply=reply, start=start, end=end, granularity=granularity, email=email))) ld = BDLineData() data = ld.get_data(queries, conn, 1, granularity=granularity, start=start, end=end) else: return HttpResponse('json call not recognized') # return data as json return HttpResponse(json.dumps(data), mimetype="application/json")