Skeleton of history generation

This commit is contained in:
Rémi Oudin 2018-02-19 22:56:16 +01:00
parent 7f343d8ad8
commit 7c13ee17d4
2 changed files with 55 additions and 5 deletions

View file

@ -3,13 +3,16 @@ entries, which looks like human-based browsing, according to a dedicated user
interests, keywords... interests, keywords...
""" """
from random import weibullvariate
from math import floor
from django.db import models from django.db import models
from profiles.models import BrowserFingerprint from profiles.models import Profile
from pinocchio.settings import HISTORY_MIN
class HistoryEntry(models.Model): class HistoryEntry(models.Model):
""" A history entry, aka a url, and a timestamp. """ A history entry, aka a url, and a timestamp.
""" """
search = models.CharField(max_length=255) search = models.URLField(help_text="The url to be searched")
timestamp = models.DateTimeField() timestamp = models.DateTimeField()
history = models.ForeignKey( history = models.ForeignKey(
'History', 'History',
@ -17,6 +20,8 @@ class HistoryEntry(models.Model):
) )
def __str__(self): def __str__(self):
""" Returns the string representation of a history entry.
"""
return "{} : {}".format(self.timestamp, self.search) return "{} : {}".format(self.timestamp, self.search)
@ -24,14 +29,56 @@ class History(models.Model):
""" A history for a user, containing some web connections (http, https). """ A history for a user, containing some web connections (http, https).
Each history is timed, in a human-behaviour manner. """ Each history is timed, in a human-behaviour manner. """
start_ts = models.DateTimeField() start_ts = models.DateTimeField(
help_text='The starting timestamp of the history. Useful for cron-like '
'structure.'
)
played = models.BooleanField(default=False) played = models.BooleanField(default=False)
user_agent = models.ForeignKey( user = models.ForeignKey(
BrowserFingerprint, Profile,
on_delete=models.CASCADE on_delete=models.CASCADE
) )
def return_history(self):
""" Returns the history, sorted by increasing timestamps
"""
history_set = self.history_set.order_by('timestamp')
return history_set
def __str__(self): def __str__(self):
""" Returns the string representation of a history.
"""
history_set = self.history_set.order_by('timestamp') history_set = self.history_set.order_by('timestamp')
header = "[History]:\n" header = "[History]:\n"
return header + "\n".join(history_set) return header + "\n".join(history_set)
def play_history(self):
""" Actually plays the history.
"""
self.played = True
self.save()
def generate_history(user, ts_start):
""" Generate a new history for the user `user`, starting from timestamp
`ts_start`.
A few heuristics are used in order to give the impression that the history
is actually played by a user.
"""
# let's defin a new history object.
history = History(start_ts=ts_start, user=user)
length = HISTORY_MIN + floor(10 * weibullvariate(1, 1.5))
history_line = 0
while history_line < length:
ts_start += random.uniform(1, 10)
new_line = HistoryEntry(
search="https://google.com:",
timestamp=ts_start,
history=history
)

View file

@ -13,6 +13,9 @@ https://docs.djangoproject.com/en/2.0/ref/settings/
import os import os
from .settings_local import BASE_DIR, DEBUG, SECRET_KEY, DATABASES from .settings_local import BASE_DIR, DEBUG, SECRET_KEY, DATABASES
HISTORY_MIN = 25
ALLOWED_HOSTS = [] ALLOWED_HOSTS = []