1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package ohos.devtools.views.distributed.util; 17 18 import ohos.devtools.views.applicationtrace.util.MathUtils; 19 import ohos.devtools.views.distributed.bean.DetailBean; 20 import ohos.devtools.views.distributed.bean.DistributedFuncBean; 21 22 import java.util.List; 23 import java.util.Objects; 24 import java.util.stream.Collectors; 25 26 /** 27 * DistributedDataPraser 28 * 29 * @since 2021/08/10 16:20 30 */ 31 public class DistributedDataPraser { 32 /** 33 * collectByName 34 * 35 * @param list list 36 * @return DetailBean DetailBean 37 */ collectByName(List<DistributedFuncBean> list)38 public static DetailBean collectByName(List<DistributedFuncBean> list) { 39 DetailBean detailBean = new DetailBean(); 40 if (list.size() == 0) { 41 return detailBean; 42 } 43 List<Long> sortList = list.stream().map(DistributedFuncBean::getDur).sorted().collect(Collectors.toList()); 44 if (sortList.size() > 1 && sortList.size() % 2 == 1) { 45 detailBean.setMiddleNs((sortList.get(sortList.size() / 2) + sortList.get(sortList.size() / 2 + 1)) / 2); 46 } else { 47 detailBean.setMiddleNs(sortList.get(sortList.size() / 2)); 48 } 49 detailBean.setAvgNs( 50 Double.valueOf(MathUtils.average(sortList.stream().map(Long::doubleValue).collect(Collectors.toList()))) 51 .longValue()); 52 53 // Set delay middle and avg 54 List<Long> delaySortList = list.stream().map(DistributedFuncBean::getDelay).filter(Objects::nonNull).sorted() 55 .collect(Collectors.toList()); 56 if (delaySortList.size() > 1 && delaySortList.size() % 2 == 1) { 57 detailBean.setMiddleDelayNS( 58 (delaySortList.get(delaySortList.size() / 2) + delaySortList.get(delaySortList.size() / 2 + 1)) / 2); 59 } else { 60 detailBean.setMiddleDelayNS(delaySortList.get(delaySortList.size() / 2)); 61 } 62 detailBean.setDelayAvgNs(Double 63 .valueOf(MathUtils.average(delaySortList.stream().map(Long::doubleValue).collect(Collectors.toList()))) 64 .longValue()); 65 return detailBean; 66 } 67 } 68