简体   繁体   English

使用 Python 和 MongoDB 进行实时分析

[英]Real time analytics with Python and MongoDB

I am a Python beginner working on a very basic real-time analytics tool using Python and MongoDB.我是一名 Python 初学者,正在使用 Python 和 MongoDB 开发一个非常基本的实时分析工具。 I have a function that updates a collection of URLs and pageviews stored in MongoDB:我有一个函数可以更新存储在 MongoDB 中的一组 URL 和浏览量:

def track(url):
   hour = datetime.utcnow().replace(minute = 0, second = 0, microsecond = 0)
   db.hourly.update({"hour": hour, "url":url}, {"$inc": {"views": 1}}, upsert = True)
   db.hourly_totals.update({"hour": hour}, {"$inc": {"views": 1}}, upsert = True)

Whenever track gets executed for a given URL, the collection of documents gets updated accordingly with pageviews for each URL incremented for collection "hourly", and total pageviews for all urls incremented for collection "hourly_totals".每当针对给定 URL 执行 track 时,文档集合都会相应地更新,每个 URL 的浏览量会随着集合“hourly”的增加而相应更新,并且所有 url 的总浏览量会随着集合“hourly_totals”的增加而增加。

What is the best way to automate this process such that track gets executed every time somebody visits a page on my website?自动执行此过程的最佳方法是什么,以便每次有人访问我网站上的页面时都执行跟踪? Could I do this in Python or do I have to embed this in a Javascript tag?我可以在 Python 中执行此操作还是必须将其嵌入到 Javascript 标记中?

You can place javascript (JQuery) code (in document.ready() method) which makes a call to a web service which in turn executes this track method.您可以放置​​ javascript (JQuery) 代码(在 document.ready() 方法中),该代码调用 Web 服务,然后执行此跟踪方法。 If you just want a simple web server for your webservice, use bottle .如果您只想为您的网络服务提供一个简单的网络服务器,请使用Bottle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM