def vis_json_dist_earnings(request): description = { "earning": ("string", _("Earning")), "acount": ("number", _("Users")), } data = list() earning_values = EggHead.objects.values_list("earning_total", flat=True) (n, bins) = numpy.histogram(earning_values, bins=15, normed=False) for k in xrange(len(n)): data.append({ "earning": "%d" % (int(bins[k]) / 10 * 10), "acount": n[k] }) data_table = gviz_api.DataTable(description) data_table.LoadData(data) try: rid = int(request.GET["tqx"].split(":")[1].strip()) # dblogger.debug("reqId is acquired from GET data: %d" % rid) return HttpResponse( data_table.ToJSonResponse(columns_order=("earning", "acount"), req_id=rid)) except Exception, e: dblogger.debug(e) dblogger.debug("reqId is not found in GET, use 4 instead") return HttpResponse( data_table.ToJSonResponse(columns_order=("earning", "acount"), req_id=4))
def vis_json_answer_rating(request, username): description = { "rating": ("string", _("Rating")), "acount": ("number", _("Answers")), } auser = User.objects.get(username=username) data = [] for k in xrange(5): acount = Answer.objects.filter(answerer=auser, rating_by_asker=(k + 1)).count() data.append({"rating": k + 1, "acount": acount}) data_table = gviz_api.DataTable(description) data_table.LoadData(data) try: rid = int(request.GET["tqx"].split(":")[1].strip()) # dblogger.debug("reqId is acquired from GET data: %d" % rid) return HttpResponse( data_table.ToJSonResponse(columns_order=("rating", "acount"), req_id=rid)) except Exception, e: dblogger.debug(e) dblogger.debug("reqId is not found in GET, use 3 instead") return HttpResponse( data_table.ToJSonResponse(columns_order=("rating", "acount"), req_id=3))
def vis_json_tag_cloud_news(request): description = { "label": ("string", "Label"), "value": ("number", "Value"), "link": ("string", "Link"), } data = [] tags = list(NewsTag.objects.order_by("-count")[:100]) random.shuffle(tags) for tag in tags: data.append({ "label": tag.name, "value": tag.count, "link": reverse("inews_tag", kwargs={"tag_id": tag.id}) }) data_table = gviz_api.DataTable(description) data_table.LoadData(data) try: rid = int(request.GET["tqx"].split(":")[1].strip()) # dblogger.debug("reqId is acquired from GET data: %d" % rid) return HttpResponse( data_table.ToJSonResponse(columns_order=("label", "value", "link"), req_id=rid)) except Exception, e: dblogger.debug(e) dblogger.debug("reqId is not found in GET, use 0 instead") return HttpResponse( data_table.ToJSonResponse(columns_order=("label", "value", "link"), req_id=0))
def vis_json_inout_pie(request): description = { "flow": ("string", "Flow"), "amount": ("number", "Amount"), } isum = get_egghead_from_user(request.user).total_income() osum = get_egghead_from_user(request.user).total_spending() data = [{ "flow": _("Inflow"), "amount": isum }, { "flow": _("Outflow"), "amount": osum }] data_table = gviz_api.DataTable(description) data_table.LoadData(data) try: rid = int(request.GET["tqx"].split(":")[1].strip()) # dblogger.debug("reqId is acquired from GET data: %d" % rid) return HttpResponse( data_table.ToJSonResponse(columns_order=("flow", "amount"), req_id=rid)) except Exception, e: dblogger.debug(e) dblogger.debug("reqId is not found in GET, use 1 instead") return HttpResponse( data_table.ToJSonResponse(columns_order=("flow", "amount"), req_id=1))
def vis_json_qa_price_vis(request): description = { "date": ("string", _("Date")), "qprice": ("number", _("Average price of questions")), "aprice": ("number", _("Average awards an answer receives")), } adate = date.today() min_dt = Question.objects.aggregate( min_datetime=Min("time_stamp"))["min_datetime"] min_date = min_dt.date() data = [] for k in xrange(365): # dblogger.debug("Date: %s" % str(adate)) qprice = Question.objects.filter( time_stamp__year=adate.year, time_stamp__month=adate.month, time_stamp__day=adate.day).aggregate( qprice=Avg("points_offered"))["qprice"] aprice = Answer.objects.filter( time_stamp__year=adate.year, time_stamp__month=adate.month, time_stamp__day=adate.day).aggregate( aprice=Avg("points_received"))["aprice"] if not qprice: qprice = 0 if not aprice: aprice = 0 # dblogger.debug("Questions: %.2f" % qprice) # dblogger.debug("Answers: %.2f" % aprice) data.append({ "date": adate.strftime("%m-%d"), "qprice": qprice, "aprice": aprice }) adate = adate - timedelta(days=1) if adate < min_date: break data.reverse() data_table = gviz_api.DataTable(description) data_table.LoadData(data) try: rid = int(request.GET["tqx"].split(":")[1].strip()) # dblogger.debug("reqId is acquired from GET data: %d" % rid) return HttpResponse( data_table.ToJSonResponse(columns_order=("date", "qprice", "aprice"), req_id=rid)) except Exception, e: dblogger.debug(e) dblogger.debug("reqId is not found in GET, use 1 instead") return HttpResponse( data_table.ToJSonResponse(columns_order=("date", "qprice", "aprice"), req_id=1))
def vis_json_qa_vis(request): description = { "date": ("date", "Date"), "questions": ("number", "Questions"), "title1": ("string", "Title1"), "text1": ("string", "Text1"), "answers": ("number", "Answers"), "title2": ("string", "Title2"), "text2": ("string", "Text2"), } adate = date.today() min_dt = Question.objects.aggregate( min_datetime=Min("time_stamp"))["min_datetime"] min_date = min_dt.date() data = [] for k in xrange(365): # dblogger.debug("Date: %s" % str(adate)) qcount = Question.objects.filter(time_stamp__year=adate.year, time_stamp__month=adate.month, time_stamp__day=adate.day).count() acount = Answer.objects.filter(time_stamp__year=adate.year, time_stamp__month=adate.month, time_stamp__day=adate.day).count() # dblogger.debug("Questions: %d" % qcount) # dblogger.debug("Answers: %d" % acount) if qcount > 0 or acount > 0: data.append({ "date": adate, "questions": qcount, "answers": acount }) adate = adate - timedelta(days=1) if adate < min_date: break data_table = gviz_api.DataTable(description) data_table.LoadData(data) try: rid = int(request.GET["tqx"].split(":")[1].strip()) # dblogger.debug("reqId is acquired from GET data: %d" % rid) return HttpResponse( data_table.ToJSonResponse( columns_order=("date", "questions", "title1", "text1", "answers", "title2", "text2"), req_id=rid)) except Exception, e: dblogger.debug(e) dblogger.debug("reqId is not found in GET, use 0 instead") return HttpResponse( data_table.ToJSonResponse( columns_order=("date", "questions", "title1", "text1", "answers", "title2", "text2"), req_id=0))
def vis_json_transactions_vis(request): description = { "date": ("date", "Date"), "tcount": ("number", _("Number of transactions")), "title1": ("string", "Title1"), "text1": ("string", "Text1"), "tamount": ("number", _("Transaction amount (x10)")), "title2": ("string", "Title2"), "text2": ("string", "Text2"), } adate = date.today() min_dt = Question.objects.aggregate( min_datetime=Min("time_stamp"))["min_datetime"] min_date = min_dt.date() data = [] for k in xrange(365): # dblogger.debug("Date: %s" % str(adate)) tcount = Transaction.objects.filter(time_stamp__year=adate.year, time_stamp__month=adate.month, time_stamp__day=adate.day).count() tamount = Transaction.objects.filter( time_stamp__year=adate.year, time_stamp__month=adate.month, time_stamp__day=adate.day).aggregate( tamount=Sum("amount"))["tamount"] if not tamount: tamount = 0 data.append({"date": adate, "tcount": tcount, "tamount": tamount / 10}) adate = adate - timedelta(days=1) if adate < min_date: break data_table = gviz_api.DataTable(description) data_table.LoadData(data) try: rid = int(request.GET["tqx"].split(":")[1].strip()) # dblogger.debug("reqId is acquired from GET data: %d" % rid) return HttpResponse( data_table.ToJSonResponse( columns_order=("date", "tcount", "title1", "text1", "tamount", "title2", "text2"), req_id=rid)) except Exception, e: dblogger.debug(e) dblogger.debug("reqId is not found in GET, use 2 instead") return HttpResponse( data_table.ToJSonResponse( columns_order=("date", "tcount", "title1", "text1", "tamount", "title2", "text2"), req_id=2))
def vis_json_answer_thumbs(request, username): description = { "thumbs": ("string", _("Thumbs")), "acount": ("number", _("Answers")), } auser = User.objects.get(username=username) data = list() tmax = Answer.objects.filter(answerer=auser).aggregate( tmax=Max("thumbs"))["tmax"] acount = Answer.objects.filter(answerer=auser, thumbs__lt=0).count() if acount > 0: data.append({"thumbs": "<0", "acount": acount}) acount = Answer.objects.filter(answerer=auser, thumbs=0).count() data.append({"thumbs": "0", "acount": acount}) if tmax > 0: bin = math.ceil(2.0 / 9.0 * tmax) for k in xrange(5): if k < 4: acount = Answer.objects.filter( answerer=auser, thumbs__range=(k * bin + 1, (k + 1) * bin)).count() if bin == 1: label = "%d" % ((k + 1) * bin) else: label = "%d~%d" % (k * bin, (k + 1) * bin) data.append({"thumbs": label, "acount": acount}) else: acount = Answer.objects.filter(answerer=auser, thumbs__gt=(k + 1) * bin).count() label = ">%d" % ((k + 1) * bin) data.append({"thumbs": label, "acount": acount}) else: data.append({"thumbs": ">0", "acount": 0}) data_table = gviz_api.DataTable(description) data_table.LoadData(data) try: rid = int(request.GET["tqx"].split(":")[1].strip()) # dblogger.debug("reqId is acquired from GET data: %d" % rid) return HttpResponse( data_table.ToJSonResponse(columns_order=("thumbs", "acount"), req_id=rid)) except Exception, e: dblogger.debug(e) dblogger.debug("reqId is not found in GET, use 3 instead") return HttpResponse( data_table.ToJSonResponse(columns_order=("thumbs", "acount"), req_id=4))
def vis_json_income_pie(request): description = { "app": ("string", "App"), "income": ("number", "Income"), } ksum = Transaction.objects.filter(app="K", dst=request.user).\ aggregate(ksum=Sum("amount"))["ksum"] dsum = Transaction.objects.filter(app="D", dst=request.user).\ aggregate(dsum=Sum("amount"))["dsum"] ssum = Transaction.objects.filter(app="S", dst=request.user).\ aggregate(ssum=Sum("amount"))["ssum"] if not ksum: ksum = 0 if not dsum: dsum = 0 if not ssum: ssum = 0 data = [{ "app": _("iKnow"), "income": ksum }, { "app": _("iDoc"), "income": dsum }, { "app": _("Subsidy"), "income": ssum }] data_table = gviz_api.DataTable(description) data_table.LoadData(data) try: rid = int(request.GET["tqx"].split(":")[1].strip()) # dblogger.debug("reqId is acquired from GET data: %d" % rid) return HttpResponse( data_table.ToJSonResponse(columns_order=("app", "income"), req_id=rid)) except Exception, e: dblogger.debug(e) dblogger.debug("reqId is not found in GET, use 0 instead") return HttpResponse( data_table.ToJSonResponse(columns_order=("app", "income"), req_id=0))
def vis_json_answer_award(request, username): description = { "award": ("string", _("Award")), "acount": ("number", _("Answers")), } auser = User.objects.get(username=username) data = list() acount = Answer.objects.filter(answerer=auser, points_received=0).count() data.append({"award": "0", "acount": acount}) interval = (0, 25, 50, 100, 200, 500) for k in xrange(len(interval)): if k < len(interval) - 1: acount = Answer.objects.filter( answerer=auser, points_received__range=(interval[k] + 1, interval[k + 1])).count() label = "%d~%d" % (interval[k], interval[k + 1]) data.append({"award": label, "acount": acount}) else: acount = Answer.objects.filter( answerer=auser, points_received__gt=interval[k]).count() label = ">%d" % (interval[k]) data.append({"award": label, "acount": acount}) data_table = gviz_api.DataTable(description) data_table.LoadData(data) try: rid = int(request.GET["tqx"].split(":")[1].strip()) # dblogger.debug("reqId is acquired from GET data: %d" % rid) return HttpResponse( data_table.ToJSonResponse(columns_order=("award", "acount"), req_id=rid)) except Exception, e: dblogger.debug(e) dblogger.debug("reqId is not found in GET, use 3 instead") return HttpResponse( data_table.ToJSonResponse(columns_order=("award", "acount"), req_id=5))
def vis_json_type_pie(request): description = { "type": ("string", "Type"), "transaction": ("number", "Transaction"), } data = [ { "type": _("Q&A"), "transaction": get_type_trans_amount("QA") }, { "type": _("Q&A Tipping"), "transaction": get_type_trans_amount("TI") }, { "type": _("Q&A Thumbing"), "transaction": get_type_trans_amount("TU") }, { "type": _("Q&A Comment Answers"), "transaction": get_type_trans_amount("AC") }, { "type": _("Q&A Comment Questions"), "transaction": get_type_trans_amount("KQC") }, { "type": _("Q&A First Question"), "transaction": get_type_trans_amount("KFQ") }, { "type": _("Q&A Points allocation"), "transaction": get_type_trans_amount("AL") }, { "type": _("iDoc Upload a doc"), "transaction": get_type_trans_amount("DUD") }, { "type": _("iDoc Download a doc"), "transaction": get_type_trans_amount("DDD") }, { "type": _("iNews Post news"), "transaction": get_type_trans_amount("NVN") }, { "type": _("iNews Vote on news"), "transaction": get_type_trans_amount("NVU") }, { "type": _("iDesgin new idea"), "transaction": get_type_trans_amount("DI") }, { "type": _("iDea thumbing"), "transaction": get_type_trans_amount("ITU") }, { "type": _("iDea rating"), "transaction": get_type_trans_amount("IRT") }, { "type": _("Logging in"), "transaction": get_type_trans_amount("LI") }, ] data_table = gviz_api.DataTable(description) data_table.LoadData(data) try: rid = int(request.GET["tqx"].split(":")[1].strip()) # dblogger.debug("reqId is acquired from GET data: %d" % rid) return HttpResponse( data_table.ToJSonResponse(columns_order=("type", "transaction"), req_id=rid)) except Exception, e: dblogger.debug(e) dblogger.debug("reqId is not found in GET, use 5 instead") return HttpResponse( data_table.ToJSonResponse(columns_order=("type", "transaction"), req_id=5))
def vis_json_market_pie(request): description = { "market": ("string", "Market"), "transaction": ("number", "Transaction"), } iknow_types = ["QA", "TI", "FO", "TU", "AC", "KQC", "KFQ", "AL"] ksum = Transaction.objects.filter(ttype__in=iknow_types).\ aggregate(ksum=Sum("amount"))["ksum"] idoc_types = ["DUD", "DDD"] dsum = Transaction.objects.filter(ttype__in=idoc_types).\ aggregate(dsum=Sum("amount"))["dsum"] idea_types = ["ITU", "ITC", "IRT", "IIC", "ITI"] isum = Transaction.objects.filter(ttype__in=idea_types).\ aggregate(isum=Sum("amount"))["isum"] inews_types = ["NVU", "NVN"] nsum = Transaction.objects.filter(ttype__in=inews_types).\ aggregate(nsum=Sum("amount"))["nsum"] idesign_types = ["DI"] esum = Transaction.objects.filter(ttype__in=idesign_types).\ aggregate(esum=Sum("amount"))["esum"] subsidy_types = [ "LI", ] ssum = Transaction.objects.filter(ttype__in=subsidy_types).\ aggregate(ssum=Sum("amount"))["ssum"] if not ksum: ksum = 0 if not dsum: dsum = 0 if not isum: isum = 0 if not ssum: ssum = 0 if not nsum: nsum = 0 if not esum: esum = 0 data = [{ "market": _("iKnow"), "transaction": ksum }, { "market": _("iDoc"), "transaction": dsum }, { "market": _("iDea"), "transaction": isum }, { "market": _("iNews"), "transaction": nsum }, { "market": _("iDesign"), "transaction": esum }, { "market": _("Subsidy"), "transaction": ssum }] data_table = gviz_api.DataTable(description) data_table.LoadData(data) try: rid = int(request.GET["tqx"].split(":")[1].strip()) # dblogger.debug("reqId is acquired from GET data: %d" % rid) return HttpResponse( data_table.ToJSonResponse(columns_order=("market", "transaction"), req_id=rid)) except Exception, e: dblogger.debug(e) dblogger.debug("reqId is not found in GET, use 5 instead") return HttpResponse( data_table.ToJSonResponse(columns_order=("market", "transaction"), req_id=5))
def vis_json_time_series(request, username): description = { "date": ("date", "Date"), "questions": ("number", _("Questions")), "title1": ("string", "Title1"), "text1": ("string", "Text1"), "answers": ("number", _("Answers")), "title2": ("string", "Title2"), "text2": ("string", "Text2"), "income": ("number", _("Income")), "title3": ("string", "Title3"), "text3": ("string", "Text3"), "spending": ("number", _("Spending")), "title4": ("string", "Title4"), "text4": ("string", "Text4"), } duser = User.objects.get(username=username) iter_date = duser.date_joined.date() data = [] while iter_date <= date.today(): # dblogger.debug("Date: %s" % str(iter_date)) qcount = filter_QS_by_date(Question, iter_date).filter(asker=duser).count() acount = filter_QS_by_date(Answer, iter_date).filter(answerer=duser).count() inflow = filter_QS_by_date(Transaction, iter_date).filter(dst=duser).\ aggregate(isum=Sum("amount"))["isum"] if not inflow: inflow = 0 outflow = filter_QS_by_date(Transaction, iter_date).filter(src=duser).\ aggregate(osum=Sum("amount"))["osum"] if not outflow: outflow = 0 data.append({ "date": iter_date, "questions": qcount, "answers": acount, "income": inflow, "spending": outflow, }) iter_date += timedelta(days=1) data_table = gviz_api.DataTable(description) data_table.LoadData(data) try: rid = int(request.GET["tqx"].split(":")[1].strip()) # dblogger.debug("reqId is acquired from GET data: %d" % rid) return HttpResponse( data_table.ToJSonResponse(columns_order=( "date", "questions", "title1", "text1", "answers", "title2", "text2", "income", "title3", "text3", "spending", "title4", "text4", ), req_id=rid)) except Exception, e: dblogger.debug(e) dblogger.debug("reqId is not found in GET, use 6 instead") return HttpResponse( data_table.ToJSonResponse( columns_order=("date", "questions", "title1", "text1", "answers", "title2", "text2"), req_id=6))
tag_dict[tag.strip()] = tag_dict[tag.strip()] + 1 sorted_list = sorted(tag_dict.iteritems(), key=itemgetter(1), reverse=True)[:15] for (k, v) in sorted_list: try: tag_obj = QuestionTag.objects.get(name=k) tag_link = reverse("iknow_tag", kwargs={"tag_id": tag_obj.id}) except Exception, e: tag_link = reverse("iknow_dashboard", kwargs={"username": username}) data.append({ "label": k, "value": v, "link": tag_link, }) data_table = gviz_api.DataTable(description) data_table.LoadData(data) try: rid = int(request.GET["tqx"].split(":")[1].strip()) # dblogger.debug("reqId is acquired from GET data: %d" % rid) return HttpResponse( data_table.ToJSonResponse(columns_order=("label", "value", "link"), req_id=rid)) except Exception, e: dblogger.debug(e) dblogger.debug("reqId is not found in GET, use 0 instead") return HttpResponse( data_table.ToJSonResponse(columns_order=("label", "value", "link"), req_id=0))