Commit e817ff5ea43f14fc57645d13db3efa6c7e7203e2

Authored by hitier
1 parent fe74c223

Random Charge Agent only <100

Showing 3 changed files with 12 additions and 2 deletions   Show diff stats
1 Plan de Charge 1 Plan de Charge
2 -------------- 2 --------------
  3 +random_charges create_db feed_from_lesia
app/commands/commands.py
1 import sys 1 import sys
2 -  
3 import click 2 import click
4 import random 3 import random
5 4
  5 +from sqlalchemy.sql import func
6 from sqlalchemy.ext.automap import automap_base 6 from sqlalchemy.ext.automap import automap_base
7 from sqlalchemy.orm import Session 7 from sqlalchemy.orm import Session
8 from sqlalchemy import create_engine 8 from sqlalchemy import create_engine
@@ -58,7 +58,7 @@ def feed_from_lesia(): @@ -58,7 +58,7 @@ def feed_from_lesia():
58 58
59 @bp.cli.command("feed_periods") 59 @bp.cli.command("feed_periods")
60 def feed_periods(): 60 def feed_periods():
61 - for y in ['2019', '2020', '2021']: 61 + for y in range(2014, 2023):
62 for s in ['S1', 'S2']: 62 for s in ['S1', 'S2']:
63 period_name = "{}_{}".format(y, s) 63 period_name = "{}_{}".format(y, s)
64 p = Period(name=period_name) 64 p = Period(name=period_name)
@@ -79,6 +79,14 @@ def random_charges(agent): @@ -79,6 +79,14 @@ def random_charges(agent):
79 capacity_id = random.choice([i for (i,) in db.session.query(Capacity.id).all()]) 79 capacity_id = random.choice([i for (i,) in db.session.query(Capacity.id).all()])
80 period_id = random.choice([i for (i,) in db.session.query(Period.id).all()]) 80 period_id = random.choice([i for (i,) in db.session.query(Period.id).all()])
81 percent = random.choice(range(10, 110, 10)) 81 percent = random.choice(range(10, 110, 10))
  82 + # check max agent charge for the period
  83 + total_charge = db.session.query(func.sum(Charge.charge_rate).label("total_charge")) \
  84 + .filter(Charge.agent_id == agent_id,
  85 + Charge.period_id == period_id
  86 + ).scalar()
  87 + if total_charge is not None and (total_charge + percent)>= 100:
  88 + print("Skipping agent {} for period {}".format(agent_id, period_id))
  89 + continue
82 charge = Charge(agent_id=agent_id, 90 charge = Charge(agent_id=agent_id,
83 project_id=project_id, 91 project_id=project_id,
84 service_id=service_id, 92 service_id=service_id,
requirements.txt
@@ -5,3 +5,4 @@ Flask-Login @@ -5,3 +5,4 @@ Flask-Login
5 Flask-SQLAlchemy 5 Flask-SQLAlchemy
6 Flask-Migrate 6 Flask-Migrate
7 PyMysql 7 PyMysql
  8 +cryptography