Search in sources :

Example 6 with TranslateContext

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

the class AssetService method convertValueUnitToMetricData.

/**
 * Sample of TranslateContext
 * {
 *    DisplayName:"Temperature|%S",
 *    Formulas:"23551d6dacf2432c8a3edbc6bbc922cd + 123456",
 *    valueData:[{id,valueUnit}]
 * }
 * One displayName may correspond to one or multiple values
 * When the API of get-latest-data use the method, One MetricName correspond one value
 * When the API of get-duration-data use the method, One MetricName correspond multiple values
 * @param displayName
 * @param formula
 * @param idAndValues
 * @return
 */
private List<MetricData> convertValueUnitToMetricData(String displayName, Function<TranslateContext, MetricData> function, String formula, Map<String, List<ValueUnit>> idAndValues) {
    List<ValueUnit> valueUnits = idAndValues.get(idAndValues.keySet().iterator().next());
    Map<String, ValueUnit> idAndValueUnitMap = null;
    // Assuming the collection length is the same
    List<MetricData> metricDatas = new ArrayList<MetricData>();
    for (int i = 0; i < valueUnits.size(); i++) {
        // Sample data: <id1, valueUnit1>,<id2, valueUnit1>
        idAndValueUnitMap = new HashMap<String, ValueUnit>();
        TranslateContext translateContext = new TranslateContext();
        for (Map.Entry<String, List<ValueUnit>> valuesEntry : idAndValues.entrySet()) {
            String id = valuesEntry.getKey();
            ValueUnit value = valuesEntry.getValue().get(i);
            idAndValueUnitMap.put(id, value);
        }
        translateContext.setDisplayName(displayName);
        translateContext.setFormula(formula);
        translateContext.setValueUnits(idAndValueUnitMap);
        MetricData metricData = function.apply(translateContext);
        if (metricData != null) {
            metricDatas.add(metricData);
        }
    }
    return metricDatas;
}
Also used : 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 7 with TranslateContext

use of com.vmware.flowgate.common.model.TranslateContext 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 8 with TranslateContext

use of com.vmware.flowgate.common.model.TranslateContext 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)

Example 9 with TranslateContext

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

the class TranslateFunctionServiceTest method testCalculationFormulaConvert.

@Test
public void testCalculationFormulaConvert() {
    String assetId1 = "752c0c7637104a39a4242031cd48785e";
    String assetId2 = "5dab088ab8a94aa994fdf9ddd8acceb6";
    String formula = "(752c0c7637104a39a4242031cd48785e+5dab088ab8a94aa994fdf9ddd8acceb6)/2";
    long currentTimeMillis = System.currentTimeMillis();
    ValueUnit humidityValue1 = new ValueUnit();
    humidityValue1.setValueNum(20);
    humidityValue1.setTime(currentTimeMillis);
    humidityValue1.setUnit(RealtimeDataUnit.Percent.toString());
    humidityValue1.setKey(MetricName.HUMIDITY);
    ValueUnit humidityValue2 = new ValueUnit();
    humidityValue2.setValueNum(19);
    humidityValue2.setTime(currentTimeMillis);
    humidityValue2.setUnit(RealtimeDataUnit.Percent.toString());
    humidityValue2.setKey(MetricName.HUMIDITY);
    String displayName = MetricKeyName.SERVER_BACK_HUMIDITY_LOCATIONX;
    String backToName = String.format(displayName, "BACK");
    Function<TranslateContext, MetricData> function = TranslateFunctionService.convert;
    Map<String, ValueUnit> valueUnitMap = new HashMap<>();
    valueUnitMap.put(assetId1, humidityValue1);
    valueUnitMap.put(assetId2, humidityValue2);
    TranslateContext translateContext = TranslateContext.buildByValueUnitsAndDisplayNameAndFormula(valueUnitMap, backToName, formula);
    MetricData metricData = function.apply(translateContext);
    TestCase.assertEquals(backToName, metricData.getMetricName());
    TestCase.assertEquals(currentTimeMillis, metricData.getTimeStamp());
    TestCase.assertEquals(19.5, metricData.getValueNum());
    TestCase.assertEquals(ValueUnit.MetricUnit.percent.toString(), 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)

Example 10 with TranslateContext

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

the class TranslateFunctionServiceTest method testExceptionConvertServerAverageUsedPowerEnergyConsumptionAverageTemperature.

@Test
public void testExceptionConvertServerAverageUsedPowerEnergyConsumptionAverageTemperature() {
    String assetId = "752c0c7637104a39a4242031cd48785e";
    long startTime = System.currentTimeMillis();
    long endTime = startTime + 30000;
    ValueUnit valueUnit = new ValueUnit();
    valueUnit.setTime(endTime);
    valueUnit.setKey(MetricName.SERVER_AVERAGE_USED_POWER);
    valueUnit.setUnit(ValueUnit.MetricUnit.kW.toString());
    valueUnit.setValueNum(0.06906666666666665);
    String displayName = MetricName.SERVER_AVERAGE_USED_POWER;
    Function<TranslateContext, MetricData> function = TranslateFunctionService.serverFormulaKeyAndFunction.get(MetricName.SERVER_AVERAGE_USED_POWER);
    Map<String, ValueUnit> valueUnitMap = new HashMap<>();
    valueUnitMap.put(assetId, valueUnit);
    TranslateContext translateContext = TranslateContext.buildByValueUnitsAndDisplayNameAndFormula(valueUnitMap, displayName, assetId);
    MetricData metricData = function.apply(translateContext);
    TestCase.assertNull(metricData);
}
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)13 TranslateContext (com.vmware.flowgate.common.model.TranslateContext)13 ValueUnit (com.vmware.flowgate.common.model.ValueUnit)13 HashMap (java.util.HashMap)13 Test (org.junit.Test)9 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)9 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Map (java.util.Map)4