This repository has been archived on 2020-03-24. You can view files and clone it, but cannot push or open issues or pull requests.
quay/workers/repositoryactioncounter.py

64 lines
2 KiB
Python
Raw Normal View History

import logging
from datetime import date, timedelta
2016-10-28 21:11:54 +00:00
from app import app # This is required to initialize the database.
from data.database import Repository, LogEntry, RepositoryActionCount, db_random_func
from workers.worker import Worker
POLL_PERIOD_SECONDS = 10
logger = logging.getLogger(__name__)
2015-07-29 18:21:29 +00:00
def count_repository_actions():
""" Aggregates repository actions from the LogEntry table and writes them to
the RepositoryActionCount table. Returns the number of repositories for
which actions were logged. Returns 0 when there is no more work.
"""
try:
# Get a random repository to count.
today = date.today()
yesterday = today - timedelta(days=1)
2016-10-28 21:11:54 +00:00
has_yesterday_actions = (RepositoryActionCount
.select(RepositoryActionCount.repository)
.where(RepositoryActionCount.date == yesterday))
2016-10-28 21:11:54 +00:00
to_count = (Repository
.select()
.where(~(Repository.id << (has_yesterday_actions)))
.order_by(db_random_func()).get())
logger.debug('Counting: %s', to_count.id)
2016-10-28 21:11:54 +00:00
actions = (LogEntry
.select()
.where(LogEntry.repository == to_count,
LogEntry.datetime >= yesterday,
LogEntry.datetime < today)
.count())
# Create the row.
2015-07-29 18:21:29 +00:00
try:
RepositoryActionCount.create(repository=to_count, date=yesterday, count=actions)
return 1
except:
logger.exception('Exception when writing count')
except Repository.DoesNotExist:
logger.debug('No further repositories to count')
2015-07-29 18:21:29 +00:00
return 0
2015-07-29 18:21:29 +00:00
class RepositoryActionCountWorker(Worker):
def __init__(self):
super(RepositoryActionCountWorker, self).__init__()
self.add_operation(self._count_repository_actions, POLL_PERIOD_SECONDS)
def _count_repository_actions(self):
""" Counts actions for a random repository for the previous day. """
2015-07-29 18:21:29 +00:00
count_repository_actions()
if __name__ == "__main__":
worker = RepositoryActionCountWorker()
worker.start()