Description
handler
- event -> type, date, addr 값을 parameter로 입력받음
- type은 총 6개/ 월단위 Third, Second, First 범위, 연단위 Third, Second, First 범위
fit
- (주소값 합칠 때 사용)
- Input은 dictionary list 형태/ Output은 dictionary 형태
- Key값 별로 다 합쳐줌/ None type인 경우 Decimal(0)으로 처리
fit_list1
- 연단위로 합칠 때 사용
- return 값은 dictionary list 형태
- partition key 는 eq만 가능해서 query를 12번 반복 (2022년의 경우 ["Items"]값이 없을 때 break)
fit_list2
- 연단위, 주소값 합칠 때 사용
- return 값은 마찬가지로 dictionary list 형태 (element -> sort key query 값을 fit()이용해서 하나의 dictionary로 합친 값)
- partition key 는 eq만 가능해서 query를 12번 반복 (2022년의 경우 ["Items"]값이 없을 때 break)
import boto3
import json
from boto3.dynamodb.conditions import Key, Attr
from decimal import Decimal
dynamodb = boto3.resource("dynamodb", region_name = "ap-northeast-2")
table = dynamodb.Table("buildingenergy")
def fit(dictlist):
result = {"gas": 0, "gas_co2": 0, "electricity": 0, "electricity_co2": 0, "co2_sum": 0, "energy_sum": 0, "count": 0}
for i in range(len(dictlist)):
result["gas"] += dictlist[i]["gas"] or 0
result["gas_co2"] += dictlist[i]["gas_co2"] or 0
result["electricity"] += dictlist[i]["electricity"] or 0
result["electricity_co2"] += dictlist[i]["electricity_co2"] or 0
result["co2_sum"] += dictlist[i]["co2_sum"] or 0
result["energy_sum"] += dictlist[i]["energy_sum"] or 0
result["count"] += dictlist[i]["count"]
return result
def fit_list1(strYear, Addr):
result = [0 for i in range(12)]
for i in range(12):
dict = {"gas": 0, "gas_co2": 0, "electricity": 0, "electricity_co2": 0, "co2_sum": 0, "energy_sum": 0, "count": 0}
response = table.query(
KeyConditionExpression = Key("date").eq(strYear[i]) & Key("addr").eq(Addr)
)
if len(response["Items"]) == 0:
break
dict["gas"] = response["Items"][0]["gas"]; dict["gas_co2"] = response["Items"][0]["gas_co2"]; dict["electricity"] = response["Items"][0]["electricity"]; dict["electricity_co2"] = response["Items"][0]["electricity_co2"]
dict["co2_sum"] = response["Items"][0]["co2_sum"]; dict["energy_sum"] = response["Items"][0]["energy_sum"]; dict["count"] = response["Items"][0]["count"]
result[i] = dict
if i < 11:
del result[i:]
return result
def fit_list2(strYear, parseAddr1, parseAddr2):
result = [0 for i in range(12)]
for i in range(12):
dict = {"gas": 0, "gas_co2": 0, "electricity": 0, "electricity_co2": 0, "co2_sum": 0, "energy_sum": 0, "count": 0}
response = table.query(
KeyConditionExpression = Key("date").eq(strYear[i]) & Key("addr").between(parseAddr1, parseAddr2)
)
if len(response["Items"]) == 0:
break
dict = fit(response["Items"])
result[i] = dict
if i < 11:
del result[i:]
return result
def lambda_handler(event, context):
Type = int(event["params"]["querystring"]["type"])
Date = event["params"]["querystring"]["date"]
Addr = event["params"]["querystring"]["addr"]
intYear = (int(Date) // 100) * 100
strYear = [0 for i in range(12)]
for i in range(12):
strYear[i] = str(intYear + (i + 1))
if Type == 0:
parseAddr1 = str((int(Addr) // 100000000) * 100000000)
parseAddr2 = str(((int(Addr) // 100000000) + 1) * 100000000)
response = table.query(
KeyConditionExpression = Key("date").eq(Date) & Key("addr").between(parseAddr1, parseAddr2)
)
result = fit(response["Items"])
elif Type == 1:
parseAddr1 = str((int(Addr) // 1000000) * 1000000)
parseAddr2 = str(((int(Addr) // 1000000) + 1) * 1000000)
response = table.query(
KeyConditionExpression = Key("date").eq(Date) & Key("addr").between(parseAddr1, parseAddr2)
)
result = fit(response["Items"])
elif Type == 2:
response = table.query(
KeyConditionExpression = Key("date").eq(Date) & Key("addr").eq(Addr)
)
result = {}
result["gas"] = response["Items"][0]["gas"]; result["gas_co2"] = response["Items"][0]["gas_co2"]; result["electricity"] = response["Items"][0]["electricity"]; result["electricity_co2"] = response["Items"][0]["electricity_co2"]
result["co2_sum"] = response["Items"][0]["co2_sum"]; result["energy_sum"] = response["Items"][0]["energy_sum"]; result["count"] = response["Items"][0]["count"]
elif Type == 3:
parseAddr1 = str((int(Addr) // 100000000) * 100000000)
parseAddr2 = str(((int(Addr) // 100000000) + 1) * 100000000)
response = fit_list2(strYear, parseAddr1, parseAddr2)
result = fit(response)
elif Type == 4:
parseAddr1 = str((int(Addr) // 1000000) * 1000000)
parseAddr2 = str(((int(Addr) // 1000000) + 1) * 1000000)
response = fit_list2(strYear, parseAddr1, parseAddr2)
result = fit(response)
elif Type == 5:
response = fit_list1(strYear, Addr)
result = fit(response)
else:
return 0
return result