Commit 43f151aa authored by Etienne Sapin's avatar Etienne Sapin
Browse files

feat (logs): can now populate 50/50 users & camps while creating logs and some sources

parent c3a61db3
......@@ -5,9 +5,10 @@ import random
import numpy
import time
import math
import string
# Source Type List => can be a API req or something retrieved from a sensor
sourceTypeList = ["API","Capteur"]
sourceTypeList = ["API","Sensor"]
# Log Type List
logTypeList = ["pressure","temperature","cloudLevel"]
......@@ -17,22 +18,45 @@ dbConn = db.DB()
dbConn.initDBconnection()
# Fill sources table
def init():
pass
def initSourcesTable(sourcesNameList):
if dbConn.conn.sources.count() < 10:
for name in sourcesNameList:
# print("Source name: " + name)
sourceTmp = {
'type': sourceTypeList[random.randint(0,1)],
'name': name
}
# print(sourceTmp)
dbConn.conn.sources.insert_one(sourceTmp)
else:
print("Sources already populated")
def generatorMain():
print("Starting generator main")
print("Generating pressure values")
pressureValues = numpy.random.normal(1.01325, 0.003, 1000)
print("Generating random sources")
sourcesNameList = []
for x in range(0,10):
sourcesNameList.append(''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(8)))
# print(sourcesNameList)
initSourcesTable(sourcesNameList)
# Fetch les sources et en choisir une aléatoirement
listSources = list(dbConn.conn.sources.find({}))
print(listSources)
listUsers = list(dbConn.conn.users.find({}))
print(listUsers)
listCamps = list(dbConn.conn.camps.find({}))
print(listCamps)
inc = 0
#
while True:
# Creation a la volee de source =!
sourceTmp = {
'type': sourceTypeList[random.randint(0,1)]
}
# print(sourceTmp)
logTypeTmp = logTypeList[random.randint(0,2)]
valueTmp = 0
......@@ -51,11 +75,24 @@ def generatorMain():
logTmp = {
'type': logTypeTmp,
'value': valueTmp,
'source': sourceTmp
'source': listSources[random.randint(0,9)]
}
print("New log entry\n" + str(logTmp))
# Insert a log into DB
result=dbConn.conn.logs.insert_one(logTmp)
# print(listCamps[0]['_id'])
# Insert 50/50 into camps and users logs
if random.randint(0,1) == 0:
# Insert camps
dbConn.conn.camps.update({'_id':listCamps[(random.randint(0,len(listCamps))-1)]['_id']}, {'$push': {'logs': logTmp}}, upsert = True)
else:
# Insert users
dbConn.conn.users.update({'_id':listUsers[(random.randint(0,len(listUsers))-1)]['_id']}, {'$push': {'logs': logTmp}}, upsert = True)
inc += 1
if inc == 1000:
inc = 0
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment