Exemplo n.º 1
0
def chartpoint():
    if request.method == 'GET':
        userid = request.args.get("userid")
        appid = request.args.get("appid")
        year = request.args.get("year")
        month = request.args.get("month")
        amount = request.args.get("amount")
        title = request.args.get("title")

        # print title
        _, last_day = calendar.monthrange(int(year), int(month))
        end = dt.datetime.strptime(
            year + '-' + month + '-' + str(last_day), "%Y-%m-%d")
        begin = end + relativedelta(months=-int(amount))

        start_row = base64.b64encode("{}_{}_{}".format(userid, appid, begin))
        end_row = base64.b64encode("{}_{}_{}".format(userid, appid, end))

        if not title:
            data = manager.fetch_part(table_result, start_row, end_row)
        else:
            column = base64.b64encode("testresults:" + title)
            data = manager.fetch_part(table_result, start_row, end_row, column)

        result = service.group_by_key(data, 'testresults')
        info_linechart = service.generate_info_result_linechart(result)

        # points = [0.5, 0.8, 1.2, 1.5, 1.7, 2.0, 1.8]
        # dates = ["2015-03-20", "2015-04-18", "2015-05-22", "2015-06-21", "2015-07-20"]
        # dates = ["January", "February", "March", "April", "May", "June", "July"]
        # lastcheck = "2015-12-25"
        # limit = 1.5
        # unit = "mg/dL"
        # print list(data)
        # print json.dumps(list(data), indent=4, separators=(',', ': '))
        # encode to JSON and get value of parameter "view_type"
        # print (json.loads(obj)).get("view_type")
        # return jsonify(points=points,dates=dates,lastcheck=lastcheck,limit=limit,unit=unit)
        # return jsonify(success="true")
        return jsonify(chart=info_linechart, data=result)
Exemplo n.º 2
0
def exercise():
    if request.method == 'POST':
        obj = request.json
        activity = obj.get("activity")
        result = obj.get("result")
        columnFamily = ""
        # print request

        userid = obj.get("userid")
        appid = obj.get("appid")
        date = obj.get("date")

        if result != None:
            title = result.get("title")
            value = result.get("value")
            goal = result.get("limit")
            columnFamily = 'water'

        elif activity != None:
            title = activity.get("title")
            value = activity.get("value")
            goal = activity.get("goal")
            columnFamily = 'activity'

        time = dt.datetime.now().strftime("%H:%M:%S")

        rowkey = userid + "_" + appid + "_" + date + "_" + time
        _value = str(value) + ',' + str(goal)

        manager.insert_data(table_exercise, rowkey,
                            columnFamily, title, _value)

        # print json.dumps(obj, indent=4, separators=(',', ': '))

        return jsonify(success="true")

    elif request.method == 'GET':
        maxDateAmount = 7
        userid = request.args.get("userid")
        appid = request.args.get("appid")
        title = request.args.get("title")
        string_date = request.args.get("date")
        amount = request.args.get("amount")
        amount = int(amount)
        amount = (amount if amount < maxDateAmount and amount >
                  0 else maxDateAmount) + 2

        end = dt.datetime.strptime(string_date, "%Y-%m-%d")
        end = end + dt.timedelta(days=1)
        begin = end - dt.timedelta(days=(amount))

        start_row = base64.b64encode("{}_{}_{}_".format(userid, appid, begin))
        end_row = base64.b64encode("{}_{}_{}_".format(userid, appid, end))
        column = base64.b64encode("activity")

        if not title:
            result = manager.fetch_part(
                table_exercise, start_row, end_row, column)
            _result = list(result)
            keys = service.group_by_key(_result, "activity").keys()
            data = {}
            for _title in keys:
                data[_title] = service.summary_by_date(
                    _result, 'activity', _title)

        else:
            data = {}
            column = base64.b64encode("{}:{}".format('activity', title))
            result = manager.fetch_part(
                table_exercise, start_row, end_row, column)
            data[title] = service.summary_by_date(result, 'activity', title)

        chart = service.generate_info_exercise_barchart(data)

        return jsonify(chart=chart, data=data)