Skip to content

Commit

Permalink
edit contact db + fix
Browse files Browse the repository at this point in the history
  • Loading branch information
m4dm4rtig4n committed Jan 1, 2023
1 parent e39b429 commit 6945068
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/issue_project.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
name: Add issue to project
runs-on: ubuntu-latest
steps:
- uses: actions/add-to-project@RELEASE_VERSION
- uses: actions/add-to-project@main
with:
# You can target a repository in a different organization
# to the issue
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
"""nullable last_distribution_tariff_change_date in contract
Revision ID: 0b59c59dad3c
Revises: c70a0702d76b
Create Date: 2022-12-30 16:29:42.282094
"""
import sys

import sqlalchemy as sa
from datetime import datetime
from alembic import op
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.sql import table, column
from sqlalchemy import String, Integer, Date, DateTime, Text

# revision identifiers, used by Alembic.
revision = '0b59c59dad3c'
down_revision = 'c70a0702d76b'
branch_labels = None
depends_on = None

Base = declarative_base()


def upgrade() -> None:
conn = op.get_bind()
res = conn.execute("select * from contracts order by id;")
contracts_data = res.fetchall()

op.rename_table('contracts', 'contracts_old')
op.create_table('contracts',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('usage_point_id', sa.Text(), nullable=False),
sa.Column('usage_point_status', sa.Text(), nullable=True),
sa.Column('meter_type', sa.Text(), nullable=True),
sa.Column('segment', sa.Text(), nullable=True),
sa.Column('subscribed_power', sa.Text(), nullable=True),
sa.Column('last_activation_date', sa.DateTime(), nullable=True),
sa.Column('distribution_tariff', sa.Text(), nullable=True),
sa.Column('offpeak_hours_0', sa.Text(), nullable=True),
sa.Column('offpeak_hours_1', sa.Text(), nullable=True),
sa.Column('offpeak_hours_2', sa.Text(), nullable=True),
sa.Column('offpeak_hours_3', sa.Text(), nullable=True),
sa.Column('offpeak_hours_4', sa.Text(), nullable=True),
sa.Column('offpeak_hours_5', sa.Text(), nullable=True),
sa.Column('offpeak_hours_6', sa.Text(), nullable=True),
sa.Column('contract_status', sa.Text(), nullable=True),
sa.Column('last_distribution_tariff_change_date', sa.DateTime(), nullable=True),
sa.Column('count', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['usage_point_id'], ['usage_points.usage_point_id'], ),
sa.PrimaryKeyConstraint('id'),
)

contracts = table('contracts',
column('id', Integer),
column('usage_point_id', Text),
column('usage_point_status', Text),
column('meter_type', Text),
column('segment', Text),
column('subscribed_power', Text),
column('last_activation_date', DateTime),
column('distribution_tariff', Text),
column('offpeak_hours_0', Text),
column('offpeak_hours_1', Text),
column('offpeak_hours_2', Text),
column('offpeak_hours_3', Text),
column('offpeak_hours_4', Text),
column('offpeak_hours_5', Text),
column('offpeak_hours_6', Text),
column('contract_status', Text),
column('last_distribution_tariff_change_date', DateTime),
column('count', Integer),
)

data = []
for contract in contracts_data:
last_activation_date = datetime.strptime(contract[6], "%Y-%m-%d %H:%M:%S.%f")
last_distribution_tariff_change_date = datetime.strptime(contract[16], "%Y-%m-%d %H:%M:%S.%f")
data.append(
{
"id": contract[0],
"usage_point_id": contract[1],
"usage_point_status": contract[2],
"meter_type": contract[3],
"segment": contract[4],
"subscribed_power": contract[5],
"last_activation_date": last_activation_date,
"distribution_tariff": contract[7],
"offpeak_hours_0": contract[8],
"offpeak_hours_1": contract[9],
"offpeak_hours_2": contract[10],
"offpeak_hours_3": contract[11],
"offpeak_hours_4": contract[12],
"offpeak_hours_5": contract[13],
"offpeak_hours_6": contract[14],
"contract_status": contract[15],
"last_distribution_tariff_change_date": last_distribution_tariff_change_date,
"count": contract[17]
}
)

op.bulk_insert(contracts, data)
op.drop_table("contracts_old")


def downgrade() -> None:
conn = op.get_bind()
res = conn.execute("select * from contracts order by id;")
contracts_data = res.fetchall()

