49 lines
1.6 KiB
Python
49 lines
1.6 KiB
Python
import psycopg2
|
|
import sqlalchemy
|
|
import pandas as pd
|
|
import argparse
|
|
import sys
|
|
import datetime
|
|
import dateutil.relativedelta
|
|
|
|
|
|
parser = argparse.ArgumentParser(description="export_month.py")
|
|
parser.add_argument('--lastmonth', '-l',
|
|
help="Export data of the last month",
|
|
required=False,
|
|
action='store_true',
|
|
default=False)
|
|
parser.add_argument('--month', '-m',
|
|
help="Export data of the given month, format: YYYY-MM-DD",
|
|
required=False,
|
|
default='0000-00-00')
|
|
|
|
args = parser.parse_args()
|
|
if not args.lastmonth and args.month == '0000-00-00':
|
|
raise Exception('Either --lastmonth or --month must be given')
|
|
if args.lastmonth and args.month != '0000-00-00':
|
|
raise Exception('Only one of --lastmonth and --month must be given')
|
|
|
|
if args.lastmonth:
|
|
startdate = datetime.date.today().replace(day=1) - dateutil.relativedelta.relativedelta(months=1)
|
|
else:
|
|
startdate = datetime.datetime.strptime(args.month, '%Y-%m-%d').replace(day=1)
|
|
|
|
enddate = startdate + dateutil.relativedelta.relativedelta(months=1)
|
|
|
|
|
|
try:
|
|
engine = sqlalchemy.create_engine('postgresql://wn@db.hottis.de/mainscnt')
|
|
with engine.connect() as conn:
|
|
dat = pd.read_sql_query(f"select time, location, freq from mainsfrequency where valid=1 and time >= '{startdate}' and time < '{enddate}'", con=conn)
|
|
finally:
|
|
engine.dispose()
|
|
|
|
|
|
dat = dat.pivot_table(index='time', columns='location', values='freq')
|
|
|
|
csv_filename = f"{startdate.strftime('%Y-%m')}.csv"
|
|
dat.to_csv(csv_filename)
|
|
|
|
|