feat: throttle no-change notifications daily
This commit is contained in:
@@ -4,6 +4,7 @@ import json
|
||||
import os
|
||||
import shutil
|
||||
from configparser import ConfigParser
|
||||
from datetime import date
|
||||
|
||||
import requests
|
||||
|
||||
@@ -11,6 +12,7 @@ import requests
|
||||
CONFIG_FILE = "ticketing.ini"
|
||||
CONFIG_TEMPLATE_FILE = "ticketing.ini.example"
|
||||
STATE_FILE = "tickets_state.json"
|
||||
NO_CHANGE_STATE_FILE = "no_change_notification_state.json"
|
||||
DEFAULT_TIMEOUT = 30
|
||||
DEFAULT_TICKET_FIELDS = ["id", "name", "stage_id", "partner_id", "user_id", "write_date"]
|
||||
DEFAULT_MAX_NOTIFIED_TICKETS = 50
|
||||
@@ -29,6 +31,10 @@ def get_state_path():
|
||||
return makepath(WAPT.private_dir, STATE_FILE)
|
||||
|
||||
|
||||
def get_no_change_state_path():
|
||||
return makepath(WAPT.private_dir, NO_CHANGE_STATE_FILE)
|
||||
|
||||
|
||||
def install():
|
||||
config_path = get_config_path()
|
||||
|
||||
@@ -53,7 +59,13 @@ def audit():
|
||||
conf_wapt.get("odoo", "model", fallback="helpdesk.ticket"),
|
||||
)
|
||||
|
||||
send_to_rocket(message, conf_wapt=conf_wapt)
|
||||
if should_send_notification(tickets_updated, get_no_change_state_path()):
|
||||
send_to_rocket(message, conf_wapt=conf_wapt)
|
||||
if not tickets_updated:
|
||||
record_no_change_notification(get_no_change_state_path())
|
||||
else:
|
||||
print("Notification aucun changement deja envoyee aujourd'hui.")
|
||||
|
||||
save_tickets_state(get_state_path(), build_tickets_state(tickets))
|
||||
return "OK"
|
||||
|
||||
@@ -284,6 +296,37 @@ def save_tickets_state(state_path, tickets_state):
|
||||
json.dump(tickets_state, state_file, indent=2, sort_keys=True)
|
||||
|
||||
|
||||
def get_today():
|
||||
return date.today().isoformat()
|
||||
|
||||
|
||||
def load_json_state(state_path):
|
||||
if not os.path.exists(state_path):
|
||||
return {}
|
||||
|
||||
with open(state_path, "r") as state_file:
|
||||
return json.load(state_file)
|
||||
|
||||
|
||||
def save_json_state(state_path, state):
|
||||
with open(state_path, "w") as state_file:
|
||||
json.dump(state, state_file, indent=2, sort_keys=True)
|
||||
|
||||
|
||||
def should_send_notification(tickets_updated, no_change_state_path):
|
||||
if tickets_updated:
|
||||
return True
|
||||
|
||||
no_change_state = load_json_state(no_change_state_path)
|
||||
return no_change_state.get("last_no_change_notification_date") != get_today()
|
||||
|
||||
|
||||
def record_no_change_notification(no_change_state_path):
|
||||
save_json_state(no_change_state_path, {
|
||||
"last_no_change_notification_date": get_today(),
|
||||
})
|
||||
|
||||
|
||||
def build_tickets_state(tickets):
|
||||
return {
|
||||
str(ticket["id"]): build_ticket_state_value(ticket)
|
||||
|
||||
Reference in New Issue
Block a user