<commit_before>from django.db import connections
from django.template import Context, Template

from cachedqueries import cacheable
import time_series

#===============================================================================

def _get_cursor(db_name):
        """Given a db name returns a cursor."""
        return connections[db_name].cursor()

@cacheable
def get_sql_data_for_report(
        string_query, db_name, context_vars, 
        fill_zeros=True, fill_empty_string=False):
        """
        Generic function to get data for reports, doing sql queries using a
        cursor.
        
        This func will receive the string with the query, the database name to
        create a cursor and the context vars using in the query.
        
        Explanation of the last 3 params:
        
        1. fill_zeros:  For time series the dates that have no value will be
        filled with zeros. 
        
        Sometimes we want to execute a query but the query wont retrieve
        datetimes, it wont be a time series, but different kind of data. For
        example data used for pie charts, for this cases we set fill_zeros=
        False so that we don't treat the data as time series.
        
        2. fill_empty_string: sometimes we want to fill with an empty string 
        instead of with zeros
        """
        
        context_vars = context_vars.copy()
        
        context_vars["start_date"] = context_vars['series_range'][0] 
        context_vars["end_date"] = context_vars['series_range'][1]
        
        cursor = _get_cursor(db_name)
        tpl_header =  "{% load reports_tags %} "
             
        tpl = Template(tpl_header + string_query)
        
        cursor.execute(tpl.render(Context(context_vars)), [])
        
        series = [(row[0], row[1]) for row in cursor.fetchall()]

        if not fill_zeros and fill_empty_string:
            return time_series.fill_missing_dates_with_zeros(
                series,
                context_vars['aggregation'][:-2], 
                context_vars['series_range'],
                True) 
        if not fill_zeros:
            return series

        return time_series.fill_missing_dates_with_zeros(
            series,
            context_vars['aggregation'][:-2], 
            context_vars['series_range'])  <commit_msg>Improving apprentice downloads report<commit_after>from django.db import connections
from django.template import Context, Template

from cachedqueries import cacheable
import time_series

#===============================================================================

def _get_cursor(db_name):
        """Given a db name returns a cursor."""
        return connections[db_name].cursor()

@cacheable
def get_sql_data_for_report(
        string_query, db_name, context_vars, 
        fill_zeros=True, fill_empty_string=False):
        """
        Generic function to get data for reports, doing sql queries using a
        cursor.
        
        This func will receive the string with the query, the database name to
        create a cursor and the context vars using in the query.
        
        Explanation of the last 3 params:
        
        1. fill_zeros:  For time series the dates that have no value will be
        filled with zeros. 
        
        Sometimes we want to execute a query but the query wont retrieve
        datetimes, it wont be a time series, but different kind of data. For
        example data used for pie charts, for this cases we set fill_zeros=
        False so that we don't treat the data as time series.
        
        2. fill_empty_string: sometimes we want to fill with an empty string 
        instead of with zeros
        """
        
        context_vars = context_vars.copy()
        
        context_vars["start_date"] = context_vars['series_range'][0] 
        context_vars["end_date"] = context_vars['series_range'][1]
        
        cursor = _get_cursor(db_name)
        tpl_header =  "{% load reports_tags %} "
             
        tpl = Template(tpl_header + string_query)
        
        cursor.execute(tpl.render(Context(context_vars)), [])
        
        #print tpl.render(Context(context_vars))
        
        series = [(row[0], row[1]) for row in cursor.fetchall()]

        if not fill_zeros and fill_empty_string:
            return time_series.fill_missing_dates_with_zeros(
                series,
                context_vars['aggregation'][:-2], 
                context_vars['series_range'],
                True) 
        if not fill_zeros:
            return series

        return time_series.fill_missing_dates_with_zeros(
            series,
            context_vars['aggregation'][:-2], 
            context_vars['series_range'])  
{'fraction_non_alphanumeric': 0.10029498525073746, 'fraction_numerical': 0.0037926675094816687, 'mean_word_length': 2.2558299039780523, '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': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5812465', 'n_tokens_mistral': 1262, 'n_tokens_neox': 1212, 'n_words': 403}