elec_charge_dao.py 10.2 KB
Newer Older
lcn's avatar
lcn committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
from pot_libs.es_util.es_utils import EsUtil
from pot_libs.logger import log
from unify_api.constants import COMPANY_15MIN_POWER, POINT_15MIN_POWER
from unify_api.utils.time_format import convert_es_str
from pot_libs.mysql_util.mysql_util import MysqlUtil

index = COMPANY_15MIN_POWER
index_point = POINT_15MIN_POWER


async def point_day_power_dao(cid, start, end):
    sql = f"""
    SELECT DATE_FORMAT(pp.create_time, '%%Y-%%m-%%d') create_time, 
    SUM(pp.kwh) kwh, SUM(pp.p) p, SUM(pp.charge) charge
     from point_1day_power pp LEFT JOIN point p 
    on pp.pid=p.pid LEFT JOIN monitor m on m.mtid=p.mtid 
    where p.cid=%s and pp.create_time>='{start}'
    and pp.create_time<='{end}' and m.demolished=0 GROUP BY pp.create_time 
    ORDER BY pp.create_time
    """
    async with MysqlUtil() as conn:
lcn's avatar
lcn committed
22
        data = await conn.fetchall(sql, args=(cid,))
lcn's avatar
lcn committed
23 24 25 26 27 28 29 30 31 32 33
    return data


async def get_total_kwh_dao(cid, start, end):
    sql = f"""
        SELECT sum(pp.kwh) total_kwh from point_1day_power pp 
        LEFT JOIN point p ON p.pid=pp.pid LEFT JOIN monitor m 
        on m.mtid=p.mtid where p.cid=%s and pp.create_time>='{start}' 
        and pp.create_time<='{end}' and m.demolished=0
        """
    async with MysqlUtil() as conn:
lcn's avatar
lcn committed
34
        total_kwh = await conn.fetchone(sql, args=(cid,))
lcn's avatar
lcn committed
35 36 37 38 39 40 41 42
    return total_kwh


async def get_kwh_charge(table_name, name, value, start, end):
    sql = f"SELECT sum(kwh) sum_kwh, sum(charge) sum_charge  " \
          f"FROM {table_name} where create_time>='{start}' and " \
          f"create_time<='{end}' and {name} = %s"
    async with MysqlUtil() as conn:
lcn's avatar
lcn committed
43
        datas = await conn.fetchone(sql, args=(value,))
lcn's avatar
lcn committed
44 45 46
    return datas


ZZH's avatar
ZZH committed
47
async def load_cmpy_charge(date_start, date_end, cids):
lcn's avatar
lcn committed
48 49 50 51 52 53
    sql = f"""
        select cid,sum(kwh) kwh,sum(charge) charge from company_15min_power
        where cid in %s and create_time BETWEEN %s and %s
        group by cid
    """
    async with MysqlUtil() as conn:
ZZH's avatar
ZZH committed
54
        datas = await conn.fetchall(sql=sql, args=(cids, date_start, date_end))
lcn's avatar
lcn committed
55 56 57
    return datas


lcn's avatar
lcn committed
58 59 60 61
async def power_charge_p_aggs(date_start, date_end, cid_list, interval):
    """
    date_histogram,
    """
lcn's avatar
lcn committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    if interval == "hour":
        time_fmt = "%%Y-%%m-%%d %%H"
    elif interval == "day":
        time_fmt = "%%Y-%%m-%%d"
    else:
        time_fmt = "%%Y-%%m-%%d %%H:%%i"
    sql = f"""
        select date_format(create_time,"{time_fmt}") as create_time,sum(kwh)
        kwh,sum(charge) charge,sum(p) p
        from company_15min_power
        where cid in %s and create_time >= %s and create_time <= %s
        group by date_format(create_time,"{time_fmt}")
    """
    async with MysqlUtil() as conn:
        results = await conn.fetchall(sql,
                                      args=(cid_list, date_start, date_end))
    return results or []
lcn's avatar
lcn committed
79 80 81 82 83 84


async def power_charge_p_cid_aggs(date_start, date_end, cid_list, interval):
    """
    excel下载, 按照cid,date_histogram两次聚合,求电量电费
    """
lcn's avatar
lcn committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    if interval == "hour":
        time_fmt = "%%Y-%%m-%%d %%H"
    elif interval == "day":
        time_fmt = "%%Y-%%m-%%d"
    else:
        time_fmt = "%%Y-%%m-%%d %%H:%%i"
    sql = f"""
        select cid,date_format(create_time,"{time_fmt}") as create_time,
        sum(kwh) kwh,sum(charge) charge,sum(p) p
        from company_15min_power
        where cid in %s and create_time >= %s and create_time <= %s
        group by cid,date_format(create_time,"{time_fmt}")
    """
    async with MysqlUtil() as conn:
        results = await conn.fetchall(sql,
                                      args=(cid_list, date_start, date_end))
    return results or []
