Passed
Push — develop ( 74cbde...8ba7ca )
by Nikolay
04:56
created

sites/admin-cabinet/assets/js/src/Providers/providers-status-worker.js   A

Complexity

Total Complexity 14
Complexity/F 1.56

Size

Lines of Code 124
Function Count 9

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 62
mnd 5
bc 5
fnc 9
dl 0
loc 124
rs 10
bpm 0.5555
cpm 1.5555
noi 6
c 0
b 0
f 0
1
/*
2
 * MikoPBX - free phone system for small business
3
 * Copyright © 2017-2023 Alexey Portnov and Nikolay Beketov
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License along with this program.
16
 * If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
20
/**
21
 * Object representing the provider status loop worker.
22
 *
23
 * @module providersStatusLoopWorker
24
 */
25
const providersStatusLoopWorker = {
26
27
    /**
28
     * Time in milliseconds before fetching new status request.
29
     * @type {number}
30
     */
31
    timeOut: 3000,
32
33
    /**
34
     * The id of the timer function for the status worker.
35
     * @type {number}
36
     */
37
    timeOutHandle: 0,
38
39
    /**
40
     * Object to store provider statuses.
41
     * @type {Object}
42
     */
43
    providerStatuses: {},
44
45
    /**
46
     * Initializes the provider status loop worker.
47
     */
48
    initialize() {
49
        DebuggerInfo.initialize();
0 ignored issues
show
Bug introduced by
The variable DebuggerInfo seems to be never declared. If this is a global, consider adding a /** global: DebuggerInfo */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
50
51
        const previousStatuses = sessionStorage.getItem('ProviderStatuses');
0 ignored issues
show
Bug introduced by
The variable sessionStorage seems to be never declared. If this is a global, consider adding a /** global: sessionStorage */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
52
        if (previousStatuses !== null) {
53
            providersStatusLoopWorker.providerStatuses = JSON.parse(previousStatuses);
54
        }
55
        providersStatusLoopWorker.restartWorker();
56
    },
57
58
    /**
59
     * Restarts the status worker.
60
     */
61
    restartWorker() {
62
        window.clearTimeout(providersStatusLoopWorker.timeoutHandle);
63
        providersStatusLoopWorker.worker();
64
    },
65
66
    /**
67
     * Executes the status worker.
68
     */
69
    worker() {
70
        window.clearTimeout(providersStatusLoopWorker.timeoutHandle);
71
        PbxApi.GetSipProvidersStatuses(providersStatusLoopWorker.cbRefreshProvidersStatus);
0 ignored issues
show
Bug introduced by
The variable PbxApi seems to be never declared. If this is a global, consider adding a /** global: PbxApi */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
72
        PbxApi.GetIaxProvidersStatuses(providersStatusLoopWorker.cbRefreshProvidersStatus);
73
    },
74
75
    /**
76
     * Callback function to accumulate provider statuses.
77
     * @param {Array} response - Response containing provider statuses.
78
     */
79
    cbRefreshProvidersStatus(response) {
80
        providersStatusLoopWorker.timeoutHandle =
81
            window.setTimeout(providersStatusLoopWorker.worker, providersStatusLoopWorker.timeOut);
82
        if (response.length === 0 || response === false) return;
83
        $.each(response, (key, value) => {
84
            if (value.state !== undefined) {
85
                providersStatusLoopWorker.providerStatuses[value.id] = value.state.toUpperCase();
86
            }
87
        });
88
        sessionStorage.setItem('ProviderStatuses', JSON.stringify(providersStatusLoopWorker.providerStatuses));
0 ignored issues
show
Bug introduced by
The variable sessionStorage seems to be never declared. If this is a global, consider adding a /** global: sessionStorage */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
89
        providersStatusLoopWorker.refreshVisualisation();
90
    },
91
92
    /**
93
     * Refreshes the visualization of provider statuses.
94
     */
95
    refreshVisualisation() {
96
97
        // Iterate over the response data and create HTML table rows for each provider status
98
        // to shows it on debug slider by double press esc button
99
        let htmlTable = '<table class="ui very compact table">';
100
        $.each(providersStatusLoopWorker.providerStatuses, (key, value) => {
101
            htmlTable += '<tr>';
102
            htmlTable += `<td>${key}</td>`;
103
            htmlTable += `<td>${value}</td>`;
104
            htmlTable += '</tr>';
105
        });
106
        htmlTable += '</table>';
107
        DebuggerInfo.UpdateContent(htmlTable);
0 ignored issues
show
Bug introduced by
The variable DebuggerInfo seems to be never declared. If this is a global, consider adding a /** global: DebuggerInfo */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
108
109
        // Define label styles for different statuses
110
        const green = '<div class="ui green empty circular label" style="width: 1px;height: 1px;"></div>';
111
        const grey = '<div class="ui grey empty circular label" style="width: 1px;height: 1px;"></div>';
112
        const yellow = '<div class="ui yellow empty circular label" style="width: 1px;height: 1px;"></div>';
113
114
        // Update provider status and failure information in the UI
115
        $('tr.provider-row').each((index, obj) => {
116
            const uniqid = $(obj).attr('id');
117
            if (providersStatusLoopWorker.providerStatuses[uniqid] !== undefined) {
118
                switch (providersStatusLoopWorker.providerStatuses[uniqid]) {
119
                    case 'REGISTERED':
120
                        $(obj).find('.provider-status').html(green);
121
                        $(obj).find('.failure').text('');
122
                        break;
123
                    case 'OK':
124
                        $(obj).find('.provider-status').html(yellow);
125
                        $(obj).find('.failure').text('');
126
                        break;
127
                    case 'OFF':
128
                        $(obj).find('.provider-status').html(grey);
129
                        $(obj).find('.failure').text('');
130
                        break;
131
                    default:
132
                        $(obj).find('.provider-status').html(grey);
133
                        $(obj).find('.failure').text(providersStatusLoopWorker.providerStatuses[uniqid]);
134
                        break;
135
                }
136
            } else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
137
                $(obj).find('.provider-status').html(grey);
138
            }
139
        });
140
    },
141
};
142
143
/**
144
 *  Initialize providers status worker on document ready
145
 */
146
$(document).ready(() => {
147
    providersStatusLoopWorker.initialize();
148
});