Search in sources :

Example 11 with MetricData

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

the class AssetControllerTest method testGetMetricsDurationAPI.

@Test
public void testGetMetricsDurationAPI() throws Exception {
    FieldDescriptor[] fieldpath = new FieldDescriptor[] { fieldWithPath("metricName").description("metric name").type(JsonFieldType.STRING), fieldWithPath("valueNum").description("valueNum.").type(JsonFieldType.NUMBER), fieldWithPath("value").description("value").type(JsonFieldType.NULL), fieldWithPath("unit").description("metric unit").type(JsonFieldType.STRING), fieldWithPath("timeStamp").description("timeStamp").type(JsonFieldType.NUMBER) };
    Asset asset = createAsset();
    List<RealTimeData> realTimeDatas = new ArrayList<RealTimeData>();
    long time = System.currentTimeMillis();
    int duration = 30 * 60 * 1000;
    long startTime = time - duration;
    RealTimeData pduUsageMetricData = createPduAllRealTimeData(startTime);
    pduUsageMetricData.setAssetID("0001bdc8b25d4c2badfd045ab61aabfa");
    RealTimeData tempRealTimeData = createTemperatureSensorRealtimeData(startTime, "00027ca37b004a9890d1bf20349d5ac1");
    RealTimeData humdityRealTimeData = createHumiditySensorRealtimeData(startTime, "34527ca37b004a9890d1bf20349d5ac1");
    RealTimeData backTemperature = createBackTemperatureSensorRealtimeData(startTime, "968765a37b004a9890d1bf20349d5ac1");
    RealTimeData backHumidity = createBackHumiditySensorRealtimeData(startTime, "486970a37b004a9890d1bf20349d5ac1");
    RealTimeData hostRealTimeData = createServerHostRealTimeData(startTime);
    hostRealTimeData.setAssetID(asset.getId());
    realTimeDatas.add(hostRealTimeData);
    realTimeDatas.add(humdityRealTimeData);
    realTimeDatas.add(tempRealTimeData);
    realTimeDatas.add(backHumidity);
    realTimeDatas.add(backTemperature);
    realTimeDatas.add(pduUsageMetricData);
    realtimeDataRepository.saveAll(realTimeDatas);
    asset = fillingMetricsformula(asset);
    HashMap<String, String> justificationfields = new HashMap<>();
    justificationfields.put(FlowgateConstant.PDU_PORT_FOR_SERVER, "power-2_FIELDSPLIT_CAN1-MDF-R01-PDU-BUILDING_FIELDSPLIT_OUTLET:1_FIELDSPLIT_0001bdc8b25d4c2badfd045ab61aabfa");
    asset.setJustificationfields(justificationfields);
    asset = assetRepository.save(asset);
    MvcResult result = this.mockMvc.perform(get("/v1/assets/{assetId}/metrics", asset.getId()).param("starttime", String.valueOf(startTime)).param("duration", String.valueOf(duration))).andDo(document("assets-getAllMetricsDataInDuration-Server-example", pathParameters(parameterWithName("assetId").description("The id of asset,generated by flowgate.")), requestParameters(parameterWithName("starttime").description("Start time of you want to query.Default value: the system current time in Millis").optional(), parameterWithName("duration").description("Duration of you want to query.Default value: 300000 ms").optional()), responseFields(fieldWithPath("[]").description("An array of realTimeDatas")).andWithPrefix("[].", fieldpath))).andReturn();
    String res = result.getResponse().getContentAsString();
    MetricData[] metricDatas = mapper.readValue(res, MetricData[].class);
    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_AVERAGE_TEMPERATURE);
    specialMetricNames.add(MetricName.SERVER_PEAK_TEMPERATURE);
    for (MetricData serverdata : metricDatas) {
        String metricName = serverdata.getMetricName();
        if (String.format(MetricKeyName.SERVER_CONNECTED_PDUX_OUTLETX_CURRENT, "0001bdc8b25d4c2badfd045ab61aabfa", "OUTLET:1").equals(metricName)) {
            TestCase.assertEquals(0.365, serverdata.getValueNum());
        } else if (String.format(MetricKeyName.SERVER_CONNECTED_PDUX_OUTLETX_POWER, "0001bdc8b25d4c2badfd045ab61aabfa", "OUTLET:1").equals(metricName)) {
            TestCase.assertEquals(0.081, serverdata.getValueNum());
        } else if (String.format(MetricKeyName.SERVER_CONNECTED_PDUX_OUTLETX_VOLTAGE, "0001bdc8b25d4c2badfd045ab61aabfa", "OUTLET:1").equals(metricName)) {
            TestCase.assertEquals(221.0, serverdata.getValueNum());
        } else if (String.format(MetricKeyName.SERVER_CONNECTED_PDUX_POWER_LOAD, "0001bdc8b25d4c2badfd045ab61aabfa").equals(metricName)) {
            TestCase.assertEquals(0.05, serverdata.getValueNum());
        } else if (String.format(MetricKeyName.SERVER_CONNECTED_PDUX_CURRENT_LOAD, "0001bdc8b25d4c2badfd045ab61aabfa").equals(metricName)) {
            TestCase.assertEquals(0.05, serverdata.getValueNum());
        } else if (String.format(MetricKeyName.SERVER_CONNECTED_PDUX_TOTAL_CURRENT, "0001bdc8b25d4c2badfd045ab61aabfa").equals(metricName)) {
            TestCase.assertEquals(1.455, serverdata.getValueNum());
        } else if (String.format(MetricKeyName.SERVER_CONNECTED_PDUX_TOTAL_POWER, "0001bdc8b25d4c2badfd045ab61aabfa").equals(metricName)) {
            TestCase.assertEquals(0.322, serverdata.getValueNum());
        } else if (String.format(MetricKeyName.SERVER_BACK_HUMIDITY_LOCATIONX, "OUTLET").equals(metricName)) {
            TestCase.assertEquals(19.0, serverdata.getValueNum());
        } else if (String.format(MetricKeyName.SERVER_BACK_TEMPREATURE_LOCATIONX, "OUTLET").equals(metricName)) {
            TestCase.assertEquals(25.0, serverdata.getValueNum());
        } else if (String.format(MetricKeyName.SERVER_FRONT_HUMIDITY_LOCATIONX, "INLET").equals(metricName)) {
            TestCase.assertEquals(serverdata.getValueNum(), 20.0);
        } else if (String.format(MetricKeyName.SERVER_FRONT_TEMPERATURE_LOCATIONX, "INLET").equals(metricName)) {
            TestCase.assertEquals(serverdata.getValueNum(), 32.0);
        } else if (MetricName.SERVER_VOLTAGE.equals(metricName)) {
            TestCase.assertEquals(221.0, serverdata.getValueNum());
        } else if (MetricName.SERVER_STORAGEUSAGE.equals(metricName)) {
            if (serverdata.getTimeStamp() == startTime + 240000) {
                TestCase.assertEquals(65.0, serverdata.getValueNum());
            }
        } else if (MetricName.SERVER_MEMORYUSAGE.equals(metricName)) {
            if (serverdata.getTimeStamp() == startTime + 20000) {
                TestCase.assertEquals(87.22, serverdata.getValueNum());
            }
        } else if (MetricName.SERVER_CPUUSEDINMHZ.equals(metricName)) {
            if (serverdata.getTimeStamp() == startTime + 20000) {
                TestCase.assertEquals(746.00, serverdata.getValueNum());
            }
        } else if (MetricName.SERVER_CPUUSAGE.equals(metricName)) {
            if (serverdata.getTimeStamp() == startTime + 20000) {
                TestCase.assertEquals(4.67, serverdata.getValueNum());
            }
        } else if (MetricName.SERVER_ACTIVEMEMORY.equals(metricName)) {
            if (serverdata.getTimeStamp() == startTime + 20000) {
                TestCase.assertEquals(1561416.00, serverdata.getValueNum());
            }
        } else if (MetricName.SERVER_SHAREDMEMORY.equals(metricName)) {
            if (serverdata.getTimeStamp() == startTime + 20000) {
                TestCase.assertEquals(8.00, serverdata.getValueNum());
            }
        } else if (MetricName.SERVER_CONSUMEDMEMORY.equals(metricName)) {
            if (serverdata.getTimeStamp() == startTime + 20000) {
                TestCase.assertEquals(18291220.00, serverdata.getValueNum());
            }
        } else if (MetricName.SERVER_SWAPMEMORY.equals(metricName)) {
            TestCase.assertEquals(0.00, serverdata.getValueNum());
        } else if (MetricName.SERVER_BALLOONMEMORY.equals(metricName)) {
            TestCase.assertEquals(0.0, serverdata.getValueNum());
        } else if (MetricName.SERVER_NETWORKUTILIZATION.equals(metricName)) {
            if (serverdata.getTimeStamp() == startTime + 20000) {
                TestCase.assertEquals(146.00, serverdata.getValueNum());
            }
        } else if (MetricName.SERVER_STORAGEIORATEUSAGE.equals(metricName)) {
            if (serverdata.getTimeStamp() == startTime + 20000) {
                TestCase.assertEquals(330.00, serverdata.getValueNum());
            }
        } else if (MetricName.SERVER_POWER.equals(metricName)) {
            if (serverdata.getTimeStamp() == startTime + 20000) {
                TestCase.assertEquals(0.069, serverdata.getValueNum());
            }
        } else if (MetricName.SERVER_ENERGY_CONSUMPTION.equals(metricName)) {
            if (serverdata.getTimeStamp() == startTime) {
                TestCase.assertEquals(0.00038805555555555555, serverdata.getValueNum());
            }
        } else if (specialMetricNames.contains(metricName)) {
            TestCase.fail("Duration API not support this metric " + metricName);
        }
    }
    assetRepository.deleteById(asset.getId());
    realtimeDataRepository.deleteById(pduUsageMetricData.getId());
    realtimeDataRepository.deleteById(tempRealTimeData.getId());
    realtimeDataRepository.deleteById(humdityRealTimeData.getId());
    realtimeDataRepository.deleteById(backHumidity.getId());
    realtimeDataRepository.deleteById(backTemperature.getId());
    realtimeDataRepository.deleteById(hostRealTimeData.getId());
}
Also used : RealTimeData(com.vmware.flowgate.common.model.RealTimeData) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MvcResult(org.springframework.test.web.servlet.MvcResult) FieldDescriptor(org.springframework.restdocs.payload.FieldDescriptor) Asset(com.vmware.flowgate.common.model.Asset) MetricData(com.vmware.flowgate.common.model.MetricData) HashSet(java.util.HashSet) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 12 with MetricData

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

