Skip to content

Commit

Permalink
updates for comments, new validation method array_ints and tests for …
Browse files Browse the repository at this point in the history
…it , fix statistics
  • Loading branch information
mudhoney committed Jun 10, 2024
1 parent b4856f0 commit fdcc4dc
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 38 deletions.
5 changes: 2 additions & 3 deletions docroot/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ function loadModule($params) {
$actions_to_keep_stats_for = [
'getClosestImage',
'takeScreenshot',
'postsScreenshot',
'postScreenshot',
'getJPX',
'getJPXClosestToMidPoint',
'uploadMovieToYouTube',
Expand All @@ -182,8 +182,7 @@ function loadModule($params) {

// Note that in addition to the above, buildMovie requests and
// addition to getTile when the tile was already in the cache.
if ( HV_ENABLE_STATISTICS_COLLECTION &&
in_array($params['action'], $actions_to_keep_stats_for) ) {
if ( HV_ENABLE_STATISTICS_COLLECTION && in_array($params['action'], $actions_to_keep_stats_for) ) {

include_once HV_ROOT_DIR.'/../src/Database/Statistics.php';
$statistics = new Database_Statistics();
Expand Down
2 changes: 1 addition & 1 deletion docroot/statistics/statistics.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var heirarchy = {
"Images":["takeScreenshot","postScreenshot","getTile","getClosestImage","getJP2Image-web","getJP2Image-jpip","getJP2Image","downloadScreenshot","getJPX","getJPXClosestToMidPoint", "downloadImage"],
"Movies":["buildMovie","getMovieStatus","queueMovie","postMovie","reQueueMovie","playMovie","downloadMovie","getUserVideos","getObservationDateVideos","uploadMovieToYouTube","checkYouTubeAuth","getYouTubeAuth"],
"Events":["getEventGlossary", "events", "getEvents","getFRMs","getEvent","getEventFRMs","getDefaultEventTypes","getEventsByEventLayers","importEvents"],
"Data":["getRandomSeed","getDataSources","getJP2Header","getDataCoverage","getStatus","getNewsFeed","getDataCoverageTimeline","getClosestData","getSolarBodiesGlossary","getSolarBodies","getTrajectoryTime","sciScript-SSWIDL","sciScript-SunPy","getSciDataScript","updateDataCoverage","getEclipseImage"],
"Data":["getRandomSeed","getDataSources","getJP2Header","getDataCoverage","getStatus","getNewsFeed","getDataCoverageTimeline","getClosestData","getSolarBodiesGlossary","getSolarBodies","getTrajectoryTime","sciScript-SSWIDL","sciScript-SunPy","getSciDataScript","updateDataCoverage","getEclipseImage","getClosestImageDatesForSources"],
"Other":["shortenURL", "goto", "getUsageStatistics","movie-notifications-granted","movie-notifications-denied","logNotificationStatistics","launchJHelioviewer", "saveWebClientState", "getWebClientState"],
"WebGL":["getTexture","getGeometryServiceData"]
};
Expand Down
4 changes: 2 additions & 2 deletions scripts/hv_stats/hv_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -2271,9 +2271,9 @@ def add_harea_if_timestamp_in_range(start_time, end_time, color, alpha, label):
"Total":["total","rate_limit_exceeded"],
"Client Sites":["standard","embed","minimal"],
"Images":["takeScreenshot","postScreenshot","getTile","getClosestImage","getJP2Image-web","getJP2Image-jpip","getJP2Image","downloadScreenshot","getJPX","getJPXClosestToMidPoint"],
"Movies":["buildMovie","getMovieStatus","queueMovie","postMovie","reQueueMovie","playMovie","downloadMovie","getUserVideos","getObservationDateVideos","uploadMovieToYouTube","checkYouTubeAuth","getYouTubeAuth"],
"Movies":["buildMovie","postMovie","getMovieStatus","queueMovie","postMovie","reQueueMovie","playMovie","downloadMovie","getUserVideos","getObservationDateVideos","uploadMovieToYouTube","checkYouTubeAuth","getYouTubeAuth"],
"Events":["getEventGlossary","getEvents","getFRMs","getEvent","getEventFRMs","getDefaultEventTypes","getEventsByEventLayers","importEvents"],
"Data":["getRandomSeed","getDataSources","getJP2Header","getDataCoverage","getStatus","getNewsFeed","getDataCoverageTimeline","getClosestData","getSolarBodiesGlossary","getSolarBodies","getTrajectoryTime","sciScript-SSWIDL","sciScript-SunPy","getSciDataScript","updateDataCoverage","getEclipseImage"],
"Data":["getRandomSeed","getDataSources","getJP2Header","getDataCoverage","getStatus","getNewsFeed","getDataCoverageTimeline","getClosestData","getSolarBodiesGlossary","getSolarBodies","getTrajectoryTime","sciScript-SSWIDL","sciScript-SunPy","getSciDataScript","updateDataCoverage","getEclipseImage", "getClosestImageDatesForSources"],
"Other":["shortenURL","getUsageStatistics","movie-notifications-granted","movie-notifications-denied","logNotificationStatistics","launchJHelioviewer", "saveWebClientState", "getWebClientState"],
"WebGL":["getTexture","getGeometryServiceData"]
};
Expand Down
16 changes: 8 additions & 8 deletions src/Database/ImgIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ public function insertScreenshot($date, $imageScale, $roi, $watermark, $layers,

$this->_dbConnect();

// old implementation removed for events strings
// used to be $this->events->serialize();
$old_events_layer_string = "";
// old implementation removed for events strings
// used to be $this->events->serialize();
$old_events_layer_string = "";

// old if events labels are shown switch , removed for new implementation
// used to be $this->eventsLabels;
$old_events_labels_bool = false;
// old if events labels are shown switch , removed for new implementation
// used to be $this->eventsLabels;
$old_events_labels_bool = false;

$sql = sprintf(
"INSERT INTO screenshots "
Expand Down Expand Up @@ -549,7 +549,7 @@ public function getClosestDataBeforeAndAfter($date, $sourceId)
. "WHERE "
. "sourceId " . " = %d AND "
. "date " . "> '%s' "
. "ORDER BY date DESC "
. "ORDER BY date ASC "
. "LIMIT 1;",
(int)$sourceId,
$this->_dbConnection->link->real_escape_string($datestr)
Expand Down
18 changes: 10 additions & 8 deletions src/Database/MovieDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,21 @@ public function insertMovie($startTime, $endTime, $reqObservationDate, $imageSca

$startTime = isoDateToMySQL($startTime);
$endTime = isoDateToMySQL($endTime);

if($reqObservationDate != false){
$reqObservationDate = '"'.$this->_dbConnection->link->real_escape_string(isoDateToMySQL($reqObservationDate)).'"';
$reqObservationDate = '"'.$this->_dbConnection->link->real_escape_string(isoDateToMySQL($reqObservationDate)).'"';
}else{
$reqObservationDate = "NULL";
$reqObservationDate = "NULL";
}

// old implementation removed for events strings
// used to be $this->events->serialize();
$old_events_layer_string = "";
// !Attention we are keeping those fields for current rows in movies and screenshots tables
// old implementation removed for events strings
// used to be $this->events->serialize();
$old_events_layer_string = "";

// old if events labels are shown switch , removed for new implementation
// used to be $this->eventsLabels;
$old_events_labels_bool = false;
// old if events labels are shown switch , removed for new implementation
// used to be $this->eventsLabels;
$old_events_labels_bool = false;

$sql = sprintf(
'INSERT INTO movies '
Expand Down
8 changes: 8 additions & 0 deletions src/Database/Statistics.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,10 @@ public function getUsageStatistics($resolution, $dateStart = null, $dateEnd = nu
// Array to keep track of counts for each action
$counts = array(
"buildMovie" => array(),
"postMovie" => array(),
"getClosestData" => array(),
"getClosestImage" => array(),
"getClosestImageDatesForSources" => array(),
"getJPX" => array(),
"getJPXClosestToMidPoint" => array(),
"takeScreenshot" => array(),
Expand All @@ -447,10 +449,12 @@ public function getUsageStatistics($resolution, $dateStart = null, $dateEnd = nu
// Summary array
$summary = array(
"buildMovie" => 0,
"postMovie" => 0,
"getClosestData" => 0,
"getClosestImage" => 0,
"getJPX" => 0,
"getJPXClosestToMidPoint" => 0,
"getClosestImageDatesForSources" => 0,
"takeScreenshot" => 0,
"postScreenshot" => 0,
"uploadMovieToYouTube" => 0,
Expand Down Expand Up @@ -832,6 +836,7 @@ private function _createCountsArray(){
'getWebClientState' => array(),
'goto' => array(),
'takeScreenshot' => array(),
'postScreenshot' => array(),
'getRandomSeed' => array(),
'getJP2Image' => array(),
'getJPX' => array(),
Expand Down Expand Up @@ -865,6 +870,7 @@ private function _createCountsArray(){
'getGeometryServiceData' => array(),
'buildMovie' => array(),//this one happens in HelioviewerMovie.php
"getClosestData" => array(),
"getClosestImageDatesForSources" => array(),
"embed" => array(),
"minimal" => array(),
"standard" => array(),
Expand All @@ -883,6 +889,7 @@ private function _createSummaryArray(){
"total" => 0,
'downloadScreenshot' => 0,
'getClosestImage' => 0,
"getClosestImageDatesForSources" => 0,
'getDataSources' => 0,
'getJP2Header' => 0,
'getNewsFeed' => 0,
Expand All @@ -899,6 +906,7 @@ private function _createSummaryArray(){
'getWebClientState' => 0,
'goto' => 0,
'takeScreenshot' => 0,
'postScreenshot' => 0,
'getRandomSeed' => 0,
'getJP2Image' => 0,
'getJPX' => 0,
Expand Down
6 changes: 2 additions & 4 deletions src/Module/WebClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,9 @@ public function getClosestImageDatesForSources() {

$imgIndex = new Database_ImgIndex();

$sources = trim($this->_params['sources']);
$source_ids = explode(',', $sources);

$results = [];

foreach($source_ids as $sid) {
foreach($this->_params['sources'] as $sid) {
$closestImages = $imgIndex->getClosestDataBeforeAndAfter($this->_params['date'], $sid);
$results[$sid]['prev_date'] = $closestImages['prev_date'];
$results[$sid]['next_date'] = $closestImages['next_date'];
Expand Down Expand Up @@ -1696,6 +1693,7 @@ public function validate() {
$expected = array(
'required' => array('date', 'sources'),
'dates' => array('date'),
'array_ints' => array('sources'),
);
break;
case 'getDataSources':
Expand Down
55 changes: 43 additions & 12 deletions src/Validation/InputValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,19 @@ public static function checkInput(&$expected, &$input, &$optional)
{
// Validation checks
$checks = array(
"required" => "checkForMissingParams",
"alphanum" => "checkAlphaNumericStrings",
"ints" => "checkInts",
"floats" => "checkFloats",
"bools" => "checkBools",
"dates" => "checkDates",
"encoded" => "checkURLEncodedStrings",
"urls" => "checkURLs",
"files" => "checkFilePaths",
"uuids" => "checkUUIDs",
"layer" => "checkLayerValidity",
"choices" => "checkChoices"
"required" => "checkForMissingParams",
"alphanum" => "checkAlphaNumericStrings",
"ints" => "checkInts",
"array_ints" => "checkOfArrayInts",
"floats" => "checkFloats",
"bools" => "checkBools",
"dates" => "checkDates",
"encoded" => "checkURLEncodedStrings",
"urls" => "checkURLs",
"files" => "checkFilePaths",
"uuids" => "checkUUIDs",
"layer" => "checkLayerValidity",
"choices" => "checkChoices"
);

// Run validation checks
Expand Down Expand Up @@ -211,6 +212,36 @@ public static function checkInts($ints, &$params)
}
}


/**
* Typecasts validates and fixes types for array integer parameters
*
* @param array $ints A list of integer array parameters which are used by an action.
* @param array &$params The parameters that were passed in
*
* @return void
*/
public static function checkOfArrayInts($ints, &$params)
{
foreach ($ints as $int) {
if (isset($params[$int])) {


$integers_to_check = explode(',',$params[$int]);
$validated_ints = [];

foreach($integers_to_check as $itc) {
if (filter_var(trim($itc), FILTER_VALIDATE_INT) === false) {
throw new InvalidArgumentException("Invalid value for $int. Please specify an integer array value, as ex:1,2,3", 25);
}
$validated_ints[] = (int) trim($itc);
}

$params[$int] = $validated_ints;
}
}
}

/**
* Typecasts validates and fixes types for float parameters
*
Expand Down
56 changes: 56 additions & 0 deletions tests/unit_tests/validation/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,60 @@ public function test_ValidateLayerArray_MultipleLayers(): void
// that no exception was raised.
$this->assertTrue(true);
}

public function test_ValidateArrayIntegersProblem1(): void
{
// The expected layer string to be given
$input = array(
'sources' => ''
);

$rules = array(
'array_ints' => array('sources')
);

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid value for sources. Please specify an integer array value, as ex:1,2,3');
Validation_InputValidator::checkInput($rules, $input, $input) ;
// checkInput will raise an exception if it fails, so assertTrue means
// that no exception was raised.
}

public function test_ValidateArrayIntegersProblem2(): void
{
// The expected layer string to be given
$input = array(
'sources' => 'a,1,2'
);

$rules = array(
'array_ints' => array('sources')
);

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid value for sources. Please specify an integer array value, as ex:1,2,3');
Validation_InputValidator::checkInput($rules, $input, $input) ;
// checkInput will raise an exception if it fails, so assertTrue means
// that no exception was raised.
}

public function test_ValidateArrayIntegersCorrectly(): void
{
// The expected layer string to be given
$input = array(
'sources' => '4,1,2'
);

$rules = array(
'array_ints' => array('sources')
);

Validation_InputValidator::checkInput($rules, $input, $input) ;

$this->assertEquals($input, [
'sources' => [4,1,2]
]);
// checkInput will raise an exception if it fails, so assertTrue means
// that no exception was raised.
}
}

0 comments on commit fdcc4dc

Please sign in to comment.