<commit_before>from pprint import pprint
from datetime import datetime

from django.urls import reverse
from django.db.models import Exists, OuterRef

from fo2.connections import db_cursor_so

from base.views import O2BaseGetView
from utils.functions import dec_month, dec_months

import comercial.models as models
import comercial.queries as queries


class VendasPorModelo(O2BaseGetView):

    def __init__(self, *args, **kwargs):
        super(VendasPorModelo, self).__init__(*args, **kwargs)
        self.template_name = 'comercial/vendas_por_modelo.html'
        self.title_name = 'Vendas por modelo'

    def mount_context_inicial(self):
        data = []
        zero_data_row = {'meta': ' ', 'data': ' '}
        zero_data_row.update({p['range']: 0 for p in self.periodos})
        total_data_row = zero_data_row.copy()

        metas = models.MetaEstoque.objects
        metas = metas.annotate(antiga=Exists(
            models.MetaEstoque.objects.filter(
                modelo=OuterRef('modelo'),
                data__gt=OuterRef('data')
            )
        ))
        metas = metas.filter(antiga=False)
        metas = metas.order_by('-meta_estoque').values()
        for row in metas:
            data_row = next(
                (dr for dr in data if dr['modelo'] == row['modelo']),
                False)
            if not data_row:
                data_row = {
                    'modelo': row['modelo'],
                    'modelo|TARGET': '_blank',
                    'modelo|LINK': reverse(
                        'comercial:define_meta__get',
                        args=[row['modelo']]),
                    **zero_data_row
                }
                data.append(data_row)
            data_row['meta'] = row['meta_estoque']
            data_row['data'] = row['data']

        for periodo in self.periodos:
            data_periodo = queries.get_vendas(
                self.cursor, ref=None, periodo=periodo['range'],
                colecao=None, cliente=None, por='modelo', zerados=False
            )
            for row in data_periodo:
                data_row = next(
                    (dr for dr in data if dr['modelo'] == row['modelo']),
                    False)
                if not data_row:
                    data_row = {
                        'modelo': row['modelo'],
                        'modelo|TARGET': '_blank',
                        'modelo|LINK': reverse(
                            'comercial:define_meta__get',
                            args=[row['modelo']]),
                        **zero_data_row
                    }
                    data.append(data_row)
                data_row[periodo['range']] = round(<commit_msg>lista primeiro só os que tem meta não zerada, mas as metas zeradas são indicadas abaixo<commit_after>from pprint import pprint
from datetime import datetime

from django.urls import reverse
from django.db.models import Exists, OuterRef

from fo2.connections import db_cursor_so

from base.views import O2BaseGetView
from utils.functions import dec_month, dec_months

import comercial.models as models
import comercial.queries as queries


class VendasPorModelo(O2BaseGetView):

    def __init__(self, *args, **kwargs):
        super(VendasPorModelo, self).__init__(*args, **kwargs)
        self.template_name = 'comercial/vendas_por_modelo.html'
        self.title_name = 'Vendas por modelo'

    def mount_context_inicial(self):
        data = []
        zero_data_row = {'meta': ' ', 'data': ' '}
        zero_data_row.update({p['range']: 0 for p in self.periodos})
        total_data_row = zero_data_row.copy()

        metas = models.MetaEstoque.objects
        metas = metas.annotate(antiga=Exists(
            models.MetaEstoque.objects.filter(
                modelo=OuterRef('modelo'),
                data__gt=OuterRef('data')
            )
        ))
        metas = metas.filter(antiga=False)
        metas = metas.order_by('-meta_estoque').values()
        meta_zerada_data = {}
        for row in metas:
            if row['meta_estoque']:
                data_row = next(
                    (dr for dr in data if dr['modelo'] == row['modelo']),
                    False)
                if not data_row:
                    data_row = {
                        'modelo': row['modelo'],
                        'modelo|TARGET': '_blank',
                        'modelo|LINK': reverse(
                            'comercial:define_meta__get',
                            args=[row['modelo']]),
                        **zero_data_row
                    }
                    data.append(data_row)
                data_row['meta'] = row['meta_estoque']
                data_row['data'] = row['data']
            else:
                meta_zerada_data[row['modelo']] = row['data']

        for periodo in self.periodos:
            data_periodo = queries.get_vendas(
                self.cursor, ref=None, periodo=periodo['range'],
                colecao=None, cliente=None, por='modelo', zerados=False
            )
            for row in data_periodo:
                data_row = next(
                    (dr for dr in data if dr['modelo'] == row['modelo']),
                    False)
                if not data_row:
                    data_row = {
                        'modelo': row['modelo'],
                        'modelo|TARGET': '_blank',
                        'modelo|LINK': reverse(
                            'comercial:define_meta__get',
                            args=[row['modelo']]),
                        **zero_data_row
                    }
                    if row['modelo'] in meta_zerada_data:
                        data_row['meta'] = 0
                        data_row['data'] = meta_zerada_data[row['modelo']]
                    data.append(data_row)
                data_row[periodo['range']] = round(
{'fraction_non_alphanumeric': 0.10576595018764927, 'fraction_numerical': 0.0015353121801432957, 'mean_word_length': 1.590808661069377, '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': '4726729', 'n_tokens_mistral': 1709, 'n_tokens_neox': 1629, 'n_words': 338}