Commit 201802c0 authored by Etienne Sapin's avatar Etienne Sapin
Browse files

Merge branch 'feature/data_generator' into develop

parents 17b4242a 43f151aa
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
.python-version
# celery beat schedule file
celerybeat-schedule
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
\ No newline at end of file
pass
\ No newline at end of file
# Data generator functions
import mongodb as db
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","Sensor"]
# Log Type List
logTypeList = ["pressure","temperature","cloudLevel"]
# Call DB class and init connection
print("Init DB connection")
dbConn = db.DB()
dbConn.initDBconnection()
# Fill sources table
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:
logTypeTmp = logTypeList[random.randint(0,2)]
valueTmp = 0
if logTypeTmp == "pressure":
valueTmp = pressureValues[inc]
elif logTypeTmp == "temperature":
# Generer une temperature aléatoire en essayant de respecter
# le fait que la nuit, la temperature baisse.
valueTmp = 12*math.cos(-0.00628*(inc+400))+30
elif logTypeTmp == "cloudLevel":
# Generer un niveau de luminosité aléatoire en essayant de respecter
# le fait que la nuit, la luminosité diminue.
valueTmp = math.fabs(0.8*math.sin(0.00628*(inc)))
else:
print("Undefined log type.")
logTmp = {
'type': logTypeTmp,
'value': valueTmp,
'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
pressureValues = numpy.random.normal(1.01325, 0.003, 1000)
time.sleep(random.randint(0,1))
\ No newline at end of file
# Main file, entry point
import generator
def main():
print("Starting Main ...")
generator.generatorMain()
if __name__ == '__main__':
main()
\ No newline at end of file
# DB file with connector
from pymongo import MongoClient
dbName = "starterkit"
class DB():
def __init__(self):
pass
def initDBconnection(self):
client = MongoClient('localhost', 27017)
self.conn = client[dbName]
\ No newline at end of file
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