django timeseries postgres初学者

时间:2017-02-02 19:52:35

标签: python django mongodb postgresql influxdb

我正在django中编写小应用程序,以便学习数据库和Web等。 应用程序应显示玩家数据并记录一段时间内的统计数据 回答诸如以下问题:
上个月/周/天有多少次随机战斗? - 图
哪些球员离开/进入战队? - 等等。

我一直在寻找和阅读,我发现了这些建议: 什么是使用技术的良好组合?

MongoDB, InfluxDB, PostgreSQL, RedisDB, or from packages django-timeseries, django-reversion.

我没有找到自己的方法,我可能,有人可以建议我使用哪个数据库以及模型应该是什么样的?

非常感谢有关数据库设计的批评或建议。

正在以第三方API的形式下载数据作为jsons。 下载的结构:

Player.json

{
  "501435906": { # this is PlayerID
    "last_battle_time": 1484160229,
    "statistics": {
      "all": {
        "battles": 70555
      },
      "random": {
        "battles": 67361
      }
    }
  }, # then next players continue
}

clan.json

{
  "500004323": { # clan ID
    "members": [
      {
        "account_id": 501435906, # PlayerID same as in Player.json
        "account_name": "Player1",
        "joined_at": 1447589992,
        "role": "private",
        "role_i18n": "Private"
      },]
    "name": "Full Clan Name",
    "tag": "TAG",
}

stronghold.json

{
  "500323931": { # PlayerID
    "stronghold_skirmish": null,
    "total_resources_earned": 0,
    "week_resources_earned": 0
  }, # next player follows
}

我的方法: 合并数据

{
  "500004323": {
    'name': 'Full Clan Name',
    'tag': 'TAG'
    "members": {
      "500012979": {
        "account_id": "500012979",
        "account_name": "Player1",
        "joined_clan_date": 1415990768,
        "last_battle_time": 1484160229,
        "role": "Commander",
        "statistics": {
          "all": {
            "battles": 70555
          },
          "random": {
            "battles": 67361
          }
        },
        "stronghold": {
          "stronghold_skirmish": {
            "battles": 2223
          },
          "total_resources_earned": 32582,
          "week_resources_earned": 80
        }
      }, # next members
    }, # next clan
}

并将此数据导入以下模型:

class Clan(models.Model):
    """Clan model"""
    clan_id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=100)
    tag = models.CharField(max_length=5)

    @property
    def members(self):
        return Player.objects.filter(clan=self)

    def kick_player(self, player):
        player.leave_clan()

class PlayerLeaversManager(models.Manager):

    def leave_clan(self, players):
        """Take list of players and remove them from their clan
        :param players:
        """
        for player in players:
            player.leave_clan()

class Player(models.Model):
    """Player model"""
    account_id = models.IntegerField(primary_key=True)
    access_token = models.TextField(blank=True,
                                    null=True)
    access_token_expires_at = models.CharField(max_length=10,
                                               blank=True,
                                               null=True)
    account_name = models.CharField(max_length=250)


    clan = models.ForeignKey('Clan',
                               on_delete=models.CASCADE,
                               blank=True,
                               null=True,
                            related_name='current_clan')

    previous_clan = models.ForeignKey('Clan',
                               on_delete=models.CASCADE,
                               blank=True,
                               null=True,
                            related_name='previous_clan')

    # objects = models.Manager()
    objects = PlayerLeaversManager()


    def __str__(self):
        return '{0} - {1}'.format(self.account_name, self.account_id)

    def get_absolute_url(self):
        return reverse('wot:player_detail',
                       args=[self.account_name])

    def leave_clan(self):
        self.previous_clan = self.clan
        self.clan = None
        self.save()

class PlayerData(models.Model):
    """Players data daily tracked"""
    created = models.DateTimeField(auto_now_add=True)

    player = models.ForeignKey('Player',
                               on_delete=models.CASCADE,
                               null=True,
                               blank=True
                               )

    joined_clan_date = models.DateTimeField(blank=True,
                                             null=True)

    role_in_clan = models.CharField(max_length=250,
                                    blank=True,
                                    null=True)

    battles_on_random = models.IntegerField(blank=True,
                                               null=True)
    battles_all = models.IntegerField(blank=True,
                                      null=True)
    battles_stronghold = models.IntegerField(blank=True,
                                             null=True)

    tank = models.ManyToManyField('Vehicle',
                                    related_name='tanks',
                                    blank=True,)

    last_battle_time = models.DateTimeField(blank=True,
                                             null=True)
    # stronghold stats
    total_resources_earned = models.IntegerField(blank=True, null=True)

    week_resources_earned = models.IntegerField(blank=True, null=True)

可以在我的github上找到整个代码https://github.com/1oglop1/anv_wot 谢谢你的所有建议。

1 个答案:

答案 0 :(得分:1)

经过一些研究并在python FB社区询问后,我将尝试以下内容: 1)标准化模型 2)使用PostgreSQL作为后端。