rebuild GUI structure(only changed modules' name)
[vnfsdk/refrepo.git] / performance / src / main / webapp / performance / js / pmUtil.js
1 /*
2  * Copyright 2016-2017, CMCC Technologies Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *         http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 var pmUtil = {};
17 function html_encode(str) {
18     vars = "";
19     if (str.length == 0)return "";
20     s = str.replace(/&/g, "&");  //1
21     s = s.replace(/</g, "&lt;");
22     s = s.replace(/>/g, "&gt;");
23     s = s.replace(/ /g, "&nbsp;");
24     s = s.replace(/\'/g, "&#39;");
25     s = s.replace(/\"/g, "&quot;");
26     s = s.replace(/\n/g, "<br>");
27     return s;
28 };
29 pmUtil.indexOperate = function (obj) {
30     
31         var aIndex={};
32         aIndex.id=obj.aData.id;
33         aIndex.name=obj.aData.indexName;
34         aIndex.dataType=obj.aData.indexDataType;
35         aIndex.indexDes=obj.aData.indexDes;
36         aIndex.strExpression=obj.aData.strExpression;
37         aIndex.resourceType=obj.aData.resourceType;
38         aIndex.moType=obj.aData.moType;
39         var index = html_encode(JSON.stringify(aIndex));
40     var indexId = obj.aData.id;
41     var innerHtml =  "<div class='table-btn'><span class='modify_user'><a class=\"btn-xs grey btn-editable\" href='#' onclick=\"pmUtil.modifyIndex('"+index+"')\"><i class=\"ict-modify\"></i>" + $.i18n.prop('com_zte_ums_ict_pm_action_modify') +
42         "</a></span><span><a href='#' class=\"btn-xs grey btn-editable\" onclick=\"pmUtil.deleteIndex('" +
43         indexId + "','"+obj.aData.moType.id+"')\"><i class=\"ict-delete\"></i>" + $.i18n.prop('com_zte_ums_ict_pm_action_delete') + "</a></span></div>";        
44
45     return innerHtml;
46 };
47 pmUtil.dataTypeRender = function (obj) {
48     var record = obj.aData;
49         obj.aData.indexDataType=obj.aData.dataType;
50     var innerHtml = "<span>"+$.i18n.prop(record.dataType)+"</span>"
51     return innerHtml;
52 };
53 pmUtil.indexNameLink = function (obj) {
54         var index = html_encode(JSON.stringify(obj.aData));
55         obj.aData.indexName=obj.aData.name;
56         return "<a  id='indexName' href='#' onclick=\"pmUtil.queryIndex('"+index+"')\">" + obj.aData.name + "</a>";
57 };
58 pmUtil.queryIndex=function(index){
59     
60        newIndexWizard(commonUtil.strToJson(index),"query");
61            pm.index.vm.title="查看指标";
62            $(".form-group").attr("disabled",true);
63            pm.index.vm.viewVisible=false;
64            pm.index.vm.rtnVisible=true;
65        pm.index.vm.indexVisible=true;
66 };
67 pmUtil.addIndex=function(){
68     
69        newIndexWizard();
70            pm.index.vm.title="新建指标";
71            pm.index.vm.viewVisible=false;
72        pm.index.vm. indexVisible=true;
73 };
74 pmUtil.modifyIndex=function(index){
75     
76        newIndexWizard(commonUtil.strToJson(index));
77            pm.index.vm.title="修改指标";
78            pm.index.vm.viewVisible=false;
79        pm.index.vm.indexVisible=true;
80 };
81 pmUtil.deleteIndex=function(indexId,moTypeId){
82         bootbox.confirm($.i18n.prop('com_zte_ums_ict_sm_confirmToDeleteIndex'), function (result) { 
83                 if (result) {
84                     var ids=[];
85                         var del={};
86                         ids.push(indexId);
87                         del.ids=ids;
88                         $.ajax({                        
89                         "dataType": 'json',
90                         "type": "DELETE",
91                         "url": "/api/umcpm/v1/motype/"+moTypeId+"/indexes",
92                         "data" : JSON.stringify(del),
93                         "contentType": 'application/json; charset=utf-8',
94                         "success": function (res, textStatus, jqXHR) {
95                                 window.location.href="./indexView.html";
96                         },
97                         "error": function () {
98                         }
99                 });
100                 }
101         });
102 }
103
104 pmUtil.idLink = function (obj) {
105         var record = obj.aData;
106     var thresholdId = new String(record.id);
107         return "<a href='#' onclick=\"pm.threshold.vm.queryThreshold('" + thresholdId + "')\">" + thresholdId + "</a>";
108 }
109
110 pmUtil.congfirmDel = function (obj) {
111         bootbox.confirm($.i18n.prop('com_zte_ums_ict_sm_confirmToDeleteThreshold'), function (result) { 
112                 if (result) {
113                         pm.threshold.vm.deleteThreshold(obj);
114                 }
115         });
116 }
117
118 pmUtil.allOperate = function (obj) {
119         
120     var record = obj.aData;
121     var thresholdId = record.id;
122         //此时的id字段已经被渲染成html片段,如<a href="#" onclick="pm.threshold.vm.queryThreshold('10001')">10001</a>需要提取id
123         thresholdId = thresholdId.substring(thresholdId.indexOf("('") + 2, thresholdId.indexOf("')"));
124         
125     var innerHtml = "";
126         
127         innerHtml = "<div class='table-btn'><span class='modify_user'><a class=\"btn-xs grey btn-editable\" href='#' onclick=\"pm.threshold.vm.modifyThreshold('" +
128         thresholdId + "')\"><i class=\"ict-modify\"></i>" + $.i18n.prop('com_zte_ums_ict_pm_action_modify') +
129         "</a></span><span><a href='#' class=\"btn-xs grey btn-editable\" onclick=\"pmUtil.congfirmDel('" +
130         thresholdId + "')\"><i class=\"ict-delete\"></i>" + $.i18n.prop('com_zte_ums_ict_pm_action_delete') + "</a></span></div>";      
131
132     return innerHtml;
133 }
134
135 //获取QueryId
136 pmUtil.getUrlParam=function(name){
137                 var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
138                 var search =decodeURIComponent(location.search.substring(1)); //decodeURIComponent() 函数可对 encodeURIComponent() 函数编码的 URI 进行解码。
139                 var r =search.match(reg);  //匹配目标参数
140                 if (r != null) return unescape(r[2]); //unescape() 函数可对通过 escape() 编码的字符串进行解码。
141                 return null; //返回参数值
142 }
143
144
145 pmUtil.addMeatask = function() {
146     newMeataskWizard();
147     pm.meatask.vm.pageTitle="新建任务";
148     pm.meatask.vm.viewVisible=false;
149     pm.meatask.vm.createMeataskVisible=true;
150     pm.meatask.vm.queryMeataskVisible=false;
151 }
152
153 pmUtil.meataskOperate = function (obj) {
154     var record = obj.aData;
155     var meataskId = record.id;
156         //此时的id字段已经被渲染成html片段,如<a href="#" onclick="pm.threshold.vm.queryThreshold('10001')">10001</a>需要提取id
157         meataskId = meataskId.substring(meataskId.indexOf("('") + 2, meataskId.indexOf("')"));
158         
159     var innerHtml = "";
160         
161         innerHtml = 
162         [
163                 "<div class=\"table-btn\">",
164                         "<span class=\"modify_user\">",
165                                 "<a class=\"btn-xs grey btn-editable\" href=\"#\" onclick=\"pmUtil.queryMeatask('" + meataskId +
166                                         "')\"><i class=\"ict-Magnifier\"></i>",
167                                         $.i18n.prop('com_zte_ums_ict_pm_action_query'),
168                                 "</a>",
169                         "</span>",
170                         "<span class=\"modify_user\">",
171                                 "<a class=\"btn-xs grey btn-editable\" href=\"#\" onclick=\"pmUtil.modifyMeatask('" + meataskId + "'," + obj.iDataRow +
172                                         ")\"><i class=\"ict-modify\"></i>",
173                                         $.i18n.prop('com_zte_ums_ict_pm_action_modify'),
174                                 "</a>",
175                         "</span>",
176                         "<span>",
177                                 "<a href=\"#\" class=\"btn-xs grey btn-editable\" onclick=\"pmUtil.deleteMeatask('" + meataskId + "'," + obj.iDataRow +
178                                         ")\"><i class=\"ict-delete\"></i>",
179                                         $.i18n.prop('com_zte_ums_ict_pm_action_delete'),
180                                 "</a>",
181                         "</span>",
182                 "</div>"
183         ].join("\n");
184     return innerHtml;
185 }
186
187 pmUtil.getActiveStatus = function(obj) {
188         var meataskId = obj.aData.id;
189         meataskId = meataskId.substring(meataskId.indexOf("('") + 2, meataskId.indexOf("')"));
190         var activeStatus = obj.aData.activeStatus;
191
192         var prefixStr = "";             
193         var suffixStr = "";     
194         if(activeStatus == 0){
195             prefixStr = "<a  href='#' class=\"label label-sm label-danger\" onclick=\"pm.meatask.vm.changeActiveStatus('"; 
196             suffixStr = ")\">暂停</a>";
197         }else{
198             prefixStr = "<a href='#' class=\"label label-sm label-info\" onclick = \"pm.meatask.vm.changeActiveStatus('";
199             suffixStr = ")\">激活</a>";
200         }
201
202         var innerHtml = prefixStr + meataskId + "'," + activeStatus + "," + obj.iDataRow + suffixStr;
203
204         return innerHtml;
205 }
206
207 pmUtil.granularityRender = function(obj) {
208         var data = obj.aData;
209         switch(data.granularity){
210                 case (300): 
211                         return "5分钟";
212                 case (900): 
213                         return "15分钟";
214                 case (3600): 
215                         return "1小时";
216         }
217         return "5分钟";
218 }
219
220 pmUtil.queryMeatask = function(meataskId) {
221         queryMeataskWizard("queryID");
222         pm.meatask.vm.pageTitle="查询任务";
223     pm.meatask.vm.viewVisible=false;
224     pm.meatask.vm.createMeataskVisible=false;
225     pm.meatask.vm.queryMeataskVisible=true;
226         
227         $("#queryMeataskSubmit").click(function() {
228         pm.meatask.vm.$showResultTable();
229     });
230 }
231
232 pmUtil.meataskIdLink = function(obj) {
233         var meatskId = obj.aData.id;
234         return "<a href='#' onclick=\"pmUtil.viewMeataskById('" + meatskId + "')\">" + meatskId + "</a>";
235 }
236
237 pmUtil.viewMeataskById = function(meatskId) {
238         newMeataskWizard(meatskId);
239     pm.meatask.vm.pageTitle="查看任务";
240     pm.meatask.vm.viewVisible=false;
241     pm.meatask.vm.createMeataskVisible=true;
242     pm.meatask.vm.queryMeataskVisible=false;
243 }
244
245 pmUtil.timeRender = function( obj ){
246     var time=obj.aData[obj.mDataProp];
247         if(time!="0"){
248     var date = new Date(obj.aData[obj.mDataProp]);
249     return commonUtil.parseDate(date , 'yyyy-MM-dd hh:mm:ss');
250         }
251 };
252
253 pmUtil.deleteMeatask=function(meataskId, row){
254         bootbox.confirm("确认删除此任务吗?", function (result) { 
255                 if (result) {
256                         $("#ict_meatask_table_div tr:eq("+(row+1)+")").attr('style', 'display:none;');
257                 }
258         });
259 }
260
261 pmUtil.modifyMeatask = function(meataskId, row) {
262         modifyMeataskWizard("modifyID");
263     pm.meatask.vm.pageTitle="修改任务";
264     pm.meatask.vm.viewVisible=false;
265     pm.meatask.vm.createMeataskVisible=true;
266     pm.meatask.vm.queryMeataskVisible=false;
267 }
268
269 pmUtil.randomCPU = function() {
270         var percent = Math.random()*100;
271         percent = Math.round(percent);
272         return percent + "%";
273 }
274
275 pmUtil.calculateTime = function(obj) {
276         var data = obj.aData;
277         var time = data.beginTime + data.granularity;
278
279         if(time!="0"){
280         var date = new Date(data.granularity * 1000 + obj.aData[obj.mDataProp]);
281         return commonUtil.parseDate(date , 'yyyy-MM-dd hh:mm:ss');
282         }
283 }
284
285 pmUtil.queryMeatask = function(meataskId) {
286         pm.meatask.vm.pageTitle="查询任务";
287     pm.meatask.vm.viewVisible=false;
288     pm.meatask.vm.createMeataskVisible=false;
289     pm.meatask.vm.queryMeataskVisible=true;
290 }
291
292 pmUtil.setDateRange = function (dataRangeId, vm) {
293
294         var open = 'right';
295
296         //var month = $.i18n.prop('ngict-iui-dateRange-month');
297
298         var optionSet1 = {
299
300                 //startDate: moment().subtract(179, 'days'),
301
302                 //endDate: moment(),
303
304                 startDate : "2015-12-31",
305
306                 endDate : "2016-2017-12-31",
307
308                 //format: 'YYYY-MM-DD',
309                 format : 'YYYY-MM-DD h:mm A',
310
311                 dateLimit : {
312                         days : 180
313                 },
314
315                 showWeekNumbers : false,
316
317                 timePicker : true,
318                 timePickerIncrement : 5,
319
320                 opens : open,
321
322                 separator : ' - ',
323
324                 locale : {
325
326                         applyLabel : $.i18n.prop('ngict-iui-dateRange-applyLabel'),
327
328                         cancelLabel : $.i18n.prop('ngict-iui-dateRange-cancelLabel'),
329
330                         fromLabel : $.i18n.prop('ngict-iui-dateRange-fromLabel'),
331
332                         toLabel : $.i18n.prop('ngict-iui-dateRange-toLabel'),
333
334                         customRangeLabel : $.i18n.prop('ngict-iui-dateRange-customRangeLabel'),
335
336                         daysOfWeek : [
337
338                                 $.i18n.prop('ngict-iui-dateRange-Sunday'),
339
340                                 $.i18n.prop('ngict-iui-dateRange-Monday'),
341
342                                 $.i18n.prop('ngict-iui-dateRange-Tuesday'),
343
344                                 $.i18n.prop('ngict-iui-dateRange-Wednesday'),
345
346                                 $.i18n.prop('ngict-iui-dateRange-Thursday'),
347
348                                 $.i18n.prop('ngict-iui-dateRange-Friday'),
349
350                                 $.i18n.prop('ngict-iui-dateRange-Saturday')],
351
352                         //monthNames : ['1' + month, '2' + month, '3' + month, '4' + month, '5' + month, '6' + month, '7' + month, '8' + month, '9' + month, '10' + month, '11' + month, '12' + month],
353                         
354                         //months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
355                         
356                         monthNames: [$.i18n.prop('ngict-iui-dateRange-Jan'),
357                                                  $.i18n.prop('ngict-iui-dateRange-Feb'),
358                                                  $.i18n.prop('ngict-iui-dateRange-Mar'),
359                                                  $.i18n.prop('ngict-iui-dateRange-Apr'),
360                                                  $.i18n.prop('ngict-iui-dateRange-May'),
361                                                  $.i18n.prop('ngict-iui-dateRange-Jun'),
362                                                  $.i18n.prop('ngict-iui-dateRange-Jul'),
363                                                  $.i18n.prop('ngict-iui-dateRange-Aug'),
364                                                  $.i18n.prop('ngict-iui-dateRange-Sep'),
365                                                  $.i18n.prop('ngict-iui-dateRange-Oct'),
366                                                  $.i18n.prop('ngict-iui-dateRange-Nov'),
367                                                  $.i18n.prop('ngict-iui-dateRange-Dec')],
368
369                         firstDay : 1
370
371                 }
372
373         };
374
375         //datarangepicker确认
376         $('input[id="' + dataRangeId + '"]').bind('apply.daterangepicker', function () {
377
378                 //获取时间范围,查询
379
380                 // 都设置为0点
381
382                 pmUtil.setTime($(this));
383
384                 // 更新tooltip内容
385                 //$(this).attr("data-original-title", $(this).val());
386
387         });
388
389         $('input[id="' + dataRangeId + '"]').daterangepicker(optionSet1);
390
391 };
392
393 //daterangepicker设置时间
394 pmUtil.setTime = function setTime($obj) {
395
396         //var title = $obj.attr('dtitle');
397
398         var arr = $obj.val().split(' - ');
399
400         //排除日历组件空字符串的情况。
401
402         if (arr[0] != '') {
403
404                 var beginTime = new Date(arr[0].replace(/-/g, "/")).getTime();
405
406                 var endTime = new Date(arr[1].replace(/-/g, "/")).getTime();
407                 
408                 pm.query.vmPmQuery.beginTime = beginTime;
409
410                 pm.query.vmPmQuery.endTime = endTime;           
411
412         }
413
414 }
415
416 pmUtil.formatTime = function formatTime(ms) {
417         var date = new Date(ms);
418         return date.format("yyyy-MM-dd hh:mm:ss");
419 }
420
421 /*格式化日期*/
422
423 Date.prototype.format = function (format) {
424
425         /*
426
427          * format="yyyy-MM-dd hh:mm:ss";
428
429          */
430
431         var o = {
432
433                 "M+" : this.getMonth() + 1,
434
435                 "d+" : this.getDate(),
436
437                 "h+" : this.getHours(),
438
439                 "m+" : this.getMinutes(),
440
441                 "s+" : this.getSeconds(),
442
443                 "q+" : Math.floor((this.getMonth() + 3) / 3),
444
445                 "S" : this.getMilliseconds()
446
447         }
448
449         if (/(y+)/.test(format)) {
450
451                 format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4
452
453                                          - RegExp.$1.length));
454
455         }
456
457         for (var k in o) {
458
459                 if (new RegExp("(" + k + ")").test(format)) {
460
461                         format = format.replace(RegExp.$1, RegExp.$1.length == 1
462
463                                          ? o[k]
464
465                                          : ("00" + o[k]).substr(("" + o[k]).length));
466
467                 }
468
469         }
470
471         return format;
472
473 }