use of com.vmware.flowgate.common.model.MetricData in project flowgate by vmware.
the class AssetControllerTest method testRealtimedataOtherExample.
@Test
public void testRealtimedataOtherExample() throws Exception {
Asset sensor = createSensor();
assetRepository.save(sensor);
String sensorId = sensor.getId();
long time = System.currentTimeMillis();
int duration = 30 * 60 * 1000;
long startTime = time - duration;
List<RealTimeData> realTimeDatas = new ArrayList<>();
RealTimeData tempRealTimeData = createTemperatureSensorRealtimeData(startTime, sensorId);
realTimeDatas.add(tempRealTimeData);
List<ValueUnit> valueunits = new ArrayList<ValueUnit>();
ValueUnit tempValue = new ValueUnit();
tempValue.setValueNum(25);
tempValue.setTime(startTime + 5 * 60 * 1000);
tempValue.setUnit(MetricUnit.C.toString());
tempValue.setKey(MetricName.TEMPERATURE);
valueunits.add(tempValue);
RealTimeData realTimeData = new RealTimeData();
realTimeData.setId(UUID.randomUUID().toString());
realTimeData.setAssetID(sensorId);
realTimeData.setValues(valueunits);
realTimeData.setTime(startTime + 5 * 60 * 1000);
realTimeDatas.add(realTimeData);
realtimeDataRepository.saveAll(realTimeDatas);
MvcResult result1 = this.mockMvc.perform(get("/v1/assets/" + sensor.getId() + "/realtimedata").param("starttime", String.valueOf(startTime)).param("duration", String.valueOf(duration))).andReturn();
String res = result1.getResponse().getContentAsString();
MetricData[] metricDatas = mapper.readValue(res, MetricData[].class);
TestCase.assertEquals(1, metricDatas.length);
try {
for (MetricData sensordata : metricDatas) {
long metricTime = sensordata.getTimeStamp();
if (metricTime == startTime + 5 * 60 * 1000) {
TestCase.assertEquals(25.0, sensordata.getValueNum());
} else {
TestCase.fail();
}
}
} finally {
assetRepository.deleteById(sensorId);
realtimeDataRepository.deleteById(tempRealTimeData.getId());
realtimeDataRepository.deleteById(realTimeData.getId());
}
}
use of com.vmware.flowgate.common.model.MetricData in project flowgate by vmware.
the class AssetControllerTest method testGetServerMetricsByIDHostUsageFormulaIsNull.
@Test
public void testGetServerMetricsByIDHostUsageFormulaIsNull() {
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");
realTimeDatas.add(humdityRealTimeData);
realTimeDatas.add(tempRealTimeData);
realTimeDatas.add(backHumidity);
realTimeDatas.add(backTemperature);
realTimeDatas.add(pduUsageMetricData);
realtimeDataRepository.saveAll(realTimeDatas);
asset = fillingMetricsformula(asset);
Map<String, String> metricFormula = asset.getMetricsformulars();
metricFormula.remove(FlowgateConstant.HOST_METRICS);
asset.setMetricsformulars(metricFormula);
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);
List<MetricData> metricDatas = assetService.getMetricsByID(asset.getId(), startTime, duration);
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());
}
}
assetRepository.deleteById(asset.getId());
realtimeDataRepository.deleteById(pduUsageMetricData.getId());
realtimeDataRepository.deleteById(tempRealTimeData.getId());
realtimeDataRepository.deleteById(humdityRealTimeData.getId());
realtimeDataRepository.deleteById(backHumidity.getId());
realtimeDataRepository.deleteById(backTemperature.getId());
}
use of com.vmware.flowgate.common.model.MetricData in project flowgate by vmware.
the class AssetControllerTest method getPduMetricsDataById.
@Test
public void getPduMetricsDataById() 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) };
List<RealTimeData> realTimeDatas = new ArrayList<RealTimeData>();
Long currentTime = System.currentTimeMillis();
RealTimeData pduRealTimeData = createPduRealTimeData(currentTime);
pduRealTimeData.setAssetID("00040717c4154b5b924ced78eafcea7a");
RealTimeData sensorRealTimeData = createSensorRealtimeData(currentTime);
sensorRealTimeData.setAssetID("00027ca37b004a9890d1bf20349d5ac1");
realTimeDatas.add(pduRealTimeData);
realTimeDatas.add(sensorRealTimeData);
Iterable<RealTimeData> result = realtimeDataRepository.saveAll(realTimeDatas);
Asset asset = createAsset();
Map<String, String> formulas = new HashMap<String, String>();
Map<String, Map<String, String>> sensorMetricFormulas = new HashMap<String, Map<String, String>>();
Map<String, String> tempSensor = new HashMap<String, String>();
tempSensor.put("INLET", "00027ca37b004a9890d1bf20349d5ac1");
sensorMetricFormulas.put(MetricName.PDU_TEMPERATURE, tempSensor);
Map<String, String> humiditySensor = new HashMap<String, String>();
humiditySensor.put("OUTLET", "00027ca37b004a9890d1bf20349d5ac1");
sensorMetricFormulas.put(MetricName.PDU_HUMIDITY, humiditySensor);
String sensorFormulaInfo = null;
try {
sensorFormulaInfo = mapper.writeValueAsString(sensorMetricFormulas);
} catch (JsonProcessingException e) {
TestCase.fail(e.getMessage());
}
formulas.put(FlowgateConstant.SENSOR, sensorFormulaInfo);
asset.setCategory(AssetCategory.PDU);
asset.setMetricsformulars(formulas);
asset.setId("00040717c4154b5b924ced78eafcea7a");
asset = assetRepository.save(asset);
MvcResult result1 = this.mockMvc.perform(get("/v1/assets/pdu/{assetId}/realtimedata", asset.getId()).param("starttime", String.valueOf(currentTime)).param("duration", "300000")).andDo(document("assets-getPduMetricsData-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 = result1.getResponse().getContentAsString();
MetricData[] datas = mapper.readValue(res, MetricData[].class);
try {
for (MetricData pduMetricdata : datas) {
String metricName = pduMetricdata.getMetricName();
if (String.format(MetricName.PDU_XLET_ACTIVE_POWER, "OUTLET:7").equals(metricName)) {
TestCase.assertEquals(pduMetricdata.getValueNum(), 0.2);
} else if (String.format(MetricName.PDU_XLET_APPARENT_POWER, "OUTLET:7").equals(metricName)) {
TestCase.assertEquals(pduMetricdata.getValueNum(), 1.033);
} else if (String.format(MetricName.PDU_XLET_CURRENT, "OUTLET:7").equals(metricName)) {
TestCase.assertEquals(pduMetricdata.getValueNum(), 0.633);
} else if (String.format(MetricName.PDU_XLET_FREE_CAPACITY, "OUTLET:7").equals(metricName)) {
TestCase.assertEquals(pduMetricdata.getValueNum(), 20.0);
} else if (String.format(MetricName.PDU_XLET_VOLTAGE, "OUTLET:7").equals(metricName)) {
TestCase.assertEquals(pduMetricdata.getValueNum(), 226.0);
} else if (String.format(MetricKeyName.PDU_HUMIDITY_LOCATIONX, "OUTLET").equals(metricName)) {
TestCase.assertEquals(pduMetricdata.getValueNum(), 20.0);
} else if (String.format(MetricKeyName.PDU_TEMPERATURE_LOCATIONX, "INLET").equals(metricName)) {
TestCase.assertEquals(pduMetricdata.getValueNum(), 32.0);
} else if (MetricName.PDU_CURRENT_LOAD.equals(metricName)) {
TestCase.assertEquals(pduMetricdata.getValueNum(), 0.1427);
} else if (MetricName.PDU_POWER_LOAD.equals(metricName)) {
TestCase.assertEquals(pduMetricdata.getValueNum(), 0.134);
} else if (MetricName.PDU_TOTAL_CURRENT.equals(metricName)) {
TestCase.assertEquals(pduMetricdata.getValueNum(), 4.566);
} else if (MetricName.PDU_TOTAL_POWER.equals(metricName)) {
TestCase.assertEquals(pduMetricdata.getValueNum(), 1.033);
} else if (String.format(MetricName.PDU_INLET_XPOLE_CURRENT, "INLET:1", "L1").equals(metricName)) {
TestCase.assertEquals(pduMetricdata.getValueNum(), 6.0);
} else if (String.format(MetricName.PDU_INLET_XPOLE_FREE_CAPACITY, "INLET:1", "L1").equals(metricName)) {
TestCase.assertEquals(pduMetricdata.getValueNum(), 34.0);
} else if (String.format(MetricName.PDU_INLET_XPOLE_VOLTAGE, "INLET:1", "L1").equals(metricName)) {
TestCase.assertEquals(pduMetricdata.getValueNum(), 220.0);
} else if (String.format(MetricName.PDU_INLET_XPOLE_CURRENT, "INLET:2", "L1").equals(metricName)) {
TestCase.assertEquals(pduMetricdata.getValueNum(), 6.0);
} else if (String.format(MetricName.PDU_INLET_XPOLE_FREE_CAPACITY, "INLET:2", "L1").equals(metricName)) {
TestCase.assertEquals(pduMetricdata.getValueNum(), 24.0);
} else if (String.format(MetricName.PDU_INLET_XPOLE_VOLTAGE, "INLET:2", "L1").equals(metricName)) {
TestCase.assertEquals(pduMetricdata.getValueNum(), 240.0);
} else {
TestCase.fail("Unkown metric");
}
}
} finally {
assetRepository.deleteById(asset.getId());
realtimeDataRepository.deleteById(pduRealTimeData.getId());
realtimeDataRepository.deleteById(sensorRealTimeData.getId());
}
}
use of com.vmware.flowgate.common.model.MetricData in project flowgate by vmware.
the class AssetControllerTest method testGetMetricDataForOther.
@Test
public void testGetMetricDataForOther() {
Asset sensor = createSensor();
assetRepository.save(sensor);
String sensorId = sensor.getId();
long time = System.currentTimeMillis();
int duration = 30 * 60 * 1000;
long startTime = time - duration;
List<RealTimeData> realTimeDatas = new ArrayList<>();
RealTimeData tempRealTimeData = createTemperatureSensorRealtimeData(startTime, sensorId);
realTimeDatas.add(tempRealTimeData);
List<ValueUnit> valueunits = new ArrayList<ValueUnit>();
ValueUnit tempValue = new ValueUnit();
tempValue.setValueNum(25);
tempValue.setTime(startTime + 5 * 60 * 1000);
tempValue.setUnit(MetricUnit.C.toString());
tempValue.setKey(MetricName.TEMPERATURE);
valueunits.add(tempValue);
RealTimeData realTimeData = new RealTimeData();
realTimeData.setId(UUID.randomUUID().toString());
realTimeData.setAssetID(sensorId);
realTimeData.setValues(valueunits);
realTimeData.setTime(startTime + 5 * 60 * 1000);
realTimeDatas.add(realTimeData);
realtimeDataRepository.saveAll(realTimeDatas);
List<MetricData> metricDatas = assetService.getMetricsByID(sensorId, startTime, duration);
TestCase.assertEquals(2, metricDatas.size());
for (MetricData sensordata : metricDatas) {
long metricTime = sensordata.getTimeStamp();
if (metricTime == startTime) {
TestCase.assertEquals(32.0, sensordata.getValueNum());
} else if (metricTime == startTime + 5 * 60 * 1000) {
TestCase.assertEquals(25.0, sensordata.getValueNum());
} else {
TestCase.fail();
}
}
assetRepository.deleteById(sensorId);
realtimeDataRepository.deleteById(tempRealTimeData.getId());
realtimeDataRepository.deleteById(realTimeData.getId());
}
use of com.vmware.flowgate.common.model.MetricData in project flowgate by vmware.
the class AssetService method getServerMetricsDataById.
public List<MetricData> getServerMetricsDataById(String assetID, long starttime, int duration) {
Optional<Asset> serverAssetOptional = assetRepository.findById(assetID);
if (!serverAssetOptional.isPresent()) {
throw WormholeRequestException.NotFound("asset", "id", assetID);
}
Asset server = serverAssetOptional.get();
List<MetricData> result = new ArrayList<MetricData>();
Map<String, String> metricFormula = server.getMetricsformulars();
if (metricFormula == null || metricFormula.isEmpty()) {
return result;
}
result.addAll(getServerHostMetric(server, starttime, duration));
Map<String, String> justficationfileds = server.getJustificationfields();
String allPduPortInfo = justficationfileds.get(FlowgateConstant.PDU_PORT_FOR_SERVER);
List<String> pduPorts = null;
Map<String, String> pduAssetIdAndUsedOutletMap = null;
if (!StringUtils.isEmpty(allPduPortInfo)) {
pduPorts = Arrays.asList(allPduPortInfo.split(FlowgateConstant.SPILIT_FLAG));
pduAssetIdAndUsedOutletMap = new HashMap<String, String>();
for (String pduPortInfo : pduPorts) {
// startport_FIELDSPLIT_endDeviceName_FIELDSPLIT_endport_FIELDSPLIT_endDeviceAssetID
// item[0] start port
// item[1] device name
// item[2] end port
// itme[3] assetid
String[] items = pduPortInfo.split(FlowgateConstant.SEPARATOR);
pduAssetIdAndUsedOutletMap.put(items[3], items[2]);
}
}
String pduFormulasInfo = metricFormula.get(FlowgateConstant.PDU);
if (pduFormulasInfo != null) {
Map<String, Map<String, String>> pduMetrics = server.metricsFormulaToMap(pduFormulasInfo, new TypeReference<Map<String, Map<String, String>>>() {
});
List<String> metricNames = new ArrayList<String>();
metricNames.add(MetricName.PDU_TOTAL_POWER);
metricNames.add(MetricName.PDU_APPARENT_POWER);
metricNames.add(MetricName.PDU_CURRENT);
metricNames.add(MetricName.PDU_VOLTAGE);
metricNames.add(MetricName.PDU_POWER_LOAD);
metricNames.add(MetricName.PDU_CURRENT_LOAD);
for (String pduId : pduMetrics.keySet()) {
List<RealTimeData> realtimedatas = realtimeDataRepository.getDataByIDAndTimeRange(pduId, starttime, duration);
List<ValueUnit> valueUnits = getValueUnits(realtimedatas, metricNames);
String outlet = UnknownOulet;
if (pduAssetIdAndUsedOutletMap != null) {
outlet = pduAssetIdAndUsedOutletMap.get(pduId);
}
result.addAll(generateServerPduMetricData(valueUnits, pduId, outlet));
}
}
String sensorFormulasInfo = metricFormula.get(FlowgateConstant.SENSOR);
if (sensorFormulasInfo != null) {
Map<String, Map<String, String>> sensorFormulars = server.metricsFormulaToMap(sensorFormulasInfo, new TypeReference<Map<String, Map<String, String>>>() {
});
Map<String, List<RealTimeData>> assetIdAndRealtimeDataMap = new HashMap<String, List<RealTimeData>>();
for (Map.Entry<String, Map<String, String>> sensorFormula : sensorFormulars.entrySet()) {
Map<String, String> locationAndIdMap = sensorFormula.getValue();
String metricName = sensorFormula.getKey();
List<ValueUnit> valueUnits = generateSensorValueUnit(assetIdAndRealtimeDataMap, starttime, duration, locationAndIdMap, metricName);
result.addAll(generateServerSensorMetricData(valueUnits, metricName));
}
}
return result;
}
Aggregations