Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6208633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:50:27+00:00 2026-05-24T05:50:27+00:00

I have an analytics dashboard screen that is visible to my django web applications

  • 0

I have an “analytics dashboard” screen that is visible to my django web applications users that takes a really long time to calculate. It’s one of these screens that goes through every single transaction in the database for a user and gives them metrics on it.

I would love for this to be a realtime operation, but calculation times can be 20-30 seconds for an active user (no paging allowed, it’s giving averages on transactions.)

The solution that comes to mind is to calculate this in the backend via a manage.py batch command and then just display cached values to the user. Is there a Django design pattern to help facilitate these types of models/displays?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:50:30+00:00Added an answer on May 24, 2026 at 5:50 am

    What you’re looking for is a combination of offline processing and caching. By offline, I mean that the computation logic happens outside the request-response cycle. By caching, I mean that the result of your expensive calculation is sufficiently valid for X time, during which you do not need to recalculate it for display. This is a very common pattern.

    Offline Processing

    There are two widely-used approaches to work which needs to happen outside the request-response cycle:

    • Cron jobs (often made easier via a custom management command)
    • Celery

    In relative terms, cron is simpler to setup, and Celery is more powerful/flexible. That being said, Celery enjoys fantastic documentation and a comprehensive test suite. I’ve used it in production on almost every project, and while it does involve some requirements, it’s not really a bear to setup.

    Cron

    Cron jobs are the time-honored method. If all you need is to run some logic and store some result in the database, a cron job has zero dependencies. The only fiddly bits with cron jobs is getting your code to run in the context of your django project — that is, your code must correctly load your settings.py in order to know about your database and apps. For the uninitiated, this can lead to some aggravation in divining the proper PYTHONPATH and such.

    If you’re going the cron route, a good approach is to write a custom management command. You’ll have an easy time testing your command from the terminal (and writing tests), and you won’t need to do any special hoopla at the top of your management command to setup a proper django environment. In production, you simply run path/to/manage.py yourcommand. I’m not sure if this approach works without the assistance of virtualenv, which you really ought to be using regardless.

    Another aspect to consider with cronjobs: if your logic takes a variable amount of time to run, cron is ignorant of the matter. A cute way to kill your server is to run a two-hour cronjob like this every hour. You can roll your own locking mechanism to prevent this, just be aware of this—what starts out as a short cronjob might not stay that way when your data grows, or when your RDBMS misbehaves, etc etc.

    In your case, it sounds like cron is less applicable because you’d need to calculate the graphs for every user every so often, without regards to who is actually using the system. This is where celery can help.

    Celery

    …is the bee’s knees. Usually people are scared off by the “default” requirement of an AMQP broker. It’s not terribly onerous setting up RabbitMQ, but it does require stepping outside of the comfortable world of Python a bit. For many tasks, I just use redis as my task store for Celery. The settings are straightforward:

    CELERY_RESULT_BACKEND = "redis"
    REDIS_HOST = "localhost"
    REDIS_PORT = 6379
    REDIS_DB = 0
    REDIS_CONNECT_RETRY = True
    

    Voilá, no need for an AMQP broker.

    Celery provides a wealth of advantages over simple cron jobs. Like cron, you can schedule periodic tasks, but you can also fire off tasks in response to other stimuli without holding up the request/response cycle.

    If you don’t want to compute the chart for every active user every so often, you will need to generate it on-demand. I’m assuming that querying for the latest available averages is cheap, computing new averages is expensive, and you’re generating the actual charts client-side using something like flot. Here’s an example flow:

    1. User requests a page which contains an averages chart.
    2. Check cache — is there a stored, nonexpired queryset containing averages for this user?
      • If yes, use that.
      • If not, fire off a celery task to recalculate it, requery and cache the result. Since querying existing data is cheap, run the query if you want to show stale data to the user in the meantime.
    3. If the chart is stale. optionally provide some indication that the chart is stale, or do some ajax fanciness to ping django every so often and ask if the refreshed chart is ready.

    You could combine this with a periodic task to recalculate the chart every hour for users that have an active session, to prevent really stale charts from being displayed. This isn’t the only way to skin the cat, but it provides you with all the control you need to ensure freshness while throttling CPU load of the calculation task. Best of all, the periodic task and the “on demand” task share the same logic—you define the task once and call it from both places for added DRYness.

    Caching

    The Django cache framework provides you with all the hooks you need to cache whatever you want for as long as you want. Most production sites rely on memcached as their cache backend, I’ve lately started using redis with the django-redis-cache backend instead, but I’m not sure I’d trust it for a major production site yet.

    Here’s some code showing off usage of the low-level caching API to accomplish the workflow laid out above:

    import pickle
    from django.core.cache import cache
    from django.shortcuts import render
    from mytasks import calculate_stuff
    
    from celery.task import task
    
    @task
    def calculate_stuff(user_id):
        # ... do your work to update the averages ...
        # now pull the latest series
        averages = TransactionAverage.objects.filter(user=user_id, ...)
        # cache the pickled result for ten minutes 
        cache.set("averages_%s" % user_id, pickle.dumps(averages), 60*10)
    
    def myview(request, user_id):
        ctx = {}
        cached = cache.get("averages_%s" % user_id, None)
        if cached:
            averages = pickle.loads(cached) # use the cached queryset
        else:
            # fetch the latest available data for now, same as in the task
            averages = TransactionAverage.objects.filter(user=user_id, ...)
            # fire off the celery task to update the information in the background
            calculate_stuff.delay(user_id) # doesn't happen in-process.
            ctx['stale_chart'] = True # display a warning, if you like
    
        ctx['averages'] = averages
        # ... do your other work ...
        render(request, 'my_template.html', ctx)
    

    Edit: worth noting that pickling a queryset loads the entire queryset into memory. If you’re pulling up a lot of data with your averages queryset this could be suboptimal. Testing with real-world data would be wise in any case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web app that I deployed in AppHarbor with Google Analytics. Development
Does anybody know what google web master tools offer I have google analytics on
So Google Analytics does not have an API that we can use to get
I have been experimenting with woopra.com A web analytics tool. Which requires a piece
I have seen at least one site in the past that had analytics on
I am evaluating Elasticsearch as a datasource for my dashboard analytics. Does anybody have
We have decided that it is time to ditch the ad-hoc method of deploying
We are releasing a paid web application. We have Google Analytics and want to
I have some daily analytics-style records that track usage on my site, and they
I have set up event tracking for the first time with Google analytics. My

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.