<commit_before>"""
Data resource for completeness data
"""
from flask_restful import Resource
from flask import jsonify, request
from dateutil.parser import parse
from sqlalchemy import extract, func, Integer, or_
from datetime import datetime, timedelta
import pandas as pd

from meerkat_api import db, app
from meerkat_api.resources.epi_week import EpiWeek, epi_week_start, epi_year_start
from meerkat_abacus.model import Data, Locations
from meerkat_api.util import get_children, is_child, fix_dates
from meerkat_abacus.util import get_locations
from meerkat_api.authentication import authenticate

from meerkat_api.resources.explore import QueryVariable, get_variables


kit_contents= {
    "barcode_albe": {
        "total":2,
        "dose":1,
        "tablets_in_kit":200
    },
    "barcode_magn": {
        "total":1,
        "dose":2,
        "tablets_in_kit":1000
    },
    "barcode_amox": {
        "total":30,
        "dose":2,
        "tablets_in_kit":3000
    },
    "barcode_benz": {
        "total":1,
        "dose":10,
        "tablets_in_kit":1
    },
    "barcode_chlo": {
        "total":1,
        "dose":"",
        "tablets_in_kit":""
    },
    "barcode_fefu": {
        "total":2,
        "dose":2,
        "tablets_in_kit":2000
    },
    "barcode_ibup": {
        "total":20,
        "dose":2,
        "tablets_in_kit":200
    },
    "barcode_mico": {
        "total":20,
        "dose":"",
        "tablets_in_kit":""
    },
    "barcode_orsl": {
        "total":2,
        "dose":"",
        "tablets_in_kit":""
    },
    "barcode_par1": {
        "total":10,
        "dose":20,
        "tablets_in_kit":1000
    },
    "barcode_par5": {
        "total":20,
        "dose":1,
        "tablets_in_kit":2000
    },
    "barcode_povi": {
        "total":12,
        "dose":"",
        "tablets_in_kit":""
    },
    "barcode_tetr": {
        "total":50,
        "dose":"",
        "tablets_in_kit":""
    },
    "barcode_zinc": {
        "total":10,
        "dose":2,
        "tablets_in_kit":1000
    }
}


class Prescriptions(Resource):
    """
    Return medicine prescription data based on scanned barcodes

    Args: \n
        location: location id
            Returns:\n
    """
    decorators = [authenticate]

    def get(self, location, start_date = None, end_date = None):

        start_date, end_date = fix_dates(start_date, end_date)

        locs = get_locations(db.session)

        clinics = get_children(parent = location, locations = locs, require_case_report = True)

        barcode_category = 'barcode_prescription'

        barcode_variables = get_variables(barcode_category)

        date_conditions = [Data.date >= start_date, Data.date < end_date]

        conditions = [Data.categories.has_key(barcode_category), Data.clinic.in_(clinics)]
        
        # Get first and last prescription for a clinic and medicine without time constraints
        first_last_prescr_query = db.session.query(Data.clinic,
                             Data.categories[barcode_category].astext,func.count(Data.id),func.min(Data.date),func.max(Data.date)).filter(
                                 *conditions).group_by(Data.clinic, Data.categories[barcode_category].astext)

        #return str(first_last_prescr_query.all())
        <commit_msg>prescriptions API refers to DB instead of hard coded values<commit_after>"""
Data resource for completeness data
"""
from flask_restful import Resource
from flask import jsonify, request
from dateutil.parser import parse
from sqlalchemy import extract, func, Integer, or_
from datetime import datetime, timedelta
import pandas as pd

from meerkat_api import db, app
from meerkat_api.resources.epi_week import EpiWeek, epi_week_start, epi_year_start
from meerkat_abacus.model import Data, Locations, CalculationParameters
from meerkat_api.util import get_children, is_child, fix_dates
from meerkat_abacus.util import get_locations
from meerkat_api.authentication import authenticate

from meerkat_api.resources.explore import QueryVariable, get_variables

class Prescriptions(Resource):
    """
    Return medicine prescription data based on scanned barcodes

    Args: \n
        location: location id
            Returns:\n
    """
    decorators = [authenticate]

    def get(self, location, start_date = None, end_date = None):

        start_date, end_date = fix_dates(start_date, end_date)

        locs = get_locations(db.session)

        clinics = get_children(parent = location, locations = locs, require_case_report = True)

        kit_contents = db.session.query(CalculationParameters.parameters)

        barcode_category = 'barcode_prescription'

        barcode_variables = get_variables(barcode_category)

        date_conditions = [Data.date >= start_date, Data.date < end_date]

        conditions = [Data.categories.has_key(barcode_category), Data.clinic.in_(clinics)]
        
        # Get first and last prescription for a clinic and medicine without time constraints
        first_last_prescr_query = db.session.query(Data.clinic,
                             Data.categories[barcode_category].astext,func.count(Data.id),func.min(Data.date),func.max(Data.date)).filter(
                                 *conditions).group_by(Data.clinic, Data.categories[barcode_category].astext)

        #return str(first_last_prescr_query.all())
        
{'fraction_non_alphanumeric': 0.10419004863449308, 'fraction_numerical': 0.012345679012345678, 'mean_word_length': 2.857864357864358, 'pattern_counts': {'":': 56, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5986408', 'n_tokens_mistral': 1748, 'n_tokens_neox': 1635, 'n_words': 349}