op.rename_table('contracts', 'contracts_old')
op.create_table('contracts',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('usage_point_id', sa.Text(), nullable=False),
sa.Column('usage_point_status', sa.Text(), nullable=False),
sa.Column('meter_type', sa.Text(), nullable=False),
sa.Column('segment', sa.Text(), nullable=False),
sa.Column('subscribed_power', sa.Text(), nullable=False),
sa.Column('last_activation_date', sa.DateTime(), nullable=False),
sa.Column('distribution_tariff', sa.Text(), nullable=False),
sa.Column('offpeak_hours_0', sa.Text(), nullable=True),
sa.Column('offpeak_hours_1', sa.Text(), nullable=True),
sa.Column('offpeak_hours_2', sa.Text(), nullable=True),
sa.Column('offpeak_hours_3', sa.Text(), nullable=True),
sa.Column('offpeak_hours_4', sa.Text(), nullable=True),
sa.Column('offpeak_hours_5', sa.Text(), nullable=True),
sa.Column('offpeak_hours_6', sa.Text(), nullable=True),
sa.Column('contract_status', sa.Text(), nullable=False),
sa.Column('last_distribution_tariff_change_date', sa.DateTime(), nullable=False),
sa.Column('count', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['usage_point_id'], ['usage_points.usage_point_id'], ),
sa.PrimaryKeyConstraint('id'),
)

contracts = table('contracts',
column('id', Integer),
column('usage_point_id', Text),
column('usage_point_status', Text),
column('meter_type', Text),
column('segment', Text),
column('subscribed_power', Text),
column('last_activation_date', DateTime),
column('distribution_tariff', Text),
column('offpeak_hours_0', Text),
column('offpeak_hours_1', Text),
column('offpeak_hours_2', Text),
column('offpeak_hours_3', Text),
column('offpeak_hours_4', Text),
column('offpeak_hours_5', Text),
column('offpeak_hours_6', Text),
column('contract_status', Text),
column('last_distribution_tariff_change_date', DateTime),
column('count', Integer),
)

data = []
for contract in contracts_data:
last_activation_date = datetime.strptime(contract[6], "%Y-%m-%d %H:%M:%S.%f")
last_distribution_tariff_change_date = datetime.strptime(contract[16], "%Y-%m-%d %H:%M:%S.%f")
data.append(
{
"id": contract[0],
"usage_point_id": contract[1],
"usage_point_status": contract[2],
"meter_type": contract[3],
"segment": contract[4],
"subscribed_power": contract[5],
"last_activation_date": last_activation_date,
"distribution_tariff": contract[7],
"offpeak_hours_0": contract[8],
"offpeak_hours_1": contract[9],
"offpeak_hours_2": contract[10],
"offpeak_hours_3": contract[11],
"offpeak_hours_4": contract[12],
"offpeak_hours_5": contract[13],
"offpeak_hours_6": contract[14],
"contract_status": contract[15],
"last_distribution_tariff_change_date": last_distribution_tariff_change_date,
"count": contract[17]
}
)

op.bulk_insert(contracts, data)
op.drop_table("contracts_old")
2 changes: 0 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@
CYCLE = cycle_minimun
CONFIG.set("cycle", cycle_minimun)

CYCLE = 60


