1
0
mirror of https://gitlab.crans.org/bde/nk20-scripts synced 2025-10-20 03:49:33 +02:00

Compare commits

2 Commits

Author SHA1 Message Date
quark
ac93fec326 easy way to compute steals 2025-10-19 20:15:30 +02:00
quark
9e7cef5c97 oauth script 2025-07-09 15:33:13 +02:00
2 changed files with 129 additions and 0 deletions

View File

@@ -0,0 +1,114 @@
# Copyright (C) 2018-2025 by BDE ENS-Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
from django.core.management.base import BaseCommand
from django.db.models import Sum
from note.models import RecurrentTransaction, TransactionTemplate
class Command(BaseCommand):
help = """
Syntax of inventory file:\n
DATE_START=YYYY-MM-DD HH:MM:SS\n
button_id=quantity\n
# some comment\n
...\n
DATE_END=YYYY-MM-DD\n
button_id=quantity\n
...\n
GROCERY\n
button_id=quantity\n
...\n
"""
def add_arguments(self, parser):
parser.add_argument('file', type=str)
def handle(self, *args, **kwargs):
if not kwargs['file']:
kwargs['file'] = '/var/inventory/current.inv'
file = open(kwargs['file'], 'r', encoding='utf-8')
inv_start, inv_end, inv_grocery = {}, {}, {}
start = True
grocery = False
for line in file:
if line[0] == '#':
continue
if start:
if 'DATE_START' in line:
date_start = line.split('=')[1]
elif 'DATE_END' in line:
date_end = line.split('=')[1]
start = False
else:
add_to_dict(line, inv_start)
elif not grocery:
if 'GROCERY' in line:
grocery = True
else:
add_to_dict(line, inv_end)
else:
add_to_dict(line, inv_grocery)
delta_real = delta_from_inv(inv_start, inv_end, inv_grocery)
delta_th = delta_from_note(date_start, date_end, delta_real.keys())
steal_dict = steal(delta_real, delta_th)
if kwargs['verbosity'] > 0:
for button in steal_dict:
text = "%.2f" % steal_dict[button] + \
f"% of steal on button: {button.name} (pk={button.pk})"
if steal_dict[button] > 20:
self.stdout.write(self.style.ERROR(text))
elif steal_dict[button] > 0:
self.stdout.write(self.style.WARNING(text))
else:
self.stdout.write(self.style.SUCCESS(text))
return 0
def add_to_dict(line, d):
pk, quantity = line.split('=')
button = TransactionTemplate.objects.get(pk=pk)
d[button] = int(quantity)
return
def delta_from_note(date_start, date_end, keys):
d = {}
for button in keys:
quantity = RecurrentTransaction.objects.filter(
valid=True,
created_at__gte=date_start,
created_at__lte=date_end,
template__pk=button.pk).aggregate(total=Sum('quantity'))['total']
d[button] = quantity
return d
def delta_from_inv(s, e, g):
d = {}
for button in s:
if button in g:
if button in e:
d[button] = s[button] + g[button] - e[button]
else:
d[button] = s[button] + g[button]
else:
if button in e:
d[button] = s[button] - e[button]
else:
d[button] = s[button]
return d
def steal(real, th):
s = {}
for b in real:
s[b] = 100 * (real[b] - th[b]) / real[b]
return s

15
shell/oauth2_latency Executable file
View File

@@ -0,0 +1,15 @@
#!/usr/bin/sh
# Copyright (C) 2018-2025 by BDE ENS-Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
# use this script for reduce latency with oauth2_provider (cf. https://gitlab.crans.org/bde/nk20/issues/134)
# Tested with django-oauth2-toolkit version 3.0.1
sed -i -e "s/get_all_scopes()/get_all_scopes(scopes=scopes)/g" /var/www/note_kfet/env/lib/python3.11/site-packages/oauth2_provider/views/base.py
sed -i -e '/get_all_scopes()/{N;s/\(.*\)\n\(.*\)/\2\n\1/;s/get_all_scopes()/get_all_scopes(scopes=token_scopes)/}' /var/www/note_kfet/env/lib/python3.11/site-packages/oauth2_provider/models.py
sed -i -e '/get_all_scopes()/{N;s/\(.*\)\n\(.*\)/\2\n\1/;s/get_all_scopes()/get_all_scopes(scopes=read_write_scopes)/}' /var/www/note_kfet/env/lib/python3.11/site-packages/oauth2_provider/views/mixins.py
sed -i -e '/get_all_scopes()/{N;s/\(.*\)\n\(.*\)/\2\n\1/;s/get_all_scopes()/get_all_scopes(scopes=read_write_scopes)/}' /var/www/note_kfet/env/lib/python3.11/site-packages/oauth2_provider/decorators.py