best_energy_consumption_by_building

ewillwin·2022년 7월 26일
0

TSMtech Record

목록 보기
12/39
import boto3
import json
from datetime import datetime
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 lambda_handler(event, context):
    
    ### get new month
    year= datetime.today().year 
    month=datetime.today().month
    if(month<=3):
        month+=9
        year-=1
    else:
        month-=3
    if(month<10):
        month='0'+str(month)
    current_date=str(year)+str(month)
    response = table.query(
                KeyConditionExpression = Key("date").eq(current_date)
                )
    if(response['Count']==0):
        if(month=='01'):
            Date=str(year-1)+'12'
        else:
            Date=str(year)+'0'+str(int(month)-1)
    else :
        Date=current_date
    intYear = (int(Date) // 100) * 100
    strYear = [0 for i in range(12)]

    response = table.query(
        KeyConditionExpression = Key("date").eq(Date)
        )
    
    flag = set() #시군구코드 저장해놓는 set
    consumption = dict() #sgg:consumption
    building_count = dict() #sgg:count
    
    items = response['Items']
    for item in items:
        tstring = item['addr']
        sgg = tstring[0:5]
        
        cnt = item['count'] or 0
        energysum = item['energy_sum'] or 0
        
        if sgg in flag: #flag에 있는 경우 consumption dictionary 의 값에 누적해서 더함 
            consumption[sgg] += energysum
            building_count[sgg] += cnt
        else: #flag에 없는 경우 consumption dictionary에 key 추가, value 추가
            flag.add(sgg)
            consumption.update({sgg:energysum})
            building_count.update({sgg:cnt})
            
    for key in consumption: #건물 개수로 나누기
        tmp = consumption[key]
        consumption[key] = tmp / building_count[key]
        
    sorted_tuple = sorted(consumption.items(), reverse=True, key=lambda item: item[1])
    
    result = []
    for i in range(5): #상위 5개 뽑기
        mydict = {"addr":0, "energy_sum":0, "co2_sum":0}
        temp_tuple = sorted_tuple[i]
        temp_list = list(temp_tuple)
        temp_list[0] += "00000"; mydict['addr'] = temp_list[0]
        mydict['energy_sum'] = temp_list[1]
        mydict['co2_sum'] = temp_list[1] * 424
        result.append(mydict)
    
    return result

상위 5개 시군구에 대해 -> 행정동코드 & 전체소비량 / 건물 개수 (kWh & g)
co2_sum = energy_sum * 424 (g)

import boto3
import json
from datetime import datetime
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 lambda_handler(event, context):
    Type = int(event["params"]["querystring"]["type"])
    
    ### get new month
    year= datetime.today().year 
    month=datetime.today().month
    if(month<=3):
        month+=9
        year-=1
    else:
        month-=3
    if(month<10):
        month='0'+str(month)
    current_date=str(year)+str(month)
    response = table.query(
                KeyConditionExpression = Key("date").eq(current_date)
                )
    if(response['Count']==0):
        if(month=='01'):
            Date=str(year-1)+'12'
        else:
            Date=str(year)+'0'+str(int(month)-1)
    else :
        Date=current_date
    intYear = (int(Date) // 100) * 100
    strYear = [0 for i in range(12)]

    response = table.query(
        KeyConditionExpression = Key("date").eq(Date)
        )
    
    flag = set() #시군구코드 저장해놓는 set
    consumption = dict() #sgg:consumption
    building_count = dict() #sgg:count
    
    items = response['Items']
    for item in items:
        tstring = item['addr']
        sgg = tstring[0:5]
        
        cnt = item['count'] or 0
        energysum = item['energy_sum'] or 0
        
        if sgg in flag: #flag에 있는 경우 consumption dictionary 의 값에 누적해서 더함 
            consumption[sgg] += energysum
            building_count[sgg] += cnt
        else: #flag에 없는 경우 consumption dictionary에 key 추가, value 추가
            flag.add(sgg)
            consumption.update({sgg:energysum})
            building_count.update({sgg:cnt})
    
    if Type == 0:        
        for key in consumption: #건물 개수로 나누기
            tmp = consumption[key]
            consumption[key] = tmp / building_count[key]
            

    sorted_tuple = sorted(consumption.items(), reverse=True, key=lambda item: item[1])
    
    result = []
    
    for i in range(5): #상위 5개 뽑기
        mydict = {"addr":0, "energy_sum":0, "co2_sum":0}
        temp_tuple = sorted_tuple[i]
        temp_list = list(temp_tuple)
        temp_list[0] += "00000"; mydict['addr'] = temp_list[0]
        mydict['energy_sum'] = temp_list[1]
        mydict['co2_sum'] = temp_list[1] * 424
        result.append(mydict)
    
    return result

건물당x 전체소비량 상위 5개 시군구

profile
Software Engineer @ LG Electronics

0개의 댓글