commit f556b30a44d1609997f91bc085df19511d82eca9
parent 91f6126feb79813c019365713ac55d13ea446814
Author: Felicitus <felicitus@felicitus.org>
Date: Sat, 7 Nov 2015 16:12:43 +0100
Added statistic service
Diffstat:
7 files changed, 152 insertions(+), 0 deletions(-)
diff --git a/app/AppKernel.php b/app/AppKernel.php
@@ -63,6 +63,7 @@ class AppKernel extends Kernel
$bundles[] = new PartKeepr\StorageLocationBundle\PartKeeprStorageLocationBundle();
$bundles[] = new PartKeepr\TipOfTheDayBundle\PartKeeprTipOfTheDayBundle();
$bundles[] = new PartKeepr\ExportBundle\PartKeeprExportBundle();
+ $bundles[] = new PartKeepr\StatisticBundle\PartKeeprStatisticBundle();
return $bundles;
}
diff --git a/app/config/routing.yml b/app/config/routing.yml
@@ -18,6 +18,11 @@ PartKeeprProjectBundle:
type: annotation
prefix: /
+PartKeeprStatisticBundle:
+ resource: "@PartKeeprStatisticBundle/Controller/"
+ type: annotation
+ prefix: /
+
PartKeeprPartBundle:
resource: "@PartKeeprPartBundle/Controller/"
type: annotation
diff --git a/src/PartKeepr/StatisticBundle/Controller/StatisticController.php b/src/PartKeepr/StatisticBundle/Controller/StatisticController.php
@@ -0,0 +1,35 @@
+<?php
+namespace PartKeepr\StatisticBundle\Controller;
+
+use FOS\RestBundle\Controller\Annotations\View;
+use FOS\RestBundle\Controller\FOSRestController;
+use Sensio\Bundle\FrameworkExtraBundle\Configuration as Routing;
+
+class StatisticController extends FOSRestController
+{
+ /**
+ * Exports the given data to a given format
+ *
+ * @Routing\Route("/api/statistics/current", defaults={"method" = "get","_format" = "json"})
+ * @View()
+ *
+ * @return array
+ */
+ public function getCurrentStatisticAction()
+ {
+ $statisticService = $this->get("partkeepr.statistic.service");
+
+ $aData = array();
+ $aData["partCount"] = $statisticService->getPartCount();
+ $aData["partCategoryCount"] = $statisticService->getPartCategoryCount();
+ $aData["totalPrice"] = $statisticService->getTotalPrice();
+ $aData["averagePrice"] = $statisticService->getAveragePrice();
+ $aData["partsWithPrice"] = $statisticService->getPartCount(true);
+ $aData["partsWithoutPrice"] = $aData["partCount"] - $aData["partsWithPrice"];
+ $aData["units"] = $statisticService->getUnitCounts();
+
+ return $aData;
+ }
+
+
+}
diff --git a/src/PartKeepr/StatisticBundle/DependencyInjection/PartKeeprStatisticExtension.php b/src/PartKeepr/StatisticBundle/DependencyInjection/PartKeeprStatisticExtension.php
@@ -0,0 +1,20 @@
+<?php
+namespace PartKeepr\StatisticBundle\DependencyInjection;
+
+
+use Symfony\Component\Config\FileLocator;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Extension\Extension;
+use Symfony\Component\DependencyInjection\Loader;
+
+class PartKeeprStatisticExtension extends Extension
+{
+ /**
+ * {@inheritdoc}
+ */
+ public function load(array $configs, ContainerBuilder $container)
+ {
+ $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
+ $loader->load('services.xml');
+ }
+}
diff --git a/src/PartKeepr/StatisticBundle/PartKeeprStatisticBundle.php b/src/PartKeepr/StatisticBundle/PartKeeprStatisticBundle.php
@@ -0,0 +1,8 @@
+<?php
+namespace PartKeepr\StatisticBundle;
+
+use Symfony\Component\HttpKernel\Bundle\Bundle;
+
+class PartKeeprStatisticBundle extends Bundle
+{
+}
diff --git a/src/PartKeepr/StatisticBundle/Resources/config/services.xml b/src/PartKeepr/StatisticBundle/Resources/config/services.xml
@@ -0,0 +1,16 @@
+<?xml version="1.0" ?>
+
+<container xmlns="http://symfony.com/schema/dic/services"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
+
+ <parameters>
+ <parameter key="partkeepr.statistic.service.class">PartKeepr\StatisticBundle\Services\StatisticService</parameter>
+ </parameters>
+
+ <services>
+ <service id="partkeepr.statistic.service" class="%partkeepr.statistic.service.class%">
+ <argument type="service" id="doctrine.orm.default_entity_manager" />
+ </service>
+ </services>
+</container>
diff --git a/src/PartKeepr/StatisticBundle/Services/StatisticService.php b/src/PartKeepr/StatisticBundle/Services/StatisticService.php
@@ -0,0 +1,67 @@
+<?php
+namespace PartKeepr\StatisticBundle\Services;
+
+use Doctrine\ORM\EntityManager;
+
+class StatisticService
+{
+ /**
+ * @var EntityManager
+ */
+ private $entityManager;
+
+ public function __construct(EntityManager $entityManager)
+ {
+ $this->entityManager = $entityManager;
+ }
+
+ public function getPartCount($withPrice = false)
+ {
+ $dql = "SELECT COUNT(p.id) FROM PartKeepr\PartBundle\Entity\Part p";
+
+ if ($withPrice === true) {
+ $dql .= " WHERE p.averagePrice IS NOT NULL";
+ }
+
+ return $this->entityManager->createQuery($dql)->getSingleScalarResult();
+ }
+
+ public function getPartCategoryCount()
+ {
+ $dql = "SELECT COUNT(c.id) FROM PartKeepr\PartBundle\Entity\PartCategory c";
+
+ return $this->entityManager->createQuery($dql)->getSingleScalarResult();
+ }
+
+ /**
+ * Returns the total price for all parts. Only parts with a price are calculated.
+ *
+ * @return float The total price
+ */
+ public function getTotalPrice()
+ {
+ $dql = "SELECT SUM(p.averagePrice * p.stockLevel) FROM PartKeepr\PartBundle\Entity\Part p";
+
+ return $this->entityManager->createQuery($dql)->getSingleScalarResult();
+ }
+
+ /**
+ * Returns the average price for all parts. Only parts with a price are calculated.
+ *
+ * @return float The average price
+ */
+ public function getAveragePrice()
+ {
+ $dql = "SELECT AVG(p.averagePrice) FROM PartKeepr\PartBundle\Entity\Part p";
+
+ return $this->entityManager->createQuery($dql)->getSingleScalarResult();
+ }
+
+ public function getUnitCounts()
+ {
+ $dql = 'SELECT SUM(p.stockLevel) AS stockLevel, pu.name AS name FROM ';
+ $dql .= 'PartKeepr\PartBundle\Entity\PartMeasurementUnit pu LEFT JOIN pu.parts p GROUP BY pu.id';
+
+ return $this->entityManager->createQuery($dql)->getArrayResult();
+ }
+}