Search in sources :

Example 31 with ValueUnit

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

the class PowerIQService method getSensorRealTimeData.

public List<RealTimeData> getSensorRealTimeData(FacilitySoftwareConfig powerIQ, List<Asset> assets) {
    HashMap<AdvanceSettingType, String> advanceSetting = getAdvanceSetting(powerIQ);
    List<RealTimeData> realtimeDatas = new ArrayList<RealTimeData>();
    String dateFormat = advanceSetting.get(AdvanceSettingType.DateFormat);
    String timezone = advanceSetting.get(AdvanceSettingType.TimeZone);
    String temperature = advanceSetting.get(AdvanceSettingType.TEMPERATURE_UNIT);
    String humidity = advanceSetting.get(AdvanceSettingType.HUMIDITY_UNIT);
    PowerIQAPIClient powerIQAPIClient = createClient(powerIQ);
    for (Asset asset : assets) {
        HashMap<String, String> sensorExtraInfo = asset.getJustificationfields();
        String sensorInfo = sensorExtraInfo.get(FlowgateConstant.SENSOR);
        Map<String, String> sensorInfoMap = null;
        try {
            sensorInfoMap = getInfoMap(sensorInfo);
        } catch (IOException e2) {
            continue;
        }
        String sensorId = sensorInfoMap.get(FlowgateConstant.SENSOR_ID_FROM_POWERIQ);
        Sensor sensor = null;
        try {
            sensor = powerIQAPIClient.getSensorById(sensorId);
        } catch (HttpClientErrorException e) {
            logger.error("Failed to query data from PowerIQ", e);
            IntegrationStatus integrationStatus = powerIQ.getIntegrationStatus();
            if (integrationStatus == null) {
                integrationStatus = new IntegrationStatus();
            }
            integrationStatus.setStatus(IntegrationStatus.Status.ERROR);
            integrationStatus.setDetail(e.getMessage());
            integrationStatus.setRetryCounter(FlowgateConstant.DEFAULTNUMBEROFRETRIES);
            updateIntegrationStatus(powerIQ);
            break;
        } catch (ResourceAccessException e1) {
            if (e1.getCause().getCause() instanceof ConnectException) {
                checkAndUpdateIntegrationStatus(powerIQ, e1.getMessage());
                break;
            }
            break;
        }
        SensorReading reading = sensor.getReading();
        if (reading == null || reading.getId() == 0) {
            continue;
        }
        RealTimeData realTimeData = new RealTimeData();
        String valueDateTime = reading.getReadingTime();
        long recordedTime = WormholeDateFormat.getLongTime(valueDateTime, dateFormat, timezone);
        if (recordedTime == -1) {
            logger.error("Failed to translate the time string: " + valueDateTime + ".And the dateformat is " + dateFormat);
            continue;
        }
        List<ValueUnit> values = new ArrayList<ValueUnit>();
        ValueUnit value = new ValueUnit();
        value.setTime(recordedTime);
        value.setKey(sensorAndMetricMap.get(sensor.getType()));
        String unit = reading.getUom();
        MetricUnit sourceUnit = null, targetUnit = null;
        switch(sensorAndMetricMap.get(sensor.getType())) {
            case MetricName.HUMIDITY:
                if (unit != null && !unit.isEmpty()) {
                    if (unit.equals("%")) {
                        sourceUnit = MetricUnit.percent;
                    } else {
                        sourceUnit = MetricUnit.valueOf(unit);
                    }
                } else {
                    if (humidity.equals("%")) {
                        sourceUnit = MetricUnit.percent;
                    } else {
                        sourceUnit = MetricUnit.valueOf(humidity);
                    }
                }
                targetUnit = MetricUnit.percent;
                break;
            case MetricName.TEMPERATURE:
                if (unit != null && !unit.isEmpty()) {
                    sourceUnit = MetricUnit.valueOf(unit.toUpperCase());
                } else {
                    sourceUnit = MetricUnit.valueOf(temperature.toUpperCase());
                }
                targetUnit = MetricUnit.C;
                break;
            default:
                break;
        }
        Double metricsValue = reading.getValue();
        if (metricsValue == null) {
            continue;
        }
        try {
            value.setValueNum(value.translateUnit(metricsValue, sourceUnit, targetUnit));
        } catch (WormholeException e) {
            logger.error("Cannot translate Unit", e);
        }
        if (targetUnit.toString().equals(MetricUnit.percent.toString())) {
            value.setUnit("%");
        } else {
            value.setUnit(targetUnit.toString());
        }
        values.add(value);
        realTimeData.setAssetID(asset.getId());
        realTimeData.setTime(recordedTime);
        realTimeData.setValues(values);
        realTimeData.setId(asset.getId() + "_" + recordedTime);
        realtimeDatas.add(realTimeData);
    }
    return realtimeDatas;
}
Also used : RealTimeData(com.vmware.flowgate.common.model.RealTimeData) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) IntegrationStatus(com.vmware.flowgate.common.model.IntegrationStatus) ArrayList(java.util.ArrayList) WormholeException(com.vmware.flowgate.common.exception.WormholeException) IOException(java.io.IOException) ResourceAccessException(org.springframework.web.client.ResourceAccessException) SensorReading(com.vmware.flowgate.poweriqworker.model.SensorReading) PowerIQAPIClient(com.vmware.flowgate.poweriqworker.client.PowerIQAPIClient) Asset(com.vmware.flowgate.common.model.Asset) MetricUnit(com.vmware.flowgate.common.model.ValueUnit.MetricUnit) ValueUnit(com.vmware.flowgate.common.model.ValueUnit) AdvanceSettingType(com.vmware.flowgate.common.model.FacilitySoftwareConfig.AdvanceSettingType) Sensor(com.vmware.flowgate.poweriqworker.model.Sensor) ConnectException(java.net.ConnectException)

