-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbatch.php
173 lines (164 loc) · 7.16 KB
/
batch.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
namespace Vanderbilt\FlightTrackerExternalModule;
use \Vanderbilt\CareerDevLibrary\CronManager;
use \Vanderbilt\CareerDevLibrary\Download;
use \Vanderbilt\CareerDevLibrary\Application;
use \Vanderbilt\CareerDevLibrary\REDCapManagement;
use \Vanderbilt\CareerDevLibrary\Links;
use \Vanderbilt\CareerDevLibrary\Sanitizer;
require_once(dirname(__FILE__)."/classes/Autoload.php");
require_once(dirname(__FILE__)."/small_base.php");
$headers = [
"Project Name",
"method",
"records",
"status",
"enqueueTs",
"firstParameter",
"startTs",
"endTs",
];
$queues = [
"",
FlightTrackerExternalModule::LOCAL_BATCH_SUFFIX,
FlightTrackerExternalModule::INTENSE_BATCH_SUFFIX,
FlightTrackerExternalModule::LONG_RUNNING_BATCH_SUFFIX,
];
if (isset($_POST['setting']) && isset($_POST['count'])) {
$setting = Sanitizer::sanitize($_POST['setting']);
$count = Sanitizer::sanitizeInteger($_POST['count']) ?: "Unknown";
$titles = [];
echo makeJobRowHTML($setting, $headers, $count, $titles);
exit;
}
require_once(dirname(__FILE__)."/charts/baseWeb.php");
echo "<style>
th { position: sticky; top: 0; background-color: #8dc63f; }
</style>";
echo "<h1>Batch Queues</h1>";
$datetime = date("m-d-Y H:i:s");
$queueLinks = [];
foreach ($queues as $suffix) {
$mgr = new CronManager($token, $server, $pid, Application::getModule(), $suffix);
$count = count($mgr->getBatchQueue());
$suffixTitle = CronManager::getTitle($suffix);
$id = $suffix;
if (!$id) {
$id = "main";
}
$queueLinks[] = "<a href='#$id' class='orange roundedBorder smallShadow small nounderline nobreak' style='margin-right: 8px; margin-left: 8px; padding: 4px 6px;'>".ucfirst($suffixTitle)." Queue ($count)</a>";
}
echo "<p class='centered max-width'>This page shows the current state of Flight Tracker's batch queues. These jobs are set up daily, usually at midnight, and run until the queue has completed.<br/><strong>Last Updated</strong>: $datetime</p>";
echo "<p class='centered max-width'>".implode("", $queueLinks)."</p>";
foreach ($queues as $suffix) {
$mgr = new CronManager($token, $server, $pid, Application::getModule(), $suffix);
$queue = $mgr->getBatchQueue();
$suffixTitle = CronManager::getTitle($suffix);
$id = $suffix;
if (!$id) {
$id = "main";
}
echo "<h2 id='$id'>$suffixTitle Queue (".count($queue)." Jobs)</h2>";
$restrictionStart = CronManager::getRestrictedTime($suffix, "start");
$restrictionEnd = CronManager::getRestrictedTime($suffix, "end");
if (
(
($restrictionStart != CronManager::UNRESTRICTED_START)
|| ($restrictionEnd != CronManager::UNRESTRICTED_START)
)
&& !Application::isLocalhost()
) {
echo "<p class='centered'>Processing does not occur on this queue from $restrictionStart until $restrictionEnd on weekdays.</p>";
}
echo "<table class='centered bordered' style='width: 100%;'>";
echo "<thead>";
echo "<tr>";
echo "<th>Pos</th>";
foreach ($headers as $header) {
if ($header == "firstParameter") {
echo "<th>First Parameter</th>";
} else if (preg_match("/Ts/", $header)) {
$header = str_replace("Ts", " Time", $header);
echo "<th>".ucfirst($header)."</th>";
} else {
echo "<th>".ucfirst($header)."</th>";
}
}
echo "</tr>";
echo "</thead>";
echo "<tbody>";
$thisUrl = Application::link("this");
$numCols = count($headers);
if (empty($queue)) {
$myNumCols = $numCols + 1;
echo "<tr><td colspan='$myNumCols' class='centered padded even'>Currently, this job queue is empty. Typically, this fills up overnight before each job is run.</td></tr>";
}
$titles = [];
for ($i = 0; $i < count($queue); $i++) {
$count = $i + 1;
$setting = $queue[$i];
$rowClass = ($count % 2 === 0) ? "even" : "odd";
if ($count <= 2) {
echo "<tr class='$rowClass'>".makeJobRowHTML($setting, $headers, $count, $titles)."</tr>";
} else {
echo "<tr class='$rowClass'><td>$count</td><td colspan='$numCols' class='centered padded'><a href='javascript:;' onclick='fetchSetting(\"$thisUrl\", \"$setting\", this, $count); return false;'>Fetch Row</a></td></tr>";
}
}
echo "</tbody>";
echo "</table>";
echo "<br/><br/><br/>";
}
function makeJobRowHTML($setting, $headers, $count, &$cachedTitles) {
$row = Application::getSystemSetting($setting) ?: [];
if (empty($row)) {
return "";
}
$html = "<td>$count</td>";
foreach ($headers as $header) {
if ($header == "Project Name") {
if ($row['token'] && $row['server']) {
$title = $cachedTitles[$row['pid']] ?? Download::projectTitle($row['pid']);
$cachedTitles[$row['pid']] = $title;
$title = str_replace("Flight Tracker - ", "", $title);
$title = str_replace(" - Flight Tracker", "", $title);
$title = preg_replace("/\s*Flight Tracker\s*/", "", $title);
if (isset($row['pid']) && $row['pid']) {
$link = Links::makeProjectHomeLink($row['pid'], $title);
$html .= "<td style='max-width: 200px;'>$link <span class='smaller nobreak'>[pid " . $row['pid'] . "]</span></td>";
} else {
$html .= "<td style='max-width: 200px;'>$title</td>";
}
} else {
$html .= "<td></td>";
}
} else if (isset($row[$header]) && ($row[$header] !== FALSE)) {
if ($header == "method") {
$shortFilename = basename($row["file"]);
$html .= "<td>{$row['method']}<br/><span class='smaller'>$shortFilename</span></td>";
} else if (preg_match("/Ts$/", $header) && is_integer($row[$header])) {
$html .= "<td>" . date("Y-m-d H:i:s", $row[$header]) . "</td>";
} else if (is_array($row[$header]) && REDCapManagement::isAssoc($row[$header])) {
$values = [];
foreach ($row[$header] as $key => $value) {
if (is_array($value)) {
$values[] = "<strong>$key</strong>: ".implode(", ", $value);
} else {
$values[] = "<strong>$key</strong>: $value";
}
}
$html .= "<td style='max-width: 200px;'><div class='scrollable'>" . implode("<br/>", $values) . "</div></td>";
} else if (is_array($row[$header]) && !REDCapManagement::isAssoc($row[$header])) {
$html .= "<td style='max-width: 200px;'><div class='scrollable'>" . implode(", ", $row[$header]) . "</div></td>";
} else {
$html .= "<td>" . $row[$header] . "</td>";
}
} else if (($header == "records") && isset($row['pids'])) {
$html .= "<td class='bolded'>" . count($row['pids']) . " pids</td>";
} else if (is_array($row[$header])) {
$html .= "<td style='max-width: 200px;'><div class='scrollable'>" . implode(", ", $row[$header]) . "</div></td>";
} else {
$html .= "<td></td>";
}
}
return $html;
}