Skip to content

Commit

Permalink
Issue #10 - New setting to set readonly web mode
Browse files Browse the repository at this point in the history
  • Loading branch information
David Monllao committed Feb 17, 2014
1 parent 4293ee7 commit c1b0ba2
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 52 deletions.
1 change: 1 addition & 0 deletions defaults.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ groupedthreshold=4
singlestepthreshold=2
includelogs=1
defaultcomparesize=S
readonlyweb=''
5 changes: 5 additions & 0 deletions delete_run.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

include(__DIR__ . '/webapp/inc.php');

$properties = properties_reader::get('readonlyweb');
if (!empty($properties['readonlyweb'])) {
die('Error: You are not allowed to perform write actions from the web interface');
}

if (!$filename = $_GET['filename']) {
die('Error: No filename to delete');
}
Expand Down
10 changes: 10 additions & 0 deletions jmeter_config.properties.dist
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,13 @@ jmeter_path=/opt/apache-jmeter-2.9
# Set includelogs="" in case you don't want to include them.
#
# includelogs=1


## Web interface #######################

# The web interface allows users to perform both read action (compare results,
# download runs results...) and write actions (delete runs) if you are planning
# to publish your results but you don't want users to delete your runs set any
# non-empty value to this setting. By default, the web interface is read & write.
#
# readonlyweb=''
78 changes: 78 additions & 0 deletions webapp/classes/properties_reader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/**
* Properties files reader.
*
* @package moodle-performance-comparison
* @copyright 2013 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class properties_reader {


/**
* Gets the vars values from the bash properties files.
*
* Allows passing a string in case you are only interested
* in one property.
*
* @param array $vars Array of vars you are interested on.
* @return array The var that are not found or it's value is '' will not be returned.
*/
public static function get($vars) {

// Single property.
if (is_scalar($vars)) {
$vars = array($vars);
}

$propertiesvalues = array();

// Ordered by preference.
$files = array(
__DIR__ . '/../../jmeter_config.properties',
__DIR__ . '/../../defaults.properties'
);

foreach ($files as $file) {

// Open the file and read each line.
if ($fh = fopen($file, 'r')) {
while (($line = fgets($fh)) !== false) {

foreach ($vars as $var) {
$return = self::extract_properties_file_value($var, $line);
if ($return && empty($propertiesvalues[$var])) {
$propertiesvalues[$var] = $return;
}
}
}
}
}

return $propertiesvalues;
}

/**
* Extracts the property value from $line.
*
* @param string $var
* @param string $line
* @return string The var value
*/
protected static function extract_properties_file_value($var, $line) {

// It can be commented.
if (strpos($line, $var . '=') !== 0) {
return false;
}

// Just in case an extra conditional as is the user the one that enters the value.
if (preg_match("/$var='?([^']*)'?/", $line, $matches)) {
return $matches[1];
}

return false;
}

}
58 changes: 9 additions & 49 deletions webapp/classes/report.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,8 @@ public function get_big_differences() {
}

/**
* Gets the default thresholds.
*
* Hopefully your eyes will not burn after reading this function's code.
*
* Uses the .properties files looking for the threshold values. Gives preference to
Expand All @@ -344,73 +346,31 @@ public function get_big_differences() {
*/
protected function get_default_thresholds() {

// Read values from the properties file.
$vars = array('groupedthreshold', 'singlestepthreshold', 'thresholds');

// Ordered by preference.
$files = array(
__DIR__ . '/../../jmeter_config.properties',
__DIR__ . '/../../defaults.properties'
);

foreach ($files as $file) {

// Open the file and read each line.
if ($fh = fopen($file, 'r')) {
while (($line = fgets($fh)) !== false) {

foreach ($vars as $var) {
$return = $this->extract_threshold_value($var, $line);
if ($return && empty($$var)) {
$$var = $return;
}
}
}
}
}
$properties = properties_reader::get($vars);

// There will always be a value in defaults.properties.
if (empty($groupedthreshold) || empty($singlestepthreshold)) {
if (empty($properties['groupedthreshold']) || empty($properties['singlestepthreshold'])) {
die('Error: defaults.properties thresholds values can not be found' . PHP_EOL);
}

// Preference to $thresholds.
if (!empty($thresholds)) {
return json_decode($thresholds, true);
if (!empty($properties['thresholds'])) {
return json_decode($properties['thresholds'], true);
}

// Generate the default thresholds array.
$thresholds = array('bystep' => array(), 'total' => array());
foreach (test_plan_run::$runvars as $var) {
$thresholds['bystep'][$var] = $singlestepthreshold;
$thresholds['total'][$var] = $groupedthreshold;
$thresholds['bystep'][$var] = $properties['singlestepthreshold'];
$thresholds['total'][$var] = $properties['groupedthreshold'];

}

return $thresholds;
}

/**
* Extracts the property value from $line
*
* @param string $var
* @param string $line
* @return string The var value
*/
protected function extract_threshold_value($var, $line) {

// It can be commented.
if (strpos($line, $var . '=') !== 0) {
return false;
}

// Just in case an extra conditional as is the user the one that enters the value
if (preg_match("/$var='?([^']*)'?/", $line, $matches)) {
return $matches[1];
}

return false;
}

/**
* Describes the changes between two values using the provided threshold
*
Expand Down
15 changes: 12 additions & 3 deletions webapp/classes/report_renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,26 @@ protected function output_runs_info() {
return false;
}

// Is the web interface read only?.
$properties = properties_reader::get('readonlyweb');

$runsinfo = array();
foreach ($this->report->get_runs() as $run) {

// Run vars.
$runinfo = $run->get_run_info();

$filenamestr = 'filename=' . $run->get_filename(false);
$returnurlstr = 'returnurl=' . urlencode('timestamps[]=' . implode('&timestamps[]=', $_GET['timestamps']));
// Adding links to download and to delete.

// Link to download the run.
$runinfo->downloadlink = '<a href="download_run.php?' . $filenamestr . '">Download</a>';
$runinfo->deletelink = '<a href="delete_run.php?' . $filenamestr . '&' . $returnurlstr . '" class="delete-run">Delete</a>';

// Only if the web is read/write.
if (empty($properties['readonlyweb'])) {
// Link to delete the run.
$returnurlstr = 'returnurl=' . urlencode('timestamps[]=' . implode('&timestamps[]=', $_GET['timestamps']));
$runinfo->deletelink = '<a href="delete_run.php?' . $filenamestr . '&' . $returnurlstr . '" class="delete-run">Delete</a>';
}

$runsinfo[] = $this->get_info_container($runinfo);
}
Expand Down
1 change: 1 addition & 0 deletions webapp/inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
ini_set('display_errors', true);
ini_set('error_reporting', E_ALL);

require_once(__DIR__ . '/classes/properties_reader.php');
require_once(__DIR__ . '/classes/google_charts_renderer.php');
require_once(__DIR__ . '/classes/google_chart.php');
require_once(__DIR__ . '/classes/test_plan_runs.php');
Expand Down

0 comments on commit c1b0ba2

Please sign in to comment.