Example 32 with ValueUnit

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

the class OpenManageJobService method getMetricDatas.

/**
 *Example of Temperature
 *     "@odata.context": "/api/$metadata#DeviceService.TemperatureResponseModel",
 *     "@odata.type": "#DeviceService.TemperatureResponseModel",
 *     "@odata.id": "/api/DeviceService/Devices(10074)/Temperature",
 *     "peakTemperatureUnit": "celsius",
 *     "avgTemperatureUnit": "celsius",
 *     "DateFormat": "CIM",
 *     "instantaneousTemperatureUnit": "celsius",
 *     "startTime": "20210201050207.000000-360",
 *     "avgTemperatureTimeStamp": "20210202093523.806515-360",
 *     "avgTemperature": "21",
 *     "instantaneousTemperature": "23",
 *     "peakTemperature": "27",
 *     "peakTemperatureTimeStamp": "20210201050207.000000-360"
 * @param integration
 * @param client
 * @return
 */
public List<RealTimeData> getMetricDatas(FacilitySoftwareConfig integration, OpenManageAPIClient client) {
    List<RealTimeData> metricDatas = new ArrayList<RealTimeData>();
    Map<String, String> assetIdAndOpenmanageDeviceIdMap = getMappedAssetIDMap(integration.getId());
    if (assetIdAndOpenmanageDeviceIdMap.isEmpty()) {
        return metricDatas;
    }
    Plugin powerManage = null;
    CommonResult<Plugin> plugins = client.getCommonResult(Plugin.class);
    for (Plugin plugin : plugins.getValue()) {
        if (PowerManager.equals(plugin.getName()) && plugin.isInstalled() && plugin.isEnabled()) {
            powerManage = plugin;
            break;
        }
    }
    for (Map.Entry<String, String> map : assetIdAndOpenmanageDeviceIdMap.entrySet()) {
        String deviceId = map.getValue();
        String assetId = map.getKey();
        Map<String, ValueUnit> metricNameAndValueUnitMap = getMetricsData(deviceId, client, powerManage);
        RealTimeData metricData = null;
        if (!metricNameAndValueUnitMap.isEmpty()) {
            metricData = new RealTimeData();
            List<ValueUnit> valueUnits = new ArrayList<ValueUnit>(metricNameAndValueUnitMap.values());
            long currentTime = valueUnits.get(0).getTime();
            metricData.setAssetID(assetId);
            metricData.setTime(currentTime);
            metricData.setValues(valueUnits);
            metricData.setId(assetId + currentTime);
            metricDatas.add(metricData);
        }
    }
    return metricDatas;
}
Also used : RealTimeData(com.vmware.flowgate.common.model.RealTimeData) ArrayList(java.util.ArrayList) ValueUnit(com.vmware.flowgate.common.model.ValueUnit) Map(java.util.Map) HashMap(java.util.HashMap) Plugin(com.vmware.flowgate.openmanage.datamodel.Plugin)

