-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimport_mysql.py
executable file
·51 lines (45 loc) · 2.25 KB
/
import_mysql.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3
# DB seeded with
# LOAD DATA INFILE "AllZones.csv" INTO TABLE zones FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES (ZoneId,Description,RegionId,CountryId,UtmGrid,GridRef,ZoneControlState,@DateCapturedUtc,LegionCount,SwarmCount,FacelessCount,@LastUpdateDateUtc,Latitude,Longitude) SET DateCapturedUtc = IF(@DateCapturedUtc = '', NULL, @DateCapturedUtc), LastUpdateDateUtc = IF(@LastUpdateDateUtc = '', NULL, @LastUpdateDateUtc);
import mysql.connector
import config
import pandas as pd
import sys
from tqdm.auto import tqdm
db = mysql.connector.connect(
host="localhost",
user=config.user,
password=config.password,
database="qonqr",
autocommit=True
)
if len(sys.argv) > 1:
files = sys.argv[1:]
cur = db.cursor(dictionary=True)
for f in files:
print(f"Loading {f}")
df = pd.read_csv(f)
df.DateCapturedUtc = pd.to_datetime(df.DateCapturedUtc).dt.floor("s")
df.LastUpdateDateUtc = pd.to_datetime(df.LastUpdateDateUtc).dt.floor("s")
print("Loaded. Fetching existing data...")
cur.execute(f"SELECT * FROM zones WHERE ZoneID IN ({','.join(df.ZoneId.astype(str))})")
existing_data = cur.fetchall()
print(f"{len(existing_data)} existing rows fetched")
updates = 0
for e in tqdm(existing_data):
match = df[df.ZoneId == e["ZoneId"]].iloc[0].to_dict()
if not e["LastUpdateDateUtc"] or match["LastUpdateDateUtc"] > e["LastUpdateDateUtc"]:
match["TotalCount"] = match["LegionCount"] + match["SwarmCount"] + match["FacelessCount"]
match["LegionDelta"] = match["LegionCount"] - e["LegionCount"]
match["SwarmDelta"] = match["SwarmCount"] - e["SwarmCount"]
match["FacelessDelta"] = match["FacelessCount"] - e["FacelessCount"]
match["TotalDelta"] = abs(match["LegionDelta"]) + abs(match["SwarmDelta"]) + abs(match["FacelessDelta"])
for k, v in match.items():
if pd.isnull(v):
match[k] = "NULL"
else:
match[k] = f'"{v}"'
sql = f"""REPLACE INTO zones ({','.join(match.keys())}) VALUES ({','.join(match.values())})"""
updates += 1
cur.execute(sql)
print(f"Did {updates} updates")