avg_from_database.py 761 B

123456789101112131415161718
  1. from abstract_api import AbstractApi
  2. from apis.operate_mysql import OperateMysql
  3. class Avg_from_database(AbstractApi):
  4. def operation(self, request):
  5. operate_mysql = OperateMysql()
  6. basic_data = request['basic_data']
  7. serial_number = basic_data.replace('c', '')
  8. res1 = operate_mysql.return_result(request, serial_number)
  9. sql = "SELECT avg(%s) FROM %s WHERE times > \'%s\' and times < \'%s\';" % (basic_data, res1['table_name'], res1['begin_time'], res1['end_time'])
  10. res2 = operate_mysql.execute_sql(sql)
  11. avg_value = res2[0]['avg(' + basic_data + ')']
  12. if avg_value is None:
  13. return None
  14. else:
  15. res = {request['key']: ("%.2f" % avg_value)}
  16. return res