Skip to content

Commit

Permalink
Improved the admin interface, including cron job management
Browse files Browse the repository at this point in the history
  • Loading branch information
mullen2 committed Oct 11, 2018
1 parent 3fcd8c0 commit 1e58a47
Show file tree
Hide file tree
Showing 8 changed files with 315 additions and 59 deletions.
24 changes: 17 additions & 7 deletions AdminConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,12 @@ public function getDayLabel($dayNumber)
return $label;
}


public function getTimes()
{
return range(0, 23);
}

public function getTimeLabel($time)
public function getHtmlTimeLabel($time)
{
$label = '';
$startTime = $time;
Expand All @@ -73,8 +72,12 @@ public function getTimeLabel($time)
if ($startTime > 12) {
$startTime -= 12;
}

if ($startTime < 10) {
$startTime = "&nbsp;".$startTime;
}

if ($endTime < 12) {
if ($endTime < 12 || $endTime == 24) {
$endTimeSuffix = 'am';
} else {
$endTimeSuffix = 'pm';
Expand All @@ -83,8 +86,12 @@ public function getTimeLabel($time)
if ($endTime > 12) {
$endTime -= 12;
}

$label = "{$startTime}{$startTimeSuffix} - {$endTime}{$endTimeSuffix}";

if ($endTime < 10) {
$endTime = "&nbsp;".$endTime;
}

$label = "{$startTime}{$startTimeSuffix}&nbsp;-&nbsp;{$endTime}{$endTimeSuffix}";

return $label;
}
Expand All @@ -110,7 +117,10 @@ public function getTimeLabels()

$endSuffix = 'am';
if ($end >= 12) {
$endSuffix = 'pm';
if ($end < 24) {
$endSuffix = 'pm';
}

if ($end > 12) {
$end -= 12;
}
Expand All @@ -120,7 +130,7 @@ public function getTimeLabels()

$end .= $endSuffix;

$labels[$i] = $start.'-'.$end;
$labels[$i] = $start.' - '.$end;
}
return $labels;
}
Expand Down
109 changes: 99 additions & 10 deletions RedCapEtlModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,17 @@ class RedCapEtlModule extends \ExternalModules\AbstractExternalModule

const CONFIG_SESSION_KEY = 'redcap-etl-config';

/**
* Cron method that is called by REDCap as configured in the
* config.json file for this module.
*/
public function cron()
{
$day = date('w'); // 0-6 (day of week; Sunday = 0)
$hour = date('G'); // 0-23 (24-hour format without leading zeroes)
$message = date('Y-m-d h:i:sa l').': '.$day.' '.$hour."\n";
file_put_contents("cron-log.txt", $message, FILE_APPEND);
\REDCap::logEvent('REDCap-ETL cron', 'Cron check for day '.$day.' and hour '.$hour);
}

public function getVersionNumber()
Expand Down Expand Up @@ -93,10 +102,10 @@ public function getUserCronJobs()
* @param string $name the name of the configuration to get.
* @return Configuration the specified configuration.
*/
public function getConfiguration($name)
public function getConfiguration($name, $username = USERID)
{
$configuraion = null;
$key = $this->getConfigurationKey($name);
$key = $this->getConfigurationKey($name, $username);

$setting = $this->getSystemSetting($key);
$configValues = json_decode($setting, true);
Expand All @@ -122,6 +131,8 @@ public function setConfigSchedule($configName, $server, $schedule)
$configuration->setProperty(Configuration::CRON_SERVER, $server);
$configuration->setProperty(Configuration::CRON_SCHEDULE, $schedule);
$this->setConfiguration($configuration);

\REDCap::logEvent('REDCap-ETL cron schedule change', 'Cron schedule changed for configuration "'.$configName.'".');
}


Expand Down Expand Up @@ -200,6 +211,66 @@ public function removeConfiguration($configName)
$key = $this->getConfigurationKey($configName);
$this->removeSystemSetting($key);
}

public function getAllCronJobs()
{
$cronJobs = array();
foreach (range(0, 6) as $day) {
$cronJobs[$day] = array();
foreach (range(0, 23) as $hour) {
$cronJobs[$day][$hour] = array();
}
}

$usernames = $this->getUsers();
foreach ($usernames as $username) {
$user = $this->getUserInfo($username);
$configNames = $user->getConfigNames();
foreach ($configNames as $configName) {
$config = $this->getConfiguration($configName, $username);
if (isset($config)) {
$server = $config->getProperty(Configuration::CRON_SERVER);
$times = $config->getProperty(Configuration::CRON_SCHEDULE);
for ($day = 0; $day < 7; $day++) {
$hour = $times[$day];
if (isset($hour)) {
$run = array('username' => $username, 'config' => $configName, 'server' => $server);
array_push($cronJobs[$day][$hour], $run);
}
}
}
}
}

return $cronJobs;
}

public function getCronJobs($day, $time)
{
$cronJobs = array();

$usernames = $this->getUsers();
foreach ($usernames as $username) {
$user = $this->getUserInfo($username);
$configNames = $user->getConfigNames();
foreach ($configNames as $configName) {
$config = $this->getConfiguration($configName, $username);
if (isset($config)) {
$server = $config->getProperty(Configuration::CRON_SERVER);
$times = $config->getProperty(Configuration::CRON_SCHEDULE);
for ($cronDay = 0; $cronDay < 7; $cronDay++) {
$cronTime = $times[$cronDay];
if (isset($cronTime) && $time == $cronTime && $day == $cronDay) {
$job = array('username' => $username, 'config' => $configName, 'server' => $server);
array_push($cronJobs, $job);
}
}
}
}
}

return $cronJobs;
}


