Search in sources :

Example 26 with MetricData

use of com.vmware.flowgate.common.model.MetricData in project flowgate by vmware.

the class AssetService method getOtherMetricsDataById.

private List<MetricData> getOtherMetricsDataById(String assetID, Long starttime, Integer duration) {
    List<MetricData> metricDataList = new ArrayList<>();
    List<RealTimeData> realTimeData = realtimeDataRepository.getDataByIDAndTimeRange(assetID, starttime, duration);
    if (realTimeData == null || realTimeData.isEmpty()) {
        return metricDataList;
    }
    RealTimeData latestData = findLatestData(realTimeData);
    List<ValueUnit> latestDataValues = latestData.getValues();
    if (latestDataValues == null || latestDataValues.isEmpty()) {
        return metricDataList;
    }
    metricDataList.addAll(generateOtherMetricData(latestDataValues));
    return metricDataList;
}
Also used : RealTimeData(com.vmware.flowgate.common.model.RealTimeData) ArrayList(java.util.ArrayList) ValueUnit(com.vmware.flowgate.common.model.ValueUnit) MetricData(com.vmware.flowgate.common.model.MetricData)

Example 27 with MetricData

use of com.vmware.flowgate.common.model.MetricData in project flowgate by vmware.

the class AssetService method generateMetricsDataForPDU.

