main.py 2.33 KB
Newer Older
1
2
3
4
5
6
7
"""
    # main.py

    - Load된 데이터들에 대해 Linear Regression을 진행합니다.
    - 진행된 후의 Weights를 파일로 저장합니다.
"""

8
import datetime
9
import os
10
import psycopg2
11
from psycopg2.extras import Json
12

13
from config import DB
14
from preprocessing import preprocess
15
16
from model import modeling

17

18
# DB 환경 변수
19
20
dbconfig = {"host": DB["host"], "port": DB["port"], "user": DB["user"],
            "password": DB["password"], "database": DB["database"]}
21
22

data_dir = os.getcwd() + "/src/data_processing/temp.csv"
23

24
25
26
27
28
29
30
31
32

def makeDateForm():
    today = datetime.datetime.today()
    year = str(today.year)
    month = str(today.month) if today.month >= 10 else '0'+str(today.month)
    day = str(today.day) if today.day >= 10 else '0'+str(today.day)
    collected_at = year + "-" + month + "-" + day

    return collected_at
33
34


35
36
# DB 연결
connection = psycopg2.connect(
37
    dbname=dbconfig["database"], user=dbconfig["user"], password=dbconfig["password"], host=dbconfig["host"], port=dbconfig["port"])
38

39
40
# DB에 대한 동작을 위한 cursor 생성
cursor = connection.cursor()
41

42
cursor.execute("SELECT email, loc_code, using_aircon FROM \"Users\"")
43
44
45
46
47
users = cursor.fetchall()

for user in users:

    host = {"email": user[0], "loc_code": user[1], "using_aircon": user[2]}
48

49
    # 데이터 전처리
50
    standard_df, mean_df, std_df = preprocess(cursor, host)
51

52
    # 데이터 분석
53
    modeling(standard_df, host["email"])
54
55

    # 데이터 분석 결과 저장
56
57
58

    collected_at = makeDateForm()

59
60
    model_file_path = "/src/data_processing/models/{0}/model.h5".format(
        host["email"])
61
62
63

    params = {"mean": mean_df.to_json(), "std": std_df.to_json()}

64
65
66
67
68
69
70
71
72
73
74
75
    cursor.execute(
        "SELECT * FROM \"Data_Processings\" WHERE host=%s", (host["email"],))
    user_checker = cursor.fetchall()

    if len(user_checker) == 0:
        cursor.execute("INSERT INTO \"Data_Processings\" (host,collected_at,model_file_path,params) VALUES (%s,%s,%s,%s)",
                       (host["email"], collected_at, model_file_path, Json(params),))

    else:
        cursor.execute("UPDATE \"Data_Processings\" SET collected_at=%s, params=%s WHERE host=%s", (
            collected_at, Json(params), host["email"],
        ))
76
77
78
79
80
81

    connection.commit()

    if os.path.isfile(data_dir):
        os.remove(data_dir)

82
83
# Cursor와 Connection 종료
cursor.close()
84
connection.close()