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).

!7

// only from machines that run #1020927 with postToServer=true

cmodule ServerLoadsTrayIcon > DynPrintLog {
  int size = goodSystemTrayIconSize().x; // icon width+height
  transient double expiryTime = 10.0;
  transient TrayIcon trayIcon; 
  transient Map<S, Load> loads = expiringMap(expiryTime);
  transient LS colors = ll("FF0000", "00FF00", "0000FF");
  transient S status;
  
  srecord Load(int cores, double percent) {}
  
  start {
    ownResource(tempTrayIcon(trayIcon = installTrayIcon(whiteImage(size, size), "Server CPUs", rThread dm_activateOSAndModule)));
    doEvery(1.0, r updateIcon);
    dm_onRemoteSystemStatusPosted((computerID, msg) -> {
      LS l = regexpFirstGroupsIC([[System CPU: (\d+)% \((\d+) core]], msg);
      if (l == null) ret;
      loads.put(computerID, new Load(parseInt(second(l)), parseInt(first(l))));
    });
  }
  
  void updateIcon enter {
    Map<S, Load> map = cloneMap(loads);
    setField(status := joinMap(", ", map, (server, load)
      -> server + ": " + iround(load.percent/load.cores) + "%"));
    int cores = 0;
    for (Load l : values(map)) cores += l.cores;
    BufferedImage img;
    double totalLoad = 0;
    if (cores == 0) img = whiteImage(1, 1);
    else {
      img = whiteImage(cores, size);
      int x = 0, i = 0;
      // go alphabetical in computer IDs
      for (Load l : valuesSortedByKeys(map)) {
        totalLoad += l.percent;
        Color color = colorFromHex(colors.get((i++) % l(colors)));
        int h = iround(l.percent/l.cores/100*size);
        fillRect(img, x, size-h, l.cores, h, color);
        x += l.cores;
      }
    }
    setTrayIconImage(trayIcon, resizeImage(img, size, size));
    trayIcon.setToolTip(nServers(map) + ", " + nCores(cores) + ", " + "total load: " + iround(totalLoad/cores) + "%");
  }

  visual centerAndSouthWithMargins(super, jCenteredSection("Server loads", dm_fieldLabel status()));
}

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: 247 / 25036
Version history: 14 change(s)
Referenced in: