summaryrefslogtreecommitdiffstats
path: root/glances/outputs/static/js/services.js
blob: ac6c782fcdb136822636c62ec3ae2390eee63c49 (plain)
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
import { store } from './store.js';
import Favico from 'favico.js';

// prettier-ignore
const fetchAll = () => fetch('api/3/all', { method: 'GET' }).then((response) => response.json());
// prettier-ignore
const fetchAllViews = () => fetch('api/3/all/views', { method: 'GET' }).then((response) => response.json());
// prettier-ignore
const fetchAllLimits = () => fetch('api/3/all/limits', { method: 'GET' }).then((response) => response.json());
// prettier-ignore
const fetchArgs = () => fetch('api/3/args', { method: 'GET' }).then((response) => response.json());
// prettier-ignore
const fetchConfig = () => fetch('api/3/config', { method: 'GET' }).then((response) => response.json());

class GlancesHelperService {
    limits = {};
    limitSuffix = ['critical', 'careful', 'warning'];

    setLimits(limits) {
        this.limits = limits;
    }

    getAlert(pluginName, limitNamePrefix, current, maximum, log) {
        current = current || 0;
        maximum = maximum || 100;
        log = log || false;

        var log_str = log ? '_log' : '';
        var value = (current * 100) / maximum;

        if (this.limits[pluginName] != undefined) {
            for (var i = 0; i < this.limitSuffix.length; i++) {
                var limitName = limitNamePrefix + this.limitSuffix[i];
                var limit = this.limits[pluginName][limitName];
                if (value >= limit) {
                    var pos = limitName.lastIndexOf('_');
                    var className = limitName.substring(pos + 1);
                    return className + log_str;
                }
            }
        }

        return 'ok' + log_str;
    }

    getAlertLog(pluginName, limitNamePrefix, current, maximum) {
        return this.getAlert(pluginName, limitNamePrefix, current, maximum, true);
    }
}

export const GlancesHelper = new GlancesHelperService();

class GlancesStatsService {
    data = undefined;

    init(REFRESH_TIME = 60) {
        let timeout = undefined;
        const fetchData = () => {
            store.status = 'PENDING';
            return Promise.all([fetchAll(), fetchAllViews()])
                .then((response) => {
                    const data = {
                        stats: response[0],
                        views: response[1],
                        isBsd: response[0]['system']['os_name'] === 'FreeBSD',
                        isLinux: response[0]['system']['os_name'] === 'Linux',
                        isSunOS: response[0]['system']['os_name'] === 'SunOS',
                        isMac: response[0]['system']['os_name'] === 'Darwin',
                        isWindows: response[0]['system']['os_name'] === 'Windows'
                    };
                    this.data = data;
                    store.data = data;
                    store.status = 'SUCCESS';
                })
                .catch((error) => {
                    console.log(error);
                    store.status = 'FAILURE';
                })
                .then(() => {
                    if (timeout) {
                        clearTimeout(timeout);
                    }
                    timeout = setTimeout(fetchData, REFRESH_TIME * 1000); // in milliseconds
                });
        };
        fetchData();

        fetchAllLimits().then((response) => {
            GlancesHelper.setLimits(response);
        });

        fetchArgs().then((response = {}) => {
            store.args = { ...store.args, ...response };
        });

        fetchConfig().then((response = {}) => {
            store.config = { ...store.config, ...response };
        });
    }

    getData() {
        return this.data;
    }
}

export const GlancesStats = new GlancesStatsService();

class GlancesFavicoService {
    constructor() {
        this.favico = new Favico({
            animation: 'none'
        });
    }
    badge(nb) {
        this.favico.badge(nb);
    }
    reset() {
        this.favico.reset();
    }
}

export const GlancesFavico = new GlancesFavicoService();