Skip to content

Instantly share code, notes, and snippets.

@kamermans
Created May 6, 2012 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamermans/2624786 to your computer and use it in GitHub Desktop.
Save kamermans/2624786 to your computer and use it in GitHub Desktop.
HTML5 MongoDB process monitor with MapReduce visual progress meter
<?php
$db = new Mongo('mongodb://127.0.0.1');
$ops = $db->ua->selectCollection('$cmd.sys.inprog');
$ops_doc = $ops->findOne();
function duration($secs) {
$vals = array(
'y' => intval($secs / 86400 / 365),
'w' => intval($secs / 86400 / 7) % 52,
'd' => $secs / 86400 % 7,
'h' => $secs / 3600 % 24,
'm' => $secs / 60 % 60,
's' => $secs % 60,
);
$ret = array();
$added = false;
foreach ($vals as $k => $v) {
if ($v > 0 || $added) {
$added = true;
$ret[] = $v . $k;
}
}
return join(' ', $ret);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Mongo Process Monitor</title>
</head>
<body>
<?php
foreach ($ops_doc['inprog'] as $op) {
$msg = isset($op['msg'])? $op['msg']: json_encode($op['query']);
if (isset($op['secs_running'])) {
$started = date('r', time() - $op['secs_running']);
$duration = duration($op['secs_running']);
} else {
$started = 'unknown';
$duration = '-';
}
if (isset($op['progress'])) {
$est = intval(($op['progress']['done'] / $op['secs_running']) * $op['progress']['total']);
$est_dur = duration($est);
$est_finish_dt = new DateTime();
$est_dur_it = new DateInterval('P0Y');
$est_dur_it->s = $est;
$est_finish_dt->add($est_dur_it);
$est_finish = $est_finish_dt->format('r');
}
?>
<div>
<h3>Operation <?php echo $op['opid']; ?></h3>
<strong><?php echo $op['ns']; ?></strong>: <?php echo $msg; ?><br/>
Running since <?php echo $started; ?> (<?php echo $duration; ?> elapsed)<br/>
<?php if (isset($op['progress'])) { ?><progress style="width: 800px" value="<?php echo $op['progress']['done']; ?>" max="<?php echo $op['progress']['total']; ?>"/> <?php } ?>
</div>
<pre><?php echo htmlspecialchars(var_export($op, true)); ?></pre>
<hr/>
<?php } ?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment