新版本四报告接口文档,接口代码,数据库定义文档已经完成。
生活习惯报告
接口代码示例:
# -*- 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)
# 获取吸烟,喝酒数量
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'], # 吸烟数量
'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')
数据库文档示例
doctor_rpt_habit 医生报告-生活习惯
date :[str] # 报告日期('2017-03'年月)
watch_id :[str] # watch_id
diet_info :[
{
food_type :[str] # 食物类型
food_amount :[int] # 食物量
calorie :[int] # 卡路里消耗
}...
]
smoke :[int] # 吸烟数量
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] # 生活饮食建议
接口文档示例
添加生活习惯报告
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>
获取生活习惯报告
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> 状态
advise :<str> 建议
num :<int> 得分
}...
],
health_score :<int. 总得分
life_advise :<str> 生活饮食建议
]
}