#==================================================================================
Expand Down Expand Up @@ -342,15 +413,25 @@ public function removeServerConfig($serverName)



public function getUserKey()
/**
* Gets the key for REDCap's external module settings table
* for the specified username, or the current username,
* if no username was specified.
*/
public function getUserKey($username = null)
{
$key = 'user:'.USERID;
if (empty($username)) {
$key = 'user:'.USERID;
} else {
$key = 'user:'.$username;
}

return $key;
}

public function getConfigurationKey($name)
public function getConfigurationKey($name, $username = USERID)
{
$key = 'user:'.USERID.';configuration:'.$name;
$key = 'user:'.$username.';configuration:'.$name;
return $key;
}

Expand All @@ -361,6 +442,10 @@ public function renderAdminTabs($activeUrl = '')
$adminLabel = '<span class="glyphicon glyphicon-cog" aria-hidden="true"></span>'
.' Configure';

$cronJobsUrl = $this->getUrl('cron_jobs.php');
$cronJobsLabel = '<span class="glyphicon glyphicon-time" aria-hidden="true"></span>'
.' Cron Detail';

$manageUsersUrl = $this->getUrl('users.php');
#$manageUsersLabel = '<span>Manage Users</span>';
#$manageUsersLabel = '<span><img aria-hidden="true" src="/redcap/redcap_v8.5.11/Resources/images/users3.png">'
Expand All @@ -375,10 +460,14 @@ public function renderAdminTabs($activeUrl = '')
$serverConfigLabel = '<span class="glyphicon glyphicon-cog" aria-hidden="true"></span>'
.' ETL Server Config';

$tabs = array($adminUrl => $adminLabel, $manageUsersUrl => $manageUsersLabel
, $serversUrl => $serversLabel
, $serverConfigUrl => $serverConfigLabel
);
$tabs = array();

$tabs[$adminUrl] = $adminLabel;
$tabs[$cronJobsUrl] = $cronJobsLabel;
$tabs[$manageUsersUrl] = $manageUsersLabel;
$tabs[$serversUrl] = $serversLabel;
$tabs[$serverConfigUrl] = $serverConfigLabel;

$this->renderTabs($tabs, $activeUrl);
}

Expand Down
3 changes: 3 additions & 0 deletions UserList.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public function jsonSerialize()
return (object) get_object_vars($this);
}

/**
* Gets a sorted list of usernames.
*/
public function getUsers()
{
$users = array_keys($this->userList);
Expand Down
31 changes: 23 additions & 8 deletions admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
$module = new \IU\RedCapEtlModule\RedCapEtlModule();
$selfUrl = $module->getUrl(basename(__FILE__));

$cronInfoUrl = $module->getUrl('cron_jobs.php');

$adminConfig = $module->getAdminConfig();

$submitValue = $_POST['submitValue'];
Expand Down Expand Up @@ -89,19 +91,20 @@
Allow ETL cron jobs? <br />

<p>Allowed ETL cron job times</p>
<table class="cron-schedule">
<table class="cron-schedule admin-cron-schedule">
<thead>
<tr>
<th>&nbsp;</th>
<?php
foreach (AdminConfig::DAY_LABELS as $dayLabel) {
echo '<th style="width: 6em">'.$dayLabel."</th>\n";
echo '<th class="day">'.$dayLabel."</th>\n";
}
?>
</tr>
</thead>
<tbody>
<?php
$cronJobs = $module->getAllCronJobs();
$row = 1;
foreach (range(0, 23) as $time) {
if ($row % 2 === 0) {
Expand All @@ -110,17 +113,25 @@
print '<tr>'."\n";
}
$row++;
$label = $adminConfig->getTimeLabel($time);
$label = $adminConfig->getHtmlTimeLabel($time);
?>
<td><?php echo $label;?></td>
<td class="time-range"><?php echo $label;?></td>

<?php
foreach (range(0, 6) as $day) {
$name = 'times['.$day.']['.$time.']';
echo '<td class="day">'."\n";
$count = count($cronJobs[$day][$time]);

$jobsUrl = $cronInfoUrl.'&selectedDay='.$day.'&selectedTime='.$time;

$checked = '';
if ($adminConfig->isAllowedCronTime($day, $time)) {
echo '<input type="checkbox" name="'.$name.'" checked>';
} else {
echo '<input type="checkbox" name="'.$name.'">';
$checked = ' checked ';
}
echo '<td class="day" style="position: relative;">'."\n";
echo '<input type="checkbox" name="'.$name.'" '.$checked.'>';
if ($count > 0) {
echo '<a href="'.$jobsUrl.'" style="position: absolute; top: 1px; right: 4px;">'.$count.'</a>';
}
echo '</td>'."\n";
}
Expand All @@ -136,4 +147,8 @@
</p>
</form>

<?php
#print "<pre>\n"; print_r($cronJobs); print "</pre>\n";
?>

<?php include APP_PATH_DOCROOT . 'ControlCenter/footer.php'; ?>
Loading

0 comments on commit 1e58a47

Please sign in to comment.