diff --git a/app/config/config.yml b/app/config/config.yml index be50683..57995a0 100644 --- a/app/config/config.yml +++ b/app/config/config.yml @@ -123,6 +123,9 @@ doctrine: orm: auto_generate_proxy_classes: "%kernel.debug%" auto_mapping: true + dql: + string_functions: + string_agg: Cacic\CommonBundle\Query\PostgreSQL\StringAggFunction # Swiftmailer Configuration swiftmailer: diff --git a/src/Cacic/CommonBundle/Entity/LogAcessoRepository.php b/src/Cacic/CommonBundle/Entity/LogAcessoRepository.php index c49e5e1..01f5e2d 100755 --- a/src/Cacic/CommonBundle/Entity/LogAcessoRepository.php +++ b/src/Cacic/CommonBundle/Entity/LogAcessoRepository.php @@ -3,6 +3,7 @@ namespace Cacic\CommonBundle\Entity; use Doctrine\ORM\EntityRepository; +use Doctrine\ORM\Query\ResultSetMapping; /** * LogAcessoRepository @@ -42,7 +43,7 @@ class LogAcessoRepository extends EntityRepository // Monta a Consulta básica... $query = $this->createQueryBuilder('log') - ->select('rede.idRede', 'rede.nmRede', 'rede.teIpRede', 'loc.nmLocal', 'loc.sgLocal', 'COUNT(DISTINCT log.idComputador) as numComp') + ->select('rede.idRede', 'rede.nmRede', 'rede.teIpRede', 'loc.nmLocal', 'loc.sgLocal', 'COUNT(DISTINCT comp.teNodeAddress) as numComp') ->innerJoin('log.idComputador', 'comp') ->innerJoin('comp.idRede', 'rede') ->innerJoin('rede.idLocal', 'loc') @@ -63,29 +64,84 @@ class LogAcessoRepository extends EntityRepository return $query->getQuery()->execute(); } - public function gerarRelatorioRede( $filtros, $idRede,$dataInicio, $dataFim ) + + + public function gerarRelatorioRede( $filtros, $idRede, $dataInicio, $dataFim ) { - $query = $this->createQueryBuilder('log') - ->select('comp.idComputador', 'comp.nmComputador', 'comp.teNodeAddress','comp.teIpComputador', 'max(log.data) AS data', 'so.idSo', 'so.inMswindows', 'so.sgSo', 'rede.idRede', 'rede.nmRede', 'rede.teIpRede', 'local.nmLocal', 'local.idLocal') - ->innerJoin('log.idComputador','comp') - ->innerJoin('comp.idSo', 'so') - ->innerJoin('comp.idRede','rede') - ->innerJoin('rede.idLocal', 'local') - ->groupBy('comp.idComputador', 'comp.nmComputador', 'comp.teNodeAddress','comp.teIpComputador', 'so.idSo', 'so.inMswindows', 'so.sgSo', 'rede.idRede', 'rede.nmRede', 'rede.teIpRede', 'local.nmLocal', 'local.idLocal'); + $rsm = new ResultSetMapping(); + $rsm->addScalarResult('te_node_address', 'teNodeAddress'); + $rsm->addScalarResult('id_computador', 'idComputador'); + $rsm->addScalarResult('te_ip_computador', 'teIpComputador'); + $rsm->addScalarResult('nm_computador', 'nmComputador'); + $rsm->addScalarResult('id_so', 'idSo'); + $rsm->addScalarResult('sg_so', 'sgSo'); + $rsm->addScalarResult('id_rede', 'idRede'); + $rsm->addScalarResult('nm_rede', 'nmRede'); + $rsm->addScalarResult('te_ip_rede', 'teIpRede'); + $rsm->addScalarResult('max_data', 'data'); + $rsm->addScalarResult('nm_local', 'nmLocal'); + $rsm->addScalarResult('id_local', 'idLocal'); + + $sql = " +SELECT c0_.te_node_address AS te_node_address, + string_agg(DISTINCT CAST(c0_.id_computador AS text), ', ') as id_computador, + string_agg(DISTINCT c0_.te_ip_computador, ', ') as te_ip_computador, + string_agg(DISTINCT c0_.nm_computador, ', ') AS nm_computador, + string_agg(DISTINCT CAST(s2_.id_so AS text), ', ') AS id_so, + string_agg(DISTINCT s2_.sg_so, ', ') AS sg_so, + string_agg(DISTINCT CAST(r3_.id_rede AS text), ', ') AS id_rede, + string_agg(DISTINCT r3_.nm_rede, ', ') AS nm_rede, + string_agg(DISTINCT r3_.te_ip_rede, ', ') AS te_ip_rede, + max(l1_.data) AS max_data, + l4_.nm_local AS nm_local, + l4_.id_local AS id_local +FROM log_acesso l1_ +INNER JOIN computador c0_ ON l1_.id_computador = c0_.id_computador +INNER JOIN so s2_ ON c0_.id_so = s2_.id_so +INNER JOIN rede r3_ ON c0_.id_rede = r3_.id_rede +INNER JOIN local l4_ ON r3_.id_local = l4_.id_local +WHERE 1 = 1 +"; /** * Verifica os filtros que foram parametrizados */ - if ( $dataInicio ) - $query->andWhere( 'log.data >= :dtInicio' )->setParameter('dtInicio', ( $dataInicio.' 00:00:00' )); + if ( $dataInicio ) { + $sql .= " AND l1_.data >= ? "; + } + + if ( $dataFim ) { + $sql .= " AND l1_.data <= ?"; + } + + if ( $idRede ) { + $sql .= " AND c0_.id_rede IN (?)"; + } + + $sql .= " +GROUP BY c0_.te_node_address, + l4_.nm_local, + l4_.id_local + "; + + $query = $this->getEntityManager()->createNativeQuery($sql, $rsm); + + /** + * Verifica os filtros que foram parametrizados + */ + if ( $dataInicio ) { + $query->setParameter(1, ( $dataInicio.' 00:00:00' )); + } + if ( $dataFim ) - $query->andWhere( 'log.data <= :dtFim' )->setParameter('dtFim', ( $dataFim.' 23:59:59' )); + $query->setParameter(2, ( $dataFim.' 23:59:59' )); - if ( ($idRede) ) - $query->andWhere( 'comp.idRede IN (:rede)' )->setParameter('rede', $idRede); + if ( $idRede ) + $query->setParameter(3, $idRede); - return $query->getQuery()->execute(); + + return $query->execute(); } /** @@ -97,7 +153,7 @@ class LogAcessoRepository extends EntityRepository public function countPorComputador() { $query = $this->createQueryBuilder('log') - ->select('COUNT(DISTINCT comp.idComputador)') + ->select('COUNT(DISTINCT comp.teNodeAddress)') ->innerJoin('CacicCommonBundle:Computador','comp', 'WITH', 'log.idComputador = comp.idComputador') ->andWhere( 'log.data >= (current_date() - 30)' ); @@ -107,12 +163,13 @@ class LogAcessoRepository extends EntityRepository return $query->execute(); } + public function faturamentoCsv( $dataInicio, $dataFim, $locais ) { // Monta a Consulta básica... $query = $this->createQueryBuilder('log') - ->select( 'loc.nmLocal', 'rede.nmRede', 'rede.teIpRede', 'COUNT(DISTINCT log.idComputador) as numComp') + ->select( 'loc.nmLocal', 'rede.nmRede', 'rede.teIpRede', 'COUNT(DISTINCT comp.teNodeAddress) as numComp') ->innerJoin('log.idComputador', 'comp') ->innerJoin('comp.idRede', 'rede') ->innerJoin('rede.idLocal', 'loc') @@ -133,29 +190,74 @@ class LogAcessoRepository extends EntityRepository return $query->getQuery()->execute(); } + public function listarCsv( $filtros, $idRede, $dataInicio, $dataFim ) { - $query = $this->createQueryBuilder('log') - ->select('comp.nmComputador', 'comp.teNodeAddress','comp.teIpComputador', 'so.sgSo', 'local.nmLocal', 'rede.nmRede','max(log.data) AS data') - ->innerJoin('log.idComputador','comp') - ->innerJoin('comp.idSo', 'so') - ->innerJoin('comp.idRede','rede') - ->innerJoin('rede.idLocal', 'local') - ->groupBy( 'comp.idComputador', 'comp.nmComputador', 'comp.teNodeAddress','comp.teIpComputador', 'so.idSo', 'so.inMswindows', 'so.sgSo', 'rede.idRede', 'rede.nmRede', 'rede.teIpRede', 'local.nmLocal', 'local.idLocal'); + + $rsm = new ResultSetMapping(); + $rsm->addScalarResult('nm_computador', 'nmComputador'); + $rsm->addScalarResult('te_node_address', 'teNodeAddress'); + $rsm->addScalarResult('te_ip_computador', 'teIpComputador'); + $rsm->addScalarResult('sg_so', 'sgSo'); + $rsm->addScalarResult('nm_local', 'nmLocal'); + $rsm->addScalarResult('nm_rede', 'nmRede'); + $rsm->addScalarResult('max_data', 'data'); + + $sql = " +SELECT c0_.te_node_address AS te_node_address, + string_agg(DISTINCT c0_.te_ip_computador, ', ') as te_ip_computador, + string_agg(DISTINCT c0_.nm_computador, ', ') AS nm_computador, + string_agg(DISTINCT s2_.sg_so, ', ') AS sg_so, + string_agg(DISTINCT r3_.nm_rede, ', ') AS nm_rede, + max(l1_.data) AS max_data, + l4_.nm_local AS nm_local +FROM log_acesso l1_ +INNER JOIN computador c0_ ON l1_.id_computador = c0_.id_computador +INNER JOIN so s2_ ON c0_.id_so = s2_.id_so +INNER JOIN rede r3_ ON c0_.id_rede = r3_.id_rede +INNER JOIN local l4_ ON r3_.id_local = l4_.id_local +WHERE 1 = 1 +"; /** * Verifica os filtros que foram parametrizados */ - if ( $dataInicio ) - $query->andWhere( 'log.data >= :dtInicio' )->setParameter('dtInicio', ( $dataInicio.' 00:00:00' )); + if ( $dataInicio ) { + $sql .= " AND l1_.data >= ? "; + } + + if ( $dataFim ) { + $sql .= " AND l1_.data <= ?"; + } + + if ( $idRede ) { + $sql .= " AND c0_.id_rede IN (?)"; + } + + $sql .= " +GROUP BY c0_.te_node_address, + l4_.nm_local, + l4_.id_local + "; + + $query = $this->getEntityManager()->createNativeQuery($sql, $rsm); + + /** + * Verifica os filtros que foram parametrizados + */ + if ( $dataInicio ) { + $query->setParameter(1, ( $dataInicio.' 00:00:00' )); + } + if ( $dataFim ) - $query->andWhere( 'log.data <= :dtFim' )->setParameter('dtFim', ( $dataFim.' 23:59:59' )); + $query->setParameter(2, ( $dataFim.' 23:59:59' )); - if ( count($idRede) ) - $query->andWhere( 'comp.idRede IN (:rede)' )->setParameter('rede', $idRede); + if ( $idRede ) + $query->setParameter(3, $idRede); - return $query->getQuery()->execute(); + + return $query->execute(); } } diff --git a/src/Cacic/CommonBundle/Query/PostgreSQL/StringAggFunction.php b/src/Cacic/CommonBundle/Query/PostgreSQL/StringAggFunction.php new file mode 100644 index 0000000..a7dab97 --- /dev/null +++ b/src/Cacic/CommonBundle/Query/PostgreSQL/StringAggFunction.php @@ -0,0 +1,40 @@ +expression->dispatch($sqlWalker) . ',' . $this->delimiter->dispatch($sqlWalker) .')'; + } + + public function parse(Parser $parser) + { + $parser->match(Lexer::T_IDENTIFIER); + $parser->match(Lexer::T_OPEN_PARENTHESIS); + $parser->match(Lexer::T_DISTINCT); + $this->expression = $parser->ArithmeticPrimary(); + $parser->match(Lexer::T_COMMA); + $this->delimiter = $parser->ArithmeticPrimary(); + $parser->match(Lexer::T_CLOSE_PARENTHESIS); + } +} \ No newline at end of file diff --git a/src/Cacic/RelatorioBundle/Controller/FaturamentoController.php b/src/Cacic/RelatorioBundle/Controller/FaturamentoController.php index ff9b29e..6d966f8 100755 --- a/src/Cacic/RelatorioBundle/Controller/FaturamentoController.php +++ b/src/Cacic/RelatorioBundle/Controller/FaturamentoController.php @@ -12,6 +12,7 @@ use Ddeboer\DataImport\Writer\CsvWriter; use Ddeboer\DataImport\ValueConverter\CallbackValueConverter; use Symfony\Component\HttpFoundation\BinaryFileResponse; + use Cacic\CommonBundle\Form\Type\ComputadorConsultaType; class FaturamentoController extends Controller { @@ -323,4 +324,30 @@ $response->headers->set('Content-Disposition', 'attachment; filename="Não Coletadas.csv"'); $response->headers->set('Content-Transfer-Encoding', 'binary'); } + + /** + * Search computer with params + * + * @param Request $request + * @return \Symfony\Component\HttpFoundation\Response + */ + public function computadorAction( Request $request ) + { + $locale = $request->getLocale(); + $data = $request->query->all(); + + $form = $this->createForm( new ComputadorConsultaType() ); + + $computadores = $this->getDoctrine()->getRepository( 'CacicCommonBundle:Computador') + ->selectIp($data['teIpComputador'],$data['nmComputador'] ,$data['teNodeAddress'] ); + + + return $this->render( 'CacicCommonBundle:Computador:buscar.html.twig', + array( + 'local'=>$locale , + 'form' => $form->createView(), + 'computadores' => ( $computadores ) + ) + ); + } } diff --git a/src/Cacic/RelatorioBundle/Resources/config/routing.yml b/src/Cacic/RelatorioBundle/Resources/config/routing.yml index a70a538..c6ccbdc 100644 --- a/src/Cacic/RelatorioBundle/Resources/config/routing.yml +++ b/src/Cacic/RelatorioBundle/Resources/config/routing.yml @@ -175,4 +175,8 @@ cacic_relatorio_csv_hardware_wmi: cacic_relatorio_csv_hardware_wmi_detalhe: pattern: /csv/hardware/{classe}/{propriedade} - defaults: { _controller: CacicRelatorioBundle:Hardware:csvWMIRelatorioDetalhe } \ No newline at end of file + defaults: { _controller: CacicRelatorioBundle:Hardware:csvWMIRelatorioDetalhe } + +cacic_faturamento_computador: + pattern: /faturamento/computador + defaults: { _controller: CacicRelatorioBundle:Faturamento:computador } \ No newline at end of file diff --git a/src/Cacic/RelatorioBundle/Resources/views/Faturamento/listar.html.twig b/src/Cacic/RelatorioBundle/Resources/views/Faturamento/listar.html.twig index b20950a..7716335 100755 --- a/src/Cacic/RelatorioBundle/Resources/views/Faturamento/listar.html.twig +++ b/src/Cacic/RelatorioBundle/Resources/views/Faturamento/listar.html.twig @@ -10,7 +10,6 @@
{{ "Computador"|trans }} | {{ "MAC Address"|trans }} | {{ "Endereço IP"|trans }} | @@ -23,14 +22,26 @@||||||||
---|---|---|---|---|---|---|---|---|---|---|
{{ loop.index }} | - {{ reg.nmComputador }} + {% for comp in nomes %} + {{ comp }} + {% else %} + {{ reg.nmComputador }} + {% endfor %} | -{{ reg.teNodeAddress }} | -{{ reg.teIpComputador }} | -{{ reg.sgSo }} | +{{ reg.teNodeAddress }} | ++ {% for comp in ips %} + {{ comp }} + {% else %} + {{ reg.teIpComputador }} + {% endfor %} + | +{{ reg.sgSo }} | {{ reg.nmLocal }} | {{ reg.nmRede }} / {{ reg.teIpRede }} | {{ reg.data|date(' d, M, Y H:m:s') }} | -- libgit2 0.21.2