Example #1
0
    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]);
    }
Example #2
0
        \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);
    }
    if (isset($forecast)) {