Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one or more
0003  * contributor license agreements.  See the NOTICE file distributed with
0004  * this work for additional information regarding copyright ownership.
0005  * The ASF licenses this file to You under the Apache License, Version 2.0
0006  * (the "License"); you may not use this file except in compliance with
0007  * the License.  You may obtain a copy of the License at
0008  *
0009  *    http://www.apache.org/licenses/LICENSE-2.0
0010  *
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
0016  */
0017 
0018 // this function works exactly the same as UIUtils.formatDuration
0019 function formatDuration(milliseconds) {
0020     if (milliseconds < 100) {
0021         return parseInt(milliseconds).toFixed(1) + " ms";
0022     }
0023     var seconds = milliseconds * 1.0 / 1000;
0024     if (seconds < 1) {
0025         return seconds.toFixed(1) + " s";
0026     }
0027     if (seconds < 60) {
0028         return seconds.toFixed(0) + " s";
0029     }
0030     var minutes = seconds / 60;
0031     if (minutes < 10) {
0032         return minutes.toFixed(1) + " min";
0033     } else if (minutes < 60) {
0034         return minutes.toFixed(0) + " min";
0035     }
0036     var hours = minutes / 60;
0037     return hours.toFixed(1) + " h";
0038 }
0039 
0040 function formatBytes(bytes, type) {
0041     if (type !== 'display') return bytes;
0042     if (bytes == 0) return '0.0 B';
0043     var k = 1024;
0044     var dm = 1;
0045     var sizes = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
0046     var i = Math.floor(Math.log(bytes) / Math.log(k));
0047     return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
0048 }
0049 
0050 function padZeroes(num) {
0051   return ("0" + num).slice(-2);
0052 }
0053 
0054 function formatTimeMillis(timeMillis) {
0055   if (timeMillis <= 0) {
0056     return "-";
0057   } else {
0058     var dt = new Date(timeMillis);
0059     return dt.getFullYear() + "-" +
0060       padZeroes(dt.getMonth() + 1) + "-" +
0061       padZeroes(dt.getDate()) + " " +
0062       padZeroes(dt.getHours()) + ":" +
0063       padZeroes(dt.getMinutes()) + ":" +
0064       padZeroes(dt.getSeconds());
0065   }
0066 }
0067 
0068 function getTimeZone() {
0069   try {
0070     return Intl.DateTimeFormat().resolvedOptions().timeZone;
0071   } catch(ex) {
0072     // Get time zone from a string representing the date,
0073     // eg. "Thu Nov 16 2017 01:13:32 GMT+0800 (CST)" -> "CST"
0074     return new Date().toString().match(/\((.*)\)/)[1];
0075   }
0076 }
0077 
0078 function formatLogsCells(execLogs, type) {
0079   if (type !== 'display') return Object.keys(execLogs);
0080   if (!execLogs) return;
0081   var result = '';
0082   $.each(execLogs, function (logName, logUrl) {
0083     result += '<div><a href=' + logUrl + '>' + logName + '</a></div>'
0084   });
0085   return result;
0086 }
0087 
0088 function getStandAloneAppId(cb) {
0089   var words = document.baseURI.split('/');
0090   var ind = words.indexOf("proxy");
0091   if (ind > 0) {
0092     var appId = words[ind + 1];
0093     cb(appId);
0094     return;
0095   }
0096   ind = words.indexOf("history");
0097   if (ind > 0) {
0098     var appId = words[ind + 1];
0099     cb(appId);
0100     return;
0101   }
0102   // Looks like Web UI is running in standalone mode
0103   // Let's get application-id using REST End Point
0104   $.getJSON(location.origin + "/api/v1/applications", function(response, status, jqXHR) {
0105     if (response && response.length > 0) {
0106       var appId = response[0].id;
0107       cb(appId);
0108       return;
0109     }
0110   });
0111 }
0112 
0113 // This function is a helper function for sorting in datatable.
0114 // When the data is in duration (e.g. 12ms 2s 2min 2h )
0115 // It will convert the string into integer for correct ordering
0116 function ConvertDurationString(data) {
0117   data = data.toString();
0118   var units = data.replace(/[\d\.]/g, '' )
0119                   .replace(' ', '')
0120                   .toLowerCase();
0121   var multiplier = 1;
0122 
0123   switch(units) {
0124     case 's':
0125       multiplier = 1000;
0126       break;
0127     case 'min':
0128       multiplier = 600000;
0129       break;
0130     case 'h':
0131       multiplier = 3600000;
0132       break;
0133     default:
0134       break;
0135   }
0136   return parseFloat(data) * multiplier;
0137 }
0138 
0139 function createTemplateURI(appId, templateName) {
0140   var words = document.baseURI.split('/');
0141   var ind = words.indexOf("proxy");
0142   if (ind > 0) {
0143     var baseURI = words.slice(0, ind + 1).join('/') + '/' + appId + '/static/' + templateName + '-template.html';
0144     return baseURI;
0145   }
0146   ind = words.indexOf("history");
0147   if(ind > 0) {
0148     var baseURI = words.slice(0, ind).join('/') + '/static/' + templateName + '-template.html';
0149     return baseURI;
0150   }
0151   return location.origin + "/static/" + templateName + "-template.html";
0152 }
0153 
0154 function setDataTableDefaults() {
0155   $.extend($.fn.dataTable.defaults, {
0156     stateSave: true,
0157     lengthMenu: [[20, 40, 60, 100, -1], [20, 40, 60, 100, "All"]],
0158     pageLength: 20
0159   });
0160 }
0161 
0162 function formatDate(date) {
0163   if (date <= 0) return "-";
0164   else return date.split(".")[0].replace("T", " ");
0165 }
0166 
0167 function createRESTEndPointForExecutorsPage(appId) {
0168     var words = document.baseURI.split('/');
0169     var ind = words.indexOf("proxy");
0170     if (ind > 0) {
0171         var appId = words[ind + 1];
0172         var newBaseURI = words.slice(0, ind + 2).join('/');
0173         return newBaseURI + "/api/v1/applications/" + appId + "/allexecutors";
0174     }
0175     ind = words.indexOf("history");
0176     if (ind > 0) {
0177         var appId = words[ind + 1];
0178         var attemptId = words[ind + 2];
0179         var newBaseURI = words.slice(0, ind).join('/');
0180         if (isNaN(attemptId)) {
0181             return newBaseURI + "/api/v1/applications/" + appId + "/allexecutors";
0182         } else {
0183             return newBaseURI + "/api/v1/applications/" + appId + "/" + attemptId + "/allexecutors";
0184         }
0185     }
0186     return location.origin + "/api/v1/applications/" + appId + "/allexecutors";
0187 }