public function test_StoreAnomaly_RetrieveGroupedAndSortedByNofAnomalies() { /* This tries to test something that's a bit difficult to test. * The anomalies should be retrieved in sorted order from highest number of anomalies to lowest, * and contain all the anomalies for each group. * * We don't test limit and minCount here */ $start = new DateTime('-5 minutes'); $end = new DateTime('now'); // Group 1 $this->conn->col('anomalies')->insert(['nid' => '1', 'sid' => '1', 'predicted' => 30, 'actual' => 70, 'mongodate' => new MongoDate($start->getTimestamp())]); $this->conn->col('anomalies')->insert(['nid' => '1', 'sid' => '1', 'predicted' => 40, 'actual' => 80, 'mongodate' => new MongoDate($end->getTimestamp())]); $this->conn->col('anomalies')->insert(['nid' => '1', 'sid' => '1', 'predicted' => 45, 'actual' => 85, 'mongodate' => new MongoDate($end->getTimestamp())]); // Group 2 $this->conn->col('anomalies')->insert(['nid' => '2', 'sid' => '1', 'predicted' => 50, 'actual' => 90, 'mongodate' => new MongoDate($start->getTimestamp())]); // Group 3 $this->conn->col('anomalies')->insert(['nid' => '2', 'sid' => '2', 'predicted' => 60, 'actual' => 100, 'mongodate' => new MongoDate($start->getTimestamp())]); $this->conn->col('anomalies')->insert(['nid' => '2', 'sid' => '2', 'predicted' => 60, 'actual' => 100, 'mongodate' => new MongoDate($end->getTimestamp())]); $pipeline = new Pipeline(); $pipelineFactory = new Factory($this->conn); $sequence = $pipelineFactory->createAnomalyAction($start, $end, ['1', '2'], ['1', '2'], 1, 3); $res = $pipeline->run($sequence); $this->assertEquals(3, count($res), 'Expected number of groups were wrong'); $this->assertEquals(3, count($res[0]['anomalies']), 'Expected number of anomalies in grp1 were wrong'); $this->assertEquals(2, count($res[1]['anomalies']), 'Expected number of anomalies in grp3 were wrong'); $this->assertEquals(1, count($res[2]['anomalies']), 'Expected number of anomalies in grp2 were wrong'); }
$nodeResolution = resolutionStrToEnum($_GET['nodeResolution']); } if (isset($_GET['formulaResolution'])) { $formulaResolution = resolutionStrToEnum($_GET['formulaResolution']); } $flotData = array(); $start = new DateTime($start); $end = new DateTime($end); DateTimeHelper::normalizeTimeRange($start, $end, $resolution); foreach ($sids as $sid) { $sequence = $pipelineFactory->createMultiAction($sid, $nids, $start, $end, $resolution, $aggregation, false, $nodeResolution, $singleNodeAggregation, $combinedNodeAggregation, $asFormula, $formulaResolution); $sequence[] = new ConvertToFlotFormat(); $data = $pipeline->run($sequence); $flotData[] = array('data' => $data, 'label' => 'Sensor ' . $sid, 'xaxis' => 1, 'yaxis' => 1); } $sequence = $pipelineFactory->createAnomalyAction($start, $end, $nids, $sids, 0, count($nids) * count($nids)); $sequence[] = new AddAnomalyState($start, $end, $resolution); $res = $pipeline->run($sequence); $anomalyStates = []; $anomalies = array(); foreach ($res as $row) { $anomalies = array_merge($anomalies, $row['anomalies']); foreach ($row['state'] as $timestamp => $state) { $anomalyStates[$timestamp] = $state; } } $data = array_map(function ($timestamp, $value) { return array($timestamp * 1000, $value); }, array_keys($anomalyStates), $anomalyStates); $flotData[] = array('data' => $data, 'label' => false, 'xaxis' => 2, 'yaxis' => 2, 'state' => array('show' => true, 'showText' => false, 'label' => '', 'states' => array(0 => "rgba(0,255,0,1)", 1 => "rgba(255,0,0,1)"))); $min = $flotData[0]['data'][0][0];
echo 'Progress: ' . $iteration . '/' . count($series) . "\r"; $iteration++; $expectedAnomaly = false; if (rand() % 1000 == 0) { $persist = $config['anomalyPersistIntervals']; } if ($persist > 0) { $persist--; $expectedAnomaly = true; $totalAnomalies++; $data['value'] *= 2; } $storage->store([new \Mongotd\CounterValue(1, 1, $data['datetime'], $data['value'])]); $datetimeStartInterval = clone $data['datetime']; $datetimeStartInterval->sub(DateInterval::createFromDateString($config['insertIntervalInSeconds'] - 1 . ' seconds')); $sequence = $pipelineFactory->createAnomalyAction($datetimeStartInterval, $data['datetime']); $anomalyList = $pipeline->run($sequence); $isAnomaly = count($anomalyList) > 0 and count($anomalyList[0]['anomalies']) > 0; if ($isAnomaly or $expectedAnomaly) { echo $data['datetime']->format('Y-m-d H:i') . ' - ' . $data['value']; if ($isAnomaly) { echo ' Detected anomaly! (predicted avg ' . $anomalyList[0]['anomalies'][0]->predicted . ')'; } if ($expectedAnomaly) { echo ' Actual anomaly!'; } if ($isAnomaly and $expectedAnomaly) { $hits++; } else { if ($isAnomaly) { $falsesPositives++;