Debian Codegraph @

library/Toplevelview/Tree/TLVTree.php

raw
1 <?php
2 /* Copyright (C) 2017 Icinga Development Team <[email protected]> */
3
4 namespace Icinga\Module\Toplevelview\Tree;
5
6 use Icinga\Module\Toplevelview\Model\View;
7
8 use Icinga\Application\Logger;
9 use Icinga\Exception\IcingaException;
10 use Icinga\Exception\NotFoundError;
11 use Icinga\Exception\ProgrammingError;
12 use Icinga\Module\Icingadb\Common\Database;
13 use Icinga\Util\Json;
14 use Icinga\Web\FileCache;
15 use stdClass;
16
17 /**
18 * TLVTree represents the root of the TLV tree
19 */
20 class TLVTree extends TLVTreeNode
21 {
22 /**
23 * @var Database
24 */
25 use Database;
26
27 protected static $titleKey = 'name';
28
29 public $registeredTypes = array();
30
31 public $registeredObjects = array();
32
33 protected $fetchedData = array();
34
35 protected $fetched = false;
36
37 protected $fetchTime;
38
39 protected $cacheLifetime = 60;
40
41 protected $viewName;
42
43 protected $viewChecksum;
44
45 /**
46 * Return a child by its ID
47 *
48 * @throws NotFoundError if the id cannot be found
49 */
50 public function getById($id)
51 {
52 $ids = explode('-', $id);
53 $currentNode = $this;
54
55 foreach ($ids as $i) {
56 $children = $currentNode->getChildren();
57 if (! empty($children) && array_key_exists($i, $children)) {
58 $currentNode = $children[$i];
59 } else {
60 throw new NotFoundError(
61 'Could not find ID %s after %s for path %s',
62 $i,
63 $currentNode->getFullId(),
64 $id
65 );
66 }
67 }
68
69 return $currentNode;
70 }
71
72 public function getViewName(): ?string
73 {
74 return $this->viewName;
75 }
76
77 public function setViewName(string $name)
78 {
79 $this->viewName = $name;
80 return $this;
81 }
82
83 public function getViewChecksum(): ?string
84 {
85 return $this->viewChecksum;
86 }
87
88 public function setViewChecksum(string $checksum)
89 {
90 $this->viewChecksum = $checksum;
91 return $this;
92 }
93
94 /**
95 * registerObject adds a new object via its type, name and class
96 *
97 * @throws ProgrammingError if the same type by multiple classes is registered
98 */
99 public function registerObject($type, $name, $class)
100 {
101 if (array_key_exists($type, $this->registeredTypes) && $this->registeredTypes[$type] !== $class) {
102 throw new ProgrammingError(
103 'Tried to register the same type by multiple classes: %s - %s - %s',
104 $type,
105 $this->registeredTypes[$type],
106 $class
107 );
108 }
109
110 $this->registeredTypes[$type] = $class;
111 $this->registeredObjects[$type][$name] = null;
112 }
113
114 protected function getCacheName()
115 {
116 $n = $this->getViewName();
117 $c = $this->getViewChecksum();
118 return sprintf('%s-%s.json', $n, $c);
119 }
120
121 protected function loadCache()
122 {
123 if (($lifetime = $this->getCacheLifetime()) <= 0) {
124 return;
125 }
126
127 $cacheName = $this->getCacheName();
128 try {
129 $cache = FileCache::instance('toplevelview');
130 $currentTime = time();
131 $newerThan = $currentTime - $lifetime;
132
133 if ($cache->has($cacheName)) {
134 $cachedData = Json::decode($cache->get($cacheName));
135
136 if (property_exists($cachedData, 'data')
137 && $cachedData->data !== null
138 && property_exists($cachedData, 'ts')
139 && $cachedData->ts <= $currentTime // too new maybe
140 && $cachedData->ts > $newerThan // too old
141 ) {
142 foreach ($cachedData->data as $type => $objects) {
143 $this->registeredObjects[$type] = (array) $objects;
144 $this->fetchedData[$type] = true;
145 }
146
147 $this->fetchTime = $cachedData->ts;
148 $this->fetched = true;
149 }
150 }
151 } catch (IcingaException $e) {
152 Logger::error('Could not load from toplevelview cache %s: %s', $cacheName, $e->getMessage());
153 }
154 }
155
156 protected function storeCache()
157 {
158 if (($lifetime = $this->getCacheLifetime()) <= 0) {
159 return;
160 }
161
162 $cacheName = $this->getCacheName();
163 try {
164 $cache = FileCache::instance('toplevelview');
165
166 $cachedData = new stdClass;
167 $cachedData->ts = $this->fetchTime;
168 $cachedData->data = $this->registeredObjects;
169
170 $cache->store($cacheName, Json::encode($cachedData));
171 } catch (IcingaException $e) {
172 Logger::error('Could not store to toplevelview cache %s: %s', $cacheName, $e->getMessage());
173 }
174 }
175
176 /**
177 * fetchType returns a given type from the registered types
178 *
179 * @throws ProgrammingError if the type has not been registered
180 */
181 protected function fetchType($type)
182 {
183 if (! array_key_exists($type, $this->registeredTypes)) {
184 throw new ProgrammingError('Type %s has not been registered', $type);
185 }
186
187 if (! array_key_exists($type, $this->fetchedData)) {
188 /** @var TLVIcingaNode $class */
189 $class = $this->registeredTypes[$type];
190 $class::fetch($this);
191 $this->fetchedData[$type] = true;
192 }
193
194 return $this;
195 }
196
197 protected function ensureFetched()
198 {
199 if ($this->fetched !== true) {
200 $this->loadCache();
201
202 if ($this->fetched !== true) {
203 foreach (array_keys($this->registeredTypes) as $type) {
204 $this->fetchType($type);
205 }
206
207 $this->fetchTime = time();
208 $this->fetched = true;
209
210 $this->storeCache();
211 }
212 }
213
214 return $this;
215 }
216
217 public function getFetched($type, $key)
218 {
219 $this->ensureFetched();
220
221 if (array_key_exists($key, $this->registeredObjects[$type])
222 && $this->registeredObjects[$type][$key] !== null
223 ) {
224 return $this->registeredObjects[$type][$key];
225 } else {
226 return null;
227 }
228 }
229
230 /**
231 * @return int time
232 */
233 public function getFetchTime()
234 {
235 $this->ensureFetched();
236
237 return $this->fetchTime;
238 }
239
240 /**
241 * @return int seconds
242 */
243 public function getCacheLifetime()
244 {
245 return $this->cacheLifetime;
246 }
247
248 /**
249 * @param int $cacheLifetime In seconds
250 *
251 * @return $this
252 */
253 public function setCacheLifetime($cacheLifetime)
254 {
255 $this->cacheLifetime = $cacheLifetime;
256 return $this;
257 }
258 }