Example 33 with ValueUnit

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

the class OpenManageJobTest method getMetricDatas.

@Test
public void getMetricDatas() {
    Mockito.when(this.openManageAPIClient.getCommonResult(Plugin.class)).thenReturn(getCommonResult());
    Mockito.when(this.openManageAPIClient.getDevicePowerMetrics("10074")).thenReturn(getPowerMetricsData());
    Mockito.when(this.openManageAPIClient.getDeviceTemperatureMetrics("10074")).thenReturn(getDeviceTemperature());
    Mockito.when(this.openManageAPIClient.getMetricsFromPowerManage(any(PowerManageMetricsRequestBody.class))).thenReturn(getDeviceMetricsResult());
    Mockito.when(this.openManageAPIClient.getCommonResult(PowerSetting.class)).thenReturn(getPowerSettings());
    FacilitySoftwareConfig config = new FacilitySoftwareConfig();
    config.setId(createAsset().getAssetSource());
    List<RealTimeData> metricDatas = openmanageJobService.getMetricDatas(config, openManageAPIClient);
    for (RealTimeData data : metricDatas) {
        if ("assetid1611766210404".equals(data.getId())) {
            // power metrics
            List<ValueUnit> values = data.getValues();
            for (ValueUnit value : values) {
                switch(value.getKey()) {
                    case MetricName.SERVER_AVERAGE_USED_POWER:
                        TestCase.assertEquals(0.074, value.getValueNum());
                        break;
                    // use powerManager value to override it
                    case MetricName.SERVER_POWER:
                        TestCase.assertEquals(0.072, value.getValueNum());
                        break;
                    case MetricName.SERVER_MINIMUM_USED_POWER:
                        TestCase.assertEquals(0.07, value.getValueNum());
                        break;
                    case MetricName.SERVER_PEAK_USED_POWER:
                        TestCase.assertEquals(0.08, value.getValueNum());
                        break;
                    case MetricName.SERVER_ENERGY_CONSUMPTION:
                        TestCase.assertEquals(800.0, value.getValueNum());
                        break;
                    case MetricName.SERVER_AVERAGE_TEMPERATURE:
                        TestCase.assertEquals(22.0, value.getValueNum());
                        break;
                    case MetricName.SERVER_TEMPERATURE:
                        TestCase.assertEquals(24.0, value.getValueNum());
                        break;
                    case MetricName.SERVER_PEAK_TEMPERATURE:
                        TestCase.assertEquals(30.0, value.getValueNum());
                        break;
                    case MetricName.SERVER_FRONT_TEMPERATURE:
                        TestCase.assertEquals(24.0, value.getValueNum());
                        break;
                    default:
                        TestCase.fail();
                        break;
                }
            }
        } else {
            TestCase.fail();
        }
    }
}
Also used : RealTimeData(com.vmware.flowgate.common.model.RealTimeData) PowerManageMetricsRequestBody(com.vmware.flowgate.openmanage.datamodel.PowerManageMetricsRequestBody) FacilitySoftwareConfig(com.vmware.flowgate.common.model.FacilitySoftwareConfig) ValueUnit(com.vmware.flowgate.common.model.ValueUnit) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 34 with ValueUnit

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

