"""
Copyright 2018 Attunity
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

# coding: utf-8
import os, datetime, socket
from snowflake import connector
import base64
import configparser

CONFIG = configparser.RawConfigParser(allow_no_value=True)
CONFIG.read('settings.ini')
sf_logins = []
for option, value in CONFIG.items('sf_targets'):
    itemlist = value.split(',')
    pw = bytes(itemlist[2], 'UTF8')
    npw = base64.b64decode(pw).decode('UTF8')
    sf_logins.append([itemlist[0],itemlist[1],npw,itemlist[3],itemlist[4],itemlist[5]])

nowstamp = datetime.datetime.now()
script_name = os.path.basename(__file__)
object_list = []
sfquery = CONFIG.get('global', 'query')
sfdict = {}
        
print(f'Starttime: {nowstamp}')
updaterange = len(sf_logins)
total_bytes = 0
seperator = ";"
csvfile = open('results.csv', 'w')
for i in range(updaterange):
    login_name=sf_logins[i][1]
    login_password=sf_logins[i][2]
    sf_account=sf_logins[i][3]
    sf_warehouse=sf_logins[i][4]
    sf_database=f'PL_{sf_logins[i][0]}'
    sf_schema=sf_logins[i][5]
    sf_env = sf_logins[i][0]
    counter = 0
    try:
        conn = connector.connect(
            user=login_name,
            password=str(login_password),
            account=sf_account,
            database=sf_database,
            schema=sf_schema,
            )
        conn._paramstyle='qmark'
    except Exception as e:
        print(f'Unable to connect to Snowflake schema {sf_schema}  in {sf_env} using {login_name}')
        #print(str(e))
    else:
        rootdir = os.getcwd()

        server_name = socket.gethostname()

        current_time = datetime.datetime.now()
        cur = conn.cursor()
        #cur.execute(f'USE DATABASE {sf_env}')
        cur.execute(sfquery)
        cur.get_results_from_sfqid(cur.sfqid)
        results = cur.fetchall()
        print(f'result retrieval successful.')
        for result in results:
            try:
                line_string = f'{sf_env};{seperator.join(result)}'
            except Exception as e:
                print(f'result set skipped for {result[1]}')
            else:
                csvfile.write(f"{line_string}\n")
                result_bytes = int(result[3].replace(' GB', ''))
                total_bytes = total_bytes + result_bytes

conn.close()
csvfile.close()
print(f"{str(total_bytes)} GB")
print(f'Endtime: {nowstamp}')