the class AssetService method generateServerPduMetricData.

private List<MetricData> generateServerPduMetricData(List<ValueUnit> valueUnits, String pduAssetId, String outLet) {
    List<MetricData> result = new ArrayList<MetricData>();
    Double serverVoltage = null;
    long serverVoltageReadTime = 0;
    for (ValueUnit value : valueUnits) {
        MetricData data = new MetricData();
        data.setTimeStamp(value.getTime());
        data.setValueNum(value.getValueNum());
        String unit = databaseUnitAndOutputUnitMap.get(value.getUnit());
        if (unit == null) {
            unit = value.getUnit();
        }
        data.setUnit(unit);
        switch(value.getKey()) {
            case MetricName.PDU_TOTAL_POWER:
                data.setMetricName(String.format(MetricKeyName.SERVER_CONNECTED_PDUX_TOTAL_POWER, pduAssetId));
                result.add(data);
                break;
            case MetricName.PDU_APPARENT_POWER:
                String outlet_pdu_power_extraidentifier = value.getExtraidentifier();
                if (outLet.equals(outlet_pdu_power_extraidentifier)) {
                    data.setMetricName(String.format(MetricKeyName.SERVER_CONNECTED_PDUX_OUTLETX_POWER, pduAssetId, value.getExtraidentifier()));
                    result.add(data);
                }
                break;
            case MetricName.PDU_CURRENT:
                String outlet_pdu_current_extraidentifier = value.getExtraidentifier();
                if (outlet_pdu_current_extraidentifier == null) {
                    data.setMetricName(String.format(MetricKeyName.SERVER_CONNECTED_PDUX_TOTAL_CURRENT, pduAssetId));
                    result.add(data);
                } else if (outLet.equals(outlet_pdu_current_extraidentifier)) {
                    data.setMetricName(String.format(MetricKeyName.SERVER_CONNECTED_PDUX_OUTLETX_CURRENT, pduAssetId, value.getExtraidentifier()));
                    result.add(data);
                }
                break;
            case MetricName.PDU_VOLTAGE:
                String extraidentifier = value.getExtraidentifier();
                serverVoltageReadTime = data.getTimeStamp();
                // some pdus without outlet metrics,but have inlet metrics
                if (serverVoltage == null) {
                    serverVoltage = data.getValueNum();
                }
                if (outLet.equals(extraidentifier)) {
                    data.setMetricName(String.format(MetricKeyName.SERVER_CONNECTED_PDUX_OUTLETX_VOLTAGE, pduAssetId, value.getExtraidentifier()));
                    result.add(data);
                    serverVoltage = data.getValueNum();
                }
                break;
            case MetricName.PDU_POWER_LOAD:
                data.setMetricName(String.format(MetricKeyName.SERVER_CONNECTED_PDUX_POWER_LOAD, pduAssetId));
                result.add(data);
                break;
            case MetricName.PDU_CURRENT_LOAD:
                data.setMetricName(String.format(MetricKeyName.SERVER_CONNECTED_PDUX_CURRENT_LOAD, pduAssetId));
                result.add(data);
                break;
            default:
                break;
        }
    }
    if (serverVoltage != null) {
        MetricData data = new MetricData();
        data.setMetricName(MetricName.SERVER_VOLTAGE);
        data.setTimeStamp(serverVoltageReadTime);
        data.setValueNum(serverVoltage);
        data.setUnit(MetricUnit.V.toString());
        result.add(data);
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) ValueUnit(com.vmware.flowgate.common.model.ValueUnit) MetricData(com.vmware.flowgate.common.model.MetricData)

Example 13 with MetricData

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

the class AssetService method translateToMetricDataForPDU.

private List<MetricData> translateToMetricDataForPDU(Map<String, List<ValueUnit>> assetAndValueUnitsMap, Asset pdu) {
    List<MetricData> metricDatas = new ArrayList<MetricData>();
    Map<String, Map<String, String>> displayNameAndFormulasMap = getMetricDispalyNameAndFormulaMapForPDU(pdu);
    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 (Map.Entry<String, Map<String, String>> displayNameAndFormulaEntry : displayNameAndFormulasMap.entrySet()) {
        String displayName = displayNameAndFormulaEntry.getKey();
        // Key:MetricName Value:Formula
        Map<String, String> valueUnitNameAndFormulaMap = displayNameAndFormulaEntry.getValue();
        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> valueUnits = new ArrayList<ValueUnit>();
                for (ValueUnit valueUnit : assetAndValueUnitsMap.get(id)) {
                    if (!valueUnitName.equals(valueUnit.getKey())) {
                        continue;
                    }
                    String extraidentifier = valueUnit.getExtraidentifier();
                    String[] extraInfo = null;
                    if (extraidentifier != null) {
                        extraInfo = extraidentifier.split("\\" + FlowgateConstant.INLET_POLE_NAME_PREFIX);
                    }
                    switch(displayName) {
                        // Current
                        case MetricName.PDU_CURRENT:
                        case MetricName.PDU_VOLTAGE:
                            if (extraidentifier == null) {
                                valueUnits.add(valueUnit);
                            }
                            break;
                        // %s|%s|Current
                        case MetricName.PDU_INLET_XPOLE_CURRENT:
                        case MetricName.PDU_INLET_XPOLE_VOLTAGE:
                        case MetricName.PDU_INLET_XPOLE_FREE_CAPACITY:
                            if (extraInfo != null && extraInfo.length == 2) {
                                valueUnits.add(valueUnit);
                            }
                            break;
                        // %s|Current
                        case MetricName.PDU_XLET_CURRENT:
                        case MetricName.PDU_XLET_VOLTAGE:
                        case MetricName.PDU_XLET_FREE_CAPACITY:
                            if (extraInfo != null && extraInfo.length == 1) {
                                valueUnits.add(valueUnit);
                            }
                            break;
                        default:
                            valueUnits.add(valueUnit);
                            break;
                    }
                }
                if (valueUnits.isEmpty()) {
                    continue;
                }
                Collections.sort(valueUnits, comparator);
                idAndValues.put(id, valueUnits);
            }
            if (!idAndValues.isEmpty()) {
                Function<TranslateContext, MetricData> function = TranslateFunctionService.convert;
                String formulaKey = displayNameAndFormulaKeyMap.get(displayName);
                if (TranslateFunctionService.pduFormulaKeyAndFunction.containsKey(formulaKey)) {
                    function = TranslateFunctionService.pduFormulaKeyAndFunction.get(formulaKey);
                }
                metricDatas.addAll(convertValueUnitToMetricData(displayName, function, formula, idAndValues));
            }
        }
    }
    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 14 with MetricData

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

