수송에너지 부분 탄소배출량 데이터 획득에 있어 다음과 같은 문제점 발생
-> 수송에너지는 실 데이터가 없어 임의로 data를 frame만 짜고 이를 기준으로 API를 구현함
(database -> DynamoDB 이용 // API CRUD 및 Query -> python boto3 이용)
idx, cname, company, value
idx, First, Second, Third, Year, OType, StandardE, RealE
StandardE와 RealE는 '목록' 자료형을 사용 -> python으로 list였나
import json
import boto3
from boto3.dynamodb.conditions import Key
from decimal import Decimal
dynamodb = boto3.resource("dynamodb", region_name = "ap-northeast-2")
table = dynamodb.Table("carbon_emissions_transport")
def lambda_handler(event, context):
count = table.scan()["Count"]
item = json.loads(json.dumps(event["body-json"]), parse_float=Decimal)
response = table.put_item(
Item = {
"idx": count,
"First": item["First"],
"Second": item["Second"],
"Third": item["Third"],
"Year": item["Year"],
#"Month": item["Month"],
"OType": item["OType"],
"StandardE": [
item["SE1"],
item["SE2"],
item["SE3"],
item["SE4"],
item["SE5"],
item["SE6"],
item["SE7"],
item["SE8"],
item["SE9"],
item["SE10"],
item["SE11"],
item["SE12"],
],
"RealE": [
item["RE1"],
item["RE2"],
item["RE3"],
item["RE4"],
item["RE5"],
item["RE6"],
item["RE7"],
item["RE8"],
item["RE9"],
item["RE10"],
item["RE11"],
item["RE12"],
],
}
)
return response
DynamoDB는 float형 지원x -> Decimal package 이용하여 json load하면서 parsing
import boto3
import json
from boto3.dynamodb.conditions import Key, Attr
dynamodb = boto3.resource("dynamodb", region_name = "ap-northeast-2")
table = dynamodb.Table("carbon_emissions_transport")
# event에 주소랑 연,월 정보가 들어감
def lambda_handler(event, context):
First = event["body-json"]["First"]
Second = event["body-json"]["Second"]
Third = event["body-json"]["Third"]
Year = event["body-json"]["Year"]
response = table.scan(
FilterExpression=Attr("First").eq(First) & Attr("Second").eq(Second) & Attr("Third").eq(Third) & Attr("Year").eq(Year)
)
item = response["Items"]
return item
해당 주소와 해당 년/월의 탄소배출량 return
수송차트 종류는 다음과 같음
1. 도 단위 배출량 합산 및 증감 계산
2. 전국 휘발유 배출량 평균 & 전국 경유 배출량 평균 계산
3. 차종별(승합차, 화물, 대형) 배출량
4. 차종별(휘발유, 경유, 하이브리드, 플러그인, 전기차, 수소차) 배출량
5. 자동차 모델별 배출량
6. 기준치 대비 배출량
idx, Emissions, Model, Oil, Size, Year
차종별 배출량 차트 때문에 일단 만든 table
import boto3
import json
from boto3.dynamodb.conditions import Key, Attr
from decimal import Decimal
dynamodb = boto3.resource("dynamodb", region_name = "ap-northeast-2")
carbonemissionstable = dynamodb.Table("carbon_emissions_transport")
carboncartypetable = dynamodb.Table("carbon_emissions_by_car_type")
#carstandardtable = dynamodb.Table("car_standard")
def lambda_handler(event, context):
items = []
result = []
chartnum = event["body-json"]["chartnum"]
# 0. 도단위 배출량 합산 및 증감 계산
if chartnum == 0:
Year = event["body-json"]["Year"]
Month = event["body-json"]["Month"]
response = carbonemissionstable.scan(
FilterExpression = Attr("Year").eq(Year)
)
items = response["Items"]
for item in items:
tmp = []
tmp.append(item["First"])
tmp.append(item["OType"])
tmp.append(item["RealE"][Month - 1] - item["RealE"][Month - 2])
result.append(tmp)
# 1. 전국 휘발유 배출량 평균 & 전국 경유 배출량 평균
elif chartnum == 1:
Year = event["body-json"]["Year"]
Month = event["body-json"]["Month"]
response = carbonemissionstable.scan(
FilterExpression = Attr("Year").eq(Year)
)
items = response["Items"]
size = 0
ttmp = []
for item in items:
tmp = []
tmp.append(item["OType"])
tmp.append(item["RealE"][Month - 1])
size += 1
ttmp.append(tmp)
sum0 = [0, 0]
sum1 = [1, 0]
for tt in ttmp:
if tt[0] == 0:
sum0[1] += tt[1]
elif tt[0] == 1:
sum1[1] += tt[1]
size = size / 2
sum0[1] = sum0[1] / Decimal(size)
sum1[1] = sum1[1] / Decimal(size)
result.append(sum0)
result.append(sum1)
# 2. 전국 차종별 (승합차, 화물, 대형) 배출량
elif chartnum == 2:
Year = event["body-json"]["Year"]
response = carboncartypetable.scan(
FilterExpression = Attr("Year").eq(Year)
)
items = response["Items"]
tmp0 = []
tmp1 = []
tmp2 = []
for item in items:
if item["Size"] == 0:
tmp0.append(item["Emissions"])
elif item["Size"] == 1:
tmp1.append(item["Emissions"])
elif item["Size"] == 2:
tmp2.append(item["Emissions"])
sum0 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
sum1 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
sum2 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
i = 1
for j in range(12):
for i in range(len(tmp0)):
sum0[j] += tmp0[i][j]
i = 1
for j in range(12):
for i in range(len(tmp1)):
sum1[j] += tmp1[i][j]
i = 1
for j in range(12):
for i in range(len(tmp2)):
sum2[j] += tmp2[i][j]
result.append(0)
result.append(sum0)
result.append(1)
result.append(sum1)
result.append(2)
result.append(sum2)
# 3. 전국 차종별 (휘발유, 경유, 하이브리드, 플러그인, 전기차, 수소차) 배출량
elif chartnum == 3:
Year = event["body-json"]["Year"]
response = carboncartypetable.scan(
FilterExpression = Attr("Year").eq(Year)
)
items = response["Items"]
tmp0 = []
tmp1 = []
tmp2 = []
tmp3 = []
tmp4 = []
tmp5 = []
for item in items:
if item["Oil"] == 0:
tmp0.append(item["Emissions"])
elif item["Oil"] == 1:
tmp1.append(item["Emissions"])
elif item["Oil"] == 2:
tmp2.append(item["Emissions"])
elif item["Oil"] == 3:
tmp3.append(item["Emissions"])
elif item["Oil"] == 4:
tmp4.append(item["Emissions"])
elif item["Oil"] == 5:
tmp5.append(item["Emissions"])
sum0 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
sum1 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
sum2 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
sum3 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
sum4 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
sum5 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
i = 1
for j in range(12):
for i in range(len(tmp0)):
sum0[j] += tmp0[i][j]
i = 1
for j in range(12):
for i in range(len(tmp1)):
sum1[j] += tmp1[i][j]
i = 1
for j in range(12):
for i in range(len(tmp2)):
sum2[j] += tmp2[i][j]
i = 1
for j in range(12):
for i in range(len(tmp3)):
sum3[j] += tmp3[i][j]
i = 1
for j in range(12):
for i in range(len(tmp4)):
sum4[j] += tmp4[i][j]
i = 1
for j in range(12):
for i in range(len(tmp5)):
sum5[j] += tmp5[i][j]
result.append(0)
result.append(sum0)
result.append(1)
result.append(sum1)
result.append(2)
result.append(sum2)
result.append(3)
result.append(sum3)
result.append(4)
result.append(sum4)
result.append(5)
result.append(sum5)
# 4. 전국 자동차 모델별 배출량
elif chartnum == 4:
Year = event["body-json"]["Year"]
Month = event["body-json"]["Month"]
response = carboncartypetable.scan(
FilterExpression = Attr("Year").eq(Year)
)
items = response["Items"]
ttmp = []
for item in items:
tmp = []
tmp.append(item["Model"])
tmp.append(item["Emissions"][Month - 1])
result.append(tmp)
# 5. 전국 기준치 대비 배출량
elif chartnum == 5:
Year = event["body-json"]["Year"]
response = carbonemissionstable.scan(
FilterExpression = Attr("Year").eq(Year)
)
items = response["Items"]
for item in items:
tmp = []
tmp.append(item["OType"])
tmp.append(item["StandardE"])
tmp.append(item["RealE"])
result.append(tmp)
else:
return 1
return result
일단 naive로 (머리 쓰는 거 시로) chart API (READ) 구현