github.com/aitjcize/Overlord@v0.0.0-20240314041920-104a804cf5e8/overlord/app/common/js/utils.js (about)

     1  // Copyright 2015 The Chromium OS Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style license that can be
     3  // found in the LICENSE file.
     4  
     5  function abbr(str, len) {
     6    if (str.length > len) {
     7      return str.substr(0, len - 3) + '...';
     8    }
     9    return str;
    10  }
    11  
    12  function ReadBlobAsText(blob, callback) {
    13    var reader = new FileReader();
    14    reader.addEventListener('loadend', function() {
    15      callback(reader.result);
    16    });
    17    reader.readAsText(blob);
    18  }
    19  
    20  function randomID() {
    21    return Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 10);
    22  }
    23  
    24  // Execute Overlord remote shell command
    25  function getRemoteCmdOutput(mid, cmd) {
    26    var url = 'ws' + ((window.location.protocol == 'https:') ? 's' : '') +
    27              '://' + window.location.host + '/api/agent/shell/' + mid +
    28              '?command=' + cmd;
    29    var sock = new WebSocket(url);
    30    var deferred = $.Deferred();
    31  
    32    sock.onopen = function(event) {
    33      var blobs = [];
    34      sock.onmessage = function(msg) {
    35        if (msg.data instanceof Blob) {
    36          blobs.push(msg.data);
    37        }
    38      };
    39      sock.onclose = function(event) {
    40        var value = '';
    41        if (blobs.length == 0) {
    42          deferred.resolve('');
    43        }
    44        for (var i = 0; i < blobs.length; i++) {
    45          ReadBlobAsText(blobs[i], function(current) {
    46            return function(text) {
    47              value += text;
    48              if (current == blobs.length - 1) {
    49                deferred.resolve(value);
    50              }
    51            }
    52          }(i));
    53        }
    54      };
    55    };
    56    return deferred.promise();
    57  }
    58  
    59  function displayClient(client) {
    60    var prefix = '';
    61    if (typeof(client.properties) != 'undefined' &&
    62        typeof(client.properties.active_test_list) != 'undefined') {
    63      prefix = '[' + client.properties.active_test_list + '] ';
    64    }
    65    return prefix + client.mid;
    66  }