Skip to content

Commit

Permalink
Merge branch 'xdmod8.5' into add_report_thumbnail_portlet3
Browse files Browse the repository at this point in the history
  • Loading branch information
mkzia authored Jul 12, 2019
2 parents a309f28 + 971dc64 commit 0df896d
Show file tree
Hide file tree
Showing 28 changed files with 2,463 additions and 9 deletions.
4 changes: 0 additions & 4 deletions classes/DataWarehouse/Query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -1157,10 +1157,6 @@ protected function setDuration($start_date, $end_date)
$end_date_parsed['year']
);

if ($this->_start_date_ts > $this->_end_date_ts) {
throw new \Exception("Invalid Date: start_date must be before end_date");
}

list($this->_min_date_id, $this->_max_date_id) = $this->_aggregation_unit->getDateRangeIds($this->_start_date, $this->_end_date);

if (!$start_date_given && !$end_date_given) {
Expand Down
2 changes: 1 addition & 1 deletion classes/ETL/Configuration/EtlConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ protected function registerAction(stdClass &$config, $sectionName)
// the action configuration.

if ( ! isset($config->options_class) ) {
$this->logAndThrowException("Options class not defined for $actionName");
$this->logAndThrowException("Key 'options_class' not defined for $actionName");
}

$optionsClassName = $config->options_class;
Expand Down
51 changes: 50 additions & 1 deletion classes/Rest/Controllers/BaseControllerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Rest\Controllers;

use DateTime;
use Rest\Utilities\Authentication;
use Rest\Utilities\Authorization;
use Silex\Application;
Expand Down Expand Up @@ -551,7 +552,7 @@ protected function getDateTimeFromUnixParam(Request $request, $name, $mandatory
FILTER_CALLBACK,
array(
"options" => function ($value) {
$value_dt = \DateTime::createFromFormat('U', $value);
$value_dt = DateTime::createFromFormat('U', $value);
if ($value_dt === false) {
return null;
}
Expand Down Expand Up @@ -705,4 +706,52 @@ public function formatLogMesssage($message, Request $request, $includeParams = f
return $retval;

} // formatLogMessage()

/**
* Checks that the `$[start|end]Date` values are valid ( `Y-m-d` ) dates and that `$startDate`
* is before `$endDate`.
*
* @param string $startDate the beginning of the date range.
* @param string $endDate the end of the date range.
* @throws BadRequestHttpException if either start or end dates are not provided in the format
* `Y-m-d`, or if the start date is after the end date.
*/
protected function checkDateRange($startDate, $endDate)
{
$startTimestamp = $this->getTimestamp($startDate, 'start_date');
$endTimestamp = $this->getTimestamp($endDate, 'end_date');

if ($startTimestamp > $endTimestamp) {
throw new BadRequestHttpException('Start Date must not be after End Date');
}
}

/**
* Attempt to convert the provided string $date value into an equivalent unix timestamp (int).
*
* @param string $date The value to be converted into a DateTime.
* @param string $paramName 'date', The name of the parameter to be included in the exception
* message if validation fails.
* @param string $format 'Y-m-d', The format that `$date` should be in.
* @return int created from the provided `$date` value.
* @throws BadRequestHttpException if the date is not in the form `Y-m-d`.
*/
protected function getTimestamp($date, $paramName = 'date', $format = 'Y-m-d')
{
$parsed = date_parse_from_format($format, $date);
$date = mktime(
$parsed['hour'],
$parsed['minute'],
$parsed['second'],
$parsed['month'],
$parsed['day'],
$parsed['year']
);

if ($date === false || $parsed['error_count'] > 0) {
throw new BadRequestHttpException("Unable to parse $paramName");
}

return $date;
}
}
62 changes: 61 additions & 1 deletion classes/Rest/Controllers/SummaryControllerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

namespace Rest\Controllers;

use Configuration\XdmodConfiguration;
use Exception;
use PDOException;
use Silex\Application;
use Silex\ControllerCollection;
use Symfony\Component\HttpFoundation\Request;
use DataWarehouse\Query\Exceptions\BadRequestException;

use Models\Services\Acls;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;

class SummaryControllerProvider extends BaseControllerProvider
{
Expand All @@ -24,7 +27,11 @@ public function setupRoutes(Application $app, ControllerCollection $controller)
$controller->post("$root/layout", "$class::setLayout");
$controller->delete("$root/layout", "$class::resetLayout");


$controller->get("$root/rolereport", "$class::getRoleReport");

$controller->get("$root/statistics", "$class::getStatistics");

}

/*
Expand Down Expand Up @@ -195,6 +202,7 @@ public function resetLayout(Request $request, Application $app)
}

/**
* Get charts based on role.
**/
public function getRoleReport(Request $request, Application $app)
Expand Down Expand Up @@ -253,5 +261,57 @@ public function getRoleReport(Request $request, Application $app)
'data' => $data
));
}

* Retrieve summary statistics
*
* @param Request $request
* @param Application $app
* @return \Symfony\Component\HttpFoundation\JsonResponse
* @throws Exception
*/
public function getStatistics(Request $request, Application $app)
{
$user = $this->getUserFromRequest($request);

$aggregationUnit = $request->get('aggregation_unit', 'auto');

$startDate = $this->getStringParam($request, 'start_date', true);
$endDate = $this->getStringParam($request, 'end_date', true);

$this->checkDateRange($startDate, $endDate);

// This try/catch block is intended to replace the "Base table or
// view not found: 1146 Table 'modw_aggregates.jobfact_by_day'
// doesn't exist" error message with something more informative for
// Open XDMoD users.
try {
$query = new \DataWarehouse\Query\Jobs\Aggregate($aggregationUnit, $startDate, $endDate, 'none', 'all');

$result = $query->execute();
} catch (PDOException $e) {
if ($e->getCode() === '42S02' && strpos($e->getMessage(), 'modw_aggregates.jobfact_by_') !== false) {
$msg = 'Aggregate table not found, have you ingested your data?';
throw new Exception($msg);
} else {
throw $e;
}
} catch (Exception $e) {
throw new BadRequestHttpException($e->getMessage());
}

$rawRoles = XdmodConfiguration::assocArrayFactory('roles.json', CONFIG_DIR);

$mostPrivileged = $user->getMostPrivilegedRole()->getName();
$formats = $rawRoles['roles'][$mostPrivileged]['statistics_formats'];

return $app->json(
array(
'totalCount' => 1,
'success' => true,
'message' => '',
'formats' => $formats,
'data' => array($result)
)
);
}
}
Loading

0 comments on commit 0df896d

Please sign in to comment.