Spaces:
Sleeping
Sleeping
SimHS98 commited on
Commit ยท
dabfb2e
1
Parent(s): 76a8333
230 unmodified lines2312322332342352362372382392402412422432442452 unmodified lines248249250251252253254255256257258259260261262263264265266267230 unmodified lineselif menu == "โ๏ธ ๋ฐ์ดํฐ ๊ด๋ฆฌ (ETL)":st.title("โ๏ธ ๋ฐ์ดํฐ ๊ด๋ฆฌ (Google Sheet-to-Sheet ETL)")@st.cache_data(ttl=600)def load_source_data(_client):spreadsheet = _client.open_by_key(SOURCE_GSHEET_KEY)ws = spreadsheet.worksheet(SOURCE_SHEET_NAME)header_rows = ws.get('1:3')data = ws.get('4:' + str(ws.row_count))max_len = max(len(r) for r in header_rows)header_rows = [r + [''] * (max_len - len(r)) for r in header_rows]data = [r + [''] * (max_len - len(r)) for r in data]def fill_left_merge(row):last = ''new = []2 unmodified lineslast = c.strip()new.append(last)return newheader_rows = [fill_left_merge(r) for r in header_rows]header_df = pd.DataFrame(header_rows).T.replace('', pd.NA).ffill(axis=1)filled = header_df.T.values.tolist()h1, h2, h3 = [filled[i] for i in range(3)]df = pd.DataFrame(data)df.columns = pd.MultiIndex.from_arrays([h1, h2, h3])df.columns = df.columns.to_frame().fillna(method="ffill").apply(tuple, axis=1).valuesreturn dfif st.button("๐ ETL ์คํ (์๋ณธ โ ๊ฒฐ๊ณผ)"):client = authenticate_google_sheets()df_wide = load_source_data(client)spreadsheet = client.open_by_key(DEST_GSHEET_KEY)ws_sales = spreadsheet.worksheet(DEST_SHEET_STG_SALES)ws_sales.clear()set_with_dataframe(ws_sales, df_wide, include_index=False, resize=True)st.success("โ ETL ์๋ฃ! ๋ฐ์ดํฐ ์ ๋ก๋ ์๋ฃ.")230 unmodified lines2312322332342352362372382392402412422432442452462472482492502512 unmodified lines254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343230 unmodified lineselif menu == "โ๏ธ ๋ฐ์ดํฐ ๊ด๋ฆฌ (ETL)":st.title("โ๏ธ ๋ฐ์ดํฐ ๊ด๋ฆฌ (Google Sheet-to-Sheet ETL)")# 1๏ธโฃ ์๋ณธ ์ํธ ๋ก๋ ๋ฐ ๋ฉํฐํค๋ ๋ณํฉ ํด์ @st.cache_data(ttl=600)def load_source_data(_client):spreadsheet = _client.open_by_key(SOURCE_GSHEET_KEY)ws = spreadsheet.worksheet(SOURCE_SHEET_NAME)header_rows = ws.get('1:3') # ์๋จ 3์ค ํค๋data = ws.get('4:' + str(ws.row_count)) # ์ค์ ๋ฐ์ดํฐ# ํ ๊ธธ์ด ๋ง์ถ๊ธฐmax_len = max(len(r) for r in header_rows)header_rows = [r + [''] * (max_len - len(r)) for r in header_rows]data = [r + [''] * (max_len - len(r)) for r in data]# ๋ณํฉ๋ ์ (์ผ์ชฝ ๊ธฐ์ค) ํ์ฅdef fill_left_merge(row):last = ''new = []2 unmodified lineslast = c.strip()new.append(last)return newheader_rows = [fill_left_merge(r) for r in header_rows]# ์ธ๋ก ๋ณํฉ ํ์ฅheader_df = pd.DataFrame(header_rows).T.replace('', pd.NA).ffill(axis=1)filled = header_df.T.values.tolist()h1, h2, h3 = [filled[i] for i in range(3)]# ๋ฉํฐํค๋ โ ํํํdf = pd.DataFrame(data)df.columns = pd.MultiIndex.from_arrays([h1, h2, h3])df.columns = ["_".join([str(x) for x in col if str(x).strip() != ""])for col in df.columns]# ์ฒซ ๋ฒ์งธ ์ด ์ด๋ฆ 'Date'๋ก ํต์ผif "์ผ์" in df.columns:df.rename(columns={"์ผ์": "Date"}, inplace=True)return df# 2๏ธโฃ Sales / Ads ์๋ ๋ถ๋ฆฌ ํจ์def transform_to_standard_format(df):if df.empty:return pd.DataFrame(), pd.DataFrame()df["Date"] = pd.to_datetime(df["Date"], errors="coerce")# ๊ตญ๊ฐ/์ฑ๋ ์ถ์ def detect_country(name):for c in ["SG", "MY", "PH", "TW", "TH", "VN", "US"]:if c in name: return creturn "Unknown"def detect_channel(name):name = name.lower()if "shopify" in name: return "Shopify"if "shopee" in name: return "Shopee"if "amazon" in name or "asin" in name: return "Amazon"if "tiktok" in name: return "TikTok"return "Unknown"# ๋งค์ถ/๊ด๊ณ ์ปฌ๋ผ ๋ถ๋ฆฌsales_cols = [c for c in df.columns if any(k in c for k in ["๋งค์ถ", "sales", "Revenue", "GMV"])]ads_cols = [c for c in df.columns if any(k in c for k in ["์ง์ถ", "๊ด๊ณ ", "ad", "spend", "cost"])]# --- stg_Sales ---df_sales = pd.melt(df, id_vars=["Date"], value_vars=sales_cols, var_name="Metric_Raw", value_name="Amount_Local")df_sales["Country"] = df_sales["Metric_Raw"].apply(lambda x: detect_country(x))df_sales["Channel"] = df_sales["Metric_Raw"].apply(lambda x: detect_channel(x))df_sales["Currency"] = df_sales["Metric_Raw"].apply(lambda x: "KRW" if "KRW" in x else "Local")df_sales["Amount_KRW"] = df_sales["Amount_Local"]df_sales = df_sales[["Date", "Country", "Channel", "Amount_Local", "Currency", "Amount_KRW"]]# --- stg_Ads ---df_ads = pd.melt(df, id_vars=["Date"], value_vars=ads_cols, var_name="Metric_Raw", value_name="Amount_Local")df_ads["Country"] = df_ads["Metric_Raw"].apply(lambda x: detect_country(x))df_ads["Channel"] = df_ads["Metric_Raw"].apply(lambda x: detect_channel(x))df_ads["Currency"] = df_ads["Metric_Raw"].apply(lambda x: "KRW" if "KRW" in x else "Local")df_ads["Amount_KRW"] = df_ads["Amount_Local"]df_ads["Campaign"] = ""df_ads = df_ads[["Date", "Country", "Channel", "Campaign", "Amount_Local", "Currency", "Amount_KRW"]]return df_sales, df_ads# 3๏ธโฃ ์คํ ๋ฒํผ (Sales + Ads ์ํธ ๊ฐ๊ฐ ์ ๋ก๋)if st.button("๐ ETL ์คํ (์๋ณธ โ ๊ฒฐ๊ณผ)"):client = authenticate_google_sheets()df_wide = load_source_data(client)# ๋ฐ์ดํฐ ๋ณํstg_sales_df, stg_ads_df = transform_to_standard_format(df_wide)spreadsheet = client.open_by_key(DEST_GSHEET_KEY)# stg_Salesws_sales = spreadsheet.worksheet(DEST_SHEET_STG_SALES)ws_sales.clear()set_with_dataframe(ws_sales, stg_sales_df, include_index=False, resize=True)# stg_Adsws_ads = spreadsheet.worksheet(DEST_SHEET_STG_ADS)ws_ads.clear()set_with_dataframe(ws_ads, stg_ads_df, include_index=False, resize=True)st.success("โ ETL ์๋ฃ! stg_Sales ๋ฐ stg_Ads ๋ฐ์ดํฐ ์ ๋ก๋ ์๋ฃ.")
main.py+81-5