the class OpenManageJobTest method getMetricDatasTestNA.

@Test
public void getMetricDatasTestNA() {
    Mockito.when(this.openManageAPIClient.getCommonResult(Plugin.class)).thenReturn(getCommonResult());
    DevicePower powerMetrics = getPowerMetricsData();
    powerMetrics.setMinimumPower("N/A");
    Mockito.when(this.openManageAPIClient.getDevicePowerMetrics("10074")).thenReturn(powerMetrics);
    Mockito.when(this.openManageAPIClient.getDeviceTemperatureMetrics("10074")).thenReturn(getDeviceTemperature());
    Mockito.when(this.openManageAPIClient.getMetricsFromPowerManage(any(PowerManageMetricsRequestBody.class))).thenReturn(getDeviceMetricsResult());
    Mockito.when(this.openManageAPIClient.getCommonResult(PowerSetting.class)).thenReturn(getPowerSettings());
    FacilitySoftwareConfig config = new FacilitySoftwareConfig();
    config.setId(createAsset().getAssetSource());
    List<RealTimeData> metricDatas = openmanageJobService.getMetricDatas(config, openManageAPIClient);
    for (RealTimeData data : metricDatas) {
        if ("assetid1611766210404".equals(data.getId())) {
            // power metrics
            List<ValueUnit> values = data.getValues();
            for (ValueUnit value : values) {
                switch(value.getKey()) {
                    case MetricName.SERVER_AVERAGE_USED_POWER:
                        TestCase.assertEquals(0.074, value.getValueNum());
                        break;
                    // use powerManager value to override it
                    case MetricName.SERVER_POWER:
                        TestCase.assertEquals(0.072, value.getValueNum());
                        break;
                    case MetricName.SERVER_MINIMUM_USED_POWER:
                        TestCase.fail("Should not contain the " + MetricName.SERVER_MINIMUM_USED_POWER);
                        break;
                    case MetricName.SERVER_PEAK_USED_POWER:
                        TestCase.assertEquals(0.08, value.getValueNum());
                        break;
                    case MetricName.SERVER_ENERGY_CONSUMPTION:
                        TestCase.assertEquals(800.0, value.getValueNum());
                        break;
                    case MetricName.SERVER_AVERAGE_TEMPERATURE:
                        TestCase.assertEquals(22.0, value.getValueNum());
                        break;
                    case MetricName.SERVER_TEMPERATURE:
                        TestCase.assertEquals(24.0, value.getValueNum());
                        break;
                    case MetricName.SERVER_PEAK_TEMPERATURE:
                        TestCase.assertEquals(30.0, value.getValueNum());
                        break;
                    case MetricName.SERVER_FRONT_TEMPERATURE:
                        TestCase.assertEquals(24.0, value.getValueNum());
                        break;
                    default:
                        TestCase.fail();
                        break;
                }
            }
        } else {
            TestCase.fail();
        }
    }
}
Also used : RealTimeData(com.vmware.flowgate.common.model.RealTimeData) DevicePower(com.vmware.flowgate.openmanage.datamodel.DevicePower) PowerManageMetricsRequestBody(com.vmware.flowgate.openmanage.datamodel.PowerManageMetricsRequestBody) FacilitySoftwareConfig(com.vmware.flowgate.common.model.FacilitySoftwareConfig) ValueUnit(com.vmware.flowgate.common.model.ValueUnit) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 35 with ValueUnit

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

the class VCDataService method getMinMaxAvgValueUnit.

