import mlflow

from cls.rfr_model import RFRModel
from cls.utils import Utils

if __name__ == "__main__":
   # Use sqlite:///mlruns.db as the local store for tracking and registery
   mlflow.set_tracking_uri("sqlite:///mlruns.db")

   # load and print dataset
   csv_path = "data/windfarm_data.csv"
   wind_farm_data = Utils.load_data(csv_path, index_col=0)
   Utils.print_pandas_dataset(wind_farm_data)

   # Get Validation data
   X_train, y_train = Utils.get_training_data(wind_farm_data)
   val_x, val_y = Utils.get_validation_data(wind_farm_data)

   # train, fit and register our model
   params_list = [
      {"n_estimators": 100},
      {"n_estimators": 200},
      {"n_estimators": 300}]

   # Iterate over few different tuning parameters
   model_name = "SKLearnWeatherForestModel"
   for params in params_list:
      rfr = RFRModel.new_instance(params)
      print("Using paramerts={}".format(params))
      runID = rfr.mlflow_run(X_train, y_train, val_x, val_y, model_name)
      print("MLflow run_id={} completed with MSE={} and RMSE={}".format(runID, rfr.mse, rfr.rsme))
Draxl, C., B.M. Hodge, A. Clifton, and J. McCaa. 2015. "The Wind Integration National Dataset (WIND) Toolkit." Applied Energy 151: 355366.

Lieberman-Cribbin, W., C. Draxl, and A. Clifton. 2014. Guide to Using the WIND Toolkit Validation Code (Technical Report, NREL/TP-5000-62595). Golden, CO: National Renewable Energy Laboratory.

King, J., A. Clifton, and B.M. Hodge. 2014. Validation of Power Output for the WIND Toolkit (Technical Report, NREL/TP-5D00-61714). Golden, CO: National Renewable Energy Laboratory.
"""

if __name__ == "__main__":
    # Use sqlite:///mlruns.db as the local store for tracking and registery
    mlflow.set_tracking_uri("sqlite:///mlruns.db")

    # Load and print dataset
    csv_path = "data/windfarm_data.csv"

    # Use column 0 (date) as the index
    wind_farm_data = Utils.load_data(csv_path, index_col=0)
    Utils.print_pandas_dataset(wind_farm_data)

    # Get Validation data
    X_train, y_train = Utils.get_training_data(wind_farm_data)
    val_x, val_y = Utils.get_validation_data(wind_farm_data)

    # Train, fit and register our model
    params_list = [{
        "n_estimators": 100
    }, {
        "n_estimators": 200
    }, {
        "n_estimators": 300
    }]
import requests
from cls.utils import Utils
import json

(x_train, y_train), (val_x, val_y) = Utils.load_data()

data = val_x[0].reshape(1, -1)
data_json = json.dumps(data.tolist())
# print(data_json)
headers = {'Content-Type': 'application/json; format=pandas-records'}
request_uri = 'http://127.0.0.1:5000/invocations'

if __name__ == '__main__':
    try:
        response = requests.post(request_uri, data=data_json, headers=headers)
        print(response.content)
        print('done!!!')
    except Exception as ex:
        raise (ex)