mirror of
https://github.com/bellingcat/auto-archiver-api.git
synced 2026-06-11 13:08:34 +03:00
update time metrics
This commit is contained in:
@@ -71,8 +71,8 @@ def count_archives(db:Session):
|
||||
def count_archive_urls(db:Session):
|
||||
return db.query(func.count(models.ArchiveUrl.url)).scalar()
|
||||
|
||||
def count_by_user_since(db:Session, time_delta: timedelta = timedelta(seconds=30)):
|
||||
time_threshold = datetime.now() - time_delta
|
||||
def count_by_user_since(db:Session, seconds_delta: int = 15):
|
||||
time_threshold = datetime.now() - timedelta(seconds=seconds_delta)
|
||||
return db.query(models.Archive.author_id,func.count().label('total'))\
|
||||
.filter(models.Archive.created_at >= time_threshold)\
|
||||
.group_by(models.Archive.author_id)\
|
||||
|
||||
@@ -49,7 +49,7 @@ async def lifespan(app: FastAPI):
|
||||
logging.getLogger("uvicorn.access").disabled = True
|
||||
asyncio.create_task(redis_subscribe_worker_exceptions())
|
||||
asyncio.create_task(refresh_user_groups())
|
||||
asyncio.create_task(measure_disk_utilization())
|
||||
asyncio.create_task(measure_regular_metrics())
|
||||
|
||||
yield # separates startup from shutdown instructions
|
||||
|
||||
@@ -236,8 +236,9 @@ DATABASE_METRICS = Gauge(
|
||||
labelnames=("query", "user")
|
||||
)
|
||||
|
||||
@repeat_every(seconds=15)
|
||||
async def measure_disk_utilization():
|
||||
REPEAT_COUNT_METRICS_SECONDS = 15
|
||||
@repeat_every(seconds=REPEAT_COUNT_METRICS_SECONDS)
|
||||
async def measure_regular_metrics():
|
||||
_total, used, free = shutil.disk_usage("/")
|
||||
DISK_UTILIZATION.labels(type="used").set(used / (2**30))
|
||||
DISK_UTILIZATION.labels(type="free").set(free / (2**30))
|
||||
@@ -252,5 +253,5 @@ async def measure_disk_utilization():
|
||||
DATABASE_METRICS.labels(query="count_archives", user="-").set(count_archives)
|
||||
DATABASE_METRICS.labels(query="count_archive_urls", user="-").set(count_archive_urls)
|
||||
|
||||
for user in crud.count_by_user_since(session):
|
||||
for user in crud.count_by_user_since(session, REPEAT_COUNT_METRICS_SECONDS):
|
||||
DATABASE_METRICS.labels(query="count_by_user", user=user.author_id).set(user.total)
|
||||
|
||||
Reference in New Issue
Block a user