Skip to content

Commit

Permalink
🌱 add seeding for unique visits and page visits
Browse files Browse the repository at this point in the history
  • Loading branch information
FredrikMorstad committed Nov 5, 2023
1 parent 9e156ef commit 3b61a56
Showing 1 changed file with 50 additions and 3 deletions.
53 changes: 50 additions & 3 deletions utils/seeding.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import json
import os
import shutil
import random
import random


# TODO fix imports so that the file can be added into the db folder
Expand All @@ -19,6 +19,49 @@
date_weights = (20, 20, 15, 5, 5, 5, 5, 3, 2, 2, 2, 2, 2, 2, 4, 2, 2, 2)


def seed_stats(db):
seed_unique_visits(db)
seed_page_visits(db)

def seed_page_visits(db):
db.pageVisitLog.delete_many({})
now = datetime.now()
base = "/event"

event_ids = db.events.find({"eid": {"$exists": True}}, {"eid": 1, "_id": 0})
for event in event_ids:
for _ in range(0, random.randint(20, 150)):
ts = generate_random_ts(now)
db.pageVisitLog.insert_one({"timestamp": ts, "metaData": f"{base}/{event['eid']}"})

base = "/jobs"
job_ids = db.jobs.find({"id": {"$exists": True}}, {"id": 1, "_id": 0})
for job in job_ids:
for _ in range(0, random.randint(20, 150)):
ts = generate_random_ts(now)
db.pageVisitLog.insert_one({"timestamp": ts, "metaData": f"{base}/{job['id']}"})

def generate_random_ts(base_date):
rand_day = random.randint(1, 100)
rand_hour = random.randint(0, 23)
rand_min = random.randint(0, 59)
return base_date - timedelta(days=rand_day, hours=rand_hour, minutes=rand_min)


# clears all entries before seeding as its easier then checking between each day
# seeds unique visits for a year not including the current date
def seed_unique_visits(db):
db.uniqueVisitLog.delete_many({})
now = datetime.now()
for i in range(0, 365):
new_date = now - timedelta(days=i)
# generates random ts in a day
for _ in range(0, random.randint(20, 100)):
rand_hour = random.randint(0, 23)
rand_min= random.randint(0, 59)
ts = new_date.replace(hour=rand_hour, minute=rand_min)
db.uniqueVisitLog.insert_one({"timestamp": ts})

def seed_random_member(db, number):
''' seed a numbr of random users '''
id = 0
Expand Down Expand Up @@ -139,7 +182,11 @@ def seed_jobs(db, seed_path):

if __name__ == "__main__":
db = get_db()
events_seed_path = f"{base_dir}/events.json"
jobs_seed_path = f"{base_dir}/jobs.json"

seed_random_member(db, 10)
seed_members(db, f"{base_dir}/members.json")
seed_events(db, f"{base_dir}/events.json")
seed_jobs(db, f"{base_dir}/jobs.json")
seed_events(db, events_seed_path)
seed_jobs(db, jobs_seed_path)
seed_stats(db)

0 comments on commit 3b61a56

Please sign in to comment.