1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import json
from pot_libs.aredis_util.aredis_utils import RedisUtils
from pot_libs.mysql_util.mysql_util import MysqlUtil
from pot_libs.es_util.es_utils import EsUtil
from pot_libs.logger import log
from unify_api.utils.time_format import convert_es_str
TSP_CURRENT = "meterdata_tsp_current"
TSP_15MIN = "poweriot_tsp_15min"
async def meterdata_tsp_current(tsp_id):
"""根据tsp_id获取redis实时数据"""
res = await RedisUtils().hget(TSP_CURRENT, tsp_id)
res = json.loads(res) if res else {}
return res
async def tsp_histogram_tsp_id(date_start, date_end, tsp_id, interval):
"""TSP信息-历史曲线"""
start_es = convert_es_str(date_start)
end_es = convert_es_str(date_end)
query_body = {
"size": 0,
"query": {
"bool": {
"must": [
{
"term": {
"tsp_id": tsp_id
}
},
{
"range": {
"quarter_time": {
"gte": start_es,
"lte": end_es
}
}
}
]
}
},
"aggs": {
"quarter_time": {
"date_histogram": {
"field": "quarter_time",
"interval": interval,
"time_zone": "+08:00",
"format": "yyyy-MM-dd HH:mm:ss"
},
"aggs": {
"pm25": {
"stats": {
"field": "pm25_max"
}
},
"pm10": {
"stats": {
"field": "pm10_max"
}
},
"tsp": {
"stats": {
"field": "tsp_max"
}
}
}
}
}
}
log.info(query_body)
async with EsUtil() as es:
es_re = await es.search_origin(body=query_body, index=TSP_15MIN)
return es_re["aggregations"]["quarter_time"]["buckets"]
async def tsp_by_tsp_id_dao(start, end, tsp_list):
sql = f'SELECT tsp_id, ' \
f'AVG(pm25_mean) pm25,AVG(pm10_mean) pm10,AVG(tsp_mean) tsp ' \
f'FROM `tsp_day_record` where tsp_id in %s and ' \
f'create_time BETWEEN "{start}" and "{end}" GROUP BY tsp_id '
async with MysqlUtil() as conn:
datas = await conn.fetchall(sql, args=(tsp_list,))
return datas
async def tsp_histogram_day_tsp_id(interval): # todo: 扬尘es 待改
"""空气优-按天聚合, 再按tsp_id聚合"""
query_body = {
"size": 0,
"aggs": {
"quarter_time": {
"date_histogram": {
"field": "quarter_time",
"interval": interval,
"time_zone": "+08:00",
"format": "yyyy-MM-dd HH:mm:ss"
},
"aggs": {
"tsps": {
"terms": {
"field": "tsp_id",
"size": 1000
},
"aggs": {
"pm25": {
"avg": {
"field": "pm25_mean"
}
},
"pm10": {
"avg": {
"field": "pm10_mean"
}
},
"tsp": {
"avg": {
"field": "tsp_mean"
}
}
}
}
}
}
}
}
log.info(query_body)
async with EsUtil() as es:
es_re = await es.search_origin(body=query_body, index=TSP_15MIN)
return es_re["aggregations"]["quarter_time"]["buckets"]
async def range_max_value(date_start, date_end):
"""今日最高PM2.5"""
start_es = convert_es_str(date_start)
end_es = convert_es_str(date_end)
query_body = {
"size": 0,
"query": {
"bool": {
"must": [
{
"range": {
"quarter_time": {
"gte": start_es,
"lte": end_es
}
}
}
]
}
},
"aggs": {
"pm25": {
"max": {
"field": "pm25_max"
}
},
"pm10": {
"avg": {
"field": "pm10_max"
}
},
"tsp": {
"avg": {
"field": "tsp_max"
}
}
}
}
log.info(query_body)
async with EsUtil() as es:
es_re = await es.search_origin(body=query_body, index=TSP_15MIN)
return es_re