Search in sources :

Example 1 with TranslateContext

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

the class TranslateFunctionServiceTest method testConvertServerAverageUsedPowerEnergyConsumptionAverageTemperature.

@Test
public void testConvertServerAverageUsedPowerEnergyConsumptionAverageTemperature() {
    String assetId = "752c0c7637104a39a4242031cd48785e";
    long startTime = System.currentTimeMillis();
    long endTime = startTime + 30000;
    ValueUnit valueUnit = new ValueUnit();
    valueUnit.setTime(endTime);
    valueUnit.setExtraidentifier(String.valueOf(startTime));
    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.assertEquals(valueUnit.getKey(), metricData.getMetricName());
    TestCase.assertEquals(startTime, 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 2 with TranslateContext

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

the class TranslateFunctionServiceTest method testServerConvertValue.

@Test
public void testServerConvertValue() {
    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.setValue("value");
    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("value", metricData.getValue());
    TestCase.assertEquals(0.0, 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 3 with TranslateContext

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

the class TranslateFunctionServiceTest method testConvertServerPeakUsedPowerMinimumUsedPowerPeakTemperature.

@Test
public void testConvertServerPeakUsedPowerMinimumUsedPowerPeakTemperature() {
    String assetId = "752c0c7637104a39a4242031cd48785e";
    long startTime = System.currentTimeMillis();
    long maxValueTime = startTime + 2000;
    long endTime = startTime + 30000;
    ValueUnit valueUnit = new ValueUnit();
    valueUnit.setTime(endTime);
    valueUnit.setExtraidentifier(startTime + FlowgateConstant.SEPARATOR + maxValueTime);
    valueUnit.setKey(MetricName.SERVER_PEAK_USED_POWER);
    valueUnit.setUnit(ValueUnit.MetricUnit.kW.toString());
    valueUnit.setValueNum(0.07);
    String displayName = MetricName.SERVER_PEAK_USED_POWER;
    Function<TranslateContext, MetricData> function = TranslateFunctionService.serverFormulaKeyAndFunction.get(MetricName.SERVER_PEAK_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.assertEquals(valueUnit.getKey(), metricData.getMetricName());
    TestCase.assertEquals(maxValueTime, 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 4 with TranslateContext

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

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

the class AssetService method translateToMetricDataForOtherAsset.

private List<MetricData> translateToMetricDataForOtherAsset(Map<String, List<ValueUnit>> assetAndValueUnitsMap, Asset asset) {
    List<MetricData> metricDatas = new ArrayList<MetricData>();
    Map<String, Map<String, String>> displayNameAndFormulasMap = getMetricDispalyNameAndFormulaMap(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 (Map.Entry<String, Map<String, String>> entry : displayNameAndFormulasMap.entrySet()) {
        String displayName = entry.getKey();
        Map<String, String> formulaMap = entry.getValue();
        for (Map.Entry<String, String> formulaEntry : formulaMap.entrySet()) {
            String valueUnitName = formulaEntry.getKey();
            String formula = formulaEntry.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;
                    }
                    valueUnits.add(valueUnit);
                }
                if (valueUnits.isEmpty()) {
                    continue;
                }
                Collections.sort(valueUnits, comparator);
                idAndValues.put(id, valueUnits);
            }
            if (idAndValues.isEmpty()) {
                continue;
            }
            Function<TranslateContext, MetricData> function = TranslateFunctionService.convert;
            String formulaKey = displayNameAndFormulaKeyMap.get(displayName);
            if (TranslateFunctionService.defaultFormulaKeyAndFunction.containsKey(formulaKey)) {
                function = TranslateFunctionService.defaultFormulaKeyAndFunction.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)

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