import json
from iotfunctions.db import Database

with open('credentials.json', encoding='utf-8') as F:
    credentials = json.loads(F.read())

db_schema = None

db = Database(credentials=credentials)

from custom.functions import PrevDayHourlyAvgDiff, PrevDayHourlyAvgPercentDiff, PrevDayDailyAvgPercentDiff
db.register_functions([PrevDayHourlyAvgDiff, PrevDayHourlyAvgPercentDiff, PrevDayDailyAvgPercentDiff],
                      url='git+https://github.com/rweidinger/functions@')

# fn = PrevDayHourlyAvgPercentDiff(
#     input_item='GallonsRemaining',
#     output_item='GallonsRemaining_PctChg')
#
# import datetime as dt
#
# today = dt.datetime.today()
# print('Right now it is:', today)
# yesterday = today - dt.timedelta(days=1)
# yesterday_date = yesterday.date()
# yesterday_hour = yesterday.hour
#
# df = fn.execute_local_test(generate_days=3, to_csv=True)
# print(df.loc[(df['deviceid'] == '73001') &
#              (df['_timestamp'].dt.date == today.date()) &
#              (df['_timestamp'].dt.hour == today.hour)]
#             [['GallonsRemaining', 'GallonsRemaining_PctChg']].tail(10))
#
# yesterday_hour_values = df.loc[(df['_timestamp'].dt.date == yesterday_date) &
#              (df['_timestamp'].dt.hour == yesterday_hour) &
#              (df['deviceid'] == '73001')][['_timestamp', 'GallonsRemaining']]
# yesterday_hour_values_mean = yesterday_hour_values['GallonsRemaining'].mean()
# print(yesterday_hour_values[['_timestamp', 'GallonsRemaining']].tail())
# print(yesterday_hour_values_mean)














