public function metricSumIs($metric, $expectedTotal, $startTime, $endTime)
{
// when are we looking for?
$humanStartTime = date('Y-m-d H:i:s', $startTime);
$humanEndTime = date('Y-m-d H:i:s', $endTime);
// what are we doing?
$log = usingLog()->startAction("ensure metric '{$metric}' sums to '{$expectedTotal}' between '{$humanStartTime}' and '{$humanEndTime}'");
// get the data from graphite
$data = fromGraphite()->getDataFor($metric, $startTime, $endTime);
// do we *have* any data?
if (empty($data) || !isset($data[0]->target, $data[0]->datapoints)) {
// graphite returns an empty data set when there is no data
//
// NOTE: this can also happen when the test is asking for
// the wrong metric :(
if ($expectedTotal !== 0) {
// we were expecting there to be some data
throw new E5xx_ExpectFailed(__METHOD__, "data available for metric '{$metric}'", "no data available for metric '{$metric}'");
}
// if we get here, it's reasonable to assume that everything is
// as it should be
$log->endAction("no data available for metric '{$metric}'; assuming success");
return;
}
// we have data ... let's make sure we're happy with it
$actualTotal = 0;
foreach ($data[0]->datapoints as $datapoint) {
if ($datapoint[0] !== null) {
$actualTotal += $datapoint[0];
}
}
// do we have the total we expected?
assertsDouble($actualTotal)->equals($expectedTotal);
// all done
$log->endAction("data was available, metric '{$metric}' sums to '{$actualTotal}'");
return;
}