calculate.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # time: 2023/6/16 10:15
  4. # file: calculate.py
  5. # author: David
  6. # company: shenyang JY
  7. import requests
  8. import json, time, datetime
  9. url = 'http://49.4.78.194:17160/apiCalculate/calculate'
  10. '''
  11. 准确率接口使用手顺:
  12. ①入口方法为 calculate_acc
  13. ② 按照参数传传值
  14. data含有C_TIME时间、realValue实际功率、ableValue可用功率(没有数值用实际功率替代)、forecastAbleValue预测功率
  15. opt为包含场站必要信息的字典,字段为:cap装机容量 province省份 formulaType公式类型 electricType电站类型 stationCode场站编码
  16. 具体介绍参考接口文档
  17. ③公式计算分为按天和按点两种,指定好opt.formulaType,即可设置公式类型,再在求取的每天或每个点的结果上进行平均,结果返回
  18. '''
  19. def wrap_json(df, opt):
  20. """
  21. 包装json
  22. :param df: 列名为 C_TIME realValue ableValue forecastAbleValue的DataFrame
  23. :param opt: 参数字典
  24. :return: json列表
  25. """
  26. d = opt.formulaType.split('_')[0]
  27. jata, dfs = [], []
  28. if d == 'POINT':
  29. df['time'] = df['C_TIME'].apply(datetime_to_timestamp)
  30. for i, row in df.iterrows():
  31. dfs.append(row.to_frame().T)
  32. elif d == 'DAY':
  33. df['time'] = df['C_TIME'].apply(datetime_to_timestamp)
  34. df['C_TIME'] = df['C_TIME'].dt.strftime('%y%m%d') # 转换成年月日
  35. for i, group in df.groupby('C_TIME'):
  36. dfs.append(group)
  37. outter_dict = {"electricCapacity": str(opt.cap), "province": opt.province, "formulaType": opt.formulaType, "electricType":opt.electricType, "stationCode": opt.stationCode}
  38. timestamp = int(time.mktime(datetime.datetime.now().timetuple()) * 1000 + datetime.datetime.now().microsecond / 1000.0)
  39. inner_dict = {"genTime": str(timestamp)+"L", "capacity": str(opt.cap), "openCapacity": str(opt.cap)}
  40. for df in dfs:
  41. calculationInfoList = df.iloc[:, 1:].to_json(orient='records')
  42. outter_dict['calculationInfoList'] = [dict(calculation, **inner_dict) for calculation in eval(calculationInfoList)]
  43. jata.append(json.dumps(outter_dict))
  44. return jata
  45. def send_reqest(url, jata):
  46. """
  47. 发送请求
  48. :param url: 请求地址
  49. :param jata: Json数据
  50. :return: 准确率
  51. """
  52. headers = {
  53. 'content-type': 'application/json;charset=UTF-8',
  54. "Authorization": "dXNlcjoxMjM0NTY="
  55. }
  56. acc, number = 0, 0
  57. for i in range(len(jata)):
  58. res = requests.post(url, headers=headers, data=jata[i])
  59. if res.json()['code'] == '500':
  60. print("没通过考核标准", res.json()['msg'])
  61. continue
  62. number += 1
  63. acc += float(res.json()['data'][:-1])
  64. acc = acc / number if number != 0 else -99
  65. return acc
  66. def calculate_acc(data, opt):
  67. """
  68. 准确率调用接口计算
  69. :param data: 列名为 C_TIME realValue ableValue forecastAbleValue的DataFrame
  70. :param opt: 参数字段
  71. :return: 计算结果
  72. """
  73. jata = wrap_json(data, opt)
  74. acc = send_reqest(url=url, jata=jata)
  75. return acc
  76. def datetime_to_timestamp(dt):
  77. return int(round(time.mktime(dt.timetuple()))*1000)