【问题标题】:How to Structure Time-Series model in django-mongodb-engine如何在 django-mongodb-engine 中构建时间序列模型
【发布时间】:2014-02-22 22:36:24
【问题描述】:

我正在尝试使用 mongodb 作为后端在 django 中定义时间序列模型。我读到了一些best practices for timeseries data at the MongoDB Blog,我想我理解得很好。但是现在,我的问题/问题是:如何使用 django 的模型语法定义这样的模型?我不确定这些是embedded documents 还是只是将arraysdicts 存储在模型字段中。这是建议的 mongo 格式:

理想的mongo文档格式:

{
  timestamp_hour: ISODate("2013-10-10T23:00:00.000Z"),
  type: “memory_used”,
  values: {
    0: { 0: 999999, 1: 999999, …, 59: 1000000 },
    1: { 0: 2000000, 1: 2000000, …, 59: 1000000 },
    …,
    58: { 0: 1600000, 1: 1200000, …, 59: 1100000 },
    59: { 0: 1300000, 1: 1400000, …, 59: 1500000 }
  }
}

一种解决方案是这样做,一个文档包含一天的数据:

# models.py
class timeseries(models.Model):
    date            = models.DateField(null=True)
    value_hour_0    = models.CharField(max_length='1024', blank=True)
    value_hour_1    = models.CharField(max_length='1024', blank=True)
    value_hour_...
    value_hour_23   = models.CharField(max_length='1024', blank=True)

即使我将arraysdicts 存储在value_hour_n 字段中,它也不能完全提供文章中提到的查询文档的优势,例如timeseries.HR.MIN。有什么建议吗?

【问题讨论】:

    标签: python django mongodb django-models django-mongodb-engine


    【解决方案1】:

    对于结构是一种理想格式,我完全不同意,而且我似乎总是看到这种表示法被用作对如何建模数组的“PHP理解”,但这不适合 Mongo 解释。

    由于我在here 中进行了更详细的介绍,我通常发现以下结构对于查询目的更灵活:

    {
      timestamp_hour: ISODate("2013-10-10T23:00:00.000Z"),
      type: “memory_used”,
      values: [
        [ 999999, 999999, …, 1000000 ],
        [ 2000000, 2000000, …, 1000000 ],
        …,
        [ 1600000, 1200000, …, 1100000 ],
        [ 1300000, 1400000, …, 1500000 ]
      ]
    }
    

    这样(如另一个答案中所述)您不必专门解决路径的任何部分以获取任何元素。子文档表示法是单向,你必须完全指定每一个,不能做范围的事情或在不同的位置找到值。

    使用数组,您将获得 free 的位置符号,因此您可以 values.59 甚至 values.20.15 如果您愿意,或者以其他方式匹配文档中的键,在数组中

    对于您的解决方案,您需要更多地尝试,但这个和其他阅读给出了一般要点。

    【讨论】:

      【解决方案2】:

      你可以做你写的,但是如果你想每 2 小时或每 30 分钟存储一次值怎么办?所以这不是一个好习惯

      这个呢:

      class MyModelStat(models.Model):
          #other fields like : nbr_views, nbr_clicks, rates ...
          my_model = models.ForeignKey(MyModel, related_name="stats")
          created_on = models.DateTimeField(auto_now_add=True)
          previous = models.ForeignKey('self', blank=True, null=True, editable=False)
      
          def save(self, **kwargs):
          current_stats = self.my_model.current_stats
          if current_stats is not None and self.id is None:
              #iterate over the fields, and do your stuff
              self.rates = current_stats.rates + 1 
              self.nbr_views = current_stats.nbr_views
              #set the current stat as the previous for the new stat
              self.previous = self.deal.current_stats
          super(MyModelStat, self).save(**kwargs)
      
      
      
      @receiver(post_save, sender=MyModelStat)
      def set_mymodel_stats(sender, *args, **kwargs):
      """
      Signal handler to ensure that a new stats is always chosen as the current stats - automatically. It simplifies stuff
      greatly. Also stores previous revision for diff-purposes
      """
      instance = kwargs['instance']
      created = kwargs['created']
      if created and instance.my_model:
          instance.my_model.current_stats = instance
          instance.my_model.save()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-04-26
        • 2012-11-16
        • 2014-03-06
        • 1970-01-01
        • 2018-06-02
        • 1970-01-01
        • 2014-03-19
        • 1970-01-01
        相关资源
        最近更新 更多