private List<MetricData> generateMetricsDataForPDU(List<ValueUnit> valueunits) {
    List<MetricData> result = new ArrayList<MetricData>();
    for (ValueUnit valueunit : valueunits) {
        MetricData metricData = new MetricData();
        metricData.setTimeStamp(valueunit.getTime());
        metricData.setValue(valueunit.getValue());
        metricData.setValueNum(valueunit.getValueNum());
        String unit = databaseUnitAndOutputUnitMap.get(valueunit.getUnit());
        if (unit == null) {
            unit = valueunit.getUnit();
        }
        metricData.setUnit(unit);
        String extraidentifier = valueunit.getExtraidentifier();
        switch(valueunit.getKey()) {
            case MetricName.PDU_ACTIVE_POWER:
                // PDU|INLET:1|ActivePower
                metricData.setMetricName(String.format(MetricName.PDU_XLET_ACTIVE_POWER, extraidentifier));
                result.add(metricData);
                break;
            case MetricName.PDU_APPARENT_POWER:
                metricData.setMetricName(String.format(MetricName.PDU_XLET_APPARENT_POWER, extraidentifier));
                result.add(metricData);
                break;
            case MetricName.PDU_CURRENT:
                if (extraidentifier != null) {
                    String[] extraidentifierList = extraidentifier.split("\\|");
                    if (extraidentifierList.length == 1) {
                        metricData.setMetricName(String.format(MetricName.PDU_XLET_CURRENT, extraidentifier));
                    } else if (extraidentifierList.length == 2) {
                        String inlet = extraidentifierList[0];
                        String pole = extraidentifierList[1];
                        metricData.setMetricName(String.format(MetricName.PDU_INLET_XPOLE_CURRENT, inlet, pole));
                    }
                    result.add(metricData);
                } else {
                    metricData.setMetricName(MetricName.PDU_TOTAL_CURRENT);
                    result.add(metricData);
                }
                break;
            case MetricName.PDU_VOLTAGE:
                if (extraidentifier != null) {
                    String[] extraidentifierList = extraidentifier.split("\\|");
                    if (extraidentifierList.length == 1) {
                        metricData.setMetricName(String.format(MetricName.PDU_XLET_VOLTAGE, extraidentifier));
                    } else if (extraidentifierList.length == 2) {
                        String inlet = extraidentifierList[0];
                        String pole = extraidentifierList[1];
                        metricData.setMetricName(String.format(MetricName.PDU_INLET_XPOLE_VOLTAGE, inlet, pole));
                    }
                    result.add(metricData);
                }
                break;
            case MetricName.PDU_FREE_CAPACITY:
                if (extraidentifier != null) {
                    String[] extraidentifierList = extraidentifier.split("\\|");
                    if (extraidentifierList.length == 1) {
                        metricData.setMetricName(String.format(MetricName.PDU_XLET_FREE_CAPACITY, extraidentifier));
                    } else if (extraidentifierList.length == 2) {
                        String inlet = extraidentifierList[0];
                        String pole = extraidentifierList[1];
                        metricData.setMetricName(String.format(MetricName.PDU_INLET_XPOLE_FREE_CAPACITY, inlet, pole));
                    }
                    result.add(metricData);
                }
                break;
            case MetricName.PDU_CURRENT_LOAD:
                metricData.setMetricName(MetricName.PDU_CURRENT_LOAD);
                result.add(metricData);
                break;
            case MetricName.PDU_POWER_LOAD:
                metricData.setMetricName(MetricName.PDU_POWER_LOAD);
                result.add(metricData);
                break;
            case MetricName.PDU_TOTAL_POWER:
                metricData.setMetricName(MetricName.PDU_TOTAL_POWER);
                result.add(metricData);
                break;
            case MetricName.PDU_HUMIDITY:
                metricData.setMetricName(String.format(MetricKeyName.PDU_HUMIDITY_LOCATIONX, extraidentifier));
                result.add(metricData);
                break;
            case MetricName.PDU_TEMPERATURE:
                metricData.setMetricName(String.format(MetricKeyName.PDU_TEMPERATURE_LOCATIONX, extraidentifier));
                result.add(metricData);
                break;
            default:
                break;
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) ValueUnit(com.vmware.flowgate.common.model.ValueUnit) MetricData(com.vmware.flowgate.common.model.MetricData)

Example 28 with MetricData

use of com.vmware.flowgate.common.model.MetricData in project flowgate by vmware.

the class AssetService method getServerHostMetric.

private List<MetricData> getServerHostMetric(Asset server, long starttime, int duration) {
    List<MetricData> metricDataList = Lists.newArrayList();
    String hostMetricsFormulaString = server.getMetricsformulars().get(FlowgateConstant.HOST_METRICS);
    if (StringUtils.isBlank(hostMetricsFormulaString)) {
        return metricDataList;
    }
    Map<String, String> hostMetricsFormula = server.metricsFormulaToMap(hostMetricsFormulaString, new TypeReference<Map<String, String>>() {
    });
    if (hostMetricsFormula == null || hostMetricsFormula.isEmpty()) {
        return metricDataList;
    }
    Map<String, List<String>> assetIdAndMetricsNameList = Maps.newHashMap();
    for (Map.Entry<String, String> itemEntry : hostMetricsFormula.entrySet()) {
        List<String> metricsNameList = assetIdAndMetricsNameList.computeIfAbsent(itemEntry.getValue(), k -> Lists.newArrayList());
        metricsNameList.add(itemEntry.getKey());
    }
    Map<String, List<RealTimeData>> realtimeDataMap = Maps.newHashMap();
    for (Map.Entry<String, List<String>> entry : assetIdAndMetricsNameList.entrySet()) {
        realtimeDataMap.put(entry.getKey(), realtimeDataRepository.getDataByIDAndTimeRange(entry.getKey(), starttime, duration));
    }
    Set<String> specialMetricNames = new HashSet<String>();
    specialMetricNames.add(MetricName.SERVER_AVERAGE_USED_POWER);
    specialMetricNames.add(MetricName.SERVER_PEAK_USED_POWER);
    specialMetricNames.add(MetricName.SERVER_MINIMUM_USED_POWER);
    specialMetricNames.add(MetricName.SERVER_ENERGY_CONSUMPTION);
    specialMetricNames.add(MetricName.SERVER_AVERAGE_TEMPERATURE);
    specialMetricNames.add(MetricName.SERVER_PEAK_TEMPERATURE);
    MetricData metricData;
    for (Map.Entry<String, List<RealTimeData>> realtimeDataEntry : realtimeDataMap.entrySet()) {
        List<String> metricsNameList = assetIdAndMetricsNameList.get(realtimeDataEntry.getKey());
        for (RealTimeData realTimeData : realtimeDataEntry.getValue()) {
            for (ValueUnit valueUnit : realTimeData.getValues()) {
                if (metricsNameList.contains(valueUnit.getKey())) {
                    metricData = new MetricData();
                    long timestamp = 0l;
                    String metricName = valueUnit.getKey();
                    String extraInfo = valueUnit.getExtraidentifier();
                    if (extraInfo == null && specialMetricNames.contains(metricName)) {
                        logger.error("The value of {} is invalid,RealtimeData Id: {}", metricName, realTimeData.getId());
                        continue;
                    }
                    switch(metricName) {
                        case MetricName.SERVER_AVERAGE_USED_POWER:
                            // Time of AvgPower is since time
                            timestamp = Long.valueOf(extraInfo);
                            break;
                        case MetricName.SERVER_PEAK_USED_POWER:
                            String[] sinceTimeAndPeakTime = extraInfo.split(FlowgateConstant.SEPARATOR);
                            if (sinceTimeAndPeakTime.length < TIMESTAMPARRAYLENGTH) {
                                logger.error("The extraInfo of {} is invalid,RealtimeData Id: {}", metricName, realTimeData.getId());
                                continue;
                            }
                            // Time of PeakPower is peakPower time
                            timestamp = Long.valueOf(sinceTimeAndPeakTime[1]);
                            break;
                        case MetricName.SERVER_MINIMUM_USED_POWER:
                            String[] sinceTimeAndMinimumTime = extraInfo.split(FlowgateConstant.SEPARATOR);
                            if (sinceTimeAndMinimumTime.length < TIMESTAMPARRAYLENGTH) {
                                logger.error("The extraInfo of {} is invalid,RealtimeData Id: {}", metricName, realTimeData.getId());
                                continue;
                            }
                            // Time of MinimumPower is minimumPower time
                            timestamp = Long.valueOf(sinceTimeAndMinimumTime[1]);
                            break;
                        case MetricName.SERVER_ENERGY_CONSUMPTION:
                            // Time of energy consumption is since time
                            timestamp = Long.valueOf(extraInfo);
                            break;
                        case MetricName.SERVER_AVERAGE_TEMPERATURE:
                            // Time of average temperature is since time
                            timestamp = Long.valueOf(extraInfo);
                            break;
                        case MetricName.SERVER_PEAK_TEMPERATURE:
                            // Time of energy consumption is since time
                            String[] temperatureSinceTimeAndPeakTime = extraInfo.split(FlowgateConstant.SEPARATOR);
                            if (temperatureSinceTimeAndPeakTime.length < TIMESTAMPARRAYLENGTH) {
                                logger.error("The extraInfo of {} is invalid,RealtimeData Id: {}", metricName, realTimeData.getId());
                                continue;
                            }
                            // Time of PeakTemperature is peak time
                            timestamp = Long.valueOf(temperatureSinceTimeAndPeakTime[1]);
                            break;
                        default:
                            // Time of other host metric is valueUnit current time
                            timestamp = valueUnit.getTime();
                            break;
                    }
                    metricData.setMetricName(valueUnit.getKey());
                    metricData.setTimeStamp(timestamp);
                    metricData.setValue(valueUnit.getValue());
                    metricData.setValueNum(valueUnit.getValueNum());
                    // This kind of logic will be deprecated in next version
                    String unit = databaseUnitAndOutputUnitMap.get(valueUnit.getUnit());
                    if (unit == null) {
                        unit = valueUnit.getUnit();
                    }
                    metricData.setUnit(unit);
                    metricDataList.add(metricData);
                }
            }
        }
    }
    return metricDataList;
}
Also used : RealTimeData(com.vmware.flowgate.common.model.RealTimeData) List(java.util.List) ArrayList(java.util.ArrayList) ValueUnit(com.vmware.flowgate.common.model.ValueUnit) Map(java.util.Map) HashMap(java.util.HashMap) MetricData(com.vmware.flowgate.common.model.MetricData) HashSet(java.util.HashSet)

Example 29 with MetricData

use of com.vmware.flowgate.common.model.MetricData in project flowgate by vmware.

the class AssetService method translateToMetricDataForServer.

private List<MetricData> translateToMetricDataForServer(Map<String, List<ValueUnit>> assetAndValueUnitsMap, Asset asset) {
    List<MetricData> metricDatas = new ArrayList<MetricData>();
    Map<String, Map<String, String>> displayNameAndFormulasMap = getMetricDispalyNameAndFormulaMapForServer(asset);
    if (displayNameAndFormulasMap.isEmpty() || !displayNameAndFormulasMap.containsKey(DISPLAYNAMEANDFORMULANAMEMAPKEY)) {
        return metricDatas;
    }
    // The displayNameAndFormulaKeyMap use to find a method to translate the valueUnit to MetricData
    Map<String, String> displayNameAndFormulaKeyMap = displayNameAndFormulasMap.get(DISPLAYNAMEANDFORMULANAMEMAPKEY);
    displayNameAndFormulasMap.remove(DISPLAYNAMEANDFORMULANAMEMAPKEY);
    CompareValueUnitByTime comparator = new CompareValueUnitByTime();
    // For server-voltage, the server voltage is the server-connected-pdu-outlet voltage
    // If the Outlet voltage is null, use the inlet voltage
    // If the server asset has multiple pdus, only use one of these pdus
    Map<String, List<ValueUnit>> serverVoltageIdAndValues = new HashMap<String, List<ValueUnit>>();
    List<ValueUnit> serverVoltages = null;
    String serverVoltageFormula = null;
    for (Map.Entry<String, Map<String, String>> displayNameAndFormulaEntry : displayNameAndFormulasMap.entrySet()) {
        String displayName = displayNameAndFormulaEntry.getKey();
        Map<String, String> valueUnitNameAndFormulaMap = displayNameAndFormulaEntry.getValue();
        // Key:MetricName Value:Formula
        if (displayName.equals(MetricName.SERVER_VOLTAGE)) {
            serverVoltageFormula = new ArrayList<String>(valueUnitNameAndFormulaMap.values()).get(0);
            continue;
        }
        for (Map.Entry<String, String> valueUnitNameAndFormula : valueUnitNameAndFormulaMap.entrySet()) {
            String valueUnitName = valueUnitNameAndFormula.getKey();
            String formula = valueUnitNameAndFormula.getValue();
            String[] ids = formula.split("\\+|-|\\*|/|\\(|\\)");
            Map<String, List<ValueUnit>> idAndValues = new HashMap<>();
            for (String id : ids) {
                List<ValueUnit> inletVoltages = null;
                List<ValueUnit> outletVoltages = null;
                if (displayName.contains(FlowgateConstant.OUTLET_NAME_PREFIX) && MetricName.PDU_VOLTAGE.equals(valueUnitName)) {
                    inletVoltages = new ArrayList<ValueUnit>();
                    outletVoltages = new ArrayList<ValueUnit>();
                }
                List<ValueUnit> valueUnits = new ArrayList<ValueUnit>();
                if (!assetAndValueUnitsMap.containsKey(id)) {
                    continue;
                }
                for (ValueUnit valueUnit : assetAndValueUnitsMap.get(id)) {
                    if (!valueUnitName.equals(valueUnit.getKey())) {
                        continue;
                    }
                    String extraidentifier = valueUnit.getExtraidentifier();
                    String outlet = null;
                    switch(valueUnitName) {
                        case MetricName.PDU_CURRENT:
                            // PDU:23551d6dacf2432c8a3edbc6bbc922cd|OUTLET:1|Current
                            if (displayName.contains(FlowgateConstant.OUTLET_NAME_PREFIX) && extraidentifier != null) {
                                outlet = displayName.split("\\|")[1];
                                if (outlet.equals(extraidentifier)) {
                                    valueUnits.add(valueUnit);
                                }
                            }
                            // PDU:23551d6dacf2432c8a3edbc6bbc922cd|Current
                            if (!displayName.contains(FlowgateConstant.OUTLET_NAME_PREFIX)) {
                                if (extraidentifier == null) {
                                    // PDU current
                                    valueUnits.add(valueUnit);
                                }
                            }
                            break;
                        case MetricName.PDU_APPARENT_POWER:
                            // Sample value of displayName: PDU:23551d6dacf2432c8a3edbc6bbc922cd|OUTLET:1|Power
                            outlet = displayName.split("\\|")[1];
                            if (outlet.equals(extraidentifier)) {
                                valueUnits.add(valueUnit);
                            }
                            break;
                        case // valueunitName
                        MetricName.PDU_VOLTAGE:
                            // DisplayName: PDU:23551d6dacf2432c8a3edbc6bbc922cd|OUTLET:1|Voltage
                            if (extraidentifier == null) {
                                continue;
                            } else {
                                outlet = displayName.split("\\|")[1];
                                if (outlet.equals(extraidentifier)) {
                                    outletVoltages.add(valueUnit);
                                } else if (extraidentifier.indexOf(FlowgateConstant.INLET_NAME_PREFIX) > 0) {
                                    inletVoltages.add(valueUnit);
                                }
                            }
                            break;
                        case // Power
                        MetricName.SERVER_POWER:
                            if (MetricName.SERVER_POWER.equals(displayName)) {
                                // When the displayName is Power,the valueUnits is only from server_Host_metrics
                                // Sample value of displayName: Power
                                valueUnits.add(valueUnit);
                            } else {
                                // Sample value of displayName: PDU:23551d6dacf2432c8a3edbc6bbc922cd|Power
                                if (extraidentifier == null) {
                                    valueUnits.add(valueUnit);
                                }
                            }
                            break;
                        default:
                            valueUnits.add(valueUnit);
                            break;
                    }
                }
                if (displayName.contains(FlowgateConstant.OUTLET_NAME_PREFIX) && MetricName.PDU_VOLTAGE.equals(valueUnitName)) {
                    valueUnits = outletVoltages;
                    if (serverVoltages == null || serverVoltages.isEmpty()) {
                        if (outletVoltages.isEmpty()) {
                            if (inletVoltages.isEmpty()) {
                                continue;
                            }
                            outletVoltages = inletVoltages;
                        }
                        serverVoltages = outletVoltages;
                        if (serverVoltages.isEmpty()) {
                            continue;
                        }
                        serverVoltageIdAndValues.put(id, serverVoltages);
                    }
                }
                if (valueUnits.isEmpty()) {
                    continue;
                }
                Collections.sort(valueUnits, comparator);
                idAndValues.put(id, valueUnits);
            }
            if (!idAndValues.isEmpty()) {
                // <id,valueUnits>
                Function<TranslateContext, MetricData> function = TranslateFunctionService.convert;
                String formulaKey = displayNameAndFormulaKeyMap.get(displayName);
                if (TranslateFunctionService.serverFormulaKeyAndFunction.containsKey(formulaKey)) {
                    function = TranslateFunctionService.serverFormulaKeyAndFunction.get(formulaKey);
                }
                metricDatas.addAll(convertValueUnitToMetricData(displayName, function, formula, idAndValues));
            }
        }
    }
    if (!serverVoltageIdAndValues.isEmpty()) {
        Function<TranslateContext, MetricData> function = TranslateFunctionService.convert;
        String formulaKey = displayNameAndFormulaKeyMap.get(MetricName.SERVER_VOLTAGE);
        if (TranslateFunctionService.serverFormulaKeyAndFunction.containsKey(formulaKey)) {
            function = TranslateFunctionService.serverFormulaKeyAndFunction.get(formulaKey);
        }
        metricDatas.addAll(convertValueUnitToMetricData(MetricName.SERVER_VOLTAGE, function, serverVoltageFormula, serverVoltageIdAndValues));
    }
    return metricDatas;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TranslateContext(com.vmware.flowgate.common.model.TranslateContext) List(java.util.List) ArrayList(java.util.ArrayList) ValueUnit(com.vmware.flowgate.common.model.ValueUnit) Map(java.util.Map) HashMap(java.util.HashMap) MetricData(com.vmware.flowgate.common.model.MetricData)

Example 30 with MetricData

use of com.vmware.flowgate.common.model.MetricData in project flowgate by vmware.

the class TranslateFunctionServiceTest method testServerConvert.

@Test
public void testServerConvert() {
    String assetId = "752c0c7637104a39a4242031cd48785e";
    long currentTimeMillis = System.currentTimeMillis();
    ValueUnit valueUnit = new ValueUnit();
    valueUnit.setTime(currentTimeMillis);
    valueUnit.setKey(MetricName.SERVER_CPUUSAGE);
    valueUnit.setUnit(ValueUnit.MetricUnit.percent.name());
    valueUnit.setValueNum(4.67);
    String displayName = MetricName.SERVER_CPUUSAGE;
    Function<TranslateContext, MetricData> function = TranslateFunctionService.convert;
    Map<String, ValueUnit> valueUnitMap = new HashMap<>();
    valueUnitMap.put(assetId, valueUnit);
    TranslateContext translateContext = TranslateContext.buildByValueUnitsAndDisplayNameAndFormula(valueUnitMap, displayName, assetId);
    MetricData metricData = function.apply(translateContext);
    TestCase.assertEquals(valueUnit.getKey(), metricData.getMetricName());
    TestCase.assertEquals(valueUnit.getTime(), metricData.getTimeStamp());
    TestCase.assertEquals(valueUnit.getValueNum(), metricData.getValueNum());
    TestCase.assertEquals(valueUnit.getUnit(), metricData.getUnit());
}
Also used : HashMap(java.util.HashMap) ValueUnit(com.vmware.flowgate.common.model.ValueUnit) TranslateContext(com.vmware.flowgate.common.model.TranslateContext) MetricData(com.vmware.flowgate.common.model.MetricData) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

MetricData (com.vmware.flowgate.common.model.MetricData)35 ArrayList (java.util.ArrayList)25 HashMap (java.util.HashMap)24 ValueUnit (com.vmware.flowgate.common.model.ValueUnit)23 Test (org.junit.Test)23 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)23 Asset (com.vmware.flowgate.common.model.Asset)16 RealTimeData (com.vmware.flowgate.common.model.RealTimeData)16 TranslateContext (com.vmware.flowgate.common.model.TranslateContext)13 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)13 MvcResult (org.springframework.test.web.servlet.MvcResult)8 Map (java.util.Map)7 List (java.util.List)6 FieldDescriptor (org.springframework.restdocs.payload.FieldDescriptor)4 HashSet (java.util.HashSet)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 AssetIPMapping (com.vmware.flowgate.common.model.AssetIPMapping)1 IntegrationStatus (com.vmware.flowgate.common.model.IntegrationStatus)1 ServerMapping (com.vmware.flowgate.common.model.ServerMapping)1 MetricClient (com.vmware.flowgate.vroworker.vro.MetricClient)1