Commit 3c3e8481 authored by Raphael Beer's avatar Raphael Beer

[init] from shadowban-eu/shadowban-eu-frontend@6141c49

parents
# Logs
logs
*.log
npm-debug.log*
# Runtime data
pids
*.pid
*.seed
# IDE
.idea
# OS generated files
.DS_Store
.DS_Store?
._*
.Spotlight-V100
ehthumbs.db
Icon?
Thumbs.db
# Babel ES6 compiles files
dist
# Documentation build
doc/build
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# node-waf configuration
.lock-wscript
# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
# Dependency directory
node_modules
# Optional npm cache directory
.npm
# Optional REPL history
.node_repl_history
# .env
.env
.env.*
.ht*
This diff is collapsed.
import copy
import traceback
import sys
from pymongo import MongoClient, errors as MongoErrors
class Database:
def __init__(self, host=None, port=27017, db='tester', collection_name='results'):
try:
print('[mongoDB] Connecting to ' + host + ':' + str(port))
print('[mongoDB] Using Collection `' + collection_name + '` in Database `' + db + '`')
self.client = MongoClient(host, port, serverSelectionTimeoutMS=3)
self.db = self.client[db]
# collection for test results
self.results = self.db[collection_name]
# collection for rate limit monitoring
self.rate_limits = self.db['rate-limits']
# test connection immediately, instead of
# when trying to write in a request, later.
self.client.admin.command('ismaster')
except MongoErrors.ServerSelectionTimeoutError:
print(traceback.format_exc())
sys.exit('MongoDB connection timed out.')
except:
print(traceback.format_exc())
sys.exit('MongoDB connection failed.')
def write_result(self, result):
# copy.deepcopy; otherwise mongo ObjectId (_id) would be added,
# screwing up later JSON serialisation of results
self.results.insert_one(copy.deepcopy(result))
def write_rate_limit(self, data):
self.rate_limits.insert_one(data)
def connect(host=None, port=27017, db='tester', collection_name='results'):
if host is None:
raise ValueError('[mongoDB] Database constructor needs a `host`name or ip!')
return Database(host=host, port=port, db=db, collection_name=collection_name)
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