index.community/backend/scraper/models.py

60 lines
2.3 KiB
Python
Raw Normal View History

2018-08-26 00:32:55 +00:00
from django.db import models
2019-02-21 10:38:49 +00:00
from django.utils import timezone
2018-08-26 00:32:55 +00:00
class Instance(models.Model):
"""
The main model that saves details of an instance and links between them in the peers
property.
Don't change the schema without verifying that the gephi script can still read the data.
"""
2018-08-29 17:58:06 +00:00
# Primary key
2018-08-26 01:17:10 +00:00
name = models.CharField(max_length=200, primary_key=True)
2018-08-26 00:32:55 +00:00
2018-08-29 17:58:06 +00:00
# Details
2018-08-29 18:04:03 +00:00
description = models.TextField(blank=True)
2018-08-28 22:22:29 +00:00
domain_count = models.IntegerField(blank=True, null=True)
status_count = models.IntegerField(blank=True, null=True)
user_count = models.IntegerField(blank=True, null=True)
version = models.CharField(max_length=1000, blank=True) # In Django CharField is never stored as NULL in the db
2018-08-26 00:32:55 +00:00
status = models.CharField(max_length=100)
2018-08-29 17:58:06 +00:00
# Foreign keys
2018-09-01 17:24:05 +00:00
peers = models.ManyToManyField('self', symmetrical=False, through='PeerRelationship')
2018-08-29 17:58:06 +00:00
2018-09-03 14:10:44 +00:00
# Graph
x_coord = models.FloatField(blank=True, null=True)
y_coord = models.FloatField(blank=True, null=True)
2018-08-29 17:58:06 +00:00
# Automatic fields
first_seen = models.DateTimeField(auto_now_add=True)
2019-02-21 10:38:49 +00:00
last_updated = models.DateTimeField(default=timezone.now)
class PeerRelationship(models.Model):
source = models.ForeignKey(Instance, related_name="following_relationship", on_delete=models.CASCADE)
target = models.ForeignKey(Instance, related_name="follower_relationships", on_delete=models.CASCADE)
# Interaction stats
2019-02-21 12:32:50 +00:00
mention_count = models.IntegerField(default=0)
statuses_seen = models.IntegerField(default=0) # because we want mention_count as a ratio
# Metadata
first_seen = models.DateTimeField(auto_now_add=True)
2019-02-21 10:38:49 +00:00
last_updated = models.DateTimeField(default=timezone.now)
2018-09-03 14:10:44 +00:00
class Edge(models.Model):
"""
2019-02-21 12:32:50 +00:00
This class is automatically generated from PeerRelationship using the build_edges command.
2018-09-03 14:10:44 +00:00
It aggregates stats from the asymmetrical PeerRelationship to a symmetrical one that's suitable for serving
to the front-end.
"""
2019-04-19 14:29:45 +00:00
source = models.ForeignKey(Instance, related_name='targets', on_delete=models.CASCADE)
target = models.ForeignKey(Instance, related_name='sources', on_delete=models.CASCADE)
2018-09-03 14:10:44 +00:00
weight = models.FloatField(blank=True, null=True)
# Metadata
2019-02-21 10:38:49 +00:00
last_updated = models.DateTimeField(default=timezone.now)