# Path: solarpi/extensions.py

# 
# Path: solarpi/pvdata/models.py
# class PVData(SurrogatePK, Model):
#     __tablename__ = "pvdata"
# 
#     id = Column(db.Integer(), nullable=False, primary_key=True)
#     created_at = Column(db.Text(), nullable=False, default=dt.datetime.utcnow)
#     dc_1_u = Column(db.Integer(), nullable=True)
#     dc_1_i = Column(db.Float(), nullable=True)
#     ac_1_u = Column(db.Integer(), nullable=True)
#     ac_1_p = Column(db.Integer(), nullable=True)
#     dc_2_u = Column(db.Integer(), nullable=True)
#     dc_2_i = Column(db.Float(), nullable=True)
#     ac_2_u = Column(db.Integer(), nullable=True)
#     ac_2_p = Column(db.Integer(), nullable=True)
#     dc_3_u = Column(db.Integer(), nullable=True)
#     dc_3_i = Column(db.Float(), nullable=True)
#     ac_3_u = Column(db.Integer(), nullable=True)
#     ac_3_p = Column(db.Integer(), nullable=True)
#     current_power = Column(db.Integer(), nullable=True)
#     daily_energy = Column(db.Float(), nullable=True)
#     total_energy = Column(db.Integer(), nullable=True)


import calendar
from datetime import datetime, timedelta
from sqlalchemy import func
from solarpi.extensions import db, cache
from solarpi.pvdata.models import PVData
        .group_by(func.strftime("%Y-%m", PVData.created_at))
        .all()
    )


def get_yearly_average_data():
    """Returns the monthly averages for the previous year
    :return: returns an array of monthly averages for previous years
    """
    current_year = str(datetime.now().year)
    query = """SELECT 
                   avg(monthly_yield),
                   min(monthly_yield),
                   max(monthly_yield) 
               FROM 
                   (
                       SELECT 
                           strftime('%m', created_at) AS month, 
                           max(total_energy) - min(total_energy) AS monthly_yield 
                       FROM 
                           pvdata 
                       WHERE 
                           strftime('%Y', created_at) < ?
                       GROUP BY 
                           strftime('%Y-%m', created_at)
                   ) subq 
               WHERE 
                   monthly_yield > 0 
               GROUP BY 
                   month;"""

# Path: solarpi/extensions.py

# 
# Path: solarpi/pvdata/models.py
# class PVData(SurrogatePK, Model):
#     __tablename__ = "pvdata"
# 
#     id = Column(db.Integer(), nullable=False, primary_key=True)
#     created_at = Column(db.Text(), nullable=False, default=dt.datetime.utcnow)
#     dc_1_u = Column(db.Integer(), nullable=True)
#     dc_1_i = Column(db.Float(), nullable=True)
#     ac_1_u = Column(db.Integer(), nullable=True)
#     ac_1_p = Column(db.Integer(), nullable=True)
#     dc_2_u = Column(db.Integer(), nullable=True)
#     dc_2_i = Column(db.Float(), nullable=True)
#     ac_2_u = Column(db.Integer(), nullable=True)
#     ac_2_p = Column(db.Integer(), nullable=True)
#     dc_3_u = Column(db.Integer(), nullable=True)
#     dc_3_i = Column(db.Float(), nullable=True)
#     ac_3_u = Column(db.Integer(), nullable=True)
#     ac_3_p = Column(db.Integer(), nullable=True)
#     current_power = Column(db.Integer(), nullable=True)
#     daily_energy = Column(db.Float(), nullable=True)
#     total_energy = Column(db.Integer(), nullable=True)


# Path: solarpi/pvdata/helper.py
import calendar
from datetime import datetime, timedelta
from sqlalchemy import func
from solarpi.extensions import db, cache
from solarpi.pvdata.models import PVData

        .group_by(func.strftime("%Y-%m", PVData.created_at))
        .all()
    )


def get_yearly_average_data():
    """Returns the monthly averages for the previous year
    :return: returns an array of monthly averages for previous years
    """
    current_year = str(datetime.now().year)
    query = """SELECT 
                   avg(monthly_yield),
                   min(monthly_yield),
                   max(monthly_yield) 
               FROM 
                   (
                       SELECT 
                           strftime('%m', created_at) AS month, 
                           max(total_energy) - min(total_energy) AS monthly_yield 
                       FROM 
                           pvdata 
                       WHERE 
                           strftime('%Y', created_at) < ?
                       GROUP BY 
                           strftime('%Y-%m', created_at)
                   ) subq 
               WHERE 
                   monthly_yield > 0 
               GROUP BY 
                   month;"""

    result = db.engine.execute(query, current_year)