Skip to content

Commit

Permalink
add queue handling script for "analysis queue" issue #61
Browse files Browse the repository at this point in the history
  • Loading branch information
osm-ToniE committed Nov 25, 2024
1 parent f6034e6 commit 950f3b1
Show file tree
Hide file tree
Showing 2 changed files with 230 additions and 0 deletions.
84 changes: 84 additions & 0 deletions results/queue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<!DOCTYPE html>
<?php include( '../script/globals.php' );
include( '../script/parse_query.php' );
include( '../script/queue.php' );
$lang_dir="../$ptna_lang/";
?>
<html lang="<?php echo $html_lang ?>">

<?php $title='Analysis Queue'; include $lang_dir.'html-head.inc'; ?>

<body>

<div id="wrapper">

<?php include $lang_dir.'header.inc'; ?>

<main id="main" class="results">

<?php $show_contents = 1;
echo "<!-- \$_SERVER = \n";
print_r( $_SERVER );
echo " -->\n";
if ( isset($_SERVER['HTTP_REFERER']) ) {
preg_match( '/\/results\/.*\/([0-9A-ZÜa-zô_.-]+)-Analysis\.[dif.]*.*html$/', $_SERVER['HTTP_REFERER'], $matches );
if ( isset($matches[1]) ) {
$network = $matches[1];
$ret_val = InsertIntoAnalysisQueue( $network );
http_response_code( $ret_val['HTTP'] );
echo "<!-- \$ret_val = \n";
print_r( $ret_val );
echo " -->\n";
if ( $ret_val['HTTP'] >= 200 && $ret_val['HTTP'] < 300 ) {
echo ' <h2 id="request">Your analysis reqeuest for ' . $network . " has been accepted</h2>\n";
} else {
echo ' <h2 id="request">Your analysis reqeuest for ' . $network . " failed</h2>\n";
echo ' <div class="indent">' . "\n";
echo ' <p>' . $ret_val['status'] . "</p>\n";
echo " </div>\n";
echo " <hr />\n";
}
} else {
preg_match( '/\/(statistics.php)$/', $_SERVER['HTTP_REFERER'], $matches );
if ( !isset($matches[1]) ) {
$show_contents = 0;
http_response_code( 404 );
echo ' <h3 id="request">Invalid analysis reqeuest: unknown ' . "'network'" . "</h3>\n";
echo " <hr />\n";
}
}
}
?>

<?php if ( $show_contents ): ?>
<h2 id="queue">Contents of analysis queue</h2>
<div class="indent">
<table id="queue-table">
<thead>
<tr class="statistics-tableheaderrow">
<th class="statistics-name">Network</th>
<th class="statistics-name">State</th>
<th class="statistics-date">Queue At</th>
<th class="statistics-date">Started At</th>
<th class="statistics-date">Finished At</th>
<th class="statistics-size">Changes</th>
<th class="statistics-date">Logs</th>
</tr>
</thead>
<tbody>
<?php PrintAnalysisQueue(); ?>
</tbody>
</table>

</div>
<?php endif; ?>

</main> <!-- main -->

<hr />

<?php include $lang_dir.'footer.inc' ?>

</div> <!-- wrapper -->
</body>
</html>
146 changes: 146 additions & 0 deletions script/queue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php

function FindAnalysisQueueSqliteDb() {
global $path_to_work;

$return_path = $path_to_work . 'ptna-analysis-queue-sqlite.db';

if ( file_exists($return_path) && filesize($return_path) ) {
return $return_path;
} else {
return '';
}
}


