@@ 395-411 (lines=17) @@ | ||
392 | ||
393 | Stats should be a list of dict (processlist, network...) |
|
394 | """ |
|
395 | if isinstance(self.stats, dict): |
|
396 | try: |
|
397 | return self._json_dumps({item: self.stats[item]}) |
|
398 | except KeyError as e: |
|
399 | logger.error("Cannot get item {} ({})".format(item, e)) |
|
400 | return None |
|
401 | elif isinstance(self.stats, list): |
|
402 | try: |
|
403 | # Source: |
|
404 | # http://stackoverflow.com/questions/4573875/python-get-index-of-dictionary-item-in-list |
|
405 | # But https://github.com/nicolargo/glances/issues/1401 |
|
406 | return self._json_dumps({item: list(map(itemgetter(item), self.stats))}) |
|
407 | except (KeyError, ValueError) as e: |
|
408 | logger.error("Cannot get item {} ({})".format(item, e)) |
|
409 | return None |
|
410 | else: |
|
411 | return None |
|
412 | ||
413 | def get_stats_value(self, item, value): |
|
414 | """Return the stats object for a specific item=value in JSON format. |
|
@@ 257-272 (lines=16) @@ | ||
254 | if item is None: |
|
255 | return self._json_dumps(s) |
|
256 | ||
257 | if isinstance(s, dict): |
|
258 | try: |
|
259 | return self._json_dumps({item: s[item]}) |
|
260 | except KeyError as e: |
|
261 | logger.error("Cannot get item history {} ({})".format(item, e)) |
|
262 | return None |
|
263 | elif isinstance(s, list): |
|
264 | try: |
|
265 | # Source: |
|
266 | # http://stackoverflow.com/questions/4573875/python-get-index-of-dictionary-item-in-list |
|
267 | return self._json_dumps({item: map(itemgetter(item), s)}) |
|
268 | except (KeyError, ValueError) as e: |
|
269 | logger.error("Cannot get item history {} ({})".format(item, e)) |
|
270 | return None |
|
271 | else: |
|
272 | return None |
|
273 | ||
274 | def get_trend(self, item, nb=6): |
|
275 | """Get the trend regarding to the last nb values. |