<commit_before>from tornado import (
    httpserver,
    ioloop,
    options,
    web
    )

from tornado.options import (
    define,
    options
    )

from datetime import date
import tornado
import asyncmongo

define("port", default=8888, help="run on the given port", type=int)


class Application(tornado.web.Application):
    def __init__(self):
        handlers = [
            (r"/api/v1/journal", JournalHandler),
            (r"/api/v1/issue", IssueHandler),
            (r"/api/v1/article", ArticleHandler),
            (r"/api/v1/pdf", PdfHandler),
        ]

        tornado.web.Application.__init__(self, handlers)


class PdfHandler(tornado.web.RequestHandler):
    pass


class ArticleHandler(tornado.web.RequestHandler):
    pass


class IssueHandler(tornado.web.RequestHandler):
    pass


class JournalHandler(tornado.web.RequestHandler):

    @property
    def db(self):
        if not hasattr(self, '_db'):
            self._db = asyncmongo.Client(
                pool_id='accesses',
                host='localhost',
                port=27017,
                maxcached=10,
                maxconnections=600,
                dbname='analytics'
            )

        return self._db

    def post(self):
        code = self.get_argument('code')
        region = self.get_argument('region')
        iso_date = date.isoformat(date.today())
        month_date = iso_date[:7]

        self.db.accesses.update(
            {"code": code},
            {'$inc': {region: 1, iso_date: 1, month_date: 1, 'total': 1}},
            safe=False,
            upsert=True
        )

    @tornado.web.asynchronous
    def get(self):
        code = self.get_argument('code')
        self.db.accesses.find_one({"code": code}, limit=1, callback=self._on_get_response)

    def _on_get_response(self, response, error):
        if error:
            raise tornado.web.HTTPError(500)

        self.write(str(response))
        self.finish()

if __name__ == '__main__':
    tornado.options.parse_command_line()
    http_server = tornado.httpserver.HTTPServer(Application())
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()




#define("mongodb_host", default="127.0.0.1:12707", help="analytics database host")
#define("mongodb_database", default="analytics", help="analytics database name")
<commit_msg>Implementada API de registro e pesquisa de acessos<commit_after>from tornado import (
    httpserver,
    ioloop,
    options,
    web
    )

from tornado.options import (
    define,
    options
    )

from datetime import date
import tornado
import asyncmongo

define("port", default=8888, help="run on the given port", type=int)


class Application(tornado.web.Application):
    def __init__(self):
        handlers = [
            (r"/api/v1/journal", JournalHandler),
            (r"/api/v1/issue", IssueHandler),
            (r"/api/v1/article", ArticleHandler),
            (r"/api/v1/pdf", PdfHandler),
        ]

        tornado.web.Application.__init__(self, handlers)


class PdfHandler(tornado.web.RequestHandler):
    @property
    def db(self):
        if not hasattr(self, '_db'):
            self._db = asyncmongo.Client(
                pool_id='accesses',
                host='localhost',
                port=27017,
                maxcached=10,
                maxconnections=600,
                dbname='analytics'
            )

        return self._db

    def post(self):
        code = self.get_argument('code')
        region = self.get_argument('region')
        journal = self.get_argument('journal')
        access_date = self.get_argument('access_date')
        iso_date = access_date
        month_date = iso_date[:7]

        self.db.accesses.update(
            {'code': code},
            {'$set': {'type': 'article', 'journal': journal}, '$inc': {region: 1, iso_date: 1, month_date: 1, 'total': 1}},
            safe=False,
            upsert=True
        )


class ArticleHandler(tornado.web.RequestHandler):
    @property
    def db(self):
        if not hasattr(self, '_db'):
            self._db = asyncmongo.Client(
                pool_id='accesses',
                host='localhost',
                port=27017,
                maxcached=10,
                maxconnections=600,
                dbname='analytics'
            )

        return self._db

    def post(self):
        code = self.get_argument('code')
        region = self.get_argument('region')
        journal = self.get_argument('journal')
        iso_date = date.isoformat(date.today())
        month_date = iso_date[:7]

        self.db.accesses.update(
            {'code': code},
            {'$set': {'type': 'article', 'journal': journal}, '$inc': {region: 1, iso_date: 1, month_date: 1, 'total': 1}},
            safe=False,
            upsert=True
        )

    @tornado.web.asynchronous
    def get(self):
        code = self.get_argument('code')
        self.db.accesses.find_one({"code": code}, limit=1, callback=self._on_get_response)

    def _on_get_response(self, response, error):
        if error:
            raise tornado.web.HTTPError(500)

        self.write(str(response))
        self.finish()


class IssueHandler(tornado.web.RequestHandler):
    @property
    def db(self):
        if not hasattr(self, '_db'):
            self._db = asyncmongo.Client(
                pool_id='accesses',
                host='localhost',
                port=27017,
                maxcached=10,
                maxconnections=600,
                dbname='analytics'
            )

        return self._db

    def post(self):
        code = self.get_argument('code')
        region = self.get_argument('region')
        journal = self.get_argument('journal')
        iso_date = date.isoformat(date.today())
        month_date = iso_date[:7]

        self.db.accesses.update(
            {'code': code},
            {'$set': {'type': 'issue', 'journal': journal}, '$inc': {region: 1, iso_date: 1, month_date: 1, 'total': 1}},
            safe=False,
            upsert=True
        )

    @tornado.web.asynchronous
    def get(self):
        code = self.get_argument('code')
        self.db.accesses.find_one({"code": code}, limit=1, callback=self._on_get_response)

    def _on_get_response(self, response, error):
        if error:
            raise tornado.web.HTTPError(500)

        self.write(str(response))
        self.finish()


class JournalHandler(tornado.web.RequestHandler):

    @property
    def db(self):
        if not hasattr(self, '_db'):
            self._db = asyncmongo.Client(
                pool_id='accesses',
                host='localhost',
                port=27017,
                maxcached=10,
                maxconnections=600,
                dbname='analytics'
            )

        return self._db

    def post(self):
        code = self.get_argument('code')
        region = self.get_argument('region')
        iso_date = date.isoformat(date.today())
        month_date = iso_date[:7]

        self.db.accesses.update(
            {'code': code},
            {'$set': {'type': 'article'}, '$inc': {region: 1, iso_date: 1, month_date: 1, 'total': 1}},
            safe=False,
            upsert=True
        )

    @tornado.web.asynchronous
    def get(self):
        code = self.get_argument('code')
        self.db.accesses.find_one({"code": code}, limit=1, callback=self._on_get_response)

    def _on_get_response(self, response, error):
        if error:
            raise tornado.web.HTTPError(500)

        self.write(str(response))
        self.finish()

if __name__ == '__main__':
    tornado.options.parse_command_line()
    http_server = tornado.httpserver.HTTPServer(Application())
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()




#define("mongodb_host", default="127.0.0.1:12707", help="analytics database host")
#define("mongodb_database", default="analytics", help="analytics database name")

{'fraction_non_alphanumeric': 0.12877643504531722, 'fraction_numerical': 0.01623867069486405, 'mean_word_length': 2.1452889944576405, 'pattern_counts': {'":': 5, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '7510823', 'n_tokens_mistral': 2556, 'n_tokens_neox': 2376, 'n_words': 455}