Statistics logging for Django - part 2
In part 1 I explained how to build middleware and an associated model to capture page accesses, and tie them to a user session. Now that we have all this useful info logged we need to do something with it, like, display it. Unfortunately Django doesn't have a facility for using GROUP BY with mysql, so you have two major choices (there are more but we can ignore them): implement a custom request in a custom Manager (see snippet and snippet, or tagged snippets), or exploit a mysql view and model it in Django. Now for me I prefer the latter because it means my custom sql becomes a mysql customisation and as far as Django is concerned it is dealing with a normal table (but don't tell Django that it is read only), and thus the model code works, so subsequent queries and manipulations can exploit the ORM easily. My subjective and non-scientific experience is that using views is a lot more efficient/quick than using custom queries in the manager (it probably has to do with whatever optimisations exist with views, and the fact that you only fetch items when Django decides you need to fetch a row). So, how the hell do we do it?
First I created a model that describes what information I want to deal with (something which maps neatly on to our other model):
class UserActivity(models.Model):
session = models.OneToOneField(Session,
db_index=True,
null=True,blank=True,
primary_key=True)
user = models.ForeignKey(User,null=True,blank=True)
date = models.DateTimeField(
help_text="Date Request started processing",
auto_now_add=True,
db_index=True)
processing_time = models.IntegerField(
help_text="Total time spent on this user")
requests = models.IntegerField(
help_text="Total Requests in this session")
stats = UserActivityManager()
def __str__(self):
return '%s: %s %s - %s - %s' % (self.user,self.session,self.date,self.processing_time,self.requests)
class Admin:
list_display= ('user','session','date','processing_time','requests')The nice thing about this set up is when we aggregate our activity logs we can pull out random stuff like total processing time for requests for a user/session, along with number of requests/user/session (and thus average request time)
But that is just our model, we still need the magic. To implement the magic nicely I put some custom initial SQL into the sql directory of my application (in my case the housing application for this is called accounts, so I make a file called accounts/sql/useractivity.sql), you can read more about initial data here, Django fixtures).My SQL looks like this:
DROP TABLE accounts_useractivity;
CREATE OR REPLACE VIEW accounts_useractivity AS
SELECT i.session_id,
i.user_id,
MAX(i.date) as date,
sum(i.request_time) AS processing_time,
count(*) AS requests
FROM accounts_activitylog i
GROUP BY 1
ORDER BY NULL;
So first I tell mysql to drop the table that django just created (accounts_useractivity), and create a view in it's place. The view is very simple, in that it just GROUP BY the session_id. The real hair puller for me was figuring out that I needed to use the MAX(i.date) (see more about aggregate functions) to get the most recent access to float to the top when it normalises the data (otherwise the GROUP BY normally ORDER BY the session_id, which helps no one), the ORDER BY NULL is an optimisation to tell GROUP BY not to ORDER BY. I am hoping that because date is an INDEX (from our logging model) it shouldn't cost too much to do a MAX. (I would like someone with Much MYSQL-fu to point out any further optimisations to this, or even alternative approaches to the whole thing).
So now we have an aggregating VIEW which Django maps using it's ORM, so that to figure out sessions which have been active in the last x minutes (where x is a datetime.timedelta object) we simply do a:
UserActivity.objects.get_query_set().filter(date__gte=datetime.now()-x)I wrote a custom manager for getting recent sessions etc., but that is an exercise for the reader. What I did include in my model is something which returns a stepped "request_weight" i.e. session requests / largest session request x steps, which in my case defaults to 6. This means I can style my users like one would a "tag cloud", so very active sessions will grow bigger than less active sessions. I needed to implement a helper function in the custom manager to return the session with the most requests.
The final tip is to use a context processor to make the information available to all your templates, although you could do it with middleware (maybe middleware is the proper way to do it?).

















