109 lines
3.6 KiB
Python
109 lines
3.6 KiB
Python
"""Lark Snooper queries built on the upstream LarkAgentX storage."""
|
|
|
|
from datetime import datetime
|
|
|
|
from larkx.storage import Storage
|
|
from larkx.storage.models import Base, Message
|
|
from sqlalchemy import BigInteger, Column, DateTime, Integer, String, asc
|
|
|
|
|
|
class SyncProgress(Base):
|
|
__tablename__ = "sync_progress"
|
|
|
|
chat_id = Column(String(32), primary_key=True)
|
|
last_synced_date = Column(String(10), default="")
|
|
min_scanned_position = Column(Integer, default=0)
|
|
min_scanned_time = Column(BigInteger, default=0)
|
|
updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now)
|
|
|
|
|
|
class SnifferStorage(Storage):
|
|
def get_received_messages(
|
|
self, start_ts: int, end_ts: int, chat_ids=None, exclude_chat_ids=None
|
|
):
|
|
"""Return incoming messages in [start_ts, end_ts), honoring chat filters."""
|
|
session = self.Session()
|
|
try:
|
|
query = session.query(Message).filter(
|
|
Message.direction == "in",
|
|
Message.create_time >= int(start_ts),
|
|
Message.create_time < int(end_ts),
|
|
)
|
|
if chat_ids:
|
|
query = query.filter(
|
|
Message.chat_id.in_([str(chat_id) for chat_id in chat_ids])
|
|
)
|
|
if exclude_chat_ids:
|
|
query = query.filter(
|
|
~Message.chat_id.in_(
|
|
[str(chat_id) for chat_id in exclude_chat_ids]
|
|
)
|
|
)
|
|
return query.order_by(asc(Message.create_time), asc(Message.id)).all()
|
|
finally:
|
|
session.close()
|
|
|
|
def has_messages_for_day(
|
|
self, start_ts: int, end_ts: int, chat_ids=None, exclude_chat_ids=None
|
|
) -> bool:
|
|
"""Check whether any messages already exist for the target day."""
|
|
session = self.Session()
|
|
try:
|
|
query = session.query(Message.id).filter(
|
|
Message.create_time >= int(start_ts),
|
|
Message.create_time < int(end_ts),
|
|
)
|
|
if chat_ids:
|
|
query = query.filter(
|
|
Message.chat_id.in_([str(chat_id) for chat_id in chat_ids])
|
|
)
|
|
if exclude_chat_ids:
|
|
query = query.filter(
|
|
~Message.chat_id.in_(
|
|
[str(chat_id) for chat_id in exclude_chat_ids]
|
|
)
|
|
)
|
|
return query.first() is not None
|
|
finally:
|
|
session.close()
|
|
|
|
|
|
def get_sync_progress(self, chat_id: str) -> SyncProgress | None:
|
|
session = self.Session()
|
|
try:
|
|
return (
|
|
session.query(SyncProgress)
|
|
.filter_by(chat_id=str(chat_id))
|
|
.first()
|
|
)
|
|
finally:
|
|
session.close()
|
|
|
|
def update_sync_progress(
|
|
self,
|
|
chat_id: str,
|
|
synced_date: str,
|
|
min_position: int,
|
|
min_time: int,
|
|
) -> None:
|
|
session = self.Session()
|
|
try:
|
|
row = (
|
|
session.query(SyncProgress)
|
|
.filter_by(chat_id=str(chat_id))
|
|
.first()
|
|
)
|
|
if not row:
|
|
row = SyncProgress(chat_id=str(chat_id))
|
|
session.add(row)
|
|
row.last_synced_date = synced_date
|
|
if min_position and (
|
|
not row.min_scanned_position
|
|
or min_position < row.min_scanned_position
|
|
):
|
|
row.min_scanned_position = min_position
|
|
row.min_scanned_time = min_time
|
|
session.commit()
|
|
finally:
|
|
session.close()
|