<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from gevent import monkey; monkey.patch_all()

from bottle import Bottle, request, response, run, abort
import bottle.ext.mongo
import bottle.ext.redis
import os
from datetime import datetime

from ghdata.config import MONGODB_URI, REDIS_HOST, REDIS_PORT, REDIS_DB
from ghdata.db import format_key as _format

app = Bottle()
app.install(bottle.ext.mongo.MongoPlugin(uri=MONGODB_URI, db="github", json_mongo=True))
app.install(bottle.ext.redis.RedisPlugin(host=REDIS_HOST, port=REDIS_PORT, database=REDIS_DB))

from beaker.cache import CacheManager
from beaker.util import parse_cache_config_options

cache_opts = {
    'cache.type': 'file',
    'cache.data_dir': '/tmp/ghdata/data',
    'cache.lock_dir': '/tmp/ghdata/lock'
}

cache = CacheManager(**parse_cache_config_options(cache_opts))


@app.hook("after_request")
def crossDomianHook():
    response.headers["Access-Control-Allow-Origin"] = "*"


@app.route(path="/languages", method="OPTIONS")
@app.route(path="/rank", method="OPTIONS")
def options1(method, *args):
    return options(*args)


@app.route(path="/users/:id", method="OPTIONS")
def options2(id, *args):
    return options(*args)


def options(*args):
    response.headers["Access-Control-Allow-Methods"] = "GET, POST, PUT, DELETE"
    if request.headers.get("Access-Control-Request-Headers"):
        response.headers["Access-Control-Allow-Headers"] = request.headers["Access-Control-Request-Headers"]


@app.get("/users/:id")
def user(id, rdb, mongodb):
    user = mongodb.users_stats.find_one({'_id': id.lower()})
    if not user:
        return abort(404)
    user['rank'] = {}
    langs = [lang for lang in user.get('contrib', {})]
    pipe = rdb.pipeline()
    if user.get('loc', {}).get('country') == 'China':
        for lang in langs:
            key = _format("country:{0}.lang:{1}:user".format(user.get('loc', {}).get('country', 'China'), lang))
            pipe.zrevrank(key, id)
        user['rank']['China'] = {lang: rank + 1 for lang, rank in zip(langs, pipe.execute()) if rank is not None}
    for lang in langs:
        key = _format("lang:{0}:user".format(lang))
        pipe.zrevrank(key, id)
    user['rank']['World'] = {lang: rank + 1 for lang, rank in zip(langs, pipe.execute()) if rank is not None}
    return user


@app.get("/languages")
def languages(mongodb):
    now = datetime.now()
    year, month = (now.year-1, 12) if now.month == 1 else (now.year, now.month-1)
    key = 'month.%d.%2d' % (year, month)
    # get languages sorted by activity of last month in the world.<commit_msg>Update app.py<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from gevent import monkey; monkey.patch_all()

from bottle import Bottle, request, response, run, abort
import bottle.ext.mongo
import bottle.ext.redis
import os
from datetime import datetime

from ghdata.config import MONGODB_URI, REDIS_HOST, REDIS_PORT, REDIS_DB
from ghdata.db import format_key as _format

app = Bottle()
app.install(bottle.ext.mongo.MongoPlugin(uri=MONGODB_URI, db="github", json_mongo=True))
app.install(bottle.ext.redis.RedisPlugin(host=REDIS_HOST, port=REDIS_PORT, database=REDIS_DB))

from beaker.cache import CacheManager
from beaker.util import parse_cache_config_options

cache_opts = {
    'cache.type': 'file',
    'cache.data_dir': '/tmp/ghdata/data',
    'cache.lock_dir': '/tmp/ghdata/lock'
}

cache = CacheManager(**parse_cache_config_options(cache_opts))


@app.hook("after_request")
def crossDomianHook():
    response.headers["Access-Control-Allow-Origin"] = "*"


@app.route(path="/languages", method="OPTIONS")
@app.route(path="/rank", method="OPTIONS")
def options1(method, *args):
    return options(*args)


@app.route(path="/users/:id", method="OPTIONS")
def options2(id, *args):
    return options(*args)


def options(*args):
    response.headers["Access-Control-Allow-Methods"] = "GET, POST, PUT, DELETE"
    if request.headers.get("Access-Control-Request-Headers"):
        response.headers["Access-Control-Allow-Headers"] = request.headers["Access-Control-Request-Headers"]


@app.get("/users/:id")
def user(id, rdb, mongodb):
    id = id.lower()
    user = mongodb.users_stats.find_one({'_id': id})
    if not user:
        return abort(404)
    user['rank'] = {}
    langs = [lang for lang in user.get('contrib', {})]
    pipe = rdb.pipeline()
    if user.get('loc', {}).get('country') == 'China':
        for lang in langs:
            key = _format("country:{0}.lang:{1}:user".format(user.get('loc', {}).get('country', 'China'), lang))
            pipe.zrevrank(key, id)
        user['rank']['China'] = {lang: rank + 1 for lang, rank in zip(langs, pipe.execute()) if rank is not None}
    for lang in langs:
        key = _format("lang:{0}:user".format(lang))
        pipe.zrevrank(key, id)
    user['rank']['World'] = {lang: rank + 1 for lang, rank in zip(langs, pipe.execute()) if rank is not None}
    return user


@app.get("/languages")
def languages(mongodb):
    now = datetime.now()
    year, month = (now.year-1, 12) if now.month == 1 else (now.year, now.month-1)
    key = 'month.%d.%2d' % (year, month)
    # get languages sorted by activity of last month in the world.
{'fraction_non_alphanumeric': 0.16615384615384615, 'fraction_numerical': 0.006538461538461538, 'mean_word_length': 4.129191321499014, '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': '6209283', 'n_tokens_mistral': 1808, 'n_tokens_neox': 1751, 'n_words': 408}