Add prices for lower quantities (500-999L)

Don't forget to run the migratedb command
This commit is contained in:
2022-08-16 00:39:22 +02:00
parent b5e981291f
commit 10d288f3c1
3 changed files with 95 additions and 16 deletions

View File

@@ -11,6 +11,39 @@ from config import conf
from models import IntegrityError, Price from models import IntegrityError, Price
def get_today_prices_from_internet():
query = """query {
getProducts(group: 1, zipcode: 1184) {
tax {
value
}
pricesZone {
price
min_quantity
}
}
}
"""
response = requests.post(
url="https://www.bretagne-multi-energies.fr/graphql",
json={"query": query},
headers={"content-type": "application/json"},
)
json_data = json.loads(response.text)["data"]
json_data = json_data["getProducts"][0]
tax = float(json_data["tax"]["value"])
zones = json_data["pricesZone"]
price_500 = next((x["price"] for x in zones if x["min_quantity"] == 500), None)
price_1000 = next((x["price"] for x in zones if x["min_quantity"] == 1000), None)
# apply tax
price_500 = price_500 * (100 + tax) / 100.0
price_1000 = price_1000 * (100 + tax) / 100.0
# Return an int, for db storage
price_500 = int(price_500 * 1000)
price_1000 = int(price_1000 * 1000)
return price_500, price_1000
def get_today_price_from_internet(): def get_today_price_from_internet():
query = """query { query = """query {
getProductGroups(category: 1, zipcode: 1184) { getProductGroups(category: 1, zipcode: 1184) {
@@ -37,16 +70,16 @@ def get_prices(n=10):
return query return query
def store_current_price(): def store_current_prices():
today = date.today() today = date.today()
# Get price returns a float (price per L) # Get price returns a float (price per L)
# We store an int (price per m^3) # We store an int (price per m^3)
price = int(get_today_price_from_internet() * 1000) price500, price1000 = get_today_prices_from_internet()
try: try:
Price.create(date=today, price=price) Price.create(date=today, price=price1000, price500=price500)
except IntegrityError: except IntegrityError:
print("Price for today already in database, not writing") print("Price for today already in database, not writing")
return price return price500, price1000
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
@@ -74,13 +107,19 @@ def argument(*name_or_flags, **kwargs):
@subcommand() @subcommand()
def update(args): def update(args):
store_current_price() store_current_prices()
@subcommand([argument("n", default=42, nargs="?", type=int)]) @subcommand([argument("n", default=42, nargs="?", type=int)])
def show(args): def show(args):
for x in get_prices(args.n): for x in get_prices(args.n):
print(x.date, x.price) print(x.date, x.price, x.price500)
@subcommand()
def show_current(args):
current_price = get_today_prices_from_internet()
print(current_price)
@subcommand() @subcommand()
@@ -100,6 +139,13 @@ def initdb(args):
init() init()
@subcommand()
def migratedb(args):
from models import migrate_001
migrate_001()
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# Main # Main

View File

@@ -35,8 +35,27 @@ class Price(BaseModel):
id = AutoField() id = AutoField()
date = DateField(unique=True) date = DateField(unique=True)
price = IntegerField() price = IntegerField()
price500 = IntegerField()
def init(): def init():
db.connect() db.connect()
db.create_tables([Price]) db.create_tables([Price])
def migrate_001():
backend = conf["db"].get("backend", None)
if backend == "sqlite":
from playhouse.migrate import SqliteMigrator
migrator = SqliteMigrator(db)
elif backend == "mysql":
from playhouse.migrate import MySQLMigrator
migrator = MysqlMigrator(db)
from playhouse.migrate import migrate
price500_field = IntegerField(null=True)
with db.atomic():
migrate(migrator.add_column("price", "price500", price500_field))

View File

@@ -9,21 +9,35 @@
<body> <body>
<h1>Prix du m³ de fioul pour Languidic</h1> <h1>Prix du m³ de fioul pour Languidic</h1>
<table id="results" class="u-full-width"> <table id="results" class="u-full-width">
<tr>
<th colspan="2"></th> <!-- Date -->
<th>1000-1499 L</th>
<th>500-999 L</th>
</tr>
% for x in results: % for x in results:
<tr class="{{day_classes[x.date.weekday()]}}"> <tr class="{{day_classes[x.date.weekday()]}}">
<td>{{x.date.strftime('%A')}}</td> <td>{{x.date.strftime('%A')}}</td>
<td>{{x.date.strftime('%d %b %Y')}}</td> <td>{{x.date.strftime('%d %b %Y')}}</td>
<td>{{x.price}}</td> <td>{{x.price}}</td>
<td>{{x.price500 if x.price500 is not None else "---"}}</td>
</tr> </tr>
% end % end
</table> </table>
<div id="price-plot"></div> <div id="price-plot"></div>
<script> <script>
var plotDiv = document.getElementById('price-plot'); var plotDiv = document.getElementById('price-plot');
var plotData = [{ var plotData = [
{
x: {{ [str(d.date) for d in results] }}, x: {{ [str(d.date) for d in results] }},
y: {{ [d.price for d in results] }}, y: {{ [d.price for d in results] }},
type: 'scatter', type: 'scatter',
name: '1000-1499 L',
},
{
x: {{ [str(d.date) for d in results if d.price500 is not None] }},
y: {{ [d.price500 for d in results if d.price500 is not None] }},
type: 'scatter',
name: '500-999 L',
}]; }];
var plotLayout = { var plotLayout = {
margin: { t: 0 } margin: { t: 0 }