Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

51
LINES

< > BotCompany Repo | #1025968 // Server loads tray icon [OK]

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 911K of libraries. Click here for Pure Java version (12647L/68K).

1  
!7
2  
3  
// only from machines that run #1020927 with postToServer=true
4  
5  
cmodule ServerLoadsTrayIcon > DynPrintLog {
6  
  int size = goodSystemTrayIconSize().x; // icon width+height
7  
  transient double expiryTime = 10.0;
8  
  transient TrayIcon trayIcon; 
9  
  transient Map<S, Load> loads = expiringMap(expiryTime);
10  
  transient LS colors = ll("FF0000", "00FF00", "0000FF");
11  
  transient S status;
12  
  
13  
  srecord Load(int cores, double percent) {}
14  
  
15  
  start {
16  
    ownResource(tempTrayIcon(trayIcon = installTrayIcon(whiteImage(size, size), "Server CPUs", rThread dm_activateOSAndModule)));
17  
    doEvery(1.0, r updateIcon);
18  
    dm_onRemoteSystemStatusPosted((computerID, msg) -> {
19  
      LS l = regexpFirstGroupsIC([[System CPU: (\d+)% \((\d+) core]], msg);
20  
      if (l == null) ret;
21  
      loads.put(computerID, new Load(parseInt(second(l)), parseInt(first(l))));
22  
    });
23  
  }
24  
  
25  
  void updateIcon enter {
26  
    Map<S, Load> map = cloneMap(loads);
27  
    setField(status := joinMap(", ", map, (server, load)
28  
      -> server + ": " + iround(load.percent/load.cores) + "%"));
29  
    int cores = 0;
30  
    for (Load l : values(map)) cores += l.cores;
31  
    BufferedImage img;
32  
    double totalLoad = 0;
33  
    if (cores == 0) img = whiteImage(1, 1);
34  
    else {
35  
      img = whiteImage(cores, size);
36  
      int x = 0, i = 0;
37  
      // go alphabetical in computer IDs
38  
      for (Load l : valuesSortedByKeys(map)) {
39  
        totalLoad += l.percent;
40  
        Color color = colorFromHex(colors.get((i++) % l(colors)));
41  
        int h = iround(l.percent/l.cores/100*size);
42  
        fillRect(img, x, size-h, l.cores, h, color);
43  
        x += l.cores;
44  
      }
45  
    }
46  
    setTrayIconImage(trayIcon, resizeImage(img, size, size));
47  
    trayIcon.setToolTip(nServers(map) + ", " + nCores(cores) + ", " + "total load: " + iround(totalLoad/cores) + "%");
48  
  }
49  
50  
  visual centerAndSouthWithMargins(super, jCenteredSection("Server loads", dm_fieldLabel status()));
51  
}

Author comment

Began life as a copy of #1025930

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1025968
Snippet name: Server loads tray icon [OK]
Eternal ID of this version: #1025968/15
Text MD5: 30d3deaf124802b076e71a163a3c0d1e
Transpilation MD5: 7dbc4e9255f3154f4aa5f004cb71a3ad
Author: stefan
Category: javax / stefan's os
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-06-28 23:34:12
Source code size: 1897 bytes / 51 lines
Pitched / IR pitched: No / No
Views / Downloads: 182 / 24954
Version history: 14 change(s)
Referenced in: [show references]