Skip to content

Commit

Permalink
Changes to give extra details for leakage service and location as par…
Browse files Browse the repository at this point in the history
…t of baseline
  • Loading branch information
HariharanAnantharaman committed Apr 11, 2018
1 parent 1ff5df8 commit 83ea2d7
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class LeakageDataController {
public @ResponseBody List<ConsumptionLeakage> getDailyConsumerLeakageStats()
{
log.info("Entering LeakageDataController.getDailyConsumerLeakageStats");
List<ConsumptionLeakage> resultList=analyticService.getCurrentDayConsumerLeakage();
List<ConsumptionLeakage> resultList=analyticService.getCurrentDayConsumerLeakageDetails();
log.info("Exiting LeakageDataController.getDailyConsumerLeakageStats");
return resultList;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,31 @@ public class ConsumptionLeakage implements Serializable {
private static final long serialVersionUID = 1L;
private String meterId;
private Date date;
private double leakage;
private double leakageVolume;
private String location;
private String startTime;
private String endTime;



public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getStartTime() {
return startTime;
}
public void setStartTime(String startTime) {
this.startTime = startTime;
}
public String getEndTime() {
return endTime;
}
public void setEndTime(String endTime) {
this.endTime = endTime;
}
public String getMeterId() {
return meterId;
}
Expand All @@ -25,11 +48,12 @@ public Date getDate() {
public void setDate(Date date) {
this.date = date;
}
public double getLeakage() {
return leakage;
public double getLeakageVolume() {
return leakageVolume;
}
public void setLeakage(double leakage) {
this.leakage = leakage;
public void setLeakageVolume(double leakageVolume) {
this.leakageVolume = leakageVolume;
}


}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.techolution.mauritius.smartwater.supply.service;

import java.sql.Date;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
Expand All @@ -23,6 +26,7 @@

import com.techolution.mauritius.smartwater.supply.InfluxProperties;
import com.techolution.mauritius.smartwater.supply.domain.ConsumptionLeakage;
import com.techolution.mauritius.smartwater.supply.domain.DailyWaterSupplyData;
import com.techolution.mauritius.smartwater.supply.domain.LeakageData;
import com.techolution.mauritius.smartwater.supply.domain.MeterConnection;
import com.techolution.mauritius.smartwater.supply.domain.MeterTrendData;
Expand Down Expand Up @@ -58,6 +62,8 @@ public class SupplyAnalyticsService {

private static String SERIES_NAME_CONSUMERLEAKAGE="consumerleakage";
private static String SERIES_NAME_NETWORKLEAKAGE="networkleakage";
private static String SERIES_NAME_CONSUMERLEAKAGE_ON="consumerleakageontime";
private static String SERIES_NAME_CONSUMERLEAKAGE_OFF="consumerleakageofftime";



Expand Down Expand Up @@ -104,6 +110,16 @@ public List<MeterTrendData> getConnectionsAboveDailyBaseline(){

}

Map <Long, MeterConnection> connMap=getConnectionsMap();

retList.forEach(trendData -> {

MeterConnection connection=connMap.get(new Long(trendData.getMeterId()));
if(connection!=null){
trendData.setLocation(connection.getHouse_namenum());
}
});


log.info("Existing SupplyAnalyticsService.getConnectionsAboveDailyBaseline");
return retList;
Expand Down Expand Up @@ -170,6 +186,15 @@ public List<MeterTrendData> getConnectionsBelowDailyBaseline(){

}

Map <Long, MeterConnection> connMap=getConnectionsMap();

retList.forEach(trendData -> {

MeterConnection connection=connMap.get(new Long(trendData.getMeterId()));
if(connection!=null){
trendData.setLocation(connection.getHouse_namenum());
}
});

log.info("Existing SupplyAnalyticsService.getConnectionsBelowDailyBaseline");
return retList;
Expand Down Expand Up @@ -458,7 +483,7 @@ public LeakageData getCurrentDayLeakageData(){
});
}

influxDB.close();
log.info("Exiting SupplyAnalyticsService.getStats");
return leakageData;

Expand Down Expand Up @@ -504,7 +529,7 @@ public List<ConsumptionLeakage> getCurrentDayConsumerLeakage(){
List<List<Object>> values=series.getValues();
if(values !=null && values.size()>0){
List<Object> value=values.get(0);
leakage.setLeakage( (Double)value.get(1));
leakage.setLeakageVolume( (Double)value.get(1));

String time=(String)value.get(0);
Instant instant=Instant.parse(time);
Expand All @@ -516,11 +541,133 @@ public List<ConsumptionLeakage> getCurrentDayConsumerLeakage(){

});
}
influxDB.close();

log.info("Exiting SupplyAnalyticsService.getCurrentDayConsumerLeakage");
return resultList;

}

public List<ConsumptionLeakage> getCurrentDayConsumerLeakageDetails(){

log.info("Entering SupplyAnalyticsService.getCurrentDayConsumerLeakageDetails");

// SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd");
List<ConsumptionLeakage> outputlist= new ArrayList<ConsumptionLeakage>();

//TODO Add date specific filtering again
/*String startTime=myFormat.format(data.getStartDate());
String endTime=myFormat.format(data.getEndDate());
Calendar currentCal=Calendar.getInstance();
Date currentTime=currentCal.getTime();
if(data.getEndDate().after(currentTime)){
currentCal.add(Calendar.DATE, -1);
endTime=myFormat.format(currentCal.getTime());
}*/

// String query = "select time,value from "+SERIES_NAME_CONSUMERLEAKAGE_ON+" , "+SERIES_NAME_CONSUMERLEAKAGE_OFF+ " where meter_id='"+data.getMeterId()+"' and time >= '"+startTime+"' and time <='"+endTime+"' order by time asc";
String query = "select * from "+SERIES_NAME_CONSUMERLEAKAGE_ON+" , "+SERIES_NAME_CONSUMERLEAKAGE_OFF+" order by time asc";


SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone(influxProperties.getDatatimezone()));
InfluxDB influxDB = InfluxDBFactory.connect(influxProperties.getUrl(),influxProperties.getUsername(),influxProperties.getPassword());
long startStarttime=System.currentTimeMillis();
QueryResult queryResult = influxDB.query(new Query(query, influxProperties.getDbname()));
long endtime=System.currentTimeMillis();
log.debug("Time After getCurrentDayConsumerLeakageDetails query execution:"+endtime);
log.debug("Time Taken for getCurrentDayConsumerLeakageDetails query execution:"+(endtime-startStarttime));
log.debug("Query is:"+query);

List<Result> resultlist=queryResult.getResults();

Map<String,ConsumptionLeakage> outputmap=new HashMap<String,ConsumptionLeakage>();
if(resultlist != null && resultlist.size()>0){

Result result=resultlist.get(0);
List<Series> serieslist=result.getSeries();
serieslist.forEach(series -> {

String name=series.getName();

List<List<Object>> valuelist=series.getValues();
valuelist.parallelStream().forEach( values -> {

String timestamp=(String)values.get(0);
String meter_id=(String)values.get(1);

Instant instant=Instant.parse(timestamp);
Date dt=Date.from(instant);



ConsumptionLeakage leakageData=outputmap.get(meter_id);
if(leakageData == null){
leakageData =new ConsumptionLeakage();
}
leakageData.setMeterId(meter_id);

if(SERIES_NAME_CONSUMERLEAKAGE_ON.equalsIgnoreCase(name)){
leakageData.setStartTime(dateFormat.format(dt));
}else{
leakageData.setEndTime(dateFormat.format(dt));
}

outputmap.put(meter_id, leakageData);


});
});

}


//TODO Add time base filter. It also assumes that data will be here whenever data is there in consumerleakageontime and consumerleakageofftime
String leakagevalquery="select sum(value) from consumerleakage group by meter_id ";
long leakagevalqueryresultStarttime=System.currentTimeMillis();
QueryResult leakagevalqueryresult = influxDB.query(new Query(leakagevalquery, influxProperties.getDbname()));
long leakagevalqueryresultendtime=System.currentTimeMillis();
log.debug("Time Taken for leakagevalquery query execution:"+(leakagevalqueryresultendtime-leakagevalqueryresultStarttime));
List<Result> leakageValResult=leakagevalqueryresult.getResults();
if(leakageValResult != null && leakageValResult.size()>0){
Result result=leakageValResult.get(0);

List<Series> serieslist=result.getSeries();

serieslist.forEach(series -> {
String meterId=series.getTags().get("meter_id");

List<List<Object>> valuelist=series.getValues();
if(valuelist !=null && valuelist.size()>0){
List<Object> value=valuelist.get(0);
ConsumptionLeakage leakage=outputmap.get(meterId);
leakage.setLeakageVolume((Double)value.get(1));
}

});
}


/*Collection<DailyWaterSupplyData> ouput=outputmap.values();
List<DailyWaterSupplyData> outputlist=new ArrayList<DailyWaterSupplyData>();
outputlist.addAll(ouput);*/

Map <Long, MeterConnection> connmap=getConnectionsMap();
Collection<ConsumptionLeakage> ouput=outputmap.values();
outputlist.addAll(ouput);
outputlist.forEach(consumptionleakage -> {
Long meterId=new Long(consumptionleakage.getMeterId());
MeterConnection meterconn= connmap.get(meterId);
if(meterconn!=null){
consumptionleakage.setLocation(meterconn.getHouse_namenum());
}
});

influxDB.close();
log.info("Exiting SupplyAnalyticsService.getCurrentDayConsumerLeakageDetails");
return outputlist;

}

}

0 comments on commit 83ea2d7

Please sign in to comment.