Skip to content

Commit

Permalink
match all occurences of pattern in pattern analyser
Browse files Browse the repository at this point in the history
  • Loading branch information
matthi4s committed Feb 20, 2021
1 parent 1673e41 commit b882ca0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
29 changes: 18 additions & 11 deletions src/Analyser/PatternAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,11 @@ public function analyse()
/** @var PatternInsightInterface $possibleInsightClass */
$patterns = $possibleInsightClass::getPatterns();
foreach ($patterns as $patternKey => $pattern) {
$insight = $this->analyseEntry($entry, $possibleInsightClass, $patternKey, $pattern);
if ($insight) {
$analysis->addInsight($insight);
$insights = $this->analyseEntry($entry, $possibleInsightClass, $patternKey, $pattern);
if ($insights) {
foreach ($insights as $insight) {
$analysis->addInsight($insight);
}
}
}
}
Expand All @@ -131,20 +133,25 @@ public function analyse()
* @param string $possibleInsightClass
* @param $patternKey
* @param string $pattern
* @return bool|PatternInsightInterface
* @return bool|PatternInsightInterface[]
*/
protected function analyseEntry(EntryInterface $entry, string $possibleInsightClass, $patternKey, string $pattern)
{
$result = preg_match($pattern, $entry, $matches);
if ($result !== 1) {
$result = preg_match_all($pattern, $entry, $matches, PREG_SET_ORDER);
if ($result === false || $result === 0) {
return false;
}

/** @var PatternInsightInterface $insight */
$insight = new $possibleInsightClass();
$insight->setMatches($matches, $patternKey);
$insight->setEntry($entry);
$return = [];
foreach ($matches as $match) {
/** @var PatternInsightInterface $insight */
$insight = new $possibleInsightClass();
$insight->setMatches($match, $patternKey);
$insight->setEntry($entry);

return $insight;
$return[] = $insight;
}

return $return;
}
}
4 changes: 3 additions & 1 deletion test/data/problem.log
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
[01.01.1970 00:00:03] [Log/INFO] This is a message without any problem
[01.01.1970 00:00:04] [Log/ERROR] I have a problem with XYZ
[01.01.1970 00:00:05] [Log/ERROR] I have a problem with ABC
[01.01.1970 00:00:06] [Log/INFO] This log was generated by software v1.2.3
[01.01.1970 00:00:06] [Log/ERROR] I have a problem with DEF
I have a problem with GHI
[01.01.1970 00:00:07] [Log/INFO] This log was generated by software v1.2.3
18 changes: 16 additions & 2 deletions test/tests/Analyser/PatternAnalyserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,24 @@ protected function getExpectedAnalysis()
->addLine((new Line())->setNumber(4)->setText("[01.01.1970 00:00:04] [Log/ERROR] I have a problem with XYZ"))
)
)
->addInsight((new TestPatternProblem())
->setCause("DEF")
->setEntry((new Entry())->setTime(6)->setLevel("ERROR")
->addLine((new Line())->setNumber(6)->setText("[01.01.1970 00:00:06] [Log/ERROR] I have a problem with DEF"))
->addLine((new Line())->setNumber(7)->setText("I have a problem with GHI"))
)
)
->addInsight((new TestPatternProblem())
->setCause("GHI")
->setEntry((new Entry())->setTime(6)->setLevel("ERROR")
->addLine((new Line())->setNumber(6)->setText("[01.01.1970 00:00:06] [Log/ERROR] I have a problem with DEF"))
->addLine((new Line())->setNumber(7)->setText("I have a problem with GHI"))
)
)
->addInsight((new TestPatternInformation())
->setValue("v1.2.3")
->setEntry((new Entry())->setTime(6)->setLevel("INFO")
->addLine((new Line())->setNumber(6)->setText("[01.01.1970 00:00:06] [Log/INFO] This log was generated by software v1.2.3"))
->setEntry((new Entry())->setTime(7)->setLevel("INFO")
->addLine((new Line())->setNumber(8)->setText("[01.01.1970 00:00:07] [Log/INFO] This log was generated by software v1.2.3"))
)
);

Expand Down

0 comments on commit b882ca0

Please sign in to comment.