class FetchAllDataScheduler(object):
JOBS = [{
Expand Down
28 changes: 19 additions & 9 deletions app/models/query_contract.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import __main__ as app
import datetime
import json
import re

from sqlalchemy.sql import func
import traceback
import datetime

from dependencies import *
from models.query import Query
Expand Down Expand Up @@ -36,16 +35,26 @@ def run(self):
response = response_json["customer"]["usage_points"][0]
usage_point = response["usage_point"]
contracts = response["contracts"]
response = contracts
response.update(usage_point)

if contracts['offpeak_hours'] is not None:
offpeak_hours = re.search('HC \((.*)\)', contracts['offpeak_hours']).group(1)
else:
offpeak_hours = ""
last_activation_date = (
datetime.datetime.strptime(contracts['last_activation_date'], '%Y-%m-%d%z')
).replace(tzinfo=None)
last_distribution_tariff_change_date = (
datetime.datetime.strptime(contracts['last_distribution_tariff_change_date'], '%Y-%m-%d%z')
).replace(tzinfo=None)
if "last_activation_date" in contracts and contracts["last_activation_date"] is not None:
last_activation_date = (
datetime.datetime.strptime(contracts['last_activation_date'], '%Y-%m-%d%z')
).replace(tzinfo=None)
else:
last_activation_date = contracts['last_activation_date']
if "last_distribution_tariff_change_date" in contracts and contracts[
"last_distribution_tariff_change_date"] is not None:
last_distribution_tariff_change_date = (
datetime.datetime.strptime(contracts['last_distribution_tariff_change_date'], '%Y-%m-%d%z')
).replace(tzinfo=None)
else:
last_distribution_tariff_change_date = contracts['last_distribution_tariff_change_date']
self.db.set_contract(self.usage_point_id, {
"usage_point_status": usage_point["usage_point_status"],
"meter_type": usage_point["meter_type"],
Expand All @@ -65,6 +74,7 @@ def run(self):
})
except Exception as e:
app.LOG.error(e)
traceback.print_exc()
response = {
"error": True,
"description": "Erreur lors de la récupération du contrat."
Expand Down
2 changes: 0 additions & 2 deletions app/models/query_daily.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,6 @@ def get(self):
result = []

while finish:
print(self.activation_date)

if self.max_days_date > begin:
# Max day reached
begin = self.max_days_date
Expand Down
19 changes: 10 additions & 9 deletions app/models/stat.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ def monthly_evolution(self):

def current_year(self, measurement_direction="consumption"):
app.LOG.log("current_year")
begin = datetime.combine(self.now_date.replace(month=1).replace(day=1), datetime.min.time())
begin = datetime.combine(self.now_date.replace(day=1).replace(month=1), datetime.min.time())
end = self.yesterday_date
for day in app.DB.get_daily_range(self.usage_point_id, begin, end, measurement_direction):
self.value_current_year = self.value_current_year + day.value
Expand All @@ -304,7 +304,7 @@ def current_year(self, measurement_direction="consumption"):
def current_year_last_year(self, measurement_direction="consumption"):
app.LOG.log("current_year_last_year")
begin = datetime.combine(
datetime.combine(self.now_date.replace(month=1).replace(day=1), datetime.min.time()) - relativedelta(
datetime.combine(self.now_date.replace(day=1).replace(month=1), datetime.min.time()) - relativedelta(
years=1),
datetime.min.time())
end = self.yesterday_date - relativedelta(years=1)
Expand All @@ -319,7 +319,7 @@ def current_year_last_year(self, measurement_direction="consumption"):

def last_year(self, measurement_direction="consumption"):
app.LOG.log("last_year")
begin = datetime.combine(self.now_date.replace(month=1).replace(day=1) - relativedelta(years=1),
begin = datetime.combine(self.now_date.replace(day=1).replace(month=1) - relativedelta(years=1),
datetime.min.time())
last_day_of_month = calendar.monthrange(int(begin.strftime("%Y")), 12)[1]
end = datetime.combine(begin.replace(day=last_day_of_month).replace(month=12), datetime.max.time())
Expand Down Expand Up @@ -373,8 +373,9 @@ def peak_offpeak_percent(self, measurement_direction="consumption"):

# STAT V2
def get_year(self, year, measure_type=None, measurement_direction="consumption"):
begin = datetime.combine(self.now_date.replace(year=year).replace(month=1).replace(day=1), datetime.min.time())
end = datetime.combine(self.now_date.replace(year=year).replace(month=12).replace(day=31), datetime.max.time())
begin = datetime.combine(self.now_date.replace(year=year).replace(day=1).replace(month=1), datetime.min.time())
last_day_of_month = calendar.monthrange(year, 12)[1]
end = datetime.combine(self.now_date.replace(year=year).replace(day=last_day_of_month).replace(month=12), datetime.max.time())
value = 0
if measure_type is None:
for day in app.DB.get_daily_range(self.usage_point_id, begin, end, measurement_direction):
Expand Down Expand Up @@ -409,9 +410,9 @@ def get_year_linear(self, idx, measure_type=None, measurement_direction="consump
def get_month(self, year, month=None, measure_type=None, measurement_direction="consumption"):
if month is None:
month = int(datetime.now().strftime('%m'))
begin = datetime.combine(self.now_date.replace(year=year).replace(month=month).replace(day=1), datetime.min.time())
begin = datetime.combine(self.now_date.replace(year=year).replace(day=1).replace(month=month), datetime.min.time())
last_day_of_month = calendar.monthrange(year, month)[1]
end = datetime.combine(self.now_date.replace(year=year).replace(month=month).replace(day=last_day_of_month), datetime.max.time())
end = datetime.combine(self.now_date.replace(year=year).replace(day=last_day_of_month).replace(month=month), datetime.max.time())
value = 0
if measure_type is None:
for day in app.DB.get_daily_range(self.usage_point_id, begin, end, measurement_direction):
Expand Down Expand Up @@ -449,8 +450,8 @@ def get_week(self, year, month=None, measure_type=None, measurement_direction="c
today = date.today()
start = today - timedelta(days=today.weekday())
end = start + timedelta(days=6)
begin = datetime.combine(self.now_date.replace(year=year).replace(month=month).replace(day=int(start.strftime("%d"))), datetime.min.time())
end = datetime.combine(self.now_date.replace(year=year).replace(month=month).replace(day=int(end.strftime("%d"))), datetime.max.time())
begin = datetime.combine(self.now_date.replace(year=year).replace(day=int(start.strftime("%d"))).replace(month=month), datetime.min.time())
end = datetime.combine(self.now_date.replace(year=year).replace(day=int(start.strftime("%d"))).replace(month=month), datetime.max.time())
value = 0
if measure_type is None:
for day in app.DB.get_daily_range(self.usage_point_id, begin, end, measurement_direction):
Expand Down
Binary file removed imgs/grafana_LilY0d4.png
Binary file not shown.
Binary file removed imgs/grafana_geobar78.png
Binary file not shown.

0 comments on commit 6945068

Please sign in to comment.