<commit_before>from __future__ import (absolute_import, division, print_function,
                        unicode_literals)
import tornado.ioloop
import tornado.web
from tornado import gen
import simplejson as json
from bson import json_util
import pymongo
import motor
import tornado.escape

from metadataservice.server import utils

__author__ = 'arkilic'

# READ THE DOCS and COMMENTS before grabbing your pitchforks and torches. A lot going on here!!

# TODO: Write your own json encoder/decoder that handles ObjectId and datetime.datetime neatly!!!!

def db_connect(database ,host, port):
    """Helper function to deal with stateful connections to motor. Connection established lazily.
    Asycnc so do not treat like mongonengine connection pool.
    Tornado needs both client and server so this routine returns both. Once we figure out how to use tornado and motor
    properly, we may need to fix this.

    Parameters
    ----------
    server: str
        The name of the server that data is stored
    host: str
        Name/address of the server that mongo daemon lives
    port: int
        Port num of the server

    Returns
    -------
    db: motor.MotorDatabase
        Async server object
    """
    client = motor.MotorClient()
    database = client[database]
    return database


class RunStartHandler(tornado.web.RequestHandler):
    """Handler for run_start insert and query operations"""
    @tornado.web.asynchronous
    @gen.coroutine
    def get(self):
        # TODO: Add sort by time!
        """Query run_start documents"""
        query = utils._unpack_params(self)
        start = query.pop('range_floor')
        stop = query.pop('range_ceil')
        cursor = db.run_start.find(query).sort('time', pymongo.DESCENDING)[start:stop]
        docs = yield cursor.to_list(None)
        self.write(json_util.dumps(docs))
        self.finish()

    @tornado.web.asynchronous
    @gen.coroutine
    def post(self):
        """Insert a run_start document"""
        db = self.settings['db']
        data = json_util.loads(self.request.body.decode("utf-8"))
        #TODO: Add validation once database is implemented
        result = yield db.run_start.insert(data)#async insert
        self.finish()


class BeamlineConfigHandler(tornado.web.RequestHandler):
    """Handler for run_start insert and query operations"""
    @tornado.web.asynchronous
    @gen.coroutine
    def get(self):
        # TODO: Add sort by time!
        """Query beamline_config documents"""
        query = utils._unpack_params(self)
        start = query.pop('range_floor')
        stop = query.pop('range_ceil')
        cursor = db.beamline_config.find(query).sort('time', pymongo.DESCENDING)[start:stop]
        docs = yield cursor.to_list(None)
        self.write(json_util.dumps(docs))
        self.finish()

    @tornado.web.asynchronous
    @gen.coroutine
    def post(self):
        """Insert a beamline_config document"""
        db = self.settings['db']
        data = json.loads(self.request.body)
        #TODO: Add validation once database is implemented
        result = yield db.beamline_config.insert(data)#async insert
        self.finish()

class EventDescriptorHandler(tornado.web.RequestHandler):
    """Handler for run_start insert and query operations"""
    @tornado.web.asynchronous
    @gen.coroutine
    def get(self):
        # TODO: Add sort by time!
        """Query event_descriptor documents"""
        query = utils._unpack_params(self)
        start = query.pop('range_floor')
        stop = query.pop('range_ceil')
        cursor = db.event_descriptor.find(query).sort('time', pymongo.DESCENDING)[start:stop]
        docs = yield cursor.to_list(None)
        self.write(json_util.dumps(docs))
        self.finish()

    @tornado.web.asynchronous
    @gen.coroutine
    def post(self):
        """Insert an event_descriptor document"""
        db = self.settings['db']
        data = json.loads(self.request.body)
        #TODO: Add validation once database is implemented
        result = yield db.event_descriptor.insert(data)#async insert
        self.finish()


class RunStopHandler(tornado.web.RequestHandler):
    """Handler for run_start insert and query operations"""
    @tornado.web.asynchronous
    @gen.coroutine
    def get(self):
        # TODO: Add sort by time!
        """Query run_start documents"""
        query = utils._unpack_params(self)
        start = query.pop('range_floor')
        stop = query.pop('range_ceil')
        cursor = db.run_stop.find(query).sort('time', pymongo.DESCENDING)[start:stop]
        docs = yield cursor.to_list(None)
        self.write(json_util.dumps(docs))
        self.finish()

    @tornado.web.asynchronous
    @gen.coroutine
    def post(self):
        """Insert a run_start document"""
        # placeholder dummy!
        db = self.settings['db']
        data = json.loads(self.request.body)
        #TODO: Add validation once database is implemented
        result = yield db.run_stop.insert(data)#async insert
        self.finish()


class EventHandler(tornado.web.RequestHandler):
    """Handler for run_start insert and query operations"""
    @tornado.web.asynchronous
    @gen.coroutine
    def get(self):
        # TODO: Add sort by time!
        """Query event documents"""
        query = utils._unpack_params(self)
        start = query.pop('range_floor')
        stop = query.pop('range_ceil')
        cursor = db.event_descriptor.find(query).sort('time', pymongo.DESCENDING)[start:stop]
        docs = yield cursor.to_list(None)
        self.write(json_util.dumps(docs))
        self.finish()
    
    @tornado.web.asynchronous
    @gen.coroutine
    def post(self):
        """Insert a run_start document"""
        # placeholder dummy!
        db = self.settings['db']
        data = json.loads(self.request.body)
        #TODO: Add validation once database is implemented<commit_msg>WIP:Finally all handlers have the correct json decoding feature for post<commit_after>from __future__ import (absolute_import, division, print_function,
                        unicode_literals)