the class AssetService method generateServerSensorMetricData.

private List<MetricData> generateServerSensorMetricData(List<ValueUnit> valueUnits, String metricName) {
    List<MetricData> result = new ArrayList<MetricData>();
    if (valueUnits == null || valueUnits.isEmpty()) {
        return result;
    }
    for (ValueUnit value : valueUnits) {
        MetricData data = new MetricData();
        data.setTimeStamp(value.getTime());
        data.setValueNum(value.getValueNum());
        String unit = databaseUnitAndOutputUnitMap.get(value.getUnit());
        if (unit == null) {
            unit = value.getUnit();
        }
        data.setUnit(unit);
        switch(value.getKey()) {
            case MetricName.HUMIDITY:
                switch(metricName) {
                    case MetricName.SERVER_BACK_HUMIDITY:
                        data.setMetricName(String.format(MetricKeyName.SERVER_BACK_HUMIDITY_LOCATIONX, value.getExtraidentifier()));
                        result.add(data);
                        break;
                    case MetricName.SERVER_FRONT_HUMIDITY:
                        data.setMetricName(String.format(MetricKeyName.SERVER_FRONT_HUMIDITY_LOCATIONX, value.getExtraidentifier()));
                        result.add(data);
                        break;
                    default:
                        break;
                }
                break;
            case MetricName.TEMPERATURE:
                switch(metricName) {
                    case MetricName.SERVER_BACK_TEMPREATURE:
                        data.setMetricName(String.format(MetricKeyName.SERVER_BACK_TEMPREATURE_LOCATIONX, value.getExtraidentifier()));
                        result.add(data);
                        break;
                    case MetricName.SERVER_FRONT_TEMPERATURE:
                        data.setMetricName(String.format(MetricKeyName.SERVER_FRONT_TEMPERATURE_LOCATIONX, value.getExtraidentifier()));
                        result.add(data);
                        break;
                    default:
                        break;
                }
                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 15 with MetricData

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

the class AssetService method generateOtherMetricData.

private List<MetricData> generateOtherMetricData(List<ValueUnit> valueUnits) {
    List<MetricData> result = new ArrayList<>();
    if (valueUnits == null || valueUnits.isEmpty()) {
        return result;
    }
    MetricData data;
    for (ValueUnit value : valueUnits) {
        data = new MetricData();
        data.setTimeStamp(value.getTime());
        data.setValueNum(value.getValueNum());
        data.setValue(value.getValue());
        data.setMetricName(value.getKey());
        String unit = databaseUnitAndOutputUnitMap.get(value.getUnit());
        if (unit == null) {
            unit = value.getUnit();
        }
        data.setUnit(unit);
        result.add(data);
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) ValueUnit(com.vmware.flowgate.common.model.ValueUnit) MetricData(com.vmware.flowgate.common.model.MetricData)

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