function InsertIntoAnalysisQueue( $network ) {

$return_array = array( 'HTTP' => 404, 'status' => "Not Found", 'queued' => '', 'started' => '', 'ip' => '' );

if ( $network && preg_match("/^[0-9A-ZÜa-zô_.-]+$/",$network) ) {

$SqliteDb = FindAnalysisQueueSqliteDb();

if ( $SqliteDb != '' ) {

try {

$db = new SQLite3( $SqliteDb );

$sql = "BEGIN TRANSACTION";
$result = $db->exec( $sql );

$sql = "SELECT COUNT(*) AS num FROM queue WHERE status='queued';";
$result = $db->querySingle( $sql, true );
if ( $result['num'] < 10 ) {
$sql = sprintf( "SELECT COUNT(*) AS num FROM queue WHERE network='%s' AND status='queued';", $network );
$result = $db->querySingle( $sql, true );
if ( $result['num'] == 0 ) {
$sql = sprintf( "SELECT COUNT(*) AS num FROM queue WHERE network='%s' AND status='started';", $network );
$result = $db->querySingle( $sql, true );
if ( $result['num'] == 0 ) {
$when = time();
if ( isset($_SERVER['REMOTE_ADDR']) ) {
$ip = $_SERVER['REMOTE_ADDR'];
} else {
$ip = 'unknown';
}
$sql = sprintf( "INSERT INTO queue (network,status,queued,ip) VALUES ('%s','%s',%d,'%s');", $network, 'queued', $when, $ip );
$db->querySingle( $sql, true );
$return_array['HTTP'] = 202;
$return_array['status'] = 'In Queue';
$return_array['queued'] = $when;
$return_array['ip'] = $ip;
} else {
$return_array['HTTP'] = 429;
$return_array['status'] = 'Is Already Running';
}
} else {
$return_array['HTTP'] = 429;
$return_array['status'] = 'Already In Queue';
}
} else {
$return_array['HTTP'] = 429;
$return_array['status'] = 'Queue Is Full';
}
$sql = "COMMIT TRANSACTION";
$result = $db->exec( $sql );
$db->close();
} catch ( Exception $ex ) {
$return_array['HTTP'] = 500;
$return_array['status'] = "Server Error '" . $ex . "'";
}
} else {
$return_array['HTTP'] = 404;
$return_array['status'] = 'Queue Not Found';
}
}

return $return_array;
}


function PrintAnalysisQueue() {

$SqliteDb = FindAnalysisQueueSqliteDb();

if ( $SqliteDb != '' ) {

try {

$db = new SQLite3( $SqliteDb );

$sql = "BEGIN TRANSACTION";
$result = $db->exec( $sql );

$sql = "SELECT * FROM queue ORDER BY queued DESC;";
$result = $db->query( $sql );
while ( $queue_infos=$result->fetchArray(SQLITE3_ASSOC) ) {
printf( "<tr class=\"statistics-tablerow\">\n" );
printf( " <td class=\"statistics-name\">%s</td>\n", $queue_infos['network'] );
printf( " <td class=\"statistics-name\">%s</td>\n", $queue_infos['status'] );
if ( $queue_infos['queued'] ) {
printf( " <td class=\"statistics-date\">%s UTC</td>\n", date("Y-m-d H:i:s",$queue_infos['queued']) );
} else {
printf( " <td class=\"statistics-date\">&nbsp;</td>\n" );
}
if ( $queue_infos['started'] ) {
printf( " <td class=\"statistics-date\">%s UTC</td>\n", date("Y-m-d H:i:s",$queue_infos['started']) );
} else {
printf( " <td class=\"statistics-date\">&nbsp;</td>\n" );
}
if ( $queue_infos['stopped'] ) {
printf( " <td class=\"statistics-date\">%s UTC</td>\n", date("Y-m-d H:i:s",$queue_infos['stopped']) );
} else {
printf( " <td class=\"statistics-date\">&nbsp;</td>\n" );
}
if ( $queue_infos['status'] == 'stopped' ) {
printf( " <td class=\"statistics-size\">%d</td>\n", $queue_infos['changes'] );
} else {
printf( " <td class=\"statistics-size\">&nbsp;</td>\n" );
}
if ( $queue_infos['status'] == 'started' || $queue_infos['status'] == 'stopped' ) {
printf( " <td class=\"statistics-name\"><a href=\"/en/showlogs.php?network=%s\" title=\"Log file\">logs</td>\n", $queue_infos['network'] );
} else if ( $queue_infos['status'] == 'locked' ) {
printf( " <td class=\"statistics-name\">Another analysis for this 'network' was already running when this one was ready to be started</td>\n" );
} else {
printf( " <td class=\"statistics-name\">&nbsp;</td>\n" );
}
printf( "</tr>\n" );
}
$sql = "COMMIT TRANSACTION";
$result = $db->exec( $sql );
$db->close();
} catch ( Exception $ex ) {
printf( "<tr class=\"statistics-tablerow\">\n" );
printf( " <td class=\"statistics-name\" colspan=7>Server Error: %s</td>\n", $ex );
printf( "</tr>\n" );
}
} else {
printf( "<tr class=\"statistics-tablerow\">\n" );
printf( " <td class=\"statistics-name\" colspan=7>Queue Not Found</td>\n" );
printf( "</tr>\n" );
}
}

?>

0 comments on commit 950f3b1

Please sign in to comment.