import tornado.ioloop
import tornado.web
from tornado import gen
import simplejson as json
from bson import json_util
import pymongo
import motor
import tornado.escape

from metadataservice.server import utils

__author__ = 'arkilic'

# READ THE DOCS and COMMENTS before grabbing your pitchforks and torches. A lot going on here!!

# TODO: Write your own json encoder/decoder that handles ObjectId and datetime.datetime neatly!!!!

def db_connect(database ,host, port):
    """Helper function to deal with stateful connections to motor. Connection established lazily.
    Asycnc so do not treat like mongonengine connection pool.
    Tornado needs both client and server so this routine returns both. Once we figure out how to use tornado and motor
    properly, we may need to fix this.

    Parameters
    ----------
    server: str
        The name of the server that data is stored
    host: str
        Name/address of the server that mongo daemon lives
    port: int
        Port num of the server

    Returns
    -------
    db: motor.MotorDatabase
        Async server object
    """
    client = motor.MotorClient()
    database = client[database]
    return database


class RunStartHandler(tornado.web.RequestHandler):
    """Handler for run_start insert and query operations"""
    @tornado.web.asynchronous
    @gen.coroutine
    def get(self):
        # TODO: Add sort by time!
        """Query run_start documents"""
        query = utils._unpack_params(self)
        start = query.pop('range_floor')
        stop = query.pop('range_ceil')
        cursor = db.run_start.find(query).sort('time', pymongo.DESCENDING)[start:stop]
        docs = yield cursor.to_list(None)
        self.write(json_util.dumps(docs))
        self.finish()

    @tornado.web.asynchronous
    @gen.coroutine
    def post(self):
        """Insert a run_start document"""
        db = self.settings['db']
        data = json_util.loads(self.request.body.decode("utf-8"))
        #TODO: Add validation once database is implemented
        result = yield db.run_start.insert(data)#async insert
        self.finish()


class BeamlineConfigHandler(tornado.web.RequestHandler):
    """Handler for run_start insert and query operations"""
    @tornado.web.asynchronous
    @gen.coroutine
    def get(self):
        # TODO: Add sort by time!
        """Query beamline_config documents"""
        query = utils._unpack_params(self)
        start = query.pop('range_floor')
        stop = query.pop('range_ceil')
        cursor = db.beamline_config.find(query).sort('time', pymongo.DESCENDING)[start:stop]
        docs = yield cursor.to_list(None)
        self.write(json_util.dumps(docs))
        self.finish()

    @tornado.web.asynchronous
    @gen.coroutine
    def post(self):
        """Insert a beamline_config document"""
        db = self.settings['db']
        data = json_util.loads(self.request.body.decode("utf-8"))
        #TODO: Add validation once database is implemented
        result = yield db.beamline_config.insert(data)#async insert
        self.finish()

class EventDescriptorHandler(tornado.web.RequestHandler):
    """Handler for run_start insert and query operations"""
    @tornado.web.asynchronous
    @gen.coroutine
    def get(self):
        # TODO: Add sort by time!
        """Query event_descriptor documents"""
        query = utils._unpack_params(self)
        start = query.pop('range_floor')
        stop = query.pop('range_ceil')
        cursor = db.event_descriptor.find(query).sort('time', pymongo.DESCENDING)[start:stop]
        docs = yield cursor.to_list(None)
        self.write(json_util.dumps(docs))
        self.finish()

    @tornado.web.asynchronous
    @gen.coroutine
    def post(self):
        """Insert an event_descriptor document"""
        db = self.settings['db']
        data = json_util.loads(self.request.body.decode("utf-8"))
        #TODO: Add validation once database is implemented
        result = yield db.event_descriptor.insert(data)#async insert
        self.finish()


class RunStopHandler(tornado.web.RequestHandler):
    """Handler for run_start insert and query operations"""
    @tornado.web.asynchronous
    @gen.coroutine
    def get(self):
        # TODO: Add sort by time!
        """Query run_start documents"""
        query = utils._unpack_params(self)
        start = query.pop('range_floor')
        stop = query.pop('range_ceil')
        cursor = db.run_stop.find(query).sort('time', pymongo.DESCENDING)[start:stop]
        docs = yield cursor.to_list(None)
        self.write(json_util.dumps(docs))
        self.finish()

    @tornado.web.asynchronous
    @gen.coroutine
    def post(self):
        """Insert a run_start document"""
        # placeholder dummy!
        db = self.settings['db']
        data = json_util.loads(self.request.body.decode("utf-8"))
        #TODO: Add validation once database is implemented
        result = yield db.run_stop.insert(data)#async insert
        self.finish()


class EventHandler(tornado.web.RequestHandler):
    """Handler for run_start insert and query operations"""
    @tornado.web.asynchronous
    @gen.coroutine
    def get(self):
        # TODO: Add sort by time!
        """Query event documents"""
        query = utils._unpack_params(self)
        start = query.pop('range_floor')
        stop = query.pop('range_ceil')
        cursor = db.event_descriptor.find(query).sort('time', pymongo.DESCENDING)[start:stop]
        docs = yield cursor.to_list(None)
        self.write(json_util.dumps(docs))
        self.finish()
    
    @tornado.web.asynchronous
    @gen.coroutine
    def post(self):
        """Insert a run_start document"""
        # placeholder dummy!
        db = self.settings['db']
        data = json_util.loads(self.request.body.decode("utf-8"))
        #TODO: Add validation once database is implemented
{'fraction_non_alphanumeric': 0.10602631799513872, 'fraction_numerical': 0.0005028916268544129, 'mean_word_length': 2.825585123436999, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5503640', 'n_tokens_mistral': 3487, 'n_tokens_neox': 3455, 'n_words': 911}