<commit_before><?php
class NagfView {
	protected $data;

	public function __construct(stdClass $data) {
		$this->data = $data;
	}

	/**
	 * @return string HTML
	 */
	public function getProjectMenu() {
		$data = $this->data;
		$projects = Graphite::getProjects();
		return '<select name="project" required class="form-control nagf-select-project">'
			. '<option value="" disabled>Select project</option>'
			. implode('', array_map(function ($project) use ($data) {
				return '<option'
					. ($project === $data->project ? ' selected' : '')
					. '>' . htmlspecialchars($project) . '</option>';
			}, $projects))
			. '</select>';
	}

	/**
	 * @return string HTML
	 */
	protected function getHostMenu() {
		$project = $this->data->project;
		$hosts = $this->data->hosts;
		$graphConfigs = $this->data->hostGraphsConfig;

		$html = '<select required class="form-control nagf-select-metric">'
			. '<option value="" disabled>Select metric</option>';
		foreach ($hosts as $host) {
			$html .= '<option value="h-' . htmlspecialchars($host) . '">' .  htmlspecialchars($host) . '</option>';
			foreach ($graphConfigs as $graphID => &$graph) {
				$html .= '<option value="h-' . htmlspecialchars("$host-$graphID") . '">'
					. htmlspecialchars("$host: {$graph['title']}")
					. '</option>';
			}
		}
		$html .= '</select>';
		return $html;
	}

	/**
	 * @return string HTML
	 */
	public function getHostForm() {
		if (!$this->data->project) {
			return '';
		}
		return '<form class="navbar-form navbar-right only-js" role="form">'
			. '<div class="form-group">'
			. $this->getHostMenu(
				$this->data->project,
				$this->data->hosts,
				$this->data->hostGraphsConfig
			)
			. '</div>'
			. '</form>';
	}

	/**
	 * @return string HTML
	 */
	public function getPage() {
		if ($this->data->project) {
			return $this->getProjectPage(
				$this->data->project,
				$this->data->hosts,
				$this->data->hostGraphsConfig
			);
		}
		return $this->getHomePage();
	}

	/**
	 * @param string $project
	 * @param Array $hosts
	 * @return string HTML
	 */
	protected function getProjectPage($project, Array $hosts, Array $graphConfigs) {
		$html = '<h1>' . htmlspecialchars($project) . '</h1>';

		array_unshift($hosts, 'overview');

		$sections = array();
		foreach ($hosts as $hostName) {
			$host = $hostName === 'overview' ? '*' : $hostName;
			$html .= '<h3 id="h-' . htmlspecialchars($host) . '">' . htmlspecialchars($hostName) . '</h3>';
			foreach ($graphConfigs as $graphID => &$graph) {
				$html .= '<h4 id="h-' . htmlspecialchars("$host-$graphID") . '">'
					. htmlspecialchars("$hostName: {$graph['title']}")
					. '</h4>';
				$targetQuery = '';

				if ($hostName !== 'overview') {
					$targets = $graph['targets'];
				} elseif (isset($graph['overview'])) {
					$targets = $graph['overview'];
				} else {
					// Default overview: sum() the source values
					$targets = array_map(function ($target) {
						return preg_replace('/HOST([^\)]+)/', 'sum(HOST$1)', $target);
					}, $graph['targets']);
				}

				foreach ($targets as $target) {
					$targetQuery .= '&target=' . urlencode(str_replace('HOST', "$project.$host", $target));
				}

				$html .= '<img width="800" height="250" src="//graphite.wmflabs.org/render/?'
					. htmlspecialchars(http_build_query(array(
						'title' => $graph['title'] . ' last day',
						'width' => 800,<commit_msg>NagfView: Avoid conflicts between metric ids and hostnames<commit_after><?php
class NagfView {
	protected $data;

	public function __construct(stdClass $data) {
		$this->data = $data;
	}

	/**
	 * @return string HTML
	 */
	public function getProjectMenu() {
		$data = $this->data;
		$projects = Graphite::getProjects();
		return '<select name="project" required class="form-control nagf-select-project">'
			. '<option value="" disabled>Select project</option>'
			. implode('', array_map(function ($project) use ($data) {
				return '<option'
					. ($project === $data->project ? ' selected' : '')
					. '>' . htmlspecialchars($project) . '</option>';
			}, $projects))
			. '</select>';
	}

	/**
	 * @return string HTML
	 */
	protected function getHostMenu() {
		$project = $this->data->project;
		$hosts = $this->data->hosts;
		$graphConfigs = $this->data->hostGraphsConfig;

		$html = '<select required class="form-control nagf-select-metric">'
			. '<option value="" disabled>Select metric</option>';
		foreach ($hosts as $host) {
			$html .= '<option value="h_' . htmlspecialchars($host) . '">' .  htmlspecialchars($host) . '</option>';
			foreach ($graphConfigs as $graphID => &$graph) {
				$html .= '<option value="h_' . htmlspecialchars("{$host}_{$graphID}") . '">'
					. htmlspecialchars("$host: {$graph['title']}")
					. '</option>';
			}
		}
		$html .= '</select>';
		return $html;
	}

	/**
	 * @return string HTML
	 */
	public function getHostForm() {
		if (!$this->data->project) {
			return '';
		}
		return '<form class="navbar-form navbar-right only-js" role="form">'
			. '<div class="form-group">'
			. $this->getHostMenu(
				$this->data->project,
				$this->data->hosts,
				$this->data->hostGraphsConfig
			)
			. '</div>'
			. '</form>';
	}

	/**
	 * @return string HTML
	 */
	public function getPage() {
		if ($this->data->project) {
			return $this->getProjectPage(
				$this->data->project,
				$this->data->hosts,
				$this->data->hostGraphsConfig
			);
		}
		return $this->getHomePage();
	}

	/**
	 * @param string $project
	 * @param Array $hosts
	 * @return string HTML
	 */
	protected function getProjectPage($project, Array $hosts, Array $graphConfigs) {
		$html = '<h1>' . htmlspecialchars($project) . '</h1>';

		array_unshift($hosts, 'overview');

		$sections = array();
		foreach ($hosts as $hostName) {
			$host = $hostName === 'overview' ? '*' : $hostName;
			$html .= '<h3 id="h_' . htmlspecialchars($host) . '">' . htmlspecialchars($hostName) . '</h3>';
			foreach ($graphConfigs as $graphID => &$graph) {
				$html .= '<h4 id="h_' . htmlspecialchars("{$host}_{$graphID}") . '">'
					. htmlspecialchars("$hostName: {$graph['title']}")
					. '</h4>';
				$targetQuery = '';

				if ($hostName !== 'overview') {
					$targets = $graph['targets'];
				} elseif (isset($graph['overview'])) {
					$targets = $graph['overview'];
				} else {
					// Default overview: sum() the source values
					$targets = array_map(function ($target) {
						return preg_replace('/HOST([^\)]+)/', 'sum(HOST$1)', $target);
					}, $graph['targets']);
				}

				foreach ($targets as $target) {
					$targetQuery .= '&target=' . urlencode(str_replace('HOST', "$project.$hostTarget", $target));
				}

				$html .= '<img width="800" height="250" src="//graphite.wmflabs.org/render/?'
					. htmlspecialchars(http_build_query(array(
						'title' => $graph['title'] . ' last day',
						'width' => 800,
{'fraction_non_alphanumeric': 0.21419107905196527, 'fraction_numerical': 0.00471073163550714, 'mean_word_length': 3.355128205128205, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 115, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1497688', 'n_tokens_mistral': 2668, 'n_tokens_neox': 2211, 'n_words': 458}