public function getDataProviderByConfig(DIContainer $container, ChartConfig $config)
 {
     $method = 'get' . $config->getChartType() . 'Provider';
     if (!method_exists($this, $method)) {
         throw new ChartDataProviderException("DataProvider for " . $config->getChartType() . ' does not exist.');
     }
     return $this->{$method}($container, $config);
 }
 /**
  * @TODO, This could be nicer, i should figure out how....
  *
  * @param ChartConfig $config
  * @return ChartTypeAdapterInterface
  * @throws ChartTypeAdapterException
  */
 public function getChartTypeAdapterByConfig(ChartConfig $config)
 {
     $method = 'get' . $config->getChartType() . 'Adapter';
     if (!method_exists($this, $method)) {
         throw new ChartTypeAdapterException("ChartType Adapter for " . $config->getChartType() . ' does not exist.');
     }
     return $this->{$method}($config->getChartProvider(), $config);
 }
 /**
  * Return the chart data you want to return based on the ChartConfig
  *
  * @param User $user
  * @param ActivityUsageRepository|StorageUsageRepository $repository
  * @param ChartConfig $chartConfig
  * @return mixed
  */
 public function getChartUsage(User $user, $repository, ChartConfig $chartConfig)
 {
     if ($user->isAdminUser($user->getSignedInUsername())) {
         $data = $repository->findAllPerMonth();
     } else {
         $data = $repository->findAllPerMonth($chartConfig->getUsername());
     }
     return $data;
 }
 /**
  * @param $language
  * @return string
  */
 public function getLabel($language)
 {
     if (strpos($this->chart->getChartType(), 'Storage') !== false) {
         $meta = json_decode($this->chart->getMetaData());
         if (!empty($meta) && $meta->size) {
             return $language->t('sizes_' . $meta->size);
         }
         return $language->t('sizes_gb');
     }
     return $language->t('activities');
 }
 public function testGetChartUsageForUpdate()
 {
     $username = '******';
     $usage = 2314234;
     $this->config->expects($this->once())->method('getUsername')->willReturn($username);
     $this->storage->expects($this->once())->method('getStorageUsage')->willReturn($usage);
     $result = $this->abstractProvider->getChartUsageForUpdate();
     $this->assertInstanceOf('OCA\\ocUsageCharts\\Entity\\Storage\\StorageUsage', $result);
     $this->assertEquals($username, $result->getUsername());
     $this->assertEquals($usage, $result->getUsage());
 }
 /**
  * Return a CURRENT storage usage for a user
  *
  * @return StorageUsage
  */
 public function getChartUsageForUpdate()
 {
     $userName = $this->chartConfig->getUsername();
     $created = new \DateTime();
     $created->setTime(0, 0, 0);
     $results = $this->repository->findAfterCreated($userName, $created);
     // The cron has already ran today, therefor ignoring a current update, only update once a day.
     if (count($results) == 0) {
         return new StorageUsage(new \Datetime(), $this->getStorageUsageFromCacheByUserName($userName), $userName);
     }
 }
 /**
  * @param ChartConfig $chartConfig
  * @param User $user
  */
 public function __construct(ChartConfig $chartConfig, User $user)
 {
     $metaData = json_decode($chartConfig->getMetaData());
     if (!empty($metaData)) {
         $size = $metaData->size;
     }
     if (empty($size) || !in_array($size, $this->allowedSizes)) {
         $size = 'gb';
     }
     $this->size = $size;
     parent::__construct($chartConfig, $user);
 }
 /**
  * Return the chart data you want to return based on the ChartConfig
  *
  * @return array
  */
 public function getChartUsage()
 {
     $return = array();
     if ($this->user->isAdminUser($this->user->getSignedInUsername())) {
         $users = $this->users->getSystemUsers();
         foreach ($users as $username) {
             $return[$username] = $this->getCollectionByUsername($username);
         }
     } else {
         $username = $this->chartConfig->getUsername();
         $return[$username] = $this->getCollectionByUsername($username);
     }
     return $return;
 }
 /**
  * Return a CURRENT storage usage for a user
  *
  * @return StorageUsage
  */
 public function getChartUsageForUpdate()
 {
     $userName = $this->chartConfig->getUsername();
     $usage = $this->storage->getStorageUsage($userName);
     $maximumUsage = $this->storage->getMaximumStorageUsage($userName);
     return new StorageUsage(new \Datetime(), $usage, $userName, $maximumUsage);
 }
 public function testFromRow()
 {
     $row = array('id' => '1', 'created' => $this->date->format('Y-m-d H:i:s'), 'username' => 'test1', 'charttype' => 'StorageUsagePerMonth', 'chartprovider' => 'c3js', 'metadata' => 'metadata');
     $config = ChartConfig::fromRow($row);
     $this->assertInstanceOf('OCA\\ocUsageCharts\\Entity\\ChartConfig', $config);
     $this->assertEquals('1', $config->getId());
     $this->assertEquals($this->date, $config->getDate());
     $this->assertEquals('test1', $config->getUsername());
     $this->assertEquals('c3js', $config->getChartProvider());
     $this->assertEquals('metadata', $config->getMetaData());
     $this->assertEquals('StorageUsagePerMonth', $config->getChartType());
 }
 /**
  * Return the proper adapter based on the chartconfig given
  *
  * @param ChartConfig $config
  * @return ChartTypeAdapterInterface
  * @throws ChartTypeAdapterException
  */
 public function getChartTypeAdapterByConfig(ChartConfig $config)
 {
     switch ($config->getChartType()) {
         case 'StorageUsageCurrent':
             $adapter = $this->getStorageUsageCurrentAdapter($config->getChartProvider(), $config);
             break;
         case 'StorageUsageLastMonth':
             $adapter = $this->getStorageUsageLastMonthAdapter($config->getChartProvider(), $config);
             break;
         case 'StorageUsagePerMonth':
             $adapter = $this->getStorageUsagePerMonthAdapter($config->getChartProvider(), $config);
             break;
         case 'ActivityUsageLastMonth':
             $adapter = $this->getActivityUsageLastMonthAdapter($config->getChartProvider(), $config);
             break;
         case 'ActivityUsagePerMonth':
             $adapter = $this->getActivityUsagePerMonthAdapter($config->getChartProvider(), $config);
             break;
         default:
             throw new ChartTypeAdapterException("ChartType Adapter for " . $config->getChartType() . ' does not exist.');
     }
     return $adapter;
 }
 /**
  * Save a chartconfig entity to the database
  *
  * @param ChartConfig $config
  */
 public function save(ChartConfig $config)
 {
     $query = $this->db->prepareQuery('INSERT INTO oc_uc_chartconfig(created, username, charttype, chartprovider) VALUES (?,?,?,?)');
     $query->execute(Array($config->getDate()->format('Y-m-d H:i:s'), $config->getUsername(), $config->getChartType(), $config->getChartProvider()));
 }
 /**
  * Save a chartconfig entity to the database
  *
  * @param ChartConfig $config
  * @return boolean
  */
 public function save(ChartConfig $config)
 {
     $id = $config->getId();
     if (!empty($id)) {
         $query = $this->db->prepareQuery('UPDATE *PREFIX*uc_chartconfig SET created = ?, username = ?, charttype = ?, chartprovider = ?, metadata = ? WHERE id = ?');
         $query->execute(array($config->getDate()->format('Y-m-d H:i:s'), $config->getUsername(), $config->getChartType(), $config->getChartProvider(), $config->getMetaData(), $config->getId()));
     } else {
         $query = $this->db->prepareQuery('INSERT INTO *PREFIX*uc_chartconfig(created, username, charttype, chartprovider, metadata) VALUES (?,?,?,?,?)');
         $query->execute(array($config->getDate()->format('Y-m-d H:i:s'), $config->getUsername(), $config->getChartType(), $config->getChartProvider(), $config->getMetaData()));
     }
     return true;
 }
 /**
  * @param ChartConfig $config
  * @return StorageUsageCurrentProvider|StorageUsageLastMonthProvider|StorageUsagePerMonthProvider
  * @throws Exception\ChartDataProviderException
  */
 public function getDataProviderByConfig(ChartConfig $config)
 {
     switch ($config->getChartType()) {
         case 'StorageUsageCurrent':
             $provider = new StorageUsageCurrentProvider($config, $this->repository, $this->user, $this->storage);
             break;
         case 'StorageUsageLastMonth':
             $provider = new StorageUsageLastMonthProvider($config, $this->repository, $this->user, $this->storage);
             break;
         case 'StorageUsagePerMonth':
             $provider = new StorageUsagePerMonthProvider($config, $this->repository, $this->user, $this->storage, $this->chartUsageHelper);
             break;
         case 'ActivityUsageLastMonth':
             $provider = new ActivityUsageLastMonthProvider($config, $this->activityUsageRepository, $this->user, $this->users);
             break;
         case 'ActivityUsagePerMonth':
             $provider = new ActivityUsagePerMonthProvider($config, $this->activityUsageRepository, $this->user, $this->chartUsageHelper);
             break;
         default:
             throw new ChartDataProviderException("DataProvider for " . $config->getChartType() . ' does not exist.');
     }
     return $provider;
 }
    /**
     * @param ChartConfig $config
     * @return mixed
     * @throws \OCA\ocUsageCharts\Exception\ChartServiceException
     */
    public function getChartByConfig(ChartConfig $config)
    {
        $adapter = '\OCA\ocUsageCharts\ChartType\\' .  $config->getChartProvider() . '\Adapters\\' . $config->getChartType() . 'Adapter';

        if ( !class_exists($adapter) )
        {
            throw new ChartServiceException("Adapter for " . $config->getChartType() . ' does not exist.');
        }
        $chartAdapter = new $adapter($config);

        if ( !in_array($config->getChartProvider(), $this->isLoaded) )
        {
            $chartAdapter->loadFrontend();
            $this->isLoaded[] = $config->getChartProvider();
        }

        return $chartAdapter;
    }
    /**
     * @TODO refactor to proper code
     *
     * @param ChartConfig $config
     * @return ChartTypeAdapterInterface
     *
     * @throws \OCA\ocUsageCharts\Exception\StorageUsageRepositoryException
     */
    public function getUsage(ChartConfig $config)
    {
        switch($config->getChartType())
        {
            default:
            case 'StorageUsageLastMonth':
                $created = new \DateTime("-1 month");
                if ( $this->isAdminUser() )
                {
                    $data = $this->findAllAfterCreated($created);
                }
                else
                {
                    $data = $this->findAfterCreated($config->getUsername(), $created);
                    $data = array($config->getUsername() => $data);
                }
                break;

            case 'StorageUsagePerMonth':
                //@TODO don't need to get everything for one user...
                // Performance and such
                $data = $this->findAllPerMonth();
                if ( !$this->isAdminUser() )
                {
                    $data = array($config->getUsername() => $data[$config->getUsername()]);
                }
                break;
            case 'StorageUsageCurrent':
                $new = array();

                $storageInfo = \OC_Helper::getStorageInfo('/');
                $free = ceil($storageInfo['free'] / 1024 / 1024);
                if ( $this->isAdminUser() )
                {
                    $data = $this->findAll(1);
                    foreach($data as $username => $items)
                    {
                        foreach($items as $item)
                        {
                            $new[$username] = ceil($item->getUsage() / 1024 / 1024);
                        }
                    }
                    $new['free'] = $free;
                    $data = $new;
                }
                else
                {
                    $free = ceil($storageInfo['free'] / 1024 / 1024);
                    $used = ceil($storageInfo['used'] / 1024 / 1024);
                    $data = array(
                        'used' => $used,
                        'free' => $free
                    );
                }
                break;
        }

        // If an adapter has been defined, format the data, else just return data parsed by the system
        $adapter = '\OCA\ocUsageCharts\ChartType\\' .  $config->getChartProvider() . '\Adapters\\' . $config->getChartType() . 'Adapter';
        if ( class_exists($adapter) )
        {
            $chartAdapter = new $adapter($config);
            return $chartAdapter->formatData($data);
        }
        return $data;
    }