lcn's avatar
lcn committed
102 103


ZZH's avatar
ZZH committed
104
async def query_charge_aggs_points(start, end, point_list):
lcn's avatar
lcn committed
105 106 107 108 109
    sql = f"SELECT pid,sum(kwh) kwh,SUM(charge) charge " \
          f"FROM `point_15min_power` " \
          f"where pid in %s and create_time BETWEEN '{start}' and '{end}' " \
          f"GROUP BY pid"
    async with MysqlUtil() as conn:
lcn's avatar
lcn committed
110
        datas = await conn.fetchall(sql, args=(point_list,))
lcn's avatar
lcn committed
111 112 113 114 115 116 117
    return datas


async def histogram_aggs_points(date_start, date_end, point_list, interval):
    """date_histogram"""
    start_es = convert_es_str(date_start)
    end_es = convert_es_str(date_end)
ZZH's avatar
ZZH committed
118

lcn's avatar
lcn committed
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 179
    query_body = {
        "size": 0,
        "query": {
            "bool": {
                "must": [
                    {
                        "terms": {
                            "pid": point_list
                        }
                    },
                    {
                        "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": {
                    "kwh": {
                        "sum": {
                            "field": "kwh"
                        }
                    },
                    "charge": {
                        "sum": {
                            "field": "charge"
                        }
                    },
                    "p": {
                        "sum": {
                            "field": "p"
                        }
                    }
                }
            }
        }
    }
    log.info(query_body)
    async with EsUtil() as es:
        es_re = await es.search_origin(body=query_body, index=index_point)
    return es_re["aggregations"]["quarter_time"]["buckets"]


async def power_charge_p_point_aggs(date_start, date_end, pid_list, interval):
    """
    excel下载, 按照pid,date_histogram两次聚合,求电量电费
    """
    start_es = convert_es_str(date_start)
    end_es = convert_es_str(date_end)
ZZH's avatar
ZZH committed
180

lcn's avatar
lcn committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
    query_body = {
        "size": 0,
        "query": {
            "bool": {
                "must": [
                    {
                        "terms": {
                            "pid": pid_list
                        }
                    },
                    {
                        "range": {
                            "quarter_time": {
                                "gte": start_es,
                                "lte": end_es
                            }
                        }
                    }
                ]
            }
        },
        "aggs": {
            "pids": {
                "terms": {
                    "field": "pid",
                    "size": 10000
                },
                "aggs": {
                    "quarter_time": {
                        "date_histogram": {
                            "field": "quarter_time",
                            "interval": interval,
                            "time_zone": "+08:00",
                            "format": "yyyy-MM-dd HH:mm:ss"
                        },
                        "aggs": {
                            "kwh": {
                                "sum": {
                                    "field": "kwh"
                                }
                            },
                            "charge": {
                                "sum": {
                                    "field": "charge"
                                }
                            },
                            "p": {
                                "sum": {
                                    "field": "p"
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    log.info(query_body)
    async with EsUtil() as es:
        es_re = await es.search_origin(body=query_body, index=index_point)
    return es_re["aggregations"]["pids"]["buckets"]


ZZH's avatar
ZZH committed
244
async def point_kwh_charge(point_list, start=None, end=None):
lcn's avatar
lcn committed
245 246 247 248 249 250 251 252 253
    """1.5版本根据pid,求电量电费"""
    if start and end:
        sql = f"SELECT sum(kwh) kwh, sum(charge) charge FROM " \
              f"`point_15min_power` " \
              f"where pid in %s and create_time BETWEEN {start} and {end}"
    else:
        sql = "SELECT sum(kwh) kwh,sum(charge) charge FROM point_15min_power" \
              " where pid in %s"
    async with MysqlUtil() as conn:
lcn's avatar
lcn committed
254
        data = await conn.fetchone(sql, args=(point_list,))
lcn's avatar
lcn committed
255 256 257 258 259 260 261 262 263
    return data


async def extended_bounds_agg(date_start, date_end, cid_list, interval):
    """
    date_histogram,
    """
    start_es = convert_es_str(date_start)
    end_es = convert_es_str(date_end)
ZZH's avatar
ZZH committed
264

lcn's avatar
lcn committed
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
    query_body = {
        "size": 0,
        "query": {
            "bool": {
                "must": [
                    {
                        "terms": {
                            "cid": cid_list
                        }
                    },
                    {
                        "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",
                    "extended_bounds": {
                        "min": date_start,
                        "max": date_end
                    }
                },
                "aggs": {
                    "kwh": {
                        "sum": {
                            "field": "kwh"
                        }
                    },
                    "charge": {
                        "sum": {
                            "field": "charge"
                        }
                    },
                    "p": {
                        "sum": {
                            "field": "p"
                        }
                    }
                }
            }
        }
    }
    log.info(query_body)
    async with EsUtil() as es:
        es_re = await es.search_origin(body=query_body, index=index)
    return es_re["aggregations"]["quarter_time"]["buckets"]