public function testLoad() { $files = array(join(DIRECTORY_SEPARATOR, array(DEFAULT_TEMPLATE_FOLDER, 'template1.php')), join(DIRECTORY_SEPARATOR, array(CUSTOM_TEMPLATE_FOLDER, 'template1.php')), join(DIRECTORY_SEPARATOR, array(DEFAULT_TEMPLATE_FOLDER, 'template1.php1')), join(DIRECTORY_SEPARATOR, array(DEFAULT_TEMPLATE_FOLDER, 'template2.simple')), join(DIRECTORY_SEPARATOR, array(DEFAULT_TEMPLATE_FOLDER, 'template2.2simple2')), join(DIRECTORY_SEPARATOR, array(DEFAULT_TEMPLATE_FOLDER, 'template3.simple.foo')), join(DIRECTORY_SEPARATOR, array(CUSTOM_TEMPLATE_FOLDER, 'template4.simple'))); $templateFiles1 = \histou\Folder::loadFolders(array(CUSTOM_TEMPLATE_FOLDER, DEFAULT_TEMPLATE_FOLDER)); $this->assertEquals(array(), $templateFiles1); foreach ($files as $file) { touch($file); } $templateFiles2 = \histou\Folder::loadFolders(array(CUSTOM_TEMPLATE_FOLDER, DEFAULT_TEMPLATE_FOLDER)); $expected = array(join(DIRECTORY_SEPARATOR, array(CUSTOM_TEMPLATE_FOLDER, 'template1.php')), join(DIRECTORY_SEPARATOR, array(CUSTOM_TEMPLATE_FOLDER, 'template4.simple')), join(DIRECTORY_SEPARATOR, array(DEFAULT_TEMPLATE_FOLDER, 'template2.simple'))); $this->assertEquals(sort($expected), sort($templateFiles2)); \histou\Folder::loadFolders(array("adsf")); }
foreach ($templates as $template) { \histou\Debug::add($template); } \histou\Debug::add("Is the first template valid: " . \histou\Debug::printBoolean($valid) . "\n"); \histou\Debug::add("Data: " . print_r($perfData, true)); if ($valid) { $template = $templates[0]; } else { $template = \histou\template\Template::findDefaultTemplate($templates, 'default.php'); } } else { // find the one $template = \histou\template\Template::findDefaultTemplate($templates, \histou\Basic::$specificTemplate); } // load ForecastTemplates $forecastTemplateFiles = \histou\Folder::loadFolders(array(FORECAST_TEMPLATE_FOLDER)); $forcastTemplateCache = new \histou\template\cache('forecast'); $forecastTemplates = $forcastTemplateCache->loadTemplates($forecastTemplateFiles, '\\histou\\template\\loader::loadForecastTemplate'); usort($forecastTemplates, '\\histou\\template\\Template::compare'); $fValid = !empty($forecastTemplates) && $forecastTemplates[0]->isValid(); \histou\Debug::add("ForecastTemplate order:"); foreach ($forecastTemplates as $ftemplate) { \histou\Debug::add($ftemplate); } \histou\Debug::add("Is the first ForecastTemplate valid: " . \histou\Debug::printBoolean($fValid) . "\n"); if ($fValid) { $forecastTemplate = $forecastTemplates[0]; $className = get_class($forecastTemplate); if ($className == 'histou\\template\\Rule') { $forecast = \histou\template\loader::loadForecastTemplate($forecastTemplate->getFileName(), true); }
public function testCreateAndLoad() { $files = array(join(DIRECTORY_SEPARATOR, array(CUSTOM_TEMPLATE_FOLDER, 'template1.php')) => '<?php $rule = new \\histou\\template\\Rule( $host = ".*", $service = ".*", $command = "NONE", $perfLabel = array("rta", "pl") ); $genTemplate = function ($perfData) { return "template1"; };', join(DIRECTORY_SEPARATOR, array(CUSTOM_TEMPLATE_FOLDER, 'template2.php')) => '<?php $rule = new \\histou\\template\\Rule( $host = ".*", $service = ".*", adsfasdfasdf $command = ".*", $perfLabel = array("rta", "pl") ); $genTemplate = function ($perfData) { return "template1"; };', join(DIRECTORY_SEPARATOR, array(DEFAULT_TEMPLATE_FOLDER, 'template3.simple')) => '#simple file host = * service = * command = * perfLabel = load1, load5, load15 #Copy the grafana dashboard below: { "hallo":"world", }'); foreach ($files as $file => $content) { file_put_contents($file, $content); } $templateFiles = \histou\Folder::loadFolders(array(CUSTOM_TEMPLATE_FOLDER, DEFAULT_TEMPLATE_FOLDER)); $templateCache = new \histou\template\cache('test'); $templates = $templateCache->loadTemplates($templateFiles, '\\histou\\template\\loader::loadTemplate'); $this->assertSame(2, sizeof($templates)); $this->assertSame(true, file_exists(join(DIRECTORY_SEPARATOR, array(TMP_FOLDER, '.histou_cache-test')))); $this->assertInstanceOf('\\histou\\template\\Template', $templates[0]); $this->assertInstanceOf('\\histou\\template\\SimpleTemplate', $templates[1]); //load from cachefile $templateCache2 = new \histou\template\cache(); $templates2 = $templateCache2->loadTemplates($templateFiles, '\\histou\\template\\loader::loadTemplate'); //TODO: nächste Zeile geht nicht bei isolation, dafür müssen Constanten weg return $this->assertSame(2, sizeof($templates2)); $this->assertInstanceOf('\\histou\\template\\Rule', $templates2[0]); $this->assertInstanceOf('\\histou\\template\\Rule', $templates2[1]); $this->assertEquals($templates[0]->getRule(), $templates2[0]); $this->assertEquals($templates[1]->getRule(), $templates2[1]); sleep(1); //make template invalid file_put_contents(join(DIRECTORY_SEPARATOR, array(CUSTOM_TEMPLATE_FOLDER, 'template1.php')), "<?php\n foo"); //just one template loaded $templateCache3 = new \histou\template\cache(); $templates3 = $templateCache3->loadTemplates($templateFiles, '\\histou\\template\\loader::loadTemplate'); $this->assertSame(1, sizeof($templates3)); //restore valid template file_put_contents(join(DIRECTORY_SEPARATOR, array(CUSTOM_TEMPLATE_FOLDER, 'template1.php')), $files[join(DIRECTORY_SEPARATOR, array(CUSTOM_TEMPLATE_FOLDER, 'template1.php'))]); //one template from file loaded one from cache $templateCache4 = new \histou\template\cache(); $templates4 = $templateCache4->loadTemplates($templateFiles); $this->assertSame(2, sizeof($templates4)); $this->assertInstanceOf('\\histou\\template\\Template', $templates4[0]); $this->assertInstanceOf('\\histou\\template\\Rule', $templates4[1]); //second again stored $templateCache5 = new \histou\template\cache(); $templates5 = $templateCache5->loadTemplates($templateFiles); $this->assertSame(2, sizeof($templates5)); $this->assertInstanceOf('\\histou\\template\\Rule', $templates5[0]); $this->assertInstanceOf('\\histou\\template\\Rule', $templates5[1]); }
public function testForecastLoad() { $files = array(join(DIRECTORY_SEPARATOR, array(FORECAST_TEMPLATE_FOLDER, 'template1.php')) => '<?php $rule = new \\histou\\template\\Rule( $host = ".*", $service = ".*", $command = "NONE", $perfLabel = array("rta", "pl") ); $forecast = <<<EOF [ { "label":"size", "method":"SimpleLinearRegression", "methodSpecificOptions":{ }, "lookback_range":"30m", "forecast_range":"10m", "forecast_interval":"1m", "update_rate":"1m" }, { "label":"time", "method":"SimpleLinearRegression", "methodSpecificOptions":{ }, "lookback_range":"30m", "forecast_range":"10m", "forecast_interval":"1m", "update_rate":"1m" } ] EOF; ', join(DIRECTORY_SEPARATOR, array(FORECAST_TEMPLATE_FOLDER, 'template2.php')) => '<?php $rule = new \\histou\\template\\Rule( $host = ".*", $service = ".*", adsfasdfasdf $command = ".*", $perfLabel = array("rta", "pl") ); $forecast = <<<EOF [ { "label":"size", "method":"SimpleLinearRegression", "methodSpecificOptions":{ }, "lookback_range":"30m", "forecast_range":"10m", "forecast_interval":"1m", "update_rate":"1m" }, { "label":"time", "method":"SimpleLinearRegression", "methodSpecificOptions":{ }, "lookback_range":"30m", "forecast_range":"10m", "forecast_interval":"1m", "update_rate":"1m" } ] EOF; ', join(DIRECTORY_SEPARATOR, array(FORECAST_TEMPLATE_FOLDER, 'template3.php')) => '<?php $rule = new \\histou\\template\\Rule( $host = ".*", $service = ".*", $command = ".*", $perfLabel = array("rta", "pl") ); $forecast = <<<EOF [ { "label":"size", "method":"SimpleLinearRegression", "methodSpecificOptions":{ }, "lookback_range":"30m", "forecast_range":"20m", "forecast_interval":"1m", "update_rate":"1m" }, { "label":"time", "method":"SimpleLinearRegression", "methodSpecificOptions":{ }, "lookback_range":"30m", "forecast_range":"30m", "forecast_interval":"1m", "update_rate":"1m" } ] EOF; '); foreach ($files as $file => $content) { file_put_contents($file, $content); } $templateFiles = \histou\Folder::loadFolders(array(FORECAST_TEMPLATE_FOLDER)); $expected = array(join(DIRECTORY_SEPARATOR, array(FORECAST_TEMPLATE_FOLDER, 'template1.php')), join(DIRECTORY_SEPARATOR, array(FORECAST_TEMPLATE_FOLDER, 'template2.php')), join(DIRECTORY_SEPARATOR, array(FORECAST_TEMPLATE_FOLDER, 'template3.php'))); $this->assertEquals(sort($expected), sort($templateFiles)); $templates = array(); foreach ($files as $file => $content) { array_push($templates, \histou\template\Loader::loadForecastTemplate($file)); } $this->assertInstanceOf('\\histou\\template\\ForecastTemplate', $templates[0]); $this->assertInstanceOf('\\histou\\template\\ForecastTemplate', $templates[2]); $this->assertSame(null, $templates[1]); //Syntaxcheck failed $this->assertContains('Parse error: syntax error', \histou\Debug::getLogAsMarkdown()); $this->assertSame(FORECAST_TEMPLATE_FOLDER, $templates[0]->getPath()); $this->assertSame('template1.php', $templates[0]->getBaseName()); $this->assertSame('template1', $templates[0]->getSimpleFileName()); $this->assertSame("File:\t{$expected['0']}:\nRule:\n\tFile:\t{$expected['0']}:\n\t\tHost: ;.*;\n\t\tService: ;.*;\n\t\tCommand: ;^\$;\n\t\tPerflabel: ;pl;, ;rta;", $templates[0]->__toString()); $rule = $templates[0]->getRule(); $this->assertSame('template1.php', $rule->getBaseName()); $this->assertSame(join(DIRECTORY_SEPARATOR, array(FORECAST_TEMPLATE_FOLDER, 'template1.php')), $rule->getFileName()); $expected = '[ { "label":"size", "method":"SimpleLinearRegression", "methodSpecificOptions":{ }, "lookback_range":"30m", "forecast_range":"10m", "forecast_interval":"1m", "update_rate":"1m" }, { "label":"time", "method":"SimpleLinearRegression", "methodSpecificOptions":{ }, "lookback_range":"30m", "forecast_range":"10m", "forecast_interval":"1m", "update_rate":"1m" } ]'; $this->assertSame($expected, $templates[0]->getJSON()); $this->assertSame(null, \histou\template\ForecastTemplate::$config); $templates[0]->setForecastDurations(); $this->assertSame(array('size' => array('method' => 'SimpleLinearRegression', 'forecast' => '10m'), 'time' => array('method' => 'SimpleLinearRegression', 'forecast' => '10m')), \histou\template\ForecastTemplate::$config); $templates[2]->setForecastDurations(); $this->assertSame(array('size' => array('method' => 'SimpleLinearRegression', 'forecast' => '20m'), 'time' => array('method' => 'SimpleLinearRegression', 'forecast' => '30m')), \histou\template\ForecastTemplate::$config); $default = \histou\template\template::findDefaultTemplate($templates, "template1.php"); $this->assertSame($templates[0], $default); $default2 = \histou\template\template::findDefaultTemplate($templates, "foo.php"); $this->assertSame(null, $default2); }