diff --git a/DataSimulator/src/com/techolution/mauritius/data/simulator/service/FlowDataSimiulator.java b/DataSimulator/src/com/techolution/mauritius/data/simulator/service/FlowDataSimiulator.java index 4e71bc7..704c22c 100644 --- a/DataSimulator/src/com/techolution/mauritius/data/simulator/service/FlowDataSimiulator.java +++ b/DataSimulator/src/com/techolution/mauritius/data/simulator/service/FlowDataSimiulator.java @@ -72,7 +72,7 @@ public void startProcess(int meterId, String startTime, String endTime, long sle } } - String query = "select last(value) from meterreadingvalues where time <='"+startTime+"' and meter_id='"+meterId+"'";// now() - 10d and meter_id = '124' group by time(1d) fill(0) + String query = "select last(value) from meterreadingvalues where time <'"+startTime+"' and meter_id='"+meterId+"'";// now() - 10d and meter_id = '124' group by time(1d) fill(0) System.out.println("Query is:"+query); @@ -103,7 +103,7 @@ public void startProcess(int meterId, String startTime, String endTime, long sle try { Telemetry telemetry=new Telemetry(); telemetry.setDate(startDate); - double flow=ThreadLocalRandom.current().nextDouble(200.25, 300.85); + double flow=ThreadLocalRandom.current().nextDouble(1.00, 2.00); telemetry.setFlow(flow); baseReadingValue=baseReadingValue+flow; telemetry.setReading(baseReadingValue); diff --git a/supplydataservice/src/main/java/com/techolution/mauritius/smartwater/supply/controller/LeakageDataController.java b/supplydataservice/src/main/java/com/techolution/mauritius/smartwater/supply/controller/LeakageDataController.java new file mode 100644 index 0000000..44657e1 --- /dev/null +++ b/supplydataservice/src/main/java/com/techolution/mauritius/smartwater/supply/controller/LeakageDataController.java @@ -0,0 +1,47 @@ +package com.techolution.mauritius.smartwater.supply.controller; + +import java.util.List; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + +import com.techolution.mauritius.smartwater.supply.domain.ConsumptionLeakage; +import com.techolution.mauritius.smartwater.supply.domain.LeakageData; +import com.techolution.mauritius.smartwater.supply.service.SupplyAnalyticsService; + +@CrossOrigin(origins = {"*"}) +@RestController +@RequestMapping(value="/leakage") + +public class LeakageDataController { + + private Log log = LogFactory.getLog(LeakageDataController.class); + + @Autowired + SupplyAnalyticsService analyticService; + + @RequestMapping(method=RequestMethod.GET,value="/daily/metrics") + public @ResponseBody LeakageData getDailyLeakageStats() + { + log.info("Entering LeakageDataController.getDailyLeakageStats"); + LeakageData resultdata=analyticService.getCurrentDayLeakageData(); + log.info("Exiting LeakageDataController.getDailyLeakageStats"); + return resultdata; + } + @RequestMapping(method=RequestMethod.GET,value="/daily/consumerleakage") + public @ResponseBody List getDailyConsumerLeakageStats() + { + log.info("Entering LeakageDataController.getDailyConsumerLeakageStats"); + List resultList=analyticService.getCurrentDayConsumerLeakage(); + log.info("Exiting LeakageDataController.getDailyConsumerLeakageStats"); + return resultList; + } + + +} diff --git a/supplydataservice/src/main/java/com/techolution/mauritius/smartwater/supply/domain/ConsumptionLeakage.java b/supplydataservice/src/main/java/com/techolution/mauritius/smartwater/supply/domain/ConsumptionLeakage.java new file mode 100644 index 0000000..5309531 --- /dev/null +++ b/supplydataservice/src/main/java/com/techolution/mauritius/smartwater/supply/domain/ConsumptionLeakage.java @@ -0,0 +1,35 @@ +package com.techolution.mauritius.smartwater.supply.domain; + +import java.io.Serializable; +import java.util.Date; + +public class ConsumptionLeakage implements Serializable { + + /** + * + */ + private static final long serialVersionUID = 1L; + private String meterId; + private Date date; + private double leakage; + + public String getMeterId() { + return meterId; + } + public void setMeterId(String meterId) { + this.meterId = meterId; + } + public Date getDate() { + return date; + } + public void setDate(Date date) { + this.date = date; + } + public double getLeakage() { + return leakage; + } + public void setLeakage(double leakage) { + this.leakage = leakage; + } + +} diff --git a/supplydataservice/src/main/java/com/techolution/mauritius/smartwater/supply/domain/LeakageData.java b/supplydataservice/src/main/java/com/techolution/mauritius/smartwater/supply/domain/LeakageData.java new file mode 100644 index 0000000..1b7e2a6 --- /dev/null +++ b/supplydataservice/src/main/java/com/techolution/mauritius/smartwater/supply/domain/LeakageData.java @@ -0,0 +1,39 @@ +package com.techolution.mauritius.smartwater.supply.domain; + +import java.io.Serializable; +import java.util.Date; + +public class LeakageData implements Serializable { + + private Date metricsDate; + + private double consumptionLeakage; + + private double networkLeakage; + + public Date getMetricsDate() { + return metricsDate; + } + + public void setMetricsDate(Date metricsDate) { + this.metricsDate = metricsDate; + } + + public double getConsumptionLeakage() { + return consumptionLeakage; + } + + public void setConsumptionLeakage(double consumptionLeakage) { + this.consumptionLeakage = consumptionLeakage; + } + + public double getNetworkLeakage() { + return networkLeakage; + } + + public void setNetworkLeakage(double networkLeakage) { + this.networkLeakage = networkLeakage; + } + + +} diff --git a/supplydataservice/src/main/java/com/techolution/mauritius/smartwater/supply/service/SupplyAnalyticsService.java b/supplydataservice/src/main/java/com/techolution/mauritius/smartwater/supply/service/SupplyAnalyticsService.java index cdce391..465b033 100644 --- a/supplydataservice/src/main/java/com/techolution/mauritius/smartwater/supply/service/SupplyAnalyticsService.java +++ b/supplydataservice/src/main/java/com/techolution/mauritius/smartwater/supply/service/SupplyAnalyticsService.java @@ -1,6 +1,8 @@ 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.List; @@ -20,6 +22,8 @@ import org.springframework.stereotype.Component; import com.techolution.mauritius.smartwater.supply.InfluxProperties; +import com.techolution.mauritius.smartwater.supply.domain.ConsumptionLeakage; +import com.techolution.mauritius.smartwater.supply.domain.LeakageData; import com.techolution.mauritius.smartwater.supply.domain.MeterConnection; import com.techolution.mauritius.smartwater.supply.domain.MeterTrendData; import com.techolution.mauritius.smartwater.supply.domain.WaterSupplyDailyConnectionStats; @@ -52,6 +56,8 @@ public class SupplyAnalyticsService { private static String DOWN="DOWN"; private static String DEFAULT_LOCATION="TEST"; + private static String SERIES_NAME_CONSUMERLEAKAGE="consumerleakage"; + private static String SERIES_NAME_NETWORKLEAKAGE="networkleakage"; @@ -290,7 +296,7 @@ public Map getAllConnections(){ public WaterSupplyDailyConnectionStats getStats(){ - log.info("Entering SupplyDataService.getStats"); + log.info("Entering SupplyAnalyticsService.getStats"); WaterSupplyDailyConnectionStats connectionStats=new WaterSupplyDailyConnectionStats(); getConnectionsMap(); @@ -396,8 +402,125 @@ public WaterSupplyDailyConnectionStats getStats(){ influxDB.close(); - log.info("Exiting SupplyDataService.getStats"); + log.info("Exiting SupplyAnalyticsService.getStats"); return connectionStats; } + + public LeakageData getCurrentDayLeakageData(){ + + log.info("Entering SupplyAnalyticsService.getStats"); + + SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd"); + + Calendar start=Calendar.getInstance(TimeZone.getTimeZone(influxProperties.getDatatimezone())); + String startTime=myFormat.format(start.getTime()); + + start.add(Calendar.DATE,1); + String endTime=myFormat.format(start.getTime()); + + String leakageval="select sum(value) from networkleakage,consumerleakage where time >='"+startTime+"' and time <'"+endTime+"' "; + + + InfluxDB influxDB = InfluxDBFactory.connect(influxProperties.getUrl(),influxProperties.getUsername(),influxProperties.getPassword()); + long startStarttime=System.currentTimeMillis(); + QueryResult queryResult = influxDB.query(new Query(leakageval, influxProperties.getDbname())); + long endtime=System.currentTimeMillis(); + log.debug("Time After countOfSupply query execution:"+endtime); + log.debug("Time Taken for query execution:"+(endtime-startStarttime)); + + LeakageData leakageData=new LeakageData(); + // leakageData.setMetricsDate((Calendar.getInstance(influxProperties.getDatatimezone()))); + + List results=queryResult.getResults(); + if(results!=null && results.size()>0){ + Result result=results.get(0); + List serieslist=result.getSeries(); + serieslist.forEach(series -> { + String seriesName= series.getName(); + List> values=series.getValues(); + if(values !=null && values.size()>0){ + List value=values.get(0); + if(SERIES_NAME_CONSUMERLEAKAGE.equalsIgnoreCase(seriesName)){ + leakageData.setConsumptionLeakage((Double)value.get(1)); + String time=(String)value.get(0); + Instant instant=Instant.parse(time); + + leakageData.setMetricsDate(Date.from(instant)); + }else{ + leakageData.setNetworkLeakage((Double)value.get(1)); + String time=(String)value.get(0); + Instant instant=Instant.parse(time); + + leakageData.setMetricsDate(Date.from(instant)); + } + } + + }); + } + + + log.info("Exiting SupplyAnalyticsService.getStats"); + return leakageData; + + } + + + public List getCurrentDayConsumerLeakage(){ + + log.info("Entering SupplyAnalyticsService.getCurrentDayConsumerLeakage"); + + SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd"); + + Calendar start=Calendar.getInstance(TimeZone.getTimeZone(influxProperties.getDatatimezone())); + String startTime=myFormat.format(start.getTime()); + + start.add(Calendar.DATE,1); + String endTime=myFormat.format(start.getTime()); + + String leakageval="select sum(value) from consumerleakage where time >='"+startTime+"' and time <'"+endTime+"' group by meter_id "; + + + InfluxDB influxDB = InfluxDBFactory.connect(influxProperties.getUrl(),influxProperties.getUsername(),influxProperties.getPassword()); + long startStarttime=System.currentTimeMillis(); + QueryResult queryResult = influxDB.query(new Query(leakageval, influxProperties.getDbname())); + long endtime=System.currentTimeMillis(); + log.debug("Time After countOfSupply query execution:"+endtime); + log.debug("Time Taken for query execution:"+(endtime-startStarttime)); + + + List resultList=new ArrayList(); + + // leakageData.setMetricsDate((Calendar.getInstance(influxProperties.getDatatimezone()))); + + List results=queryResult.getResults(); + if(results!=null && results.size()>0){ + Result result=results.get(0); + List serieslist=result.getSeries(); + serieslist.forEach(series -> { + ConsumptionLeakage leakage= new ConsumptionLeakage(); + + String meterId=series.getTags().get(METER_ID); + leakage.setMeterId(meterId); + List> values=series.getValues(); + if(values !=null && values.size()>0){ + List value=values.get(0); + leakage.setLeakage( (Double)value.get(1)); + + String time=(String)value.get(0); + Instant instant=Instant.parse(time); + leakage.setDate(Date.from(instant)); + + + } + resultList.add(leakage); + + }); + } + + + log.info("Exiting SupplyAnalyticsService.getCurrentDayConsumerLeakage"); + return resultList; + +} } diff --git a/test/src/test/Test.java b/test/src/test/Test.java new file mode 100644 index 0000000..3385019 --- /dev/null +++ b/test/src/test/Test.java @@ -0,0 +1,929 @@ +package com.techolution.mauritius.smartwater.connection.service; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.Instant; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.TimeZone; +import java.util.concurrent.TimeUnit; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.influxdb.BatchOptions; +import org.influxdb.InfluxDB; +import org.influxdb.InfluxDB.ConsistencyLevel; +import org.influxdb.InfluxDBFactory; +import org.influxdb.dto.BatchPoints; +import org.influxdb.dto.Point; +import org.influxdb.dto.Query; +import org.influxdb.dto.QueryResult; +import org.influxdb.dto.QueryResult.Result; +import org.influxdb.dto.QueryResult.Series; +import org.json.JSONException; +import org.json.JSONObject; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Component; + +import com.techolution.mauritius.smartwater.InfluxProperties; +import com.techolution.mauritius.smartwater.connection.domain.ConnectionKpiData; +import com.techolution.mauritius.smartwater.connection.domain.Data; +import com.techolution.mauritius.smartwater.connection.domain.KeyValue; +import com.techolution.mauritius.smartwater.connection.domain.Kpi; +import com.techolution.mauritius.smartwater.connection.domain.RequestData; +import com.techolution.mauritius.smartwater.connection.domain.SeriesPointData; +import com.techolution.mauritius.smartwater.connection.domain.Telemetry; +import com.techolution.mauritius.smartwater.connection.domain.TelemetryRequestData; + + +@Component +public class ConnectionStatisticsService { + + private Log log = LogFactory.getLog(ConnectionStatisticsService.class); + + + + @Autowired + InfluxProperties influxProperties; + + + @Autowired + InfluxDBUtils influxdbutils; + + //TODO replace with spring properties or DB + public static final int TOTALCAPACITY=4200; + public static final int TOTALPOWER=3500; + public static double TEMPERATURE=84.2; + + //TODO ENum + public static String HEALTH_GOOD="GOOD"; + public static String HEALTH_MODERATE="MODERATE"; + public static String HEALTH_WEAK="WEAK"; + public static String HEALTH_POOR="POOR"; + + + @Autowired + InfluxQueryCallBack querycallback; + + /** + * + * @param data + * @return + * @throws ParseException + */ + + public List getData(RequestData data) throws ParseException{ + + + + + + + + /*String startTime = myFormat.format(data.getStart_Time().getTime()); + String endTime = myFormat.format(data.getEnd_Time().getTime());*/ + String startTime = data.getStart_Time(); + //String startTime = "2018-03-01"; + + String endTime = data.getEnd_Time(); + + endTime = getNextDay( endTime); + + //String endTime = "2018-03-15"; + + String groupVal = getGroupVal(data); + + + int deviceId=data.getHouse_ID(); + //int deviceId=123; + String query = "select sum(value) from flowvalues where time >='"+startTime+"' and time<'"+endTime+"' and meter_id='"+deviceId+"' group by time("+groupVal+") fill(0)";// now() - 10d and meter_id = '124' group by time(1d) fill(0) + log.debug("Query is:"+query); + + + List retlist = getDailyMetrics(deviceId, query); + return retlist; + } + + + private String getNextDay(String endTime) throws ParseException { + + SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd"); + //Instant instant=Instant.parse(endTime); + Date date=myFormat.parse(endTime); + //Date date=Date.from(instant); + + Calendar calendar=Calendar.getInstance(); + calendar.setTime(date); + calendar.add(Calendar.DATE, 1); + endTime = myFormat.format(calendar.getTime()); + return endTime; + } + + +public List getDailyFowRateData(RequestData data) throws ParseException{ + + + //SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); + + + /*String startTime = myFormat.format(data.getStart_Time().getTime()); + String endTime = myFormat.format(data.getEnd_Time().getTime());*/ + String startTime = data.getStart_Time(); + //String startTime = "2018-03-01"; + + String endTime = data.getEnd_Time(); + //String endTime = "2018-03-15"; + + + + String groupVal = getGroupVal(data); + + + int deviceId=data.getHouse_ID(); + //int deviceId=123; + endTime = getNextDay( endTime); + + + String query ="select mean(hourlyval) from (select sum(value) as hourlyval from flowvalues where meter_id='"+deviceId+"' and time >='"+startTime+"' and time <'"+endTime+"' group by time(1h)) where time >='"+startTime+"' and time <= '"+endTime+"' group by time("+groupVal+") fill(0)"; + log.debug("Query is:"+query); + + + List retlist = getDailyMetrics(deviceId, query); + return retlist; + } + + private String getGroupVal(RequestData data) { + int distanceValue=data.getSample_Distance_value(); + //int distanceValue=30; + String disVal=String.valueOf(distanceValue); + + String code="d"; + String groupVal=null; + log.debug("Sample Distance:"+data.getSample_Distance()); + log.debug("distanceValue:"+distanceValue); + //String groupVal="1d"; + if(data.getSample_Distance().equalsIgnoreCase("Day")){ + code="d"; + groupVal=disVal+code; + } + else if(data.getSample_Distance().equalsIgnoreCase("Hour")){ + code="h"; + groupVal=disVal+code; + }else if(data.getSample_Distance().equalsIgnoreCase("Month")){ + int monthgroupval=distanceValue*30; + groupVal=String.valueOf(monthgroupval)+"d"; + + }else{ + code="d"; + groupVal=disVal+code; + } + return groupVal; + } + + private List getDailyMetrics(int deviceId, String query) { + //InfluxDB influxDB = InfluxDBFactory.connect("http://localhost:32770", "root", "root"); + 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 getDailyMetrics query execution:"+endtime); + log.debug("Time Taken for query execution:"+(endtime-startStarttime)); + String locationName= "TEST"; + + List resultlist=queryResult.getResults(); + // int recordSize=0; + List retlist=new ArrayList(); + // SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd"); + //SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-ddTHH:mm:ssZ"); + //dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); + //Date date1=new SimpleDateFormat("yyyy-MM-DDTHH:mm:ssz").parse(sDate1); + Data resultData=null; + //Instant instant=null; + for(Result result:resultlist){ + List serieslist=result.getSeries(); + if(serieslist == null){ + break; + } + for(Series series:serieslist){ + List> valuelist=series.getValues(); + for(List results:valuelist){ + String endTimeReturned=(String)results.get(0); + /*log.debug("Date is:"+(endTimeReturned.split("T"))[0]); + log.debug("Date2 is:"+(endTimeReturned.split("T"))[1]);*/ + // instant= Instant.parse( endTimeReturned); + //Date date=java.util.Date.from(instant); + //Date date=dateFormat.parse(endTimeReturned.split("T")[0]); + + resultData=new Data(); + resultData.setDevid(deviceId); + resultData.setName(endTimeReturned.split("T")[0]); + resultData.setValue(Math.round(((Double)results.get(1)).doubleValue()*100D)/100D); + resultData.setSensor_locationname(locationName); + retlist.add(resultData); + } + + + } + + } + influxDB.close(); + return retlist; + } + + public List geBatterytData(RequestData data) throws ParseException{ + + log.debug("Entering ConnectionStatisticsService.geBatterytData"); + + // SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); + + + /*String startTime = myFormat.format(data.getStart_Time().getTime()); + String endTime = myFormat.format(data.getEnd_Time().getTime());*/ + String startTime = data.getStart_Time(); + //String startTime = "2018-03-01"; + + String endTime = data.getEnd_Time(); + //String endTime = "2018-03-15"; + + endTime = getNextDay( endTime); + String groupVal = getGroupVal(data); + + + int deviceId=data.getHouse_ID(); + //int deviceId=123; + String query = "select last(value) from batterylevelvalues where time >='"+startTime+"' and time<'"+endTime+"' and meter_id='"+deviceId+"' group by time("+groupVal+")";// now() - 10d and meter_id = '124' group by time(1d) fill(0) + log.debug("Query is:"+query); + + + + + long jsonstarttime=System.currentTimeMillis(); + List retlist =null; + String locationName= "TEST"; + // retlist=getBatteryDataUsingNativeHttp(deviceId, query, jsonstarttime, locationName); + + + + + retlist = getBatteryResultUsingInfluxAPI(deviceId, query, locationName); + log.debug("Exiting ConnectionStatisticsService.geBatterytData"); + return retlist; + } + +private List getBatteryDataUsingNativeHttp(int deviceId, String query, long jsonstarttime, String locationName) { + List retlist = null; + try { + JSONObject responsejson=influxdbutils.executeQuery(query); + long jsonendtime=System.currentTimeMillis(); + + log.debug("JSON response is:"+responsejson.toString()); + log.debug("Timetake to execute as JSON is:"+(jsonendtime-jsonstarttime)); + + retlist = querycallback.proccessBatteryUsingHTTPCall(deviceId, locationName, responsejson); + + + + } catch (IOException | JSONException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (URISyntaxException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return retlist; +} + + +private List getBatteryResultUsingInfluxAPI(int deviceId, String query, String locationName) { + //InfluxDB influxDB = InfluxDBFactory.connect("http://localhost:32770", "root", "root"); + InfluxDB influxDB = InfluxDBFactory.connect(influxProperties.getUrl(),influxProperties.getUsername(),influxProperties.getPassword()); + long startStarttime=System.currentTimeMillis(); + log.debug("Time before getBattery query execution:"+startStarttime); + QueryResult queryResult = influxDB.query(new Query(query, influxProperties.getDbname())); + long endtime=System.currentTimeMillis(); + log.debug("Time After getBattery query execution:"+endtime); + log.debug("Time Taken for query execution:"+(endtime-startStarttime)); + List resultlist=queryResult.getResults(); +// int recordSize=0; + List retlist=new ArrayList(); + querycallback.processBatteryAPIResult(deviceId, locationName, resultlist, retlist); + influxDB.close(); + return retlist; +} + + + + /** + * + * @param data + * @return + * @throws ParseException + */ + public List geInstanceTelemetrytData(TelemetryRequestData data) throws ParseException{ + + log.debug("Entering ConnectionStatisticsService.geInstanceTelemetrytData"); + + + int distanceValue=data.getSampleDistanceValue(); + //int distanceValue=30; + String disVal=String.valueOf(distanceValue); + + String code="d"; + String groupVal=null; + log.debug("Sample Distance:"+data.getSampleDistance()); + log.debug("distanceValue:"+distanceValue); + boolean giveTimeStamp=false; + //String groupVal="1d"; + if(data.getSampleDistance().equalsIgnoreCase("Day")){ + code="d"; + groupVal=disVal+code; + } + else if(data.getSampleDistance().equalsIgnoreCase("Hour")){ + code="h"; + groupVal=disVal+code; + giveTimeStamp=true; + }else if(data.getSampleDistance().equalsIgnoreCase("Month")){ + int monthgroupval=distanceValue*30; + groupVal=String.valueOf(monthgroupval)+"d"; + + }else{ + code="d"; + groupVal=disVal+code; + } + + + int deviceId=data.getHouseId(); + String seriesname=getSeriesForMetrics(data.getMetrics()); + + String endTime=data.getEndTime(); + endTime = getNextDay( endTime); + //int deviceId=123; + String query = "select last(value) from "+ seriesname+" where time >='"+data.getStartTime()+"' and time< '"+endTime+"' and meter_id='"+deviceId+"' group by time("+groupVal+")";// now() - 10d and meter_id = '124' group by time(1d) fill(0) + if(data.getDefaultValueForMissingData()!=null){ + query = query+"fill("+data.getDefaultValueForMissingData()+")"; + } + log.debug("Query is:"+query); + + + //InfluxDB influxDB = InfluxDBFactory.connect("http://localhost:32770", "root", "root"); + 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 getBattery query execution:"+endtime); + log.debug("Time Taken for query execution:"+(endtime-startStarttime)); + String locationName= "TEST"; + + List resultlist=queryResult.getResults(); + + List retlist=new ArrayList(); + Data resultData=null; + + // Instant instant=null; + for(Result result:resultlist){ + List serieslist=result.getSeries(); + if(serieslist == null){ + break; + } + for(Series series:serieslist){ + List> valuelist=series.getValues(); + for(List results:valuelist){ + String endTimeReturned=(String)results.get(0); + + if(results.get(1)!=null){ + + + resultData=new Data(); + resultData.setDevid(deviceId); + if(giveTimeStamp){ + resultData.setName(endTimeReturned); + }else{ + resultData.setName(endTimeReturned.split("T")[0]); + } + + + resultData.setValue(Math.round(((Double)results.get(1)).doubleValue()*100D)/100D); + resultData.setSensor_locationname(locationName); + retlist.add(resultData); + } + } + + + } + + } + influxDB.close(); + log.debug("Entering ConnectionStatisticsService.geInstanceTelemetrytData"); + return retlist; + } + + /** + * + * @param telemetry + */ + @Async + public void insertData(Telemetry telemetry){ + + log.info("Entering ConnectionStatisticsService.insertData"); + log.debug(" TimeZone is:"+influxProperties.getDatatimezone()); + log.debug(" dbname is:"+influxProperties.getDbname()); + if(telemetry.getDate()==null){ + log.info("Date is null.Setting defaule date."); + Calendar date=Calendar.getInstance(TimeZone.getTimeZone(influxProperties.getDatatimezone())); + telemetry.setDate(date.getTime()); + }else{ + + Calendar date=Calendar.getInstance(TimeZone.getTimeZone(influxProperties.getDatatimezone())); + + //Date inputDate=telemetry.getDate(); + Calendar inputCal=Calendar.getInstance(TimeZone.getTimeZone(influxProperties.getDatatimezone())); + inputCal.setTime(telemetry.getDate()); + date.set(Calendar.YEAR,inputCal.get(Calendar.YEAR)); + date.set(Calendar.MONTH,inputCal.get(Calendar.MONTH)); + date.set(Calendar.DAY_OF_MONTH, inputCal.get(Calendar.DAY_OF_MONTH)); + date.set(Calendar.HOUR_OF_DAY,inputCal.get(Calendar.HOUR_OF_DAY)); + date.set(Calendar.MINUTE,inputCal.get(Calendar.MINUTE)); + date.set(Calendar.SECOND,inputCal.get(Calendar.SECOND)); + date.set(Calendar.MILLISECOND,inputCal.get(Calendar.MILLISECOND)); + telemetry.setDate(date.getTime()); + log.info("Time to set is:"+telemetry.getDate().getTime()); + } + //InfluxDB influxDB = InfluxDBFactory.connect("http://localhost:32770", "root", "root"); + InfluxDB influxDB =InfluxDBFactory.connect(influxProperties.getUrl(),influxProperties.getUsername(),influxProperties.getPassword()); + + influxDB.setDatabase(influxProperties.getDbname()); + influxDB.enableBatch(BatchOptions.DEFAULTS); + String rpName = "aRetentionPolicy2"; + // influxDB.createRetentionPolicy(rpName, influxProperties.getDbname(), "365d", "30m", 2, true); + influxDB.setRetentionPolicy("aRetentionPolicy2"); + + BatchPoints batchPoints = BatchPoints + .database(influxProperties.getDbname()) +// .tag("async", "true") + .retentionPolicy(rpName) + .consistency(ConsistencyLevel.ALL) + .build(); + + + + if(telemetry.getFlow()!=null){ + insertFlow(telemetry,influxDB,batchPoints); + + if(telemetry.getReading() == null){ + SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + + double meterReading=getLastMeterReading(myFormat.format(telemetry.getDate()), telemetry.getMeter_id()); + log.debug("last flow value:"+meterReading); + double newmeterreading= meterReading+telemetry.getFlow(); + telemetry.setReading(newmeterreading); + } + }else{ + log.info("No flow data. Not inserting"); + } + + + if(telemetry.getBattery()!=null){ + insertBattery(telemetry,batchPoints); + }else{ + log.info("No battery data. Not inserting"); + } + + if(telemetry.getFlowrate()!=null){ + insertFlowrate(telemetry,batchPoints); + }else{ + log.info("No flowrate data. Not inserting"); + } + + if(telemetry.getReading()!=null){ + insertMeterReading(telemetry,batchPoints); + }else{ + log.info("No meterreading data. Not inserting"); + } + + //influxDB.flush(); + influxDB.write(batchPoints); + + influxDB.close(); + + log.info("Exiting ConnectionStatisticsService.insertData"); + + } + + private void insertFlow(Telemetry telemetry,InfluxDB influxDB ,BatchPoints batchPoints){ + + log.info("Entering ConnectionStatisticsService.insertFlow"); + + /* + influxDB.write(Point.measurement("flow") + .time(telemetry.getDate().getTime(), TimeUnit.MILLISECONDS) + .addField("meter_id", telemetry.getMeter_id()) + .addField("value", telemetry.getFlow()) + .build()); +*/ + Map tagMap=new HashMap(); + tagMap.put("meter_id", Integer.toString(telemetry.getMeter_id())); + Point point1 = Point.measurement("flowvalues") + .time(telemetry.getDate().getTime(), TimeUnit.MILLISECONDS) + .tag(tagMap) + //.addField("meter_id", telemetry.getMeter_id()) + .addField("value", telemetry.getFlow()) + .build(); + + batchPoints.point(point1); + + + log.debug("Inserted flow into db"); + log.info("Exiting ConnectionStatisticsService.insertFlow"); + + } + private void insertBattery(Telemetry telemetry,BatchPoints batchPoints){ + + log.info("Entering ConnectionStatisticsService.insertBattery"); + + /*influxDB.write(Point.measurement("batterylevel") + .time(telemetry.getDate().getTime(), TimeUnit.MILLISECONDS) + .addField("meter_id", telemetry.getMeter_id()) + .addField("value", telemetry.getBattery()) + .build()); + */ + Map tagMap=new HashMap(); + tagMap.put("meter_id", Integer.toString(telemetry.getMeter_id())); + Point point1 = Point.measurement("batterylevelvalues") + .time(telemetry.getDate().getTime(), TimeUnit.MILLISECONDS) + //.addField("meter_id", telemetry.getMeter_id()) + .tag(tagMap) + .addField("value", telemetry.getBattery()) + .build(); + + batchPoints.point(point1); + + log.debug("Inserted battery into db"); + + log.info("Exiting ConnectionStatisticsService.insertBattery"); + + } + + + + + private void insertFlowrate(Telemetry telemetry,BatchPoints batchPoints){ + + log.info("Entering ConnectionStatisticsService.insertFlowrate"); + + /*influxDB.write(Point.measurement("flowrate") + .time(telemetry.getDate().getTime(), TimeUnit.MILLISECONDS) + .addField("meter_id", telemetry.getMeter_id()) + .addField("value", telemetry.getFlowrate()) + .build());*/ + + Map tagMap=new HashMap(); + tagMap.put("meter_id",Integer.toString( telemetry.getMeter_id())); + Point point1 = Point.measurement("flowratevalues") + .time(telemetry.getDate().getTime(), TimeUnit.MILLISECONDS) + .tag(tagMap) + //.addField("meter_id", telemetry.getMeter_id()) + .addField("value", telemetry.getFlowrate()) + .build(); + batchPoints.point(point1); + log.debug("Inserted Flowrate into db"); + + log.info("Exiting ConnectionStatisticsService.insertFlowrate"); + + } + + private void insertMeterReading(Telemetry telemetry,BatchPoints batchPoints){ + + log.info("Entering ConnectionStatisticsService.insertMeterReading"); + + /*influxDB.write(Point.measurement("flowrate") + .time(telemetry.getDate().getTime(), TimeUnit.MILLISECONDS) + .addField("meter_id", telemetry.getMeter_id()) + .addField("value", telemetry.getFlowrate()) + .build());*/ + + Map tagMap=new HashMap(); + tagMap.put("meter_id",Integer.toString( telemetry.getMeter_id())); + Point point1 = Point.measurement("meterreadingvalues") + .time(telemetry.getDate().getTime(), TimeUnit.MILLISECONDS) + .tag(tagMap) + //.addField("meter_id", telemetry.getMeter_id()) + .addField("value", telemetry.getReading()) + .build(); + batchPoints.point(point1); + log.debug("Inserted meterreading into db"); + + log.info("Exiting ConnectionStatisticsService.insertMeterReading"); + + } + + /** + * This methods gets data for specific meter for the day + * @param meterId + * @return + * @throws ParseException + */ + + public ConnectionKpiData getAllMetricsForConnectionForDay(int meterId) throws ParseException{ + + + log.info("Entering ConnectionStatisticsService.getAllMetricsForConnectionForDay"); + SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd"); + + Calendar dayStart=Calendar.getInstance(TimeZone.getTimeZone("UTC")); + dayStart.set(Calendar.HOUR_OF_DAY, 0); + dayStart.set(Calendar.MINUTE, 0); + dayStart.set(Calendar.SECOND, 0); + + + Calendar dayend=Calendar.getInstance(TimeZone.getTimeZone("UTC")); + dayend.set(Calendar.HOUR_OF_DAY, 0); + dayend.set(Calendar.MINUTE, 59); + dayend.set(Calendar.SECOND, 59); + dayend.add(Calendar.DATE,1); + + RequestData requestData=new RequestData(); + requestData.setHouse_ID(meterId); + + requestData.setStart_Time(myFormat.format(dayStart.getTime())); + requestData.setEnd_Time(myFormat.format(dayend.getTime())); + requestData.setSample_Distance_value(1); + requestData.setSample_Distance("Day"); + List batteryData=geBatterytData(requestData); + + Double batterylevel = new Double(2000); + if(batteryData != null && batteryData.size()>0){ + batterylevel=batteryData.get(0).getValue(); + } + List consumptionData=getData(requestData); + Double consumption = new Double(0); + if(consumptionData != null && consumptionData.size()>0){ + consumption=(consumptionData.get(0).getValue()); + } + + int status=getCurrentDeviceStatus(meterId); + + Kpi consumptionkpi= new Kpi("Consumption",consumption); + Kpi batterykpi= new Kpi("Battery",batterylevel); + String deviceStatus="IN ACTIVE"; + if(status == 0){ + deviceStatus="NOT WORKING"; + }else if(status == 1){ + deviceStatus="WORKING"; + }else if(status == -1){ + deviceStatus="IN ACTIVE"; + } + Kpi statuskpi=new Kpi("Status",deviceStatus); + + List kpiList=new ArrayList(); + kpiList.add(consumptionkpi); + kpiList.add(batterykpi); + kpiList.add(statuskpi); + + ConnectionKpiData data=new ConnectionKpiData(meterId, kpiList); + + + log.info("Exiting ConnectionStatisticsService.getAllMetricsForConnectionForDay"); + return data; + + } + + public int getCurrentDeviceStatus(int meterId){ + log.info("Entering ConnectionStatisticsService.getCurrentDeviceStatus"); + + + String query = "select last(value) from devicestatus where meter_id='"+meterId+"'";// now() - 10d and meter_id = '124' group by time(1d) fill(0) + log.debug("Query is:"+query); + + + //InfluxDB influxDB = InfluxDBFactory.connect("http://localhost:32770", "root", "root"); + InfluxDB influxDB = InfluxDBFactory.connect(influxProperties.getUrl(),influxProperties.getUsername(),influxProperties.getPassword()); + long startStarttime=System.currentTimeMillis(); + QueryResult queryResult = influxDB.query(new Query(query, influxProperties.getDbname())); + log.info("Entering ConnectionStatisticsService.getCurrentDeviceStatus"); + long endtime=System.currentTimeMillis(); + log.debug("Time After getBattery query execution:"+endtime); + log.debug("Time Taken for query execution:"+(endtime-startStarttime)); + List resultList=queryResult.getResults(); + + int returnval=-1; + + if(resultList != null && resultList.size()>0){ + Result result=resultList.get(0); + + List serieslist=result.getSeries(); + if(serieslist != null && serieslist.size()>0){ + Series series=serieslist.get(0); + + List> resultrow=series.getValues(); + if(resultrow != null && resultrow.size()>0){ + List row=resultrow.get(0); + returnval=((Double)row.get(1)).intValue(); + } + + } + } + influxDB.close(); + return returnval; + + + + } + + /** + * + * @param metrics + * @return + */ + private String getSeriesForMetrics(String metrics){ + //TODO CHange this to take from DB or file + String returnVal=null; + + if("readings".equalsIgnoreCase(metrics)){ + returnVal="meterreadingvalues"; + }else if("meteron".equalsIgnoreCase(metrics)){ + returnVal="supplyondata"; + }else if ("meteroff".equalsIgnoreCase(metrics)){ + returnVal="supplyoffdata"; + } + return returnVal; + } + + private double getLastMeterReading(String startTime,int meterId){ + + + double baseReadingValue =0; + + String query = "select last(value) from meterreadingvalues where time <='"+startTime+"' and meter_id='"+meterId+"'";// now() - 10d and meter_id = '124' group by time(1d) fill(0) + log.info("Query is:"+query); + + + //InfluxDB influxDB = InfluxDBFactory.connect("http://localhost:32768", "root", "root"); + InfluxDB influxDB = InfluxDBFactory.connect(influxProperties.getUrl(),influxProperties.getUsername(),influxProperties.getPassword()); + + QueryResult queryResult = influxDB.query(new Query(query, influxProperties.getDbname())); + + List results=queryResult.getResults(); + if(results != null && results.size()>0){ + Result result = results.get(0); + + List serieslist=result.getSeries(); + if(serieslist !=null && serieslist.size()>0){ + Series series=serieslist.get(0); + List> objects=series.getValues(); + List resultvals=objects.get(0); + Double double1=(Double)resultvals.get(1); + baseReadingValue=double1.doubleValue(); + } + + + } + return baseReadingValue; + + } + + public Double getAverageMonthlyForOneYear(int meterId) throws ParseException{ + + log.info("Entering ConnectionStatisticsService.getAverageMonthlyForOneYear"); + + Calendar endTimeCal=Calendar.getInstance(TimeZone.getTimeZone("UTC")); + endTimeCal.set(Calendar.DAY_OF_MONTH, 1); + //endTimeCal.add(Calendar.DATE, -1); + + Calendar startTimeCal=Calendar.getInstance(TimeZone.getTimeZone("UTC")); + startTimeCal.set(Calendar.DAY_OF_MONTH, 1); + startTimeCal.add(Calendar.YEAR, -1); + + Double resultValue=0.0; + + SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd"); + + String startTime=myFormat.format(startTimeCal.getTime()); + + + String endTime=myFormat.format(endTimeCal.getTime()); + + endTime = getNextDay( endTime); + String query = "select mean(monthlyconsumption) from (select sum(value) as monthlyconsumption from flowvalues where time >='"+startTime+"' and time<'"+endTime+"' and meter_id='"+meterId+"' group by time(30d))";// now() - 10d and meter_id = '124' group by time(1d) fill(0) + log.info("Query is:"+query); + + + //InfluxDB influxDB = InfluxDBFactory.connect("http://localhost:32768", "root", "root"); + InfluxDB influxDB = InfluxDBFactory.connect(influxProperties.getUrl(),influxProperties.getUsername(),influxProperties.getPassword()); + //String influxProperties.getDbname() = influxProperties.getDbname(); + QueryResult queryResult = influxDB.query(new Query(query, influxProperties.getDbname())); + + List results=queryResult.getResults(); + if(results != null && results.size()>0){ + Result result = results.get(0); + + List serieslist=result.getSeries(); + if(serieslist !=null && serieslist.size()>0){ + Series series=serieslist.get(0); + List> objects=series.getValues(); + List resultvals=objects.get(0); + resultValue=(Double)resultvals.get(1); + if(resultValue!=null){ + resultValue= Math.round(resultValue*100D)/100D; + } + + } + + + } + log.info("Exiting ConnectionStatisticsService.getAverageMonthlyForOneYear"); + return resultValue; + + } + + public void insertTimeSeriesData(SeriesPointData pointData){ + + + InfluxDB influxDB =InfluxDBFactory.connect(influxProperties.getUrl(),influxProperties.getUsername(),influxProperties.getPassword()); + + log.info("Entering ConnectionStatisticsService.insertTimeSeriesData"); + + influxDB.setDatabase(influxProperties.getDbname()); + influxDB.enableBatch(BatchOptions.DEFAULTS); + String rpName = "aRetentionPolicy2"; + // influxDB.createRetentionPolicy(rpName, influxProperties.getDbname(), "365d", "30m", 2, true); + influxDB.setRetentionPolicy("aRetentionPolicy"); + + BatchPoints batchPoints = BatchPoints + .database(influxProperties.getDbname()) +// .tag("async", "true") + .retentionPolicy(rpName) + .consistency(ConsistencyLevel.ALL) + .build(); + + String name=pointData.getName(); + String seriesName=getSeriesForMetrics(name); + + Map tagMap=new HashMap(); + + List tags=pointData.getTags(); + tags.forEach(tag -> { + tagMap.put(tag.getKey(),tag.getValue().toString()); + }); + + Map filedMap=new HashMap(); + List fieldlist=pointData.getValues(); + + fieldlist.forEach(tag -> { + filedMap.put(tag.getKey(),tag.getValue()); + }); + + Date timeStamp=pointData.getTimestamp(); + + if(timeStamp ==null){ + Calendar cal=Calendar.getInstance(TimeZone.getTimeZone(influxProperties.getDatatimezone())); + timeStamp=cal.getTime(); + }else{ + + + + Calendar date=Calendar.getInstance(TimeZone.getTimeZone(influxProperties.getDatatimezone())); + //Date inputDate=telemetry.getDate(); + Calendar inputCal=Calendar.getInstance(TimeZone.getTimeZone(influxProperties.getDatatimezone())); + inputCal.setTime(pointData.getTimestamp()); + date.set(Calendar.YEAR,inputCal.get(Calendar.YEAR)); + date.set(Calendar.MONTH,inputCal.get(Calendar.MONTH)); + date.set(Calendar.DAY_OF_MONTH, inputCal.get(Calendar.DAY_OF_MONTH)); + date.set(Calendar.HOUR,inputCal.get(Calendar.HOUR_OF_DAY)); + date.set(Calendar.MINUTE,inputCal.get(Calendar.MINUTE)); + date.set(Calendar.SECOND,inputCal.get(Calendar.SECOND)); + date.set(Calendar.MILLISECOND,inputCal.get(Calendar.MILLISECOND)); + pointData.setTimestamp(date.getTime()); + + } + + + + + Point point1 = Point.measurement(seriesName) + .time(timeStamp.getTime(), TimeUnit.MILLISECONDS) + .tag(tagMap) + .fields(filedMap) + .build(); + + + + batchPoints.point(point1); + + influxDB.write(batchPoints); + + influxDB.close(); + + log.info("Exiting ConnectionStatisticsService.insertTimeSeriesData"); + + + + } + +} \ No newline at end of file