# up celery task for counter statistic
This commit is contained in:
parent
9571be7cf8
commit
282aa75719
|
@ -43,7 +43,7 @@ class MosqPost(models.Model):
|
|||
pass
|
||||
|
||||
|
||||
class MosqPostStatic(models.Model):
|
||||
class MosqPostStatistic(models.Model):
|
||||
increment = models.PositiveIntegerField(verbose_name='增量')
|
||||
date = models.DateField(auto_now_add=True, verbose_name='日期')
|
||||
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
import pytz
|
||||
from datetime import datetime, time
|
||||
from django.db.models import Max
|
||||
from celery import task
|
||||
from counter.models import DeviceCount
|
||||
from mosquito.models import MosqPostStatistic
|
||||
|
||||
|
||||
@task()
|
||||
def update_daily_statistic():
|
||||
queryset = DeviceCount.objects.raw(
|
||||
'select id, device_id, max(data_time) as max_date '
|
||||
'from device_count group by device_id, date(data_time) order by max_date')
|
||||
|
||||
if queryset:
|
||||
ret = []
|
||||
for q in queryset:
|
||||
entry = DeviceCount.objects.get(device_id=q.device_id, data_time=q.data_time)
|
||||
ret.append((entry.data_time, entry.device_id, entry.count))
|
||||
|
||||
@task()
|
||||
def update_latest_statistic():
|
||||
pass
|
|
@ -0,0 +1,7 @@
|
|||
from __future__ import absolute_import, unicode_literals
|
||||
|
||||
# This will make sure the app is always imported when
|
||||
# Django starts so that shared_task will use this app.
|
||||
from .celery import app as celery_app
|
||||
|
||||
__all__ = ['celery_app']
|
|
@ -0,0 +1,22 @@
|
|||
from __future__ import absolute_import, unicode_literals
|
||||
import os
|
||||
from celery import Celery
|
||||
|
||||
# set the default Django settings module for the 'celery' program.
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'stargazer.settings')
|
||||
|
||||
app = Celery('stargazer')
|
||||
|
||||
# Using a string here means the worker doesn't have to serialize
|
||||
# the configuration object to child processes.
|
||||
# - namespace='CELERY' means all celery-related configuration keys
|
||||
# should have a `CELERY_` prefix.
|
||||
app.config_from_object('django.conf:settings', namespace='CELERY')
|
||||
|
||||
# Load task modules from all registered Django app configs.
|
||||
app.autodiscover_tasks()
|
||||
|
||||
|
||||
@app.task(bind=True)
|
||||
def debug_task(self):
|
||||
print('Request: {0!r}'.format(self.request))
|
|
@ -13,6 +13,7 @@ https://docs.djangoproject.com/en/2.0/ref/settings/
|
|||
import os
|
||||
import sys
|
||||
import datetime
|
||||
from celery.schedules import crontab
|
||||
|
||||
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
|
||||
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
|
@ -45,6 +46,7 @@ INSTALLED_APPS = [
|
|||
'mosquito',
|
||||
'smart',
|
||||
'counter',
|
||||
'django_celery_results',
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
|
@ -179,7 +181,7 @@ REST_FRAMEWORK = {
|
|||
}
|
||||
|
||||
JWT_AUTH = {
|
||||
'JWT_EXPIRATION_DELTA': datetime.timedelta(seconds=1800),
|
||||
'JWT_EXPIRATION_DELTA': datetime.timedelta(seconds=3600),
|
||||
}
|
||||
|
||||
# Custom auth backend
|
||||
|
@ -197,3 +199,20 @@ CACHES = {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Celery settings
|
||||
CELERY_BROKER_URL = 'redis://127.0.0.1:6379/2'
|
||||
CELERY_RESULT_BACKEND = 'django-db'
|
||||
CELERY_ACCEPT_CONTENT = ['json']
|
||||
CELERY_TASK_SERIALIZER = 'json'
|
||||
CELERY_TIMEZONE = 'Asia/Shanghai'
|
||||
CELERY_BEAT_SCHEDULE = {
|
||||
'update-daily-statistic': {
|
||||
'task': 'counter.tasks.update_daily_statistic',
|
||||
'schedule': crontab(minute='*/1'),
|
||||
},
|
||||
'update-latest-statistic': {
|
||||
'task': 'counter.tasks.update_latest_statistic',
|
||||
'schedule': crontab(minute='*/1'),
|
||||
},
|
||||
}
|
Loading…
Reference in New Issue