Ejemplo n.º 1
0
    def build_std_calcs(self, options):
        """Constrói campos calculados de valor, como min, max e normalizado """
        val_field = getattr(self, 'VAL_FIELD', None)
        if val_field is None or self.get_default_partitioning(options) is None:
            return ''

        # Pega o valor passado ou padrão, para montar a query
        if QueryBuilder.check_params(options, ['valor']):
            val_field = options['valor']

        # Pega o valor do particionamento
        res_partition = None
        if QueryBuilder.check_params(options, ['partition']):
            res_partition = options.get('partition')
        elif self.get_default_partitioning(options) != '':
            res_partition = self.get_default_partitioning(options)

        # Transforma o campo de valor em campo agregado conforme query
        if QueryBuilder.check_params(options, ['agregacao']):
            val_field = QueryBuilder.get_simple_agr_string(
                options['agregacao'][0], options['valor'][0])
            if QueryBuilder.check_params(options, ['pivot']):
                res_partition = self.exclude_from_partition(
                    options['categorias'], options['agregacao'])
        elif isinstance(val_field, list):
            val_field = val_field[0]

        str_res_partition = res_partition
        if res_partition is not None and isinstance(res_partition, list):
            str_res_partition = ",".join(res_partition)

        # Constrói a query
        arr_calcs = []
        for calc in options['calcs']:
            # Always appends min and max when calc is not one of them
            if calc not in ['min_part', 'max_part']:
                pattern = self.replace_partition('min_part', options)
                if str_res_partition is None:
                    pattern = pattern.replace('PARTITION BY {partition}', '')
                arr_calcs.append(
                    pattern.format(val_field=val_field,
                                   partition=str_res_partition,
                                   calc='min_part'))

                pattern = self.replace_partition('max_part', options)
                if str_res_partition is None:
                    pattern = pattern.replace('PARTITION BY {partition}', '')
                arr_calcs.append(
                    pattern.format(val_field=val_field,
                                   partition=str_res_partition,
                                   calc='max_part'))
            # Resumes identification of calc
            pattern = self.replace_partition(calc, options)
            if str_res_partition is None:
                pattern = pattern.replace('PARTITION BY {partition}', '')
            arr_calcs.append(
                pattern.format(val_field=val_field,
                               partition=str_res_partition,
                               calc=calc))
        return ', '.join(arr_calcs)
Ejemplo n.º 2
0
 def build_categorias(self, categorias, options):
     ''' Constrói a parte dos atributos selecionados na query '''
     if not QueryBuilder.check_params(options, ['categorias']):
         raise ValueError('Invalid Categories - required')
     categorias = QueryBuilder.transform_categorias(categorias)
     prepended_aggr = QueryBuilder.prepend_aggregations(options.get('agregacao'))
     str_calcs = ''
     if QueryBuilder.check_params(options, ['calcs']):
         calcs_options = options.copy()
         calcs_options['categorias'] = categorias
         str_calcs += self.build_std_calcs(calcs_options)
     if QueryBuilder.check_params(options, ['agregacao', 'valor']):
         tmp_cats = self.combine_val_aggr(options.get('valor'), options.get('agregacao'))
         if not isinstance(tmp_cats, list):
             categorias += tmp_cats.split(", ")
         else:
             categorias += tmp_cats
     elif (not QueryBuilder.check_params(options, ['agregacao']) and
           QueryBuilder.check_params(options, ['valor'])):
         categorias += options['valor']
     elif (QueryBuilder.check_params(options, ['agregacao']) and
           not QueryBuilder.check_params(options, ['valor'])):
         categorias += self.build_generic_agr_array(options['agregacao'])
     if (QueryBuilder.validate_field_array(categorias) and
             QueryBuilder.validate_field_array(prepended_aggr)):
         if 'calcs' not in options or options['calcs'] is None or str_calcs == '':
             return ' '.join(prepended_aggr) + ' ' + ', '.join(categorias)
         return ' '.join(prepended_aggr) + ' ' + ', '.join(categorias) + ', ' + str_calcs
     raise ValueError('Invalid attributes')
Ejemplo n.º 3
0
    def build_std_calcs(self, options):
        '''Constrói campos calculados de valor, como min, max e normalizado '''
        if self.VAL_FIELD is None or self.get_default_partitioning(options) is None:
            return ''

        # Pega o valor passado ou padrão, para montar a query
        val_field = self.VAL_FIELD
        if QueryBuilder.check_params(options, ['valor']):
            val_field = options['valor']

        # Pega o valor do particionamento
        if not QueryBuilder.check_params(options, ['partition']):
            if self.get_default_partitioning(options) != '':
                res_partition = self.get_default_partitioning(options)
            else:
                res_partition = "'1'"
        else:
            res_partition = options['partition']

        # Transforma o campo de valor em campo agregado conforme query
        if QueryBuilder.check_params(options, ['agregacao']):
            val_field = QueryBuilder.get_simple_agr_string(
                options['agregacao'][0],
                options['valor'][0]
            )
            if QueryBuilder.check_params(options, ['pivot']):
                res_partition = self.exclude_from_partition(
                    options['categorias'],
                    options['agregacao']
                )

        str_res_partition = res_partition
        if isinstance(res_partition, list):
            str_res_partition = ",".join(res_partition)

        # Constrói a query
        arr_calcs = []
        for calc in options['calcs']:
            # Always appends min and max when calc is not one of them
            if calc not in ['min_part', 'max_part']:
                arr_calcs.append(
                    self.replace_partition('min_part').format(
                        val_field=val_field,
                        partition=str_res_partition,
                        calc='min_part'
                    )
                )
                arr_calcs.append(
                    self.replace_partition('max_part').format(
                        val_field=val_field,
                        partition=str_res_partition,
                        calc='max_part'
                    )
                )
            # Resumes identification of calc
            arr_calcs.append(
                self.replace_partition(calc, options).format(
                    val_field=val_field,
                    partition=str_res_partition,
                    calc=calc
                )
            )
        return ', '.join(arr_calcs)