/usr/share/php/Icinga/Web/Widget/Chart/HistoryColorGrid.php is in php-icinga 2.1.0-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | <?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
namespace Icinga\Web\Widget\Chart;
use DateInterval;
use DateTime;
use Icinga\Util\Color;
use Icinga\Web\Widget\AbstractWidget;
/**
* Display a colored grid that visualizes a set of values for each day
* on a given time-frame.
*/
class HistoryColorGrid extends AbstractWidget {
const CAL_GROW_INTO_PAST = 'past';
const CAL_GROW_INTO_PRESENT = 'present';
const ORIENTATION_VERTICAL = 'vertical';
const ORIENTATION_HORIZONTAL = 'horizontal';
public $weekFlow = self::CAL_GROW_INTO_PAST;
public $orientation = self::ORIENTATION_VERTICAL;
public $weekStartMonday = true;
private $maxValue = 1;
private $start = null;
private $end = null;
private $data = array();
private $color;
public $opacity = 1.0;
public function __construct($color = '#51e551', $start = null, $end = null) {
$this->setColor($color);
if (isset($start)) {
$this->start = $this->tsToDateStr($start);
}
if (isset($end)) {
$this->end = $this->tsToDateStr($end);
}
}
/**
* Set the displayed data-set
*
* @param $events array The history events to display as an array of arrays:
* value: The value to display
* caption: The caption on mouse-over
* url: The url to open on click.
*/
public function setData(array $events)
{
$this->data = $events;
$start = time();
$end = time();
foreach ($this->data as $entry) {
$entry['value'] = intval($entry['value']);
}
foreach ($this->data as $date => $entry) {
$time = strtotime($date);
if ($entry['value'] > $this->maxValue) {
$this->maxValue = $entry['value'];
}
if ($time > $end) {
$end = $time;
}
if ($time < $start) {
$start = $time;
}
}
if (!isset($this->start)) {
$this->start = $this->tsToDateStr($start);
}
if (!isset($this->end)) {
$this->end = $this->tsToDateStr($end);
}
}
/**
* Set the used color.
*
* @param $color
*/
public function setColor($color)
{
$this->color = $color;
}
/**
* Set the used opacity
*
* @param $opacity
*/
public function setOpacity($opacity)
{
$this->opacity = $opacity;
}
/**
* Calculate the color to display for the given value.
*
* @param $value integer
*
* @return string The color-string to use for this entry.
*/
private function calculateColor($value)
{
$saturation = $value / $this->maxValue;
return Color::changeSaturation($this->color, $saturation);
}
/**
* Render the html to display the given $day
*
* @param $day string The day to display YYYY-MM-DD
*
* @return string The rendered html
*/
private function renderDay($day)
{
if (array_key_exists($day, $this->data) && $this->data[$day]['value'] > 0) {
$entry = $this->data[$day];
return '<a ' .
'style="background-color: ' . $this->calculateColor($entry['value']) . ';'
. ' opacity: ' . $this->opacity . ';" ' .
'aria-label="' . $entry['caption'] . '" ' .
'title="' . $entry['caption'] . '" ' .
'href="' . $entry['url'] . '" ' .
'data-tooltip-delay="0"' .
'></a>';
} else {
return '<span ' .
'style="background-color: ' . $this->calculateColor(0) . '; opacity: ' . $this->opacity . ';" ' .
'title="No entries for ' . $day . '" ' .
'></span>';
}
}
/**
* Render the grid with an horizontal alignment.
*
* @param array $grid The values returned from the createGrid function
*
* @return string The rendered html
*/
private function renderHorizontal($grid)
{
$weeks = $grid['weeks'];
$months = $grid['months'];
$years = $grid['years'];
$html = '<table class="historycolorgrid">';
$html .= '<tr><th></th>';
$old = -1;
foreach ($months as $week => $month) {
if ($old !== $month) {
$old = $month;
$txt = $this->monthName($month, $years[$week]);
} else {
$txt = '';
}
$html .= '<th>' . $txt . '</th>';
}
$html .= '</tr>';
for ($i = 0; $i < 7; $i++) {
$html .= $this->renderWeekdayHorizontal($i, $weeks);
}
$html .= '</table>';
return $html;
}
/**
* @param $grid
*
* @return string
*/
private function renderVertical($grid)
{
$years = $grid['years'];
$weeks = $grid['weeks'];
$months = $grid['months'];
$html = '<table class="historycolorgrid">';
$html .= '<tr>';
for ($i = 0; $i < 7; $i++) {
$html .= '<th>' . $this->weekdayName($this->weekStartMonday ? $i + 1 : $i) . "</th>";
}
$html .= '</tr>';
$old = -1;
foreach ($weeks as $index => $week) {
for ($i = 0; $i < 7; $i++) {
if (array_key_exists($i, $week)) {
$html .= '<td>' . $this->renderDay($week[$i]) . '</td>';
} else {
$html .= '<td></td>';
}
}
if ($old !== $months[$index]) {
$old = $months[$index];
$txt = $this->monthName($old, $years[$index]);
} else {
$txt = '';
}
$html .= '<td class="weekday">' . $txt . '</td></tr>';
}
$html .= '</table>';
return $html;
}
/**
* Render the row for the given weekday.
*
* @param integer $weekday The day to render (0-6)
* @param array $weeks The weeks
*
* @return string The formatted table-row
*/
private function renderWeekdayHorizontal($weekday, &$weeks)
{
$html = '<tr><td class="weekday">'
. $this->weekdayName($this->weekStartMonday ? $weekday + 1 : $weekday)
. '</td>';
foreach ($weeks as $week) {
if (array_key_exists($weekday, $week)) {
$html .= '<td>' . $this->renderDay($week[$weekday]) . '</td>';
} else {
$html .= '<td></td>';
}
}
$html .= '</tr>';
return $html;
}
/**
* @return array
*/
private function createGrid()
{
$weeks = array(array());
$week = 0;
$months = array();
$years = array();
$start = strtotime($this->start);
$year = intval(date('Y', $start));
$month = intval(date('n', $start));
$day = intval(date('j', $start));
$weekday = intval(date('w', $start));
if ($this->weekStartMonday) {
// 0 => monday, 6 => sunday
$weekday = $weekday === 0 ? 6 : $weekday - 1;
}
$date = $this->toDateStr($day, $month, $year);
$weeks[0][$weekday] = $date;
$years[0] = $year;
$months[0] = $month;
while ($date !== $this->end) {
$day++;
$weekday++;
if ($weekday > 6) {
$weekday = 0;
$weeks[] = array();
// PRESENT => The last day of week determines the month
if ($this->weekFlow === self::CAL_GROW_INTO_PRESENT) {
$months[$week] = $month;
$years[$week] = $year;
}
$week++;
}
if ($day > date('t', mktime(0, 0, 0, $month, 1, $year))) {
$month++;
if ($month > 12) {
$year++;
$month = 1;
}
$day = 1;
}
if ($weekday === 0) {
// PAST => The first day of each week determines the month
if ($this->weekFlow === self::CAL_GROW_INTO_PAST) {
$months[$week] = $month;
$years[$week] = $year;
}
}
$date = $this->toDateStr($day, $month, $year);
$weeks[$week][$weekday] = $date;
};
$years[$week] = $year;
$months[$week] = $month;
if ($this->weekFlow == self::CAL_GROW_INTO_PAST) {
return array(
'weeks' => array_reverse($weeks),
'months' => array_reverse($months),
'years' => array_reverse($years)
);
}
return array(
'weeks' => $weeks,
'months' => $months,
'years' => $years
);
}
/**
* Get the localized month-name for the given month
*
* @param integer $month The month-number
*
* @return string The
*/
private function monthName($month, $year)
{
// TODO: find a way to render years without messing up the layout
$dt = new DateTime($year . '-' . $month . '-01');
return $dt->format('M');
}
/**
* @param $weekday
*
* @return string
*/
private function weekdayName($weekday)
{
$sun = new DateTime('last Sunday');
$interval = new DateInterval('P' . $weekday . 'D');
$sun->add($interval);
return substr($sun->format('D'), 0, 2);
}
/**
*
*
* @param $timestamp
*
* @return bool|string
*/
private function tsToDateStr($timestamp)
{
return date('Y-m-d', $timestamp);
}
/**
* @param $day
* @param $mon
* @param $year
*
* @return string
*/
private function toDateStr($day, $mon, $year)
{
$day = $day > 9 ? (string)$day : '0' . (string)$day;
$mon = $mon > 9 ? (string)$mon : '0' . (string)$mon;
return $year . '-' . $mon . '-' . $day;
}
/**
* @return string
*/
public function render()
{
if (empty($this->data)) {
return '<div>No entries</div>';
}
$grid = $this->createGrid();
if ($this->orientation === self::ORIENTATION_HORIZONTAL) {
return $this->renderHorizontal($grid);
}
return $this->renderVertical($grid);
}
}
|