'request.session' is no longer a Session object, it's a SessionStore object that is different based on whether you use DB, file or cache sessions. I switched the 'session' part of the model to just be a charfield that is populated with the session id. I really only cared about which 'activity's were grouped as part of a session.
Essay AND Custom Research Paper AND Term Papers
Your article is great and there is no solution to this issue most of the companies are making money and wiping of their people. Its funny to real the article relation with real estate lolzzz. I have been looking for rent Apartment in Dubai since I move to Dubai . Properties for rent in dubai are expensive for me but one of my friend wants to Sell villa in Arabian ranches which might be a better option in this down market what do you think guys ?
Great post!
Djongo made the process of creating middleware very easy and now I can easily create addresses for datacall. Discount Sunglasses
You will surprise to find the high quality tiffany jewelry in much.Everyone will focus on the shinning of
tiffany co jewelry without awareness of
rovide 100% sterling silver Tiffany jewelry,you can buy discount Tiffany & Co jewelry here.Tiffany And Co Jewelry is the best jewellry.
discount tiffany jewelry, Tiffany Jewelry, Tiffany Jewellery, Tiffany Silver, Tiffany, Tiffanys, Tiffany's Jewelry.We Provide a wide range of fashionable
tiffany co jewelry, including
Links London
In 1950 Tiffany's had its biggest boost in popularity as Truman Capote's Breakfast at Tiffany's was published. Tiffany's was shot to worldwide fame in 1961 when the film adaptation staring Audrey Hepburn was released. This film and its star became icons, as did Tiffany's
Tiffany Bracelets
Tiffany Rings
cheap tiffany with.Discounted Tiffany & Co silver jewelries are provided in our Tiffany’s online outlet store
Links Of London
Thanks for sharing.
thanks for sharing
Titan Bet Gratta e Vinci Titan Bet Bingo Online
Some great info on here, why did i not find this site sooner! Many thanks and keep posting. how to get 6 pack abs | how to get six pack abs | how to get a 6 pack quick | how to get 6 pack abs fast
Como ganar mas dinero
Ganar mas dinero
Como hacerse rico
Como hacerse rico
Como ganar mas dinero
Como ganar mas dinero
Como ganar mas dinero
Very true, I agree with all you said.
Ganar mas dinero
Como ganar mas dinero
Como ganar mas dinero
Djongo made the process of creating statistics very easy and now I can easily create addresses for data call.
Thesis Help | Dissertation Help | Essay Help | Assignment Help
So first I tell mysql to drop the table that django just created (accounts_useractivity), and create a view in it's place.
Apex Professionals
Great post! games
How long have you been in this field? You seem to know a lot more than I do, I’d love to know your sources!
Albi,
cheap laptop
I've been making quite a few mistakes about the codes. I can't seem to get them working right as expected. I think I'm gonna look for other easy tutorials. canvas porch awnings - kids closet organizers - registry cleaner reviews
Excelent post
Como ganar mas dinero
Como ganar mas dinero
Como ganar mas dinero
Como ganar mas dinero
Como ganar mas dinero
How do I know when user Session has expired or removed?
________
SEO
I don't know If I said it already, but this so good stuff keep up the good work. I read a lot of blogs on a daily basis and for the most part POLITIC just wanted to make a quick comment to say I’m glad I found your blog. Thanks.
Best regards
Seo Motivation | Blogosphere news | Berita Di Blogospheree | Automotive | kontes astaga lifestyle indonesia | by Oes tsetnoc | Seo Motivation | Gadget Reviewers | Blog Gadget Review | Belajar seo blogspot | Belajar blogging | Antonfkip Blog | miss925.com
That is good information thanks for this one
Online Dating
Online Dating Service
Online Dating Site
Online Dating Website
Free Online Dating
Great post!
I´ve been using Django for a long in my projects (Otimização de sites, acompanhantes sao paulo, desentupidora sane, perder peso rapido, iluminação, relogio de ponto, cartoes de visita) and this article helped me a lot!
Thanks.
I liek your post
encuestas pagadas
encuestas pagadas
hacer dinero con encuestas pagas
encuestas pagas
encuestas pagadas
fraude encuesta pagas
ganar dinero con encuestas pagas
encuestas pagas
encuestas pagadas en linea
encuestas pagas
Djongo made the process of creating middleware very easy and now I can easily create addresses for datacal
My subjective and non-scientific experience is that using views is a lot more efficient/quick than using custom queries in the manager (it probably has to do with whatever optimisations exist with views, and the fact that you only fetch items when Django decides you need to fetch a row).Apex Professionals
000-200
000-330
132-S-911
156-215
1Y0-A05
1z0-042
1z0-051
1z0-052
1z0-146
220-601
220-602
220-701
310-065
350-001
350-018
350-029
350-030
352-001
640-553
640-721
640-801
640-802
640-816
640-822
642-072
642-373
642-383
642-415
642-426
642-453
642-504
642-515
642-524
642-566
642-611
642-642
642-654
642-661
642-691
642-736
642-812
642-825
642-845
642-892
642-901
642-971
642-972
642-974
646-204
646-230
646-363
646-563
646-588
646-656
646-671
650-251
650-393
650-621
CISSP
ec0-350
ex0-101
HP0-J22
JK0-016
JN0-100
jn0-350
jn0-400
JN0-522
pk0-002
sy0-201
VCP-310
VCP-410
xk0-002
310-200
310-202
640-460
640-863
642-105
642-164
642-436
642-446
642-456
642-481
642-533
642-655
642-873
642-973
646-046
JN0-342
jn0-532
N10-004
I think you'll be able to do what you want using a custom manager within your Django model.
Dissertation AND Thesis
Yes I agree completely, the Django model provides for a custom manager to use however you want.
weight loss tips with Nutrisystem reviews
Under the circumstance of 642-901financial crisis people’s job are pretty insecure and they long for the obtaining of CCNA ratification to stay in their position.sy0-201It is until people are determined 642-825to start preparations for 640-802 exams that they find a lot of difficulties.It is too 640-816 hard to learn by heart all the knowledge that is irrelevant to daily life, though I have tried to remember them once 642-642 after another. What’s worse, quite a lot of the reference materials are far 642-436 away from main points of the test. Thus, I was doing nothing but wasting time when I was doing the excercises.N10-004The Pass4sure product offers the superb quality 640-802 exercises 642-892 with lowest price. Containing all knowledge points in the exam the Pass4sure enables you to neglect 642-873 all other references in the market to pass the examination.Pass4sure provides 24*7 642-873 service, any problems can be solved at the first minute. When having any questions, you 640-802 can press the CONTACT US button on the page to use the Live Chat service to contact vcp-310 with customer service.Pass4sure includes various types of information required for the 350-029 exam, it is ensured that users can pass the CCNA exam in the first time. If you
VCP-410 do not pass or not pass the exam in the first time, Pass4sure promises a full refund unconditionally 640-553 in case of 352-001 customers.Pass4sure provides free trial demo for eliminating the suspicion that the customers have about the quality of Pass4sure
642-812 Many people that participate in the CompTIA exam with unpractical
640-822 expectations often end inHP0-S18 failure. Many people owned the following expectations before participating 350-030Even without
jn0-400 still I can take CCNA Nowadays there is a great upsurge in pursuing the CCNA certificate. Many examinees pass the exam 642-566 and get the certificate smoothly with the help of Pass4sure.However, many people are wondering: can I pass 642-426 without Pass4sure? It is known to all that JN0-342 does avail the examinees a lot, which offers similar questions to the real ones in the exam and covers the knowledge jn0-350 points 100 percent, helping the examinees save a plenty of time and energy. 1z0-146 With the help of Pass4sure it is no wonder to get the certificate.650-393 On the other 650-621 if you ec0-350 may not catch the chance of Pass4sure, the way to success will be much more difficult 646-230 and 646-588 much extra time and 646-563 will be spent on642-661 the exam. 640-863 Nevertheless, it's not impossible to get the CCNA by passing the Cisco 640-802 Examination with appropriate review and hard work.642-105 As there is too much test database on the internet, 642-164 the exam-takers are over dependent on it, which is still far from being satisfactory.If the exam-takers 642-373 are over dependent on test database, their learning motive and learning efficiency will fade. 642-415 Therefore, knowledge which can be originally absorbed will be reduced. Besides, 642-453 some test database of low quality will not help but cause a waste of time and energy.Materials from 640-721 are really the best and most reliable for people to prepare for 350-001 the exam in the market.But even so, it is only an auxiliary tool, students should not over depend on 220-601 it and be more reasonable when using it.Each candidates should keep in mind that the purpose is not for the paper, 156-215the more important is whether you learned knowledge and skills 156-215, whether your capacity improved or not. JN0-331 Let\'s say it confidently that if there is no Pass4sure, I can also get the certificate!
good blog
This is really some awesome advice. Keep it up, my colleagues would love this.
I found your website perfect for my needs. It contains wonderful and helpful posts. I have read most of them and got a lot from them. To me, you are doing the great work. Carry on this. work at home In the end, I would like to thank you for making such a nice website.
webpage screenshot | ip address checker | web content | dissertation writing
You're right there, this is some great information. Gotta hand it to Brad, he knows his stuff!
make money online
I believe the information covered in the discussion is top notch. I've been doing a research on the subject and your blog just cleared up a lot
of questions. I am working on a custom essays and
custom research papers for my English class and currently reading lots of blogs to study.
college papers | custom essays
Quite true, the information here is first class and so helpful for all Django users.
This is really some awesome advice. Keep it up, my colleagues would love this.
How to Make a Website
Get Paid to Take Surveys
Very nice review! I searched for some tutorials at http://www.picktorrent.com , but found nothing informative, your article helped me much!
We need to be simplistic and it seems you are trying in the same direction.Dubai Property | Buy Dubai Properties
Very detailed explanation. It make my work simpler with your sample. carpenter ants
Exactly right, this is perfect for any users of Django who need to expand on their code to make better use of it. zone diet delivery
very appreciating informative
Thanks for the useful information on django.
Blaine
How to Make Money Online
weight loss|weight loss
weight loss|weight loss|weight lossweight loss|weight loss|weight loss
weight loss|weight loss
weight loss
weight loss|weight loss|weight loss|weight loss|weight loss
weight loss|weight loss|weight loss
weight loss
weight loss|weight loss
weight loss|weight loss
weight loss
weight loss
weight loss|weight loss
weight loss
Very detailed explanation. Thanks it will work ;)
You always make complicated maths looking very easy. I think this is your major achievement.
I find it very handy to be able to expose parts of settings.py in templates. The Django docs show you how to do this using context processors and give the code for the MEDIA_URL context processor here. However, it seems silly to me to have to write custom processors all the time. Instead, I propose a single processor that exposes a list of settings (stored in settings.py itself) to the template layer.
Yes I agree it does seem silly to have to write custom processors all the time, I think you have come up with a great solution there!
it seems as though all this stuff is going a little over my head, but thanks for trying to help. i may have to read over a few times.
business insurance forum
Installing new blog software is generally easy, but if you have legacy content that you need to preserve, the ability to move content between systems becomes of utmost importance. Fortunately, it’s quite common for popular software to provide import/export features; Having good tools to migrate content reduces switching costs, making it easy to try new software without fear of content lock-in. Unfortunately, with a home-grow blog platform, these tools need to be written from scratch.
I went the route of using a separate user for FF2, but you could probably use profiles? So:
Great blog
Adoption Process Tips
Just stumbled across your article. Interesting information. I have been trying to learn a little bit about Django. It seems pretty complex.
Post new comment