<commit_before>import logging
import falcon
import ujson as json
import traceback
from falcon import HTTPInvalidParam, HTTPMissingParam
from survey_stats import log
from survey_stats import settings

db_conf = None

def check_media_type(req, resp, params):
    if req.client_accepts_json:
        return
    raise falcon.HTTPUnsupportedMediaType(
        'Media Type not Supported',
        'This API only supports the JSON media type.',
        'http://docs.examples.com/api/json')


def fetch_svy_stats_for_slice(dset_id, svy_id, q, r, f, s):
    (k, cfg) = st.dset[dset_id].fetch_config(national=True, year=None)
    svy = st.dset[dset_id].surveys[k]
    res = svy.fetch_stats_for_slice(q, r, f, s, cfg)
    return res


def remap_vars(cfg, coll, into=True):
    def map_if(pv, k):
        return pv[k] if k in pv else k
    pv = ({v: k for k, v in cfg['pop_vars'].items()} if
          not into else cfg['pop_vars'])
    res = None
    typ = type(coll)
    if isinstance(coll, str):
        res = coll
    elif isinstance(coll, Sequence):
        res = [map_if(pv, k) for k in coll]
    elif isinstance(coll, Mapping):
        res = {map_if(pv, k): remap_vars(cfg, v, into) for
               k, v in coll.items()}
    else:
        res = coll
    return res


class StatsResource:


    def __init__(self):
        self.logger = log.getLogger()


    def on_post(self, req, resp):
        try:
            raw_json = req.stream.read()
        except Exception:
            raise falcon.HTTPError(falcon.HTTP_748,
                                   'Read Error',
                                   'Could not read the request body. Must be '+
                                   'them ponies again.')

        try:
            slice = json.loads(raw_json, 'utf-8')
            self.logger.info(json.dumps(slice))
        except ValueError:
            raise falcon.HTTPBadRequest(
                'Malformed JSON',
                'Could not decode the request body. The ' +
                'JSON was incorrect.')
        try:
            result = fetch_svy_stats_for_slice(**slice)<commit_msg>added name to microservice log stream<commit_after>import logging
import falcon
import ujson as json
import traceback
from falcon import HTTPInvalidParam, HTTPMissingParam
from survey_stats import log
from survey_stats import settings

db_conf = None

def check_media_type(req, resp, params):
    if req.client_accepts_json:
        return
    raise falcon.HTTPUnsupportedMediaType(
        'Media Type not Supported',
        'This API only supports the JSON media type.',
        'http://docs.examples.com/api/json')


def fetch_svy_stats_for_slice(dset_id, svy_id, q, r, f, s):
    (k, cfg) = st.dset[dset_id].fetch_config(national=True, year=None)
    svy = st.dset[dset_id].surveys[k]
    res = svy.fetch_stats_for_slice(q, r, f, s, cfg)
    return res


def remap_vars(cfg, coll, into=True):
    def map_if(pv, k):
        return pv[k] if k in pv else k
    pv = ({v: k for k, v in cfg['pop_vars'].items()} if
          not into else cfg['pop_vars'])
    res = None
    typ = type(coll)
    if isinstance(coll, str):
        res = coll
    elif isinstance(coll, Sequence):
        res = [map_if(pv, k) for k in coll]
    elif isinstance(coll, Mapping):
        res = {map_if(pv, k): remap_vars(cfg, v, into) for
               k, v in coll.items()}
    else:
        res = coll
    return res


class StatsResource:


    def __init__(self):
        self.logger = log.getLogger('statsworker.' + __name__)


    def on_post(self, req, resp):
        try:
            raw_json = req.stream.read()
        except Exception:
            raise falcon.HTTPError(falcon.HTTP_748,
                                   'Read Error',
                                   'Could not read the request body. Must be '+
                                   'them ponies again.')

        try:
            slice = json.loads(raw_json, 'utf-8')
            self.logger.info(json.dumps(slice))
        except ValueError:
            raise falcon.HTTPBadRequest(
                'Malformed JSON',
                'Could not decode the request body. The ' +
                'JSON was incorrect.')
        try:
            result = fetch_svy_stats_for_slice(**slice)
{'fraction_non_alphanumeric': 0.09945793070940373, 'fraction_numerical': 0.0018854584020740043, 'mean_word_length': 2.064259927797834, '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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5900314', 'n_tokens_mistral': 1344, 'n_tokens_neox': 1318, 'n_words': 381}