public List<ValueUnit> getMinMaxAvgValueUnit(List<ValueUnit> valueUnits) {
    List<ValueUnit> statisticsValueUnit = new ArrayList<>(3);
    if (valueUnits == null || valueUnits.isEmpty()) {
        return statisticsValueUnit;
    }
    ValueUnit firstValueUnit = valueUnits.get(0);
    ValueUnit minValueUnit = new ValueUnit();
    minValueUnit.setUnit(firstValueUnit.getUnit());
    minValueUnit.setValueNum(firstValueUnit.getValueNum());
    minValueUnit.setTime(firstValueUnit.getTime());
    ValueUnit maxValueUnit = new ValueUnit();
    maxValueUnit.setUnit(firstValueUnit.getUnit());
    maxValueUnit.setValueNum(firstValueUnit.getValueNum());
    maxValueUnit.setTime(firstValueUnit.getTime());
    ValueUnit averageValueUnit = new ValueUnit();
    averageValueUnit.setUnit(firstValueUnit.getUnit());
    if (MetricName.SERVER_POWER.equals(firstValueUnit.getKey())) {
        minValueUnit.setKey(MetricName.SERVER_MINIMUM_USED_POWER);
        maxValueUnit.setKey(MetricName.SERVER_PEAK_USED_POWER);
        averageValueUnit.setKey(MetricName.SERVER_AVERAGE_USED_POWER);
    } else {
        return statisticsValueUnit;
    }
    double sum = 0.0d;
    long startTime = Long.MAX_VALUE;
    long endTime = Long.MIN_VALUE;
    for (ValueUnit value : valueUnits) {
        double valueNum = value.getValueNum();
        long time = value.getTime();
        sum += valueNum;
        if (valueNum < minValueUnit.getValueNum()) {
            minValueUnit.setValueNum(valueNum);
            minValueUnit.setTime(time);
        }
        if (valueNum > maxValueUnit.getValueNum()) {
            maxValueUnit.setValueNum(valueNum);
            maxValueUnit.setTime(time);
        }
        startTime = Math.min(time, startTime);
        endTime = Math.max(time, endTime);
    }
    // Record the since time and peak power time, for example  1612417403074_FIELDSPLIT_1612415606985
    minValueUnit.setExtraidentifier(startTime + FlowgateConstant.SEPARATOR + minValueUnit.getTime());
    minValueUnit.setTime(endTime);
    maxValueUnit.setExtraidentifier(startTime + FlowgateConstant.SEPARATOR + maxValueUnit.getTime());
    maxValueUnit.setTime(endTime);
    averageValueUnit.setValueNum(sum / valueUnits.size());
    averageValueUnit.setExtraidentifier(String.valueOf(startTime));
    averageValueUnit.setTime(endTime);
    statisticsValueUnit.add(minValueUnit);
    statisticsValueUnit.add(maxValueUnit);
    statisticsValueUnit.add(averageValueUnit);
    return statisticsValueUnit;
}
Also used : ArrayList(java.util.ArrayList) ValueUnit(com.vmware.flowgate.common.model.ValueUnit)

Aggregations

ValueUnit (com.vmware.flowgate.common.model.ValueUnit)63 ArrayList (java.util.ArrayList)42 RealTimeData (com.vmware.flowgate.common.model.RealTimeData)28 HashMap (java.util.HashMap)25 MetricData (com.vmware.flowgate.common.model.MetricData)23 Test (org.junit.Test)23 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)22 TranslateContext (com.vmware.flowgate.common.model.TranslateContext)13 Map (java.util.Map)11 List (java.util.List)9 Asset (com.vmware.flowgate.common.model.Asset)8 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 WormholeException (com.vmware.flowgate.common.exception.WormholeException)3 MetricUnit (com.vmware.flowgate.common.model.ValueUnit.MetricUnit)3 PowerManageMetricsRequestBody (com.vmware.flowgate.openmanage.datamodel.PowerManageMetricsRequestBody)3 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)3 FacilitySoftwareConfig (com.vmware.flowgate.common.model.FacilitySoftwareConfig)2 DevicePower (com.vmware.flowgate.openmanage.datamodel.DevicePower)2 Outlet (com.vmware.flowgate.poweriqworker.model.Outlet)2 Pdu (com.vmware.flowgate.poweriqworker.model.Pdu)2