Refactor logging
Update 4 files
This commit is contained in:
parent
fa366534f8
commit
6a7e996406
4 changed files with 48 additions and 57 deletions
|
|
@ -183,6 +183,9 @@ class OrderStatus(BaseModel):
|
||||||
null=True
|
null=True
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f"{self.pool} {self.id}"
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
table_name = 'order_status'
|
table_name = 'order_status'
|
||||||
|
|
||||||
|
|
|
||||||
23
main.py
23
main.py
|
|
@ -17,28 +17,7 @@ logger = logging.getLogger(__name__)
|
||||||
async def do_order_issue(orders_pool_id):
|
async def do_order_issue(orders_pool_id):
|
||||||
orders_pool = orders_pool_by_id(orders_pool_id)
|
orders_pool = orders_pool_by_id(orders_pool_id)
|
||||||
|
|
||||||
issue_result = await order_issue(orders_pool)
|
await order_issue(orders_pool)
|
||||||
|
|
||||||
if 'order_status' in issue_result:
|
|
||||||
order_status = issue_result['order_status']
|
|
||||||
logger.info(f'Issued order id {order_status.id}')
|
|
||||||
timeline_event_put(
|
|
||||||
TIMELINE_ORDER_ISSUED,
|
|
||||||
order_status.text.split("\n")[0],
|
|
||||||
user=orders_pool.user,
|
|
||||||
orders_pool=orders_pool,
|
|
||||||
order_status=order_status,
|
|
||||||
extra={
|
|
||||||
"mastodon_status_url": issue_result['mastodon_status']['url']
|
|
||||||
}
|
|
||||||
)
|
|
||||||
elif 'reason' in issue_result:
|
|
||||||
timeline_event_put(
|
|
||||||
TIMELINE_ORDER_NOT_ISSUED,
|
|
||||||
issue_result['reason'],
|
|
||||||
user=orders_pool.user,
|
|
||||||
orders_pool=orders_pool
|
|
||||||
)
|
|
||||||
|
|
||||||
if __name__=='__main__':
|
if __name__=='__main__':
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
|
|
|
||||||
47
orders.py
47
orders.py
|
|
@ -2,7 +2,7 @@ import logging
|
||||||
import datetime
|
import datetime
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
from db.constants import TIMELINE_ORDER_CONFIRMED, TIMELINE_ORDER_NOT_PUNISHED, TIMELINE_ORDER_PUNISHED
|
from db.constants import TIMELINE_ORDER_CONFIRMED, TIMELINE_ORDER_ISSUED, TIMELINE_ORDER_NOT_ISSUED, TIMELINE_ORDER_NOT_PUNISHED, TIMELINE_ORDER_PUNISHED
|
||||||
from util import make_session
|
from util import make_session
|
||||||
from generate import generate_order, generate_punishment
|
from generate import generate_order, generate_punishment
|
||||||
from db.queries import domsubusers_doms, order_status_by_id, order_status_put, order_status_confirm, timeline_event_put
|
from db.queries import domsubusers_doms, order_status_by_id, order_status_put, order_status_confirm, timeline_event_put
|
||||||
|
|
@ -70,8 +70,14 @@ async def order_issue(orders_pool):
|
||||||
user = orders_pool.user
|
user = orders_pool.user
|
||||||
|
|
||||||
if user.mastodon_username is None:
|
if user.mastodon_username is None:
|
||||||
logger.info('Cannot issue order without mastodon username')
|
logger.info(f"{orders_pool} - Cannot issue order without mastodon username")
|
||||||
await order_telegram_post_need_mastodon(session, orders_pool)
|
await order_telegram_post_need_mastodon(session, orders_pool)
|
||||||
|
timeline_event_put(
|
||||||
|
TIMELINE_ORDER_NOT_ISSUED,
|
||||||
|
"Cannot issue order without mastodon username",
|
||||||
|
user=orders_pool.user,
|
||||||
|
orders_pool=orders_pool
|
||||||
|
)
|
||||||
return { "reason": "Cannot issue order without mastodon username" }
|
return { "reason": "Cannot issue order without mastodon username" }
|
||||||
|
|
||||||
orders_info = generate_order(orders_pool)
|
orders_info = generate_order(orders_pool)
|
||||||
|
|
@ -79,6 +85,12 @@ async def order_issue(orders_pool):
|
||||||
if 'orders' not in orders_info:
|
if 'orders' not in orders_info:
|
||||||
logger.info(f"{orders_pool} - {orders_info['reason']}")
|
logger.info(f"{orders_pool} - {orders_info['reason']}")
|
||||||
await order_telegram_post_none(session, orders_pool)
|
await order_telegram_post_none(session, orders_pool)
|
||||||
|
timeline_event_put(
|
||||||
|
TIMELINE_ORDER_NOT_ISSUED,
|
||||||
|
orders_info['reason'],
|
||||||
|
user=orders_pool.user,
|
||||||
|
orders_pool=orders_pool
|
||||||
|
)
|
||||||
return { "reason": orders_info['reason'] }
|
return { "reason": orders_info['reason'] }
|
||||||
|
|
||||||
orders_str = "\n".join(orders_info['orders'])
|
orders_str = "\n".join(orders_info['orders'])
|
||||||
|
|
@ -113,8 +125,7 @@ async def order_issue(orders_pool):
|
||||||
verify_at=verify_at
|
verify_at=verify_at
|
||||||
)
|
)
|
||||||
|
|
||||||
return {
|
order_status = order_status_put(
|
||||||
"order_status" : order_status_put(
|
|
||||||
orders_pool,
|
orders_pool,
|
||||||
orders_pool.user,
|
orders_pool.user,
|
||||||
m_status['id'],
|
m_status['id'],
|
||||||
|
|
@ -122,7 +133,23 @@ async def order_issue(orders_pool):
|
||||||
due_at,
|
due_at,
|
||||||
orders_str,
|
orders_str,
|
||||||
verify_at=verify_at
|
verify_at=verify_at
|
||||||
),
|
)
|
||||||
|
|
||||||
|
timeline_event_put(
|
||||||
|
TIMELINE_ORDER_ISSUED,
|
||||||
|
order_status.text.split("\n")[0],
|
||||||
|
user=orders_pool.user,
|
||||||
|
orders_pool=orders_pool,
|
||||||
|
order_status=order_status,
|
||||||
|
extra={
|
||||||
|
"mastodon_status_url": m_status['url']
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
logger.info(f"Issued {order_status}")
|
||||||
|
|
||||||
|
return {
|
||||||
|
"order_status" : order_status,
|
||||||
"mastodon_status": m_status
|
"mastodon_status": m_status
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -156,7 +183,7 @@ async def punishment_telegram_post(session, orders_pool, punishment_str, m_url):
|
||||||
|
|
||||||
async def punishment_issue(session, order_status):
|
async def punishment_issue(session, order_status):
|
||||||
if order_status.pool is None or order_status.pool.punishment_pool is None:
|
if order_status.pool is None or order_status.pool.punishment_pool is None:
|
||||||
logger.info(f'Unable to issue a punishment for {order_status.id}, no punishment pool for order pool {order_status.pool.name}')
|
logger.info(f'Unable to issue a punishment for {order_status}, no punishment pool for order pool {order_status.pool.name}')
|
||||||
return { "reason": "No punishment pool"}
|
return { "reason": "No punishment pool"}
|
||||||
|
|
||||||
punishment_pool = order_status.pool.punishment_pool
|
punishment_pool = order_status.pool.punishment_pool
|
||||||
|
|
@ -217,7 +244,7 @@ async def order_check(order_status_id):
|
||||||
user = order_status.user
|
user = order_status.user
|
||||||
|
|
||||||
if order_status.punishment.count() > 0:
|
if order_status.punishment.count() > 0:
|
||||||
logger.info(f'Punishment already issued for {order_status.id}')
|
logger.info(f'Punishment already issued for {order_status}')
|
||||||
return
|
return
|
||||||
|
|
||||||
m = Mastodon(session)
|
m = Mastodon(session)
|
||||||
|
|
@ -267,7 +294,7 @@ async def order_check(order_status_id):
|
||||||
|
|
||||||
confirmed_at = d['created_at']
|
confirmed_at = d['created_at']
|
||||||
order_status_confirm(order_status.id, confirmed_at)
|
order_status_confirm(order_status.id, confirmed_at)
|
||||||
logger.info('Confirmed order %s' % (order_status.id))
|
logger.info(f"Confirmed order {order_status}")
|
||||||
timeline_event_put(
|
timeline_event_put(
|
||||||
TIMELINE_ORDER_CONFIRMED,
|
TIMELINE_ORDER_CONFIRMED,
|
||||||
order_status.text.split("\n")[0],
|
order_status.text.split("\n")[0],
|
||||||
|
|
@ -281,7 +308,7 @@ async def order_check(order_status_id):
|
||||||
break
|
break
|
||||||
|
|
||||||
if confirmed_at is None:
|
if confirmed_at is None:
|
||||||
logger.info('Order %s remains unconfirmed' % (order_status.id))
|
logger.info(f"Order {order_status} remains unconfirmed")
|
||||||
|
|
||||||
due_at = datetime.datetime.fromisoformat(order_status.due_at)
|
due_at = datetime.datetime.fromisoformat(order_status.due_at)
|
||||||
if(due_at < datetime.datetime.now(datetime.UTC)):
|
if(due_at < datetime.datetime.now(datetime.UTC)):
|
||||||
|
|
@ -297,7 +324,7 @@ async def order_check(order_status_id):
|
||||||
elif user.verify_mastodon_favorite and had_favorites is False:
|
elif user.verify_mastodon_favorite and had_favorites is False:
|
||||||
reason = "No replies had a favorite from a dom or tagged account"
|
reason = "No replies had a favorite from a dom or tagged account"
|
||||||
|
|
||||||
logger.info('Time to issue a punishment for %s' % order_status.id)
|
logger.info(f"Time to issue a punishment for {order_status}")
|
||||||
|
|
||||||
issue_result = await punishment_issue(session, order_status)
|
issue_result = await punishment_issue(session, order_status)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -92,12 +92,11 @@ class OrderScheduler():
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
logger.info(f'Issuing order for {orders_pool.name}[{orders_pool.user.telegram_username}]')
|
|
||||||
|
|
||||||
issue_result = await order_issue(orders_pool)
|
issue_result = await order_issue(orders_pool)
|
||||||
|
|
||||||
if 'order_status' in issue_result:
|
if 'order_status' in issue_result:
|
||||||
order_status = issue_result['order_status']
|
order_status = issue_result['order_status']
|
||||||
|
|
||||||
if order_status.due_at is not None or order_status.verify_at is not None:
|
if order_status.due_at is not None or order_status.verify_at is not None:
|
||||||
# Schedule check
|
# Schedule check
|
||||||
check_time = order_status.verify_at if order_status.verify_at is not None else order_status.due_at
|
check_time = order_status.verify_at if order_status.verify_at is not None else order_status.due_at
|
||||||
|
|
@ -106,23 +105,6 @@ class OrderScheduler():
|
||||||
self.scheduled_check,
|
self.scheduled_check,
|
||||||
args=(order_status.id,)
|
args=(order_status.id,)
|
||||||
)
|
)
|
||||||
timeline_event_put(
|
|
||||||
TIMELINE_ORDER_ISSUED,
|
|
||||||
order_status.text.split("\n")[0],
|
|
||||||
user=orders_pool.user,
|
|
||||||
orders_pool=orders_pool,
|
|
||||||
order_status=order_status,
|
|
||||||
extra={
|
|
||||||
"mastodon_status_url": issue_result['mastodon_status']['url']
|
|
||||||
}
|
|
||||||
)
|
|
||||||
elif 'reason' in issue_result:
|
|
||||||
timeline_event_put(
|
|
||||||
TIMELINE_ORDER_NOT_ISSUED,
|
|
||||||
issue_result['reason'],
|
|
||||||
user=orders_pool.user,
|
|
||||||
orders_pool=orders_pool
|
|
||||||
)
|
|
||||||
|
|
||||||
async def scheduled_check(self, outstanding_order_id):
|
async def scheduled_check(self, outstanding_order_id):
|
||||||
await order_check(outstanding_order_id)
|
await order_check(outstanding_order_id)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue