From d5d81d8c96c2418804bcae0f22d7890de12c78e7 Mon Sep 17 00:00:00 2001 From: Jeanette Sperhac Date: Tue, 21 Mar 2017 13:46:06 -0400 Subject: [PATCH] Convert internal dashboard supremm dataflow from old REST stack to NewRest stack, to enable elimination of old REST stack code. --- build.json | 1 + .../SupremmDataflowControllerProvider.php | 305 ++++++++++++++++++ classes/REST/Supremm/Explorer.php | 246 -------------- configuration/rest.d/supremm_dataflow.json | 6 + html/internal_dashboard/supremm/js/stats.js | 18 +- html/internal_dashboard/supremm/stats.php | 14 +- .../DashboardSupremmTest.php | 304 +++++++++++++++++ 7 files changed, 636 insertions(+), 258 deletions(-) create mode 100644 classes/NewRest/Controllers/SupremmDataflowControllerProvider.php delete mode 100644 classes/REST/Supremm/Explorer.php create mode 100644 configuration/rest.d/supremm_dataflow.json create mode 100644 tests/integration_tests/lib/REST/internal_dashboard/DashboardSupremmTest.php diff --git a/build.json b/build.json index 1139e9e6..bcf2f123 100644 --- a/build.json +++ b/build.json @@ -69,6 +69,7 @@ "etc": { "configuration/portal_settings.d/supremm.ini": "portal_settings.d/supremm.ini", "configuration/datawarehouse.d/supremm.json": "datawarehouse.d/supremm.json", + "configuration/rest.d/supremm_dataflow.json": "rest.d/supremm_dataflow.json", "configuration/roles.d/supremm.json": "roles.d/supremm.json", "configuration/roles.d/supremm-job-viewer.json": "roles.d/supremm-job-viewer.json", "configuration/setup.d/supremm.json": "setup.d/supremm.json", diff --git a/classes/NewRest/Controllers/SupremmDataflowControllerProvider.php b/classes/NewRest/Controllers/SupremmDataflowControllerProvider.php new file mode 100644 index 00000000..21fb3327 --- /dev/null +++ b/classes/NewRest/Controllers/SupremmDataflowControllerProvider.php @@ -0,0 +1,305 @@ +prefix; + $base = get_class($this); + + // QUERY ROUTES + $controller + ->get("$root/resources", "$base::getResources"); + + $controller + ->get("$root/dbstats", "$base::getDbstats"); + + } // function setupRoutes + + /** + * Retrieve list of all available Supremm resources. + * + * @param Request $request + * @param Application $app + * @return JsonResponse + */ + public function getResources(Request $request, Application $app) + { + $action = __FUNCTION__; + $payload = array( + 'success' => false, + 'action' => $action, + ); + + $user = $this->authorize($request, array(ROLE_ID_MANAGER)); + + $pdo = DB::factory('database'); + $query = "SELECT r.code as name, jj.resource_id as id FROM modw.resourcefact r, (SELECT DISTINCT j.resource_id as resource_id FROM modw_supremm.job j) jj WHERE jj.resource_id = r.id"; + $data = $pdo->query($query); + + $payload['data'] = $data; + $payload['success'] = true; + + return $app->json($payload); + } // function getResources + + /** + * Retrieve dbstats describing specified supremm resource and db. + * + * @param Request $request + * @param Application $app + * @return JsonResponse + */ + public function getDbstats(Request $request, Application $app) + { + $resourceid = $this->getIntParam($request, 'resource_id'); + $dbid = $this->getStringParam($request, 'db_id'); + + $action = __FUNCTION__; + $payload = array( + 'success' => false, + 'action' => $action, + 'message' => 'success' + ); + + $user = $this->authorize($request, array(ROLE_ID_MANAGER)); + $data = $this->queryDbstats($dbid, $resourceid); + + if (isset($data)) { + $payload['data'] = $data; + $payload['success'] = true; + } else { + throw new NotFoundHttpException("There was no result found for the given database ($dbid) and resource ($resourceid)"); + } + + return $app->json($payload); + } // function getDbstats + + /** + * Query supremm database for most recent data flow information. + * + * @param string $dbid + * @param int $resourceid + * @return array of data flow information + */ + private function queryDbstats($dbid, $resourceid) + { + if ($dbid === null || $resourceid === null) { + throw new BadRequestHttpException("Null db id or resource id was supplied"); + } + + switch ($dbid) { + + case "summarydb": + $s = new \DataWarehouse\Query\SUPREMM\SupremmDbInterface(); + return $s->getdbstats($resourceid); + break; + + case "accountdb": + $pdo = DB::factory('database'); + $query = "SELECT count(*) as count, max(end_time_ts) as recent FROM ts_analysis.accountfact WHERE resource_id = :id"; + $res = $pdo->query($query, array("id" => $resourceid)); + $query2 = "SELECT count(*) as count FROM ts_analysis.accountfact WHERE resource_id = :id AND process_version > 0"; + $res2 = $pdo->query($query2, array("id" => $resourceid)); + $query3 = "SELECT (data_length + index_length) / table_rows as ave_rowsize FROM information_schema.tables WHERE table_schema = 'ts_analysis' and table_name = 'accountfact'"; + $res3 = $pdo->query($query3); + + $result = array( + "resource_id" => $resourceid, + "data" => array( + "last_job" => $this->time2str($res[0]['recent']), + "last_job_tm" => gmdate('c', $res[0]['recent']), + "total" => $res[0]['count'], + "processed" => $res2[0]['count'], + "pending" => $res[0]['count'] - $res2[0]['count'], + "approx_size" => $this->formatDataSize($res3[0]['ave_rowsize'] * $res[0]['count']) + ) + ); + + return $result; + break; + + case "jobfact": + $pdo = DB::factory('database'); + $query = "SELECT count(*) as count, max(end_time_ts) as recent FROM modw_supremm.job WHERE resource_id = :id"; + $res = $pdo->query($query, array("id" => $resourceid)); + + $query2 = "SELECT SUM(data_length + index_length) total_storage FROM information_schema.TABLES WHERE table_schema = 'modw_supremm'"; + $res1 = $pdo->query($query2); + + $query3 = "SELECT count(*) as count FROM modw_supremm.job"; + $res2 = $pdo->query($query3); + + $approx_storage = $res1[0]['total_storage'] * 1.0 * $res[0]['count'] / $res2[0]['count']; + + $result = array( + "resource_id" => $resourceid, + "data" => array( + "last_job" => $this->time2str($res[0]['recent']), + "last_job_tm" => gmdate('c', $res[0]['recent']), + "total" => $res[0]['count'], + "approx_size" => $this->formatDataSize($approx_storage) + ) + ); + + return $result; + break; + + case "aggregates": + $pdo = DB::factory('database'); + $query = "SELECT d.day_end_ts as recent FROM modw.days d WHERE d.id = (SELECT max(day_id) FROM modw_aggregates.supremmfact_by_day WHERE resource_id = :id)"; + $res = $pdo->query($query, array("id" => $resourceid)); + + $sizequery = "SELECT (SELECT count(*) FROM modw_aggregates.supremmfact_by_day) as totalrows, (SELECT count(*) FROM modw_aggregates.supremmfact_by_day WHERE resource_id = :id) as resrows, (SELECT SUM(data_length + index_length) FROM information_schema.TABLES WHERE table_schema = 'modw_aggregates' AND table_name in ('supremmfact_by_day_joblist', 'supremmfact_by_day', 'supremmfact_by_month', 'supremmfact_by_quarter', 'supremmfact_by_year')) as datasize"; + $sizeres = $pdo->query($sizequery, array("id" => $resourceid)); + + $result = array( + "resource_id" => $resourceid, + "data" => array( + "last_day" => $this->time2str($res[0]['recent']), + "last_day_tm" => gmdate('c', $res[0]['recent']), + "approx_size" => $this->formatDataSize($sizeres[0]['resrows'] * $sizeres[0]['datasize'] / $sizeres[0]['totalrows']) + ) + ); + + return $result; + break; + + default: + throw new NotFoundHttpException("There was no result found for the given database ($dbid)"); + break; + } + return null; + } // function queryDbstats + + /** + * Format and label timestamp appropriately for report output (minutes, hours, weeks, or months). + * + * @param timestamp $ts + * @return mixed + */ + private function time2str($ts) + { + $diff = time() - $ts; + + if ($diff == 0) { + return 'now'; + + } elseif ($diff > 0) { + + $day_diff = floor($diff / 86400); + + if ($day_diff == 0) { + + if ($diff < 60) { + return 'just now'; + } + if ($diff < 120) { + return '1 minute ago'; + } + if ($diff < 3600) { + return floor($diff / 60) . ' minutes ago'; + } + if ($diff < 7200) { + return '1 hour ago'; + } + if ($diff < 86400) { + return floor($diff / 3600) . ' hours ago'; + } + } + if ($day_diff == 1) { + return floor($diff / 3600) . ' hours ago'; + } + if ($day_diff < 7) { + return $day_diff . ' days ago'; + } + if ($day_diff < 31) { + return ceil($day_diff / 7) . ' weeks ago'; + } + if ($day_diff < 60) { + return 'last month'; + } + + return date('F Y', $ts); + + } else { + + $diff = abs($diff); + $day_diff = floor($diff / 86400); + + if ($day_diff == 0) { + if ($diff < 120) { + return 'in a minute'; + } + if ($diff < 3600) { + return 'in ' . floor($diff / 60) . ' minutes'; + } + if ($diff < 7200) { + return 'in an hour'; + } + if ($diff < 86400) { + return 'in ' . floor($diff / 3600) . ' hours'; + } + } + if ($day_diff == 1) { + return 'Tomorrow'; + } + if ($day_diff < 4) { + return date('l', $ts); + } + if ($day_diff < 7 + (7 - date('w'))) { + return 'next week'; + } + if (ceil($day_diff / 7) < 4) { + return 'in ' . ceil($day_diff / 7) . ' weeks'; + } + if (date('n', $ts) == date('n') + 1) { + return 'next month'; + } + + return date('F Y', $ts); + } + } // function time2str() + + /** + * Format and label data sizes appropriately for report output (as GB, MB, kB, or B). + * + * @param numeric $d + * @return mixed + */ + private function formatDataSize($d) + { + if ($d > 1024 * 1024 * 1024) { + return sprintf("%.2f GB", $d / (1024 * 1024 * 1024)); + } elseif ($d > 1024 * 1024) { + return sprintf("%.2f MB", $d / (1024 * 1024)); + } elseif ($d > 1024) { + return sprintf("%.2f kB", $d / 1024); + } else { + return sprintf("%.2f B", $d); + } + } // function formatDataSize +} // class SupremmDataflowControllerProvider diff --git a/classes/REST/Supremm/Explorer.php b/classes/REST/Supremm/Explorer.php deleted file mode 100644 index 4ad69fd9..00000000 --- a/classes/REST/Supremm/Explorer.php +++ /dev/null @@ -1,246 +0,0 @@ - 1024 * 1024 * 1024) { - return sprintf("%.2f GB", $d / (1024 * 1024 * 1024)); - } else if ($d > 1024 * 1024) { - return sprintf("%.2f MB", $d / (1024 * 1024)); - } else if ($d > 1024) { - return sprintf("%.2f kB", $d / 1024); - } else { - return sprintf("%.2f B", $d); - } -} - - -class Explorer extends \aRestAction -{ - protected $logger = NULL; - - - // -------------------------------------------------------------------------------- - // @see aRestAction::__call() - // -------------------------------------------------------------------------------- - - public function __call($target, $arguments) - { - // Verify that the target method exists and call it. - - $method = $target . ucfirst($this->_operation); - - if (!method_exists($this, $method)) { - - if ($this->_operation == 'Help') { - // The help method for this action does not exist, so attempt to generate a response - // using that action's Documentation() method - - $documentationMethod = $target . 'Documentation'; - - if (!method_exists($this, $documentationMethod)) { - throw new Exception("Help cannot be found for action '$target'"); - } - - return $this->$documentationMethod()->getRESTResponse(); - - } else { - throw new Exception("Unknown action '$target' in category '" . strtolower(__CLASS__) . "'"); - } - - } // if ( ! method_exists($this, $method) ) - - return $this->$method($arguments); - - } // __call() - - // -------------------------------------------------------------------------------- - - public function __construct($request) - { - parent::__construct($request); - - // Initialize the logger - - $params = $this->_parseRestArguments(""); - $verbose = (isset($params['debug']) && $params['debug']); - $maxLogLevel = ($verbose ? PEAR_LOG_DEBUG : PEAR_LOG_INFO); - $logConf = array('mode' => 0644); - $logfile = LOG_DIR . "/" . \xd_utilities\getConfiguration('datawarehouse', 'rest_logfile'); - $this->logger = \Log::factory('file', $logfile, 'Supremm', $logConf, $maxLogLevel); - - } // __construct - - // -------------------------------------------------------------------------------- - // @see aRestAction::factory() - // -------------------------------------------------------------------------------- - - public static function factory($request) - { - return new Explorer($request); - } - - private function resourcesAction() - { - $user = $this->_authenticateUser(); - if ($user->getUserType() != \XDUser::INTERNAL_USER) { - throw new \Exception("Access denied", 401); - } - - $pdo = DB::factory('database'); - $query = "SELECT r.code as name, jj.resource_id as id FROM modw.resourcefact r, (SELECT DISTINCT j.resource_id as resource_id FROM modw_supremm.job j) jj WHERE jj.resource_id = r.id"; - $res = $pdo->query($query); - return array("data" => $res); - } - - private function resourcesDocumentation() - { - $doc = new \RestDocumentation(); - $doc->setDescription("Retrieve the list of resources in the SUPReMM realm."); - $doc->setAuthenticationRequirement(true); - $doc->addReturnElement("resources", "An array enumerating the available resources in the datawarehouse."); - return $doc; - } - - private function dbstatsAction() - { - $user = $this->_authenticateUser(); - if ($user->getUserType() != \XDUser::INTERNAL_USER) { - throw new \Exception("Access denied", 401); - } - - $params = $this->_parseRestArguments(); - $dbid = isset($params['db_id']) ? $params['db_id'] : null; - $resourceid = isset($params['resource_id']) ? $params['resource_id'] : null; - - if ($dbid === null || $resourceid === null) { - throw new \DataWarehouse\Query\Exceptions\BadRequestException(); - } - - switch ($dbid) { - case "summarydb": - $s = new \DataWarehouse\Query\SUPREMM\SupremmDbInterface(); - return $s->getdbstats($resourceid); - break; - case "accountdb": - $pdo = DB::factory('database'); - $query = "SELECT count(*) as count, max(end_time_ts) as recent FROM ts_analysis.accountfact WHERE resource_id = :id"; - $res = $pdo->query($query, array("id" => $resourceid)); - $query2 = "SELECT count(*) as count FROM ts_analysis.accountfact WHERE resource_id = :id AND process_version > 0"; - $res2 = $pdo->query($query2, array("id" => $resourceid)); - $query3 = "SELECT (data_length + index_length) / table_rows as ave_rowsize FROM information_schema.tables WHERE table_schema = 'ts_analysis' and table_name = 'accountfact'"; - $res3 = $pdo->query($query3); - - $result = array( - "resource_id" => $resourceid, - "data" => array( - "last_job" => $this->time2str($res[0]['recent']), - "last_job_tm" => gmdate('c', $res[0]['recent']), - "total" => $res[0]['count'], - "processed" => $res2[0]['count'], - "pending" => $res[0]['count'] - $res2[0]['count'], - "approx_size" => formatDataSize($res3[0]['ave_rowsize'] * $res[0]['count']) - ) - ); - return $result; - break; - case "jobfact": - $pdo = DB::factory('database'); - $query = "SELECT count(*) as count, max(end_time_ts) as recent FROM modw_supremm.job WHERE resource_id = :id"; - $res = $pdo->query($query, array("id" => $resourceid)); - - $query2 = "SELECT SUM(data_length + index_length) total_storage FROM information_schema.TABLES WHERE table_schema = 'modw_supremm'"; - $res1 = $pdo->query($query2); - - $query3 = "SELECT count(*) as count FROM modw_supremm.job"; - $res2 = $pdo->query($query3); - - $approx_storage = $res1[0]['total_storage'] * 1.0 * $res[0]['count'] / $res2[0]['count']; - - $result = array("resource_id" => $resourceid, "data" => array( - "last_job" => $this->time2str($res[0]['recent']), - "last_job_tm" => gmdate('c', $res[0]['recent']), - "total" => $res[0]['count'], - "approx_size" => formatDataSize($approx_storage) - )); - return $result; - break; - case "aggregates": - $pdo = DB::factory('database'); - $query = "SELECT d.day_end_ts as recent FROM modw.days d WHERE d.id = (SELECT max(day_id) FROM modw_aggregates.supremmfact_by_day WHERE resource_id = :id)"; - $res = $pdo->query($query, array("id" => $resourceid)); - - $sizequery = "SELECT (SELECT count(*) FROM modw_aggregates.supremmfact_by_day) as totalrows, (SELECT count(*) FROM modw_aggregates.supremmfact_by_day WHERE resource_id = :id) as resrows, (SELECT SUM(data_length + index_length) FROM information_schema.TABLES WHERE table_schema = 'modw_aggregates' AND table_name in ('supremmfact_by_day_joblist', 'supremmfact_by_day', 'supremmfact_by_month', 'supremmfact_by_quarter', 'supremmfact_by_year')) as datasize"; - $sizeres = $pdo->query($sizequery, array("id" => $resourceid)); - - $result = array("resource_id" => $resourceid, "data" => array( - "last_day" => $this->time2str($res[0]['recent']), - "last_day_tm" => gmdate('c', $res[0]['recent']), - "approx_size" => formatDataSize($sizeres[0]['resrows'] * $sizeres[0]['datasize'] / $sizeres[0]['totalrows']) - )); - return $result; - break; - } - return null; - } - - // TODO - move this function and all of the sql query stuff to a dedicated class. - private function time2str($ts) - { - $diff = time() - $ts; - if ($diff == 0) { - return 'now'; - } elseif ($diff > 0) { - $day_diff = floor($diff / 86400); - if ($day_diff == 0) { - if ($diff < 60) return 'just now'; - if ($diff < 120) return '1 minute ago'; - if ($diff < 3600) return floor($diff / 60) . ' minutes ago'; - if ($diff < 7200) return '1 hour ago'; - if ($diff < 86400) return floor($diff / 3600) . ' hours ago'; - } - if ($day_diff == 1) { - return floor($diff / 3600) . ' hours ago'; - } - if ($day_diff < 7) return $day_diff . ' days ago'; - if ($day_diff < 31) return ceil($day_diff / 7) . ' weeks ago'; - if ($day_diff < 60) return 'last month'; - return date('F Y', $ts); - } else { - $diff = abs($diff); - $day_diff = floor($diff / 86400); - if ($day_diff == 0) { - if ($diff < 120) return 'in a minute'; - if ($diff < 3600) return 'in ' . floor($diff / 60) . ' minutes'; - if ($diff < 7200) return 'in an hour'; - if ($diff < 86400) return 'in ' . floor($diff / 3600) . ' hours'; - } - if ($day_diff == 1) return 'Tomorrow'; - if ($day_diff < 4) return date('l', $ts); - if ($day_diff < 7 + (7 - date('w'))) return 'next week'; - if (ceil($day_diff / 7) < 4) return 'in ' . ceil($day_diff / 7) . ' weeks'; - if (date('n', $ts) == date('n') + 1) return 'next month'; - return date('F Y', $ts); - } - } - - private function dbstatsDocumentation() - { - $doc = new \RestDocumentation(); - $doc->setDescription("Retrieve statistics for a database in the SUPReMM data processing workflow."); - $doc->setAuthenticationRequirement(false); - $doc->addArgument("resource_id", "A valid resource id.", TRUE); - $doc->addArgument("db_id", "A valid database identifier.", TRUE); - $doc->addReturnElement("data", "An array with the record statistics."); - $doc->addReturnElement("resource_id", "The resource_id that was requested."); - return $doc; - } -} // class Explorer - -?> diff --git a/configuration/rest.d/supremm_dataflow.json b/configuration/rest.d/supremm_dataflow.json new file mode 100644 index 00000000..7d96f4ff --- /dev/null +++ b/configuration/rest.d/supremm_dataflow.json @@ -0,0 +1,6 @@ +{ + "supremm_dataflow": { + "prefix": "supremm_dataflow", + "controller": "NewRest\\Controllers\\SupremmDataflowControllerProvider" + } +} diff --git a/html/internal_dashboard/supremm/js/stats.js b/html/internal_dashboard/supremm/js/stats.js index 412092b9..6ceecc2e 100644 --- a/html/internal_dashboard/supremm/js/stats.js +++ b/html/internal_dashboard/supremm/js/stats.js @@ -1,4 +1,5 @@ + Ext.ns('XDMoD'); jsPlumb.ready(function() { var common = { @@ -62,22 +63,23 @@ var print_list = function(data, selector) { var h = ""; $(selector).html(h); }; - $.getJSON( "/rest/supremm/explorer/dbstats", { token: token, resource_id: resource_id, db_id: "accountdb" }, + $.getJSON(XDMoD.REST.url + '/supremm_dataflow/dbstats', { token: XDMoD.REST.token, resource_id: resource_id, db_id: 'accountdb' }, function(data) { print_list(data.data, "#accountfact_content"); }); - $.getJSON( "/rest/supremm/explorer/dbstats", { token: token, resource_id: resource_id, db_id: "summarydb" }, + $.getJSON(XDMoD.REST.url + '/supremm_dataflow/dbstats', { token: XDMoD.REST.token, resource_id: resource_id, db_id: 'summarydb' }, function(data) { print_list(data.data, "#mongo_content"); }); - $.getJSON( "/rest/supremm/explorer/dbstats", { token: token, resource_id: resource_id, db_id: "jobfact" }, + $.getJSON(XDMoD.REST.url + '/supremm_dataflow/dbstats', { token: XDMoD.REST.token, resource_id: resource_id, db_id: 'jobfact' }, function(data) { print_list(data.data, "#jobfact_content"); }); - $.getJSON( "/rest/supremm/explorer/dbstats", { token: token, resource_id: resource_id, db_id: "aggregates" }, + $.getJSON(XDMoD.REST.url + '/supremm_dataflow/dbstats', { token: XDMoD.REST.token, resource_id: resource_id, db_id: 'aggregates' }, function(data) { print_list(data.data, "#aggregates_content"); }); $("#pagetitle").text("Data flow information for " + resource_map[resource_id] ); @@ -90,7 +92,7 @@ loadstats( $("#resourceselect").val() ); }); - $.getJSON("/rest/supremm/explorer/resources", {token: token}, function(data) { + $.getJSON(XDMoD.REST.url + '/supremm_dataflow/resources', { token: XDMoD.REST.token }, function (data) { var select = document.getElementById("resourceselect"); for (var i = 0; i < data.data.length ; i++) { diff --git a/html/internal_dashboard/supremm/stats.php b/html/internal_dashboard/supremm/stats.php index 396b0dca..5e443481 100644 --- a/html/internal_dashboard/supremm/stats.php +++ b/html/internal_dashboard/supremm/stats.php @@ -1,7 +1,7 @@ - + @@ -9,9 +9,15 @@ Job timeseries demo - + + + + @@ -45,7 +51,7 @@
- Aggregate job data + Aggregate job data
diff --git a/tests/integration_tests/lib/REST/internal_dashboard/DashboardSupremmTest.php b/tests/integration_tests/lib/REST/internal_dashboard/DashboardSupremmTest.php new file mode 100644 index 00000000..453c15f1 --- /dev/null +++ b/tests/integration_tests/lib/REST/internal_dashboard/DashboardSupremmTest.php @@ -0,0 +1,304 @@ + true ); + $this->xdmodhelper = new \TestHarness\XdmodTestHelper($xdmodConfig); + + $this->endpoint = 'rest/v0.1/supremm_dataflow/'; + + // validate as manager, for dashboard access + $this->validateAsUser = 'mgr'; + } + + private function invalidSupremmResourceEntries($params) + { + // without performing validation: expect to receive a 401; + // if wrong user authenticated: expect to receive a 403 + $result = $this->xdmodhelper->get($this->endpoint . 'resources', $params); + + // expect success=false + $this->assertArrayHasKey('success', $result[0]); + $this->assertEquals($result[0]['success'], false); + + // expect no data returned + $data = $result[0]['data']; + $this->assertEquals(sizeof($data), 0); + + return $result; + } + + private function validateSupremmResourceEntries() + { + $this->xdmodhelper->authenticate($this->validateAsUser); + + $result = $this->xdmodhelper->get($this->endpoint . 'resources', null); + $this->assertEquals(200, $result[1]['http_code']); + + $this->assertArrayHasKey('success', $result[0]); + $this->assertEquals($result[0]['success'], true); + + $data = $result[0]['data']; + + // result set has at least one element + $this->assertGreaterThanOrEqual(1, sizeof($data)); + + foreach ($data as $item) { + $this->assertArrayHasKey('id', $item); + $this->assertArrayHasKey('name', $item); + } + return $data; + } + + // return arbitrary ResourceId + private function fetchResourceId() + { + $result = $this->validateSupremmResourceEntries(); + + $resourceid = $result[0]['id']; + return $resourceid; + } + + private function invalidSupremmDbstatsEntries($db) + { + // without performing validation : expect to receive a 401 + + // hardcode the params for resource id + $params = array( + 'resource_id' => 2791, + 'db_id' => $db + ); + $result = $this->xdmodhelper->get($this->endpoint . 'dbstats', $params); + + $this->assertArrayHasKey('success', $result[0]); + $this->assertEquals($result[0]['success'], false); + + // expect 401 + $this->assertEquals(401, $result[1]['http_code']); + } + + private function invalidParamsSupremmDbstatsEntries() + { + // validate properly + $this->xdmodhelper->authenticate($this->validateAsUser); + + // send null params, expect 400 + $result = $this->xdmodhelper->get($this->endpoint . 'dbstats', null); + $this->assertEquals(400, $result[1]['http_code']); + + $this->assertArrayHasKey('success', $result[0]); + $this->assertEquals($result[0]['success'], false); + } + + private function invalidResParamsNotFoundSupremmDbstatsEntries() + { + // validate properly + $this->xdmodhelper->authenticate($this->validateAsUser); + + // hardcode and send bogus resource_id param + $params = array( + 'resource_id' => 99999, + 'db_id' => 'summarydb' + ); + $result = $this->xdmodhelper->get($this->endpoint . 'dbstats', $params); + + // Message will contain "no result found" + $this->assertContains("no result found for the given database", $result[0]['message']); + + // result has success='false' + $this->assertArrayHasKey('success', $result[0]); + $this->assertEquals($result[0]['success'], false); + + // should return a 404 + $this->assertEquals(404, $result[1]['http_code']); + } + + private function invalidParamsNotFoundSupremmDbstatsEntries() + { + // validate properly + $this->xdmodhelper->authenticate($this->validateAsUser); + + // hardcode and send bogus db_id param + $params = array( + 'resource_id' => $this->fetchResourceId(), + 'db_id' => 'db_does_not_exist' + ); + $result = $this->xdmodhelper->get($this->endpoint . 'dbstats', $params); + + // Message will contain "no result found" + $this->assertContains("no result found for the given database", $result[0]['message']); + + // result has success='false' + $this->assertArrayHasKey('success', $result[0]); + $this->assertEquals($result[0]['success'], false); + + // should return a 404 + $this->assertEquals(404, $result[1]['http_code']); + } + + private function invalidSupremmUserDbstatsEntries($db, $userRole) + { + // when validating as wrong user type: expect to receive a 403: + // + // First, resource id is fetched using a valid user + // so that we can check the functionality of Dbstats + $params = array( + 'resource_id' => $this->fetchResourceId(), + 'db_id' => $db + ); + + // reauthenticate as some (invalid) user role, not a 'mgr' role + $this->xdmodhelper->authenticate($userRole); + $result = $this->xdmodhelper->get($this->endpoint . 'dbstats', $params); + + // result has success='false' + $this->assertArrayHasKey('success', $result[0]); + $this->assertEquals($result[0]['success'], false); + + // expect 403 + $this->assertEquals(403, $result[1]['http_code']); + } + + private function validateSupremmDbstatsEntries($db) + { + $this->xdmodhelper->authenticate($this->validateAsUser); + + $params = array( + 'resource_id' => $this->fetchResourceId(), + 'db_id' => $db + ); + $result = $this->xdmodhelper->get($this->endpoint . 'dbstats', $params); + $this->assertEquals(200, $result[1]['http_code']); + + // result has success='true' + $this->assertArrayHasKey('success', $result[0]); + $this->assertEquals($result[0]['success'], true); + + // result set has at least one element + $this->assertGreaterThanOrEqual(1, sizeof($result[0]['data'])); + + $item = $result[0]['data']['data']; + return $item; + } + + public function testInvalidUserPOSupremmResourceEntries() + { + // with wrong user authenticated: expect to receive a 403 + $user = 'po'; + $this->xdmodhelper->authenticate($user); + + $result = $this->invalidSupremmResourceEntries(null); + $this->assertEquals(403, $result[1]['http_code']); + } + + public function testInvalidUserCDSupremmResourceEntries() + { + // with wrong user authenticated: expect to receive a 403 + $user = 'cd'; + $this->xdmodhelper->authenticate($user); + + $result = $this->invalidSupremmResourceEntries(null); + $this->assertEquals(403, $result[1]['http_code']); + } + + public function testInvalidUserSupremmResourceEntries() + { + // with no user authenticated: expect to receive a 401 + $result = $this->invalidSupremmResourceEntries(null); + $this->assertEquals(401, $result[1]['http_code']); + } + + public function testResourceNullParam() + { + $data = $this->validateSupremmResourceEntries(); + + // should return at least one element + $this->assertGreaterThanOrEqual(1, sizeof($data)); + } + + public function testInvalidUserPOSupremmDbstatsEntries() + { + $user = 'po'; + $db = 'summarydb'; + $this->invalidSupremmUserDbstatsEntries($db, $user); + } + + public function testInvalidUserCDSupremmDbstatsEntries() + { + $user = 'cd'; + $db = 'summarydb'; + $this->invalidSupremmUserDbstatsEntries($db, $user); + } + + public function testInvalidParamsSupremmDbstatsEntries() + { + $this->invalidParamsSupremmDbstatsEntries(); + } + + public function testInvalidSupremmDbstatsEntries() + { + $supremmDb = 'summarydb'; + $this->invalidSupremmDbstatsEntries($supremmDb); + } + + public function testInvalidParamsNotFoundSupremmDbstatsEntries() + { + $this->invalidParamsNotFoundSupremmDbstatsEntries(); + } + + public function testInvalidResParamsNotFoundSupremmDbstatsEntries() + { + $this->invalidResParamsNotFoundSupremmDbstatsEntries(); + } + + // fetch summarydb stats + public function testFetchDbstatsSummary($db = 'summarydb') { + + $item = $this->validateSupremmDbstatsEntries($db); + + $this->assertArrayHasKey('total', $item); + $this->assertArrayHasKey('avgObjSize', $item); + $this->assertArrayHasKey('storageSize', $item); + $this->assertArrayHasKey('size', $item); + $this->assertArrayHasKey('processed', $item); + $this->assertArrayHasKey('pending', $item); + } + + // fetch accountdb stats + public function testFetchDbstatsAccount($db = 'accountdb') { + + $item = $this->validateSupremmDbstatsEntries($db); + + $this->assertArrayHasKey('total', $item); + $this->assertArrayHasKey('approx_size', $item); + $this->assertArrayHasKey('last_job', $item); + $this->assertArrayHasKey('last_job_tm', $item); + $this->assertArrayHasKey('processed', $item); + $this->assertArrayHasKey('pending', $item); + } + + // fetch jobfact stats + public function testFetchDbstatsJobfact($db = 'jobfact') { + + $item = $this->validateSupremmDbstatsEntries($db); + + $this->assertArrayHasKey('total', $item); + $this->assertArrayHasKey('approx_size', $item); + $this->assertArrayHasKey('last_job', $item); + $this->assertArrayHasKey('last_job_tm', $item); + } + + // fetch aggregates stats + public function testFetchDbstatsAggregates($db = 'aggregates') { + + $item = $this->validateSupremmDbstatsEntries($db); + + $this->assertArrayHasKey('approx_size', $item); + $this->assertArrayHasKey('last_day', $item); + $this->assertArrayHasKey('last_day_tm', $item); + } +}