2015-04-13 17:31:07 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from apscheduler.schedulers.blocking import BlockingScheduler
|
|
|
|
|
2015-04-13 18:25:09 +00:00
|
|
|
from app import app
|
2015-04-22 21:11:08 +00:00
|
|
|
from data.database import (Repository, LogEntry, RepositoryActionCount, db_random_func, fn,
|
|
|
|
UseThenDisconnect)
|
2015-04-13 17:31:07 +00:00
|
|
|
from datetime import date, datetime, timedelta
|
|
|
|
|
|
|
|
POLL_PERIOD_SECONDS = 30
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
sched = BlockingScheduler()
|
|
|
|
|
|
|
|
@sched.scheduled_job(trigger='interval', seconds=10)
|
|
|
|
def count_repository_actions():
|
|
|
|
""" Counts actions for a random repository for the previous day. """
|
|
|
|
|
2015-04-22 21:11:08 +00:00
|
|
|
with UseThenDisconnect(app.config):
|
2015-04-13 17:31:07 +00:00
|
|
|
try:
|
2015-04-22 21:11:08 +00:00
|
|
|
# Get a random repository to count.
|
|
|
|
today = date.today()
|
|
|
|
yesterday = today - timedelta(days=1)
|
|
|
|
has_yesterday_actions = (RepositoryActionCount.select(RepositoryActionCount.repository)
|
|
|
|
.where(RepositoryActionCount.date == yesterday))
|
|
|
|
|
|
|
|
to_count = (Repository.select()
|
|
|
|
.where(~(Repository.id << (has_yesterday_actions)))
|
|
|
|
.order_by(db_random_func()).get())
|
|
|
|
|
|
|
|
logger.debug('Counting: %s', to_count.id)
|
|
|
|
|
|
|
|
actions = (LogEntry.select()
|
|
|
|
.where(LogEntry.repository == to_count,
|
|
|
|
LogEntry.datetime >= yesterday,
|
|
|
|
LogEntry.datetime < today)
|
|
|
|
.count())
|
|
|
|
|
|
|
|
# Create the row.
|
|
|
|
try:
|
|
|
|
RepositoryActionCount.create(repository=to_count, date=yesterday, count=actions)
|
|
|
|
except:
|
|
|
|
logger.exception('Exception when writing count')
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
except Repository.DoesNotExist:
|
|
|
|
logger.debug('No further repositories to count')
|
|
|
|
return False
|
2015-04-13 17:31:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
sched.start()
|