-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathupdate_timezone.py
More file actions
56 lines (49 loc) · 1.9 KB
/
Copy pathupdate_timezone.py
File metadata and controls
56 lines (49 loc) · 1.9 KB
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
52
53
54
55
56
#!/usr/bin/python3
'''Converting timestamps in the Inrix database to local time.'''
import logging
from psycopg2 import connect
def get_yyyymm(yyyy, mm):
'''Combine integer yyyy and mm into a string yyyymm.'''
if mm < 10:
return str(yyyy)+'0'+str(mm)
else:
return str(yyyy)+str(mm)
def _update_table(yyyymm, logger, con, cursor):
'''Update the inrix.raw_data partitioned table with yyyymm
to the local timezone. '''
table = 'inrix.raw_data'+yyyymm
logger.info('Updating timestamps on table %s', table)
cursor.execute("UPDATE {table} "
"SET tx = (tx AT TIME ZONE 'UTC') AT TIME ZONE 'America/Toronto' ;"
.format(table=table))
con.commit()
def update_tables(years, dbset, logger):
'''Update a series of tables based on the years dictionary \
and the dbset database connection.'''
logger.info('Connecting to host:%s database: %s with user %s',
dbset['database'],
dbset['host'],
dbset['user'])
con = connect(database=dbset['database'],
host=dbset['host'],
user=dbset['user'],
password=dbset['password'])
cursor = con.cursor()
for year in years:
for month in years[year]:
yyyymm = get_yyyymm(year, month)
_update_table(yyyymm, logger, con, cursor)
con.close()
if __name__ == "__main__":
#For initial run, creating years and months of available data as a python dictionary
YEARS = {"2012":range(7, 13),
"2013":range(1, 7),
"2016":range(1, 7),
"2014":range(1, 13),
"2015":range(1, 13)}
#Configure logging
FORMAT = '%(asctime)-15s %(message)s'
logging.basicConfig(level=logging.INFO, format=FORMAT)
LOGGER = logging.getLogger(__name__)
from dbsettings import dbsetting
update_tables(YEARS, dbsetting, LOGGER)