def water(): maxDateAmount = 7 userid = request.args.get("userid") appid = request.args.get("appid") string_date = request.args.get("date") amount = request.args.get("amount") amount = int(amount) amount = (amount if amount < maxDateAmount and amount > 0 else maxDateAmount) 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)) data = {} column = base64.b64encode("{}:{}".format('water', 'water')) result = manager.fetch_part(table_exercise, start_row, end_row, column) data['water'] = service.summary_by_date(result, 'water', 'water') chart = service.generate_info_exercise_barchart(data) return jsonify(data=data, chart=chart)
def chartnutrient(): maxDateAmount = 7 if request.method == 'GET': userid = request.args.get("userid") appid = request.args.get("appid") string_date = request.args.get("date") title = request.args.get("title") amount = int(request.args.get("amount") ) if request.args.get("amount") else 0 amount = (amount if amount < maxDateAmount and amount > 0 else maxDateAmount) + 1 # print amount end = dt.datetime.strptime(string_date, "%Y-%m-%d") end = end + dt.timedelta(days=1) columnFamily = "nutrient" limit = mock.get_nutrient_limit() maxValue = limit.get(title).get("maxVal") minValue = limit.get(title).get("minVal") # start row - end row begin = end - dt.timedelta(days=(amount)) # date = begin.strftime("%Y-%m-%d") start_row = base64.b64encode("{}_{}_{}_".format(userid, appid, begin)) end_row = base64.b64encode("{}_{}_{}_".format(userid, appid, end)) column = base64.b64encode("{}:{}".format(columnFamily, title)) result = list(manager.fetch_part( table_nutrient, start_row, end_row, column)) # print result if not result: return jsonify(nodata="no data") # print json.dumps(list(result), indent=4, separators=(',', ': ')) dic = service.summary_by_date(result, columnFamily, title) data_chart = service.generate_info_nutrient_linechart( dic, minValue, maxValue, begin, amount) return jsonify(chart=data_chart)
def nutrientImage(): maxDateAmount = 7 userid = '' appid = '' string_date = '' title = '' amount = 0 result = '' if request.method == 'GET': userid = request.args.get("userid") appid = request.args.get("appid") string_date = request.args.get("date") title = request.args.get("title") amount = int(request.args.get("amount") ) if request.args.get("amount") else 0 amount = (amount if amount < maxDateAmount and amount > 0 else maxDateAmount) + 1 elif request.method == 'POST': obj = request.json userid = obj.get("userid") appid = obj.get("appid") title = obj.get("title") string_date = obj.get("date") amount = int(obj.get("amount")) if obj.get("amount") else 0 amount = (amount if amount < maxDateAmount and amount > 0 else maxDateAmount) + 1 # print amount end = dt.datetime.strptime(string_date, "%Y-%m-%d") end = end + dt.timedelta(days=1) columnFamily = "nutrient" limit = mock.get_nutrient_limit() maxValue = limit.get(title).get("maxVal") minValue = limit.get(title).get("minVal") # start row - end row begin = end - dt.timedelta(days=(amount)) # date = begin.strftime("%Y-%m-%d") # print appid # print userid start_row = base64.b64encode("{}_{}_{}_".format(userid, appid, begin)) end_row = base64.b64encode("{}_{}_{}_".format(userid, appid, end)) column = base64.b64encode("{}:{}".format(columnFamily, title)) result = list(manager.fetch_part( table_nutrient, start_row, end_row, column)) if not result: return jsonify(nodata="no data") # print json.dumps(list(result), indent=4, separators=(',', ': ')) dic = service.summary_by_date(result, columnFamily, title) # print json.dumps(dic, indent=4, separators=(',', ': ')) date_value_list = service.generate_date_value_list(dic, begin, amount) date_list = date_value_list[0] value_list = date_value_list[1] chart_title = "{} (min: {}, max: {})".format(title, minValue, maxValue) # print date_value_list img_path = service.generate_linechart_img( title, date_list, value_list, "Date", "Value (g/day)", chart_title, maxValue, minValue, amount) with open(img_path, "rb") as image_file: encoded_string = base64.b64encode(image_file.read()) return jsonify(image=encoded_string) # return send_file(img_path, mimetype='image/png') # return "ok" return jsonify(success="true")
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)