import influxdb_client, os, time
from influxdb_client import InfluxDBClient, Point, WritePrecision
from influxdb_client.client.write_api import SYNCHRONOUS
from sqlalchemy import create_engine
import pandas as pd

token = "AcU0_L5vtfH-9ycHnk0txF3jgHIcWRcMOElzAVtIrAGXC0oNSjwZUzPBsWCBZHNbaJmr-9x34E1Hycow59MnQg=="
org = "econ"
url = "http://localhost:8086"
bucket="econ"

posturl='postgresql://admin:econ@localhost:15432/econ'

write_client = None
EXInit=False
EXBooksData=[]
EXTradeData=[]
BUSINESSData=[]
def get_client():
    global write_client
    if write_client==None:
        write_client=influxdb_client.InfluxDBClient(url=url, token=token, org=org)
    return write_client
def writeEXData():
    db = create_engine(posturl)
   
    df=pd.DataFrame(EXBooksData)
    types=df.dtypes
    start=time.time()
    df.to_sql('cx_books', con=db, if_exists='replace',
          index=False,chunksize=200000)
    t1=time.time()
    print(f"cx_books completed: {t1-start}/{df.size}")
    df=pd.DataFrame(EXTradeData)
    df.to_sql("cx_trades",con=db, if_exists='replace',
          index=False,chunksize=10000)
    t2=time.time()
    print(f"cx_trades completed: {t2-t1}/{df.size}")

def writeBusinessData():
    db = create_engine(posturl)
    df=pd.DataFrame(BUSINESSData)
    df.to_sql('business', con=db, if_exists='replace',
          index=False,chunksize=10000)
    print(f"business completed: {df.size}")
