From 2e6b535af9a999092ff61a2618122093bb3124de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Efe=20Ayd=C4=B1n?= <3feaydin@gmail.com> Date: Wed, 24 Apr 2019 15:31:15 +0300 Subject: [PATCH] utility improve --- server_side/api/modules/utility.py | 42 ++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/server_side/api/modules/utility.py b/server_side/api/modules/utility.py index 1b82328..1227870 100644 --- a/server_side/api/modules/utility.py +++ b/server_side/api/modules/utility.py @@ -16,18 +16,50 @@ with open(db_path, 'r') as f: class Resources(Resource): def post(self): + """ + POST Data: + user_id=&type=&bill= + :return: + """ user = utils.find_by_id(users.values(), request.form['user_id']) if request.form["type"] == "electricity": + usage = user['daily_electricity_usage'] + ideal = user['ideal_electricity_usage'] + + diff = [abs(a-b) for a in usage for b in ideal] + + sum_diff = sum(diff) + + point = 10 - (sum_diff * 0.0001) + eff = point * 10 + bill = (int(request.form['bill']) * eff)/100 return { - 'daily_electricity_usage': user['daily_electricity_usage'], - 'ideal_electricity_usage': user['ideal_electricity_usage'] + 'daily_electricity_usage': usage, + 'ideal_electricity_usage': ideal, + 'points': point, + 'efficiency': eff, + 'bill': bill } elif request.form["type"] == "water": + usage = user['daily_water_usage'] + ideal = user['ideal_water_usage'] + + diff = [abs(a-b) for a in usage for b in ideal] + + sum_diff = sum(diff) + + point = 10 - (sum_diff * 0.0001) + eff = point * 10 + bill = (int(request.form['bill']) * eff)/100 + return { - 'daily_water_usage': user['daily_water_usage'], - 'ideal_water_usage': user['ideal_water_usage'] - } + 'daily_water_usage': usage, + 'ideal_water_usage': ideal, + 'point': point, + 'efficiency': eff, + 'bill': bill + } if __name__ == '__main__': api.add_resource(Resources, '/resources', '/resources/')