53 lines
2.6 KiB
Python
53 lines
2.6 KiB
Python
import sys
|
|
import time
|
|
import datetime
|
|
import argparse
|
|
import os.path
|
|
from influxdb import InfluxDBClient
|
|
import pandas as pd
|
|
import pymssql
|
|
from sqlalchemy import create_engine, MetaData, Table, select
|
|
from datetime import date, timedelta
|
|
|
|
def parse_args():
|
|
parser = argparse.ArgumentParser(
|
|
description='Sync MSSQL for PowerBI with InfluxDB database')
|
|
parser.add_argument('--influxhost', type=str, required=False, default='itmetricsdb.mathworks.com',
|
|
help='hostname of InfluxDB http API')
|
|
parser.add_argument('--influxport', type=int, required=False, default=8086,
|
|
help='port of InfluxDB http API')
|
|
parser.add_argument('--mssqlhost', type=str, required=False, default='itmr-00-ah.mathworks.com',
|
|
help='hostname of Microsoft SQL server holding PowerBI data')
|
|
parser.add_argument('--mssqldb', type=str, required=False, default='ASPM',
|
|
help='MSSQL Table Name of data to be synced from InfluxDB')
|
|
parser.add_argument('--mssqltable', type=str, required=False, default='ApdexScore',
|
|
help='MSSQL Table Name of data to be synced from InfluxDB')
|
|
parser.add_argument('--days', type=str, required=False, default=1,
|
|
help='Number of days to sync - will go from midnight UTC prior day back this many days')
|
|
return parser.parse_args()
|
|
|
|
# Main
|
|
def main(influxhost, influxport):
|
|
yesterday = str(date.today() - timedelta(90))
|
|
today = str(date.today())
|
|
# create a configuration
|
|
# Set influxDB data and connect
|
|
influxuser = 'CMWrite'
|
|
influxpass = 'CMWrite'
|
|
mssqluser = 'apdex'
|
|
mssqlpass = 'apdex'
|
|
databasename = mssqldb
|
|
client = InfluxDBClient(host=influxhost, port=influxport, username=influxuser, password=influxpass, database=databasename)
|
|
query = "select * from ApdexScore where time >= \'" + yesterday + "\' and time < \'" + today + "\'"
|
|
points = client.query(query, chunked=True, chunk_size=10000).get_points()
|
|
df = pd.DataFrame(points)
|
|
print df
|
|
## instance a python db connection object- same form as psycopg2/python-mysql drivers also
|
|
engine = create_engine('mssql+pyodbc://apdex:apdex@' + 'itmr-00-ah' + '/' + 'apdex?driver=SQL+Server+Native+Client+11.0')
|
|
conn = engine.connect()
|
|
df.to_sql(con=engine, name='apdex', if_exists='replace')
|
|
|
|
# grab arguments
|
|
if __name__ == '__main__':
|
|
args = parse_args()
|
|
main(influxhost=args.influxhost, influxport=args.influxport, mssqlhost=args.mssqlhost, mssqltable=args.mssqltable, days=args.days, mssqldb=args.mssqldb) |