Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User Dashboard - Summary Statistics Portlet #930

Merged
merged 26 commits into from
Jul 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
75fded5
Initial Prototype of Novice User Portal
jpwhite4 Jan 15, 2019
5f42b61
Added the SummaryStatisticsPortlet
ryanrath Apr 16, 2019
2f57180
Added new Integration Tests and artifacts
ryanrath May 8, 2019
7441d3a
Really adding the artifacts
ryanrath May 8, 2019
ff32395
Adding last artifact
ryanrath May 8, 2019
5a639ea
Updated Tests
ryanrath May 20, 2019
9daf01c
Updates per @jpwhite4 code review comments
ryanrath May 20, 2019
e0b0720
Prettifying Test Artifacts
ryanrath May 21, 2019
e409576
Reformatted Code
ryanrath May 21, 2019
091af1f
Merge branch 'xdmod8.5' into novice_summary_statistics
ryanrath May 21, 2019
956628b
Merge branch 'xdmod8.5' into novice_summary_statistics
ryanrath Jun 13, 2019
a4b53cd
Removing unused code
ryanrath Jun 13, 2019
867f4cc
Merge branch 'xdmod8.5' into novice_summary_statistics
ryanrath Jun 13, 2019
a982383
Merge branch 'xdmod8.5' into novice_summary_statistics
ryanrath Jun 13, 2019
2366f56
Updated statistics per recent `avg` statistic updates
ryanrath Jun 14, 2019
f9ba264
Added more descriptive error messages
ryanrath Jun 14, 2019
3f2f929
Update to add date validation to new REST stack
ryanrath Jun 14, 2019
4fb85a5
derrrr
ryanrath Jun 14, 2019
3759f93
Adding some documentation to BaseControllerProvider
ryanrath Jun 17, 2019
4011ce6
Minor rework to date/time validation
ryanrath Jun 18, 2019
8434fb3
Merge branch 'xdmod8.5' into novice_summary_statistics
ryanrath Jun 19, 2019
91c515a
Minor Cleanup
ryanrath Jun 19, 2019
f08d9d6
Updated `getDateTime` -> `getTimestamp`
ryanrath Jun 19, 2019
3dcaac4
Removing Unused function argument
ryanrath Jun 19, 2019
2a5de9a
asdfj
ryanrath Jun 19, 2019
4b0b1bd
Merge branch 'xdmod8.5' into novice_summary_statistics
ryanrath Jun 26, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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;
}
}
61 changes: 60 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 @@ -23,6 +26,8 @@ public function setupRoutes(Application $app, ControllerCollection $controller)

$controller->post("$root/layout", "$class::setLayout");
$controller->delete("$root/layout", "$class::resetLayout");

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

/*
Expand Down Expand Up @@ -191,4 +196,58 @@ public function resetLayout(Request $request, Application $app)
'total' => 1
));
}

/**
* 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