-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsystem-monitor.coffee
131 lines (104 loc) · 3.72 KB
/
system-monitor.coffee
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
commands =
cpu : "ps -A -o %cpu | awk '{s+=$1} END {printf(\"%.2f\",s/8);}'"
mem : "ps -A -o %mem | awk '{s+=$1} END {print s \"%\"}' "
hdd : "df / | awk 'END{print $5}'"
net : "sh ./supernerd.widget/scripts/getnetwork.sh"
command: "echo " +
"$(#{ commands.cpu }):::" +
"$(#{ commands.mem }):::" +
"$(#{ commands.hdd }):::" +
"$(#{ commands.net }):::"
refreshFrequency: '1s'
render: ( ) ->
"""
<div class="tray hidden" id="time-tray">
<div class="widg red" id="upl">
<div class="icon-container" id='upl-icon-container'>
<i class="fa fa-upload"></i>
</div>
<span class="output" id="upl-output"></span>
</div>
<div class="widg blue" id="dwl">
<div class="icon-container" id='dwl-icon-container'>
<i class="fa fa-download"></i>
</div>
<span class="output" id="dwl-output"></span>
</div>
<div class="widg" id="cpu">
<div class="icon-container" id='cpu-icon-container'>
<i class="fa fa-spinner"></i>
</div>
<span class="output" id="cpu-output"></span>
</div>
<div class="widg" id="mem">
<div class="icon-container" id='mem-icon-container'>
<i class="fas fa-server"></i>
</div>
<span class="output" id="mem-output"></span>
</div>
<div class="widg" id="hdd">
<div class="icon-container" id='hdd-icon-container'>
<i class="fas fa-hdd"></i>
</div>
<span class="output" id="hdd-output"></span>
</div>
</div>
<div class="widg green tray-button" id="sysmon">
<div class="icon-container" id='sysmon-icon-container'>
<i class="fa fa-power-off"></i>
</div>
</div>
"""
convertBytes: (bytes) ->
kb = bytes / 1024
mb = kb / 1024
if mb < 0.01
return "0.00MB"
return "#{parseFloat(mb.toFixed(2))}MB"
update: ( output, domEl ) ->
output = output.split( /:::/g )
cpu = output[ 0 ]
mem = output[ 1 ]
hdd = output[ 2 ]
net = output[ 3 ].split( /@/g )
upl = net[ 0 ]
dwl = net[ 1 ]
$( "#cpu-output").text("#{ cpu }%")
$( "#mem-output").text("#{ mem }")
$( "#hdd-output").text("#{ hdd }")
$( "#upl-output").text("#{ @convertBytes(upl) }")
$( "#dwl-output").text("#{ @convertBytes(dwl) }")
@handleSysmon( domEl, Number( cpu ), '#cpu' )
@handleSysmon( domEl, Number( mem.replace( /%/g, "") ), '#mem' )
@handleSysmon( domEl, Number( hdd.replace( /%/g, "") ), '#hdd' )
#
# ─── HANDLE SYSMON –─────────────────────────────────────────────────────────
#
handleSysmon: ( domEl, sysmon, monid ) ->
div = $(domEl)
div.find(monid).removeClass('blue')
div.find(monid).removeClass('cyan')
div.find(monid).removeClass('green')
div.find(monid).removeClass('yellow')
div.find(monid).removeClass('magenta')
div.find(monid).removeClass('red')
if sysmon <= 10
div.find(monid).addClass('blue')
else if sysmon <= 20
div.find(monid).addClass('blue')
else if sysmon <= 40
div.find(monid).addClass('cyan')
else if sysmon <= 50
div.find(monid).addClass('green')
else if sysmon <= 75
div.find(monid).addClass('yellow')
else
div.find(monid).addClass('red')
#
# ─── UNIVERSAL CLICK AND ANIMATION HANDLING ─────────────────────────────────────────────────────────
#
afterRender: (domEl) ->
$(domEl).on 'mouseover', ".widg", (e) => $(domEl).find( $($(e.target))).addClass('open')
$(domEl).on 'mouseout', ".widg", (e) => $(domEl).find( $($(e.target))).removeClass('open')
$(domEl).on 'click', ".widg", (e) => $(domEl).find( $($(e.target))).toggleClass('pinned')
$(domEl).on 'click', ".tray-button", (e) => $(domEl).find(".tray").toggleClass('hidden')