简体   繁体   English

如何将所有用户活动存储在网站中?

[英]How to store all user activites in a website..?

I have a web application build in Django + Python that interact with web services (written in JAVA ). 我在Django + Python中构建了一个与Web服务交互的Web应用程序(用JAVA编写)。

Now all the database management part is done by web-services ie all CRUD operations to actual database is done by web-services. 现在,所有数据库管理部分均由Web服务完成,即,对实际数据库的所有CRUD操作均由Web服务完成。


Now i have to track all User Activities done on my website in some log table . 现在,我必须在某些日志表中 跟踪在我的网站上完成的所有用户活动

Like If User posted a new article, then a new row is created into Articles table by web-services and side by side, i need to add a new row into log table , something like " User : Raman has posted a new article (with ID, title etc) " 就像如果用户发布了新文章,然后通过Web服务并排在“ 文章”表中创建了新行,我需要在日志表中添加新行,例如“ 用户:拉曼发布了新文章(带有ID,标题等)

I have to do this for all Objects in my database like "Article", "Media", "Comments" etc 我必须对数据库中的所有对象(例如“文章”,“媒体”,“评论”等)执行此操作


Note : I am using PostgreSQL 注意:我正在使用PostgreSQL


So what is the best way to achieve this..?? 那么实现此目标的最佳方法是什么? (Should I do it in PostgreSQL OR JAVA ..??..And How..??) (我应该在PostgreSQL还是 JAVA中做到.. ?? ..以及如何.. ??)

So, you have UI <-> Web Services <-> DB 因此,您拥有UI <-> Web Services <-> DB

Since the web services talk to the DB, and the web services contain the business logic (ie I guess you validate stuff there, create your queries and execute them), then the best place to 'log' activities is in the services themselves. 由于Web服务与DB通信,并且Web服务包含业务逻辑(即,我猜您在那里验证内容,创建查询并执行它们),因此“记录”活动的最佳位置是服务本身。

IMO, logging PostgreSQL transactions is a different thing. IMO,记录PostgreSQL事务是另一回事。 It's not the same as logging 'user activities' anymore. 它不再与记录“用户活动”相同。

EDIT: This still means you create DB schema for 'logs' and write them to DB. 编辑:这仍然意味着您为“日志”创建数据库架构并将其写入数据库。

Second EDIT: Catching log worthy events in the UI and then logging them from there might not be the best idea either. 第二次编辑:UI中捕获有价值的事件,然后从那里进行记录可能也不是最好的主意。 You will have to rewrite logging if you ever decide to replace the UI, or for example, write an alternate UI for, say mobile devices, or something else. 如果您决定替换UI,或者例如为移动设备之类的东西编写备用UI,则必须重写日志记录。

For an audit table within the DB itself, have a look at the PL/pgSQL Trigger Audit Example 有关数据库本身内的审计表,请查看PL / pgSQL触发器审计示例

This logs every INSERT, UPDATE, DELETE into another table. 这会将每个INSERT,UPDATE,DELETE记录到另一个表中。

In your log table you can have various columns, including: 在您的日志表中,您可以具有各种列,包括:

  • user_id (the user that did the action) user_id (执行操作的用户)
  • activity_type (the type of activity, such as view or commented_on ) activity_typeactivity_type类型,例如viewcommented_on
  • object_id (the actual object that it concerns, such as the Article or Media) object_id (它涉及的实际对象,例如文章或媒体)
  • object_type (the type of object; this can be used later, in combination with object_id to lookup the object in the database) object_type (对象的类型;以后可以将其与object_id结合使用以在数据库中查找对象)

This way, you can keep track of all actions the users do. 这样,您可以跟踪用户执行的所有操作。 You'd need to update this table whenever something happens that you wish to track. 每当您希望跟踪某些事情时,都需要更新此表。

Whenever we had to do this, we overrode signals for every model and possible action. 每当我们必须这样做时,我们都会覆盖每个模型和可能采取的措施的信号。

https://docs.djangoproject.com/en/dev/topics/signals/ https://docs.djangoproject.com/en/dev/topics/signals/

You can have the signal do whatever you want, from injecting some HTML into the page, to making an entry in the database. 您可以让信号做任何您想做的事情,从向页面中注入HTML到在数据库中进行输入。 They're an excellent tool to learn to use. 它们是学习使用的出色工具。

I used django-audit-log and I am very satisfied. 我使用了django-audit-log ,对此感到非常满意。

Django-audit-log can track multiple models each in it's own additional table. Django-audit-log可以在自己的附加表中跟踪多个模型。 All of these tables are pretty unified, so it should be fairly straightforward to create a SQL view that shows data for all models. 所有这些表都非常统一,因此创建显示所有模型数据的SQL视图应该相当简单。

Here is what I've done to track a single model ("Pauza"): 这是我跟踪单个模型(“ Pauza”)所做的工作:

class Pauza(models.Model):
    started      = models.TimeField(null=True, blank=False)
    ended        = models.TimeField(null=True, blank=True)
    #... more fields ...

    audit_log = AuditLog() 

If you want changes to show in Django Admin, you can create an unmanaged model (but this is by no means required): 如果您希望更改显示在Django Admin中,则可以创建一个非托管模型 (但这绝不是必需的):

class PauzaAction(models.Model):

    started      = models.TimeField(null=True, blank=True)
    ended        = models.TimeField(null=True, blank=True)
    #... more fields ...

    # fields added by Audit Trail:
    action_id    = models.PositiveIntegerField(primary_key=True, default=1, blank=True)
    action_user  = models.ForeignKey(User, null=True, blank=True)
    action_date  = models.DateTimeField(null=True, blank=True)
    action_type  = models.CharField(max_length=31, choices=(('I', 'create'), ('U', 'update'), ('D', 'delete'),), null=True, blank=True)
    pauza        = models.ForeignKey(Pauza, db_column='id', on_delete=models.DO_NOTHING, default=0, null=True, blank=True)

    class Meta:
        db_table = 'testapp_pauzaauditlogentry'
        managed = False
        app_label = 'testapp'

Table testapp_pauzaauditlogentry is automatically created by django-audit-log, this merely creates a model for displaying data from it. testapp_pauzaauditlogentry由django-audit-log自动创建,这只是创建一个模型来显示其中的数据。 It may be a good idea to throw in some rude tamper protection: 进行一些粗鲁的篡改保护可能是一个好主意:

class PauzaAction(models.Model):

    # ... all like above, plus:

    def save(self, *args, **kwargs):
        raise Exception('Permission Denied')
    def delete(self, *args, **kwargs):
        raise Exception('Permission Denied')

As I said, I imagine you could create a SQL view with the four action_ fields and an additional 'action_model' field that could contain varchar references to model itself (maybe just the original table name). 就像我说的,我想您可以使用四个action_字段和一个额外的'action_model'字段创建一个SQL视图,其中可以包含对模型本身的varchar引用(也许只是原始表名)。

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

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