新版本四報告接口文檔抵栈,接口代碼,數(shù)據(jù)庫定義文檔已經(jīng)完成。
生活習(xí)慣報告
接口代碼示例:
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from collections import OrderedDict
from flask import request
from .comm.wrappers import Resource, api
from .comm.errno import *
from comm.db import db
import datetime
import time
era = datetime.datetime(1970, 1, 1)
class ReportLifeHabit(Resource):
def get(self, watch_id):
result = self.create_report(watch_id)
return {
'code': E_SUCCESS,
'result': result
}
def create_report(self, watch_id):
food_data = self.get_food_data(watch_id)
habit_data = self.get_habit_data(watch_id)
return [food_data, habit_data]
def get_food_data(self, watch_id):
"""
膳食記錄獲取
param:
start_time
end_time
"""
result = []
query = {'maketime': {'$gte': float(request.data['start_time']), '$lte': float(request.data['end_time'])},
'watch_id': watch_id}
date_group_mode = {'m': {'$month': '$time'}, 'd': {'$dayOfMonth': '$time'},
'type': '$type', 'name': '$name'}
print query
cursor = db.diet.aggregate([
{'$match': query},
{'$project': {
'time': {'$add': [{'$multiply': [1000, {'$add': ['$maketime', 28800]}]}, era]},
'name': '$food_name',
'type': '$type',
'n': '$food_value',
'c': '$calorie'
}},
{'$group': {
'_id': date_group_mode,
'n': {'$sum': '$n'},
'c': {'$sum': '$c'}
}}
])
return_data = {}
for i in cursor:
day = str(i['_id']['m']) + '-' + str(i['_id']['d'])
ty = i['_id']['type']
na = i['_id']['name']
try:
daily_data = return_data[day]
except KeyError:
daily_data = {'breakfast': {}, 'lunch': {}, 'dinner': {}, 'calorie': 0}
return_data[day] = daily_data
monment = daily_data[ty]
monment[na] = i['n']
result = []
for k, v in return_data.items():
i = v
i['maketime'] = k
result.append(i)
return result
def get_habit_data(self, watch_id):
result = {'smoke_num': 0, 'alcoholic': {'beer': 0, 'red': 0, 'wine': 0}}
cursor = db.habit.find({'maketime': {'$gte': float(request.data['start_time']), '$lte': float(request.data['end_time'])},
'watch_id': watch_id})
for i in cursor:
result['smoke_num'] += i.get('cigarette', 0)
result['alcoholic']['beer'] += i['alcoholic'].get(u'啤酒', 0)
result['alcoholic']['wine'] += i['alcoholic'].get(u'白酒', 0)
result['alcoholic']['red'] += i['alcoholic'].get(u'紅酒', 0)
return result
class ReportLifeHabits(Resource):
def get(self, watch_id):
try:
habit_rpt_obj = db.doctor_rpt_habit.find_ont({
'watch_id': watch_id,
'date': request.data['date']
})
except KeyError, e:
return {'code': 4002, 'msg': e.message}
result = {}
if habit_rpt_obj is not None:
result = {
'date': habit_rpt_obj.get('date'),
'watch_id': habit_rpt_obj.get('watch_id'),
'diet_info': habit_rpt_obj.get('diet_info'),
'smoke_num': habit_rpt_obj.get('smoke'),
'drink_total': habit_rpt_obj.get('drink'),
'high_fiber': request.data.get('high_fiber'), # 高膳食纖維 ({'status': '', 'advise': '', 'score': ''})
'cereal': request.data.get('cereal'), # 谷類食物
'food_collocation': request.data.get('food_collocation'), # 食物搭配合理
'energy_intake': request.data.get('energy_intake'), # 能量攝入
'meat': request.data.get('meat'), # 肉類
'vegetable': request.data.get('vegetable'), # 蔬菜類
'food_types': request.data.get('food_types'), # 食物種類豐富
'smoke': request.data.get('smoke'), # 吸煙
'drink': request.data.get('drink'), # 喝酒
'health_score': habit_rpt_obj.get('health_score'),
'life_advise': habit_rpt_obj.get('life_advise')
}
return {'code': E_SUCCESS, 'result': result}
def post(self, watch_id):
# 查詢膳食記錄
query = {'maketime': {'$gte': float(request.data['start_time']), '$lte': float(request.data['end_time'])},
'watch_id': watch_id}
date_group_mode = {'m': {'$month': '$time'}, 'd': {'$dayOfMonth': '$time'},
'type': '$type', 'name': '$name'}
cursor = db.diet.aggregate([
{'$match': query},
{'$project': {
'time': {'$add': [{'$multiply': [1000, {'$add': ['$maketime', 28800]}]}, era]},
'name': '$food_name',
'type': '$type',
'n': '$food_value',
'c': '$calorie'
}},
{'$group': {
'_id': date_group_mode,
'n': {'$sum': '$n'},
'c': {'$sum': '$c'}
}}
])
data = {}
for i in cursor:
day = str(i['_id']['m']) + '-' + str(i['_id']['d'])
ty = i['_id']['type']
na = i['_id']['name']
try:
daily_data = data[day]
except KeyError:
daily_data = {'breakfast': {}, 'lunch': {}, 'dinner': {}, 'calorie': 0}
data[day] = daily_data
monment = daily_data[ty]
monment[na] = i['n']
diet_info = []
for k, v in data.items():
i = v
i['maketime'] = k
diet_info.append(i)
# 獲取吸煙,喝酒數(shù)量
smoke_drink_data = {'smoke_num': 0, 'alcoholic': {'beer': 0, 'red': 0, 'wine': 0}}
cursor = db.habit.find({'maketime': {'$gte': float(request.data['start_time']), '$lte': float(request.data['end_time'])},
'watch_id': watch_id})
for i in cursor:
smoke_drink_data['smoke_num'] += i.get('cigarette', 0)
smoke_drink_data['alcoholic']['beer'] += i['alcoholic'].get(u'啤酒', 0)
smoke_drink_data['alcoholic']['wine'] += i['alcoholic'].get(u'白酒', 0)
smoke_drink_data['alcoholic']['red'] += i['alcoholic'].get(u'紅酒', 0)
# 獲取各項評分
inserts = {
'date': request.data.get('date'), # 報告日期
'watch_id': watch_id, # watch_id
'diet_info': diet_info, # 膳食記錄
'smoke_num': smoke_drink_data['smoke_num'], # 吸煙數(shù)量
'drink_total': smoke_drink_data, # 喝酒量
'high_fiber': request.data.get('high_fiber'), # 高膳食纖維 ({'status': '', 'advise': '', 'score': ''})
'cereal': request.data.get('cereal'), # 谷類食物
'food_collocation': request.data.get('food_collocation'), # 食物搭配合理
'energy_intake': request.data.get('energy_intake'), # 能量攝入
'meat': request.data.get('meat'), # 肉類
'vegetable': request.data.get('vegetable'), # 蔬菜類
'food_types': request.data.get('food_types'), # 食物種類豐富
'smoke': request.data.get('smoke'), # 吸煙
'drink': request.data.get('drink'), # 喝酒
'health_score': request.data.get('health_score'), # 健康總分
'life_advise': request.data.get('life_advise'), # 生活建議
}
db.doctor_rpt_habit.insert(inserts)
def __mount__():
api.add_resource(ReportLifeHabit,'/report/<objectid:watch_id>/habit')
數(shù)據(jù)庫文檔示例
doctor_rpt_habit 醫(yī)生報告-生活習(xí)慣
date :[str] # 報告日期('2017-03'年月)
watch_id :[str] # watch_id
diet_info :[
{
food_type :[str] # 食物類型
food_amount :[int] # 食物量
calorie :[int] # 卡路里消耗
}...
]
smoke :[int] # 吸煙數(shù)量
drink :[
{
beer/wine/red :[str] # 酒類型-量
}
]
high_fiber :[list] # 高膳食纖維
({'status': '', 'advise': '', 'score': ''})
cereal :[list] # 谷類食物
food_collocation :[list] # 食物搭配合理
energy_intake :[list] # 能量攝入
meat :[list] # 肉類
vegetable :[list] # 蔬菜類
food_types :[list] # 食物種類豐富
smoke :[list] # 吸煙
drink :[list] # 喝酒
health_score :[int] # 總得分
life_advise :[str] # 生活飲食建議
接口文檔示例
添加生活習(xí)慣報告
url: /report/[watch_id]/habit
method: POST
header:
'session' : <str> <need> //用戶session
params:
'date' : <str>報告日期(2017-03)
'high_fiber' : <obj>高膳食纖維
{'status': '', 'advise': '', 'score': ''}
'cereal' : <obj>谷類食物
'food_collocation' : <obj>食物搭配合理
'energy_intake' : <obj>能量攝入
'meat' : <obj>肉類
'vegetable' : <obj>蔬菜類
'food_types' : <obj>食物種類豐富
'smoke' : <obj>吸煙
'drink' : <obj>喝酒
'health_score' : <int>健康總分
'life_advise' : <str>生活建議
result:
'code' : <int>
獲取生活習(xí)慣報告
url: /report/[watch_id]/habit
method: GET
header:
'session' : <str> <need> //用戶session
params:
'date' : <str> 報告日期(2017-03)
result:
{
code : <int>
result :[ <list>
[
{
'breakfast' :{'food_name': 'value'}
'lunch' :午餐:{食物名稱:食物量}
'dinner' :
'calorie' : 卡路里
'maketime' : 日期(月-日)
}...
],
{
'alcoholic' : {"beer":0,"red":0,"wine":0}喝酒量
'smoke_num' : <int> 吸煙量
},
health_assess :[
{
name :<str> 項目名稱
status :<str> 狀態(tài)
advise :<str> 建議
num :<int> 得分
}...
],
health_score :<int. 總得分
life_advise :<str> 生活飲食建議
]
}