|
1 | 1 | package com.techolution.mauritius.smartwater.supply.service;
|
2 | 2 |
|
| 3 | +import java.sql.Date; |
3 | 4 | import java.text.SimpleDateFormat;
|
| 5 | +import java.time.Instant; |
4 | 6 | import java.util.ArrayList;
|
5 | 7 | import java.util.Calendar;
|
6 | 8 | import java.util.List;
|
|
20 | 22 | import org.springframework.stereotype.Component;
|
21 | 23 |
|
22 | 24 | import com.techolution.mauritius.smartwater.supply.InfluxProperties;
|
| 25 | +import com.techolution.mauritius.smartwater.supply.domain.ConsumptionLeakage; |
| 26 | +import com.techolution.mauritius.smartwater.supply.domain.LeakageData; |
23 | 27 | import com.techolution.mauritius.smartwater.supply.domain.MeterConnection;
|
24 | 28 | import com.techolution.mauritius.smartwater.supply.domain.MeterTrendData;
|
25 | 29 | import com.techolution.mauritius.smartwater.supply.domain.WaterSupplyDailyConnectionStats;
|
@@ -52,6 +56,8 @@ public class SupplyAnalyticsService {
|
52 | 56 | private static String DOWN="DOWN";
|
53 | 57 | private static String DEFAULT_LOCATION="TEST";
|
54 | 58 |
|
| 59 | + private static String SERIES_NAME_CONSUMERLEAKAGE="consumerleakage"; |
| 60 | + private static String SERIES_NAME_NETWORKLEAKAGE="networkleakage"; |
55 | 61 |
|
56 | 62 |
|
57 | 63 |
|
@@ -290,7 +296,7 @@ public Map <Long, MeterConnection> getAllConnections(){
|
290 | 296 |
|
291 | 297 | public WaterSupplyDailyConnectionStats getStats(){
|
292 | 298 |
|
293 |
| - log.info("Entering SupplyDataService.getStats"); |
| 299 | + log.info("Entering SupplyAnalyticsService.getStats"); |
294 | 300 |
|
295 | 301 | WaterSupplyDailyConnectionStats connectionStats=new WaterSupplyDailyConnectionStats();
|
296 | 302 | getConnectionsMap();
|
@@ -396,8 +402,125 @@ public WaterSupplyDailyConnectionStats getStats(){
|
396 | 402 |
|
397 | 403 |
|
398 | 404 | influxDB.close();
|
399 |
| - log.info("Exiting SupplyDataService.getStats"); |
| 405 | + log.info("Exiting SupplyAnalyticsService.getStats"); |
400 | 406 | return connectionStats;
|
401 | 407 | }
|
| 408 | + |
| 409 | + public LeakageData getCurrentDayLeakageData(){ |
| 410 | + |
| 411 | + log.info("Entering SupplyAnalyticsService.getStats"); |
| 412 | + |
| 413 | + SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd"); |
| 414 | + |
| 415 | + Calendar start=Calendar.getInstance(TimeZone.getTimeZone(influxProperties.getDatatimezone())); |
| 416 | + String startTime=myFormat.format(start.getTime()); |
| 417 | + |
| 418 | + start.add(Calendar.DATE,1); |
| 419 | + String endTime=myFormat.format(start.getTime()); |
| 420 | + |
| 421 | + String leakageval="select sum(value) from networkleakage,consumerleakage where time >='"+startTime+"' and time <'"+endTime+"' "; |
| 422 | + |
| 423 | + |
| 424 | + InfluxDB influxDB = InfluxDBFactory.connect(influxProperties.getUrl(),influxProperties.getUsername(),influxProperties.getPassword()); |
| 425 | + long startStarttime=System.currentTimeMillis(); |
| 426 | + QueryResult queryResult = influxDB.query(new Query(leakageval, influxProperties.getDbname())); |
| 427 | + long endtime=System.currentTimeMillis(); |
| 428 | + log.debug("Time After countOfSupply query execution:"+endtime); |
| 429 | + log.debug("Time Taken for query execution:"+(endtime-startStarttime)); |
| 430 | + |
| 431 | + LeakageData leakageData=new LeakageData(); |
| 432 | + // leakageData.setMetricsDate((Calendar.getInstance(influxProperties.getDatatimezone()))); |
| 433 | + |
| 434 | + List<Result> results=queryResult.getResults(); |
| 435 | + if(results!=null && results.size()>0){ |
| 436 | + Result result=results.get(0); |
| 437 | + List<Series> serieslist=result.getSeries(); |
| 438 | + serieslist.forEach(series -> { |
| 439 | + String seriesName= series.getName(); |
| 440 | + List<List<Object>> values=series.getValues(); |
| 441 | + if(values !=null && values.size()>0){ |
| 442 | + List<Object> value=values.get(0); |
| 443 | + if(SERIES_NAME_CONSUMERLEAKAGE.equalsIgnoreCase(seriesName)){ |
| 444 | + leakageData.setConsumptionLeakage((Double)value.get(1)); |
| 445 | + String time=(String)value.get(0); |
| 446 | + Instant instant=Instant.parse(time); |
| 447 | + |
| 448 | + leakageData.setMetricsDate(Date.from(instant)); |
| 449 | + }else{ |
| 450 | + leakageData.setNetworkLeakage((Double)value.get(1)); |
| 451 | + String time=(String)value.get(0); |
| 452 | + Instant instant=Instant.parse(time); |
| 453 | + |
| 454 | + leakageData.setMetricsDate(Date.from(instant)); |
| 455 | + } |
| 456 | + } |
| 457 | + |
| 458 | + }); |
| 459 | + } |
| 460 | + |
| 461 | + |
| 462 | + log.info("Exiting SupplyAnalyticsService.getStats"); |
| 463 | + return leakageData; |
| 464 | + |
| 465 | + } |
| 466 | + |
| 467 | + |
| 468 | + public List<ConsumptionLeakage> getCurrentDayConsumerLeakage(){ |
| 469 | + |
| 470 | + log.info("Entering SupplyAnalyticsService.getCurrentDayConsumerLeakage"); |
| 471 | + |
| 472 | + SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd"); |
| 473 | + |
| 474 | + Calendar start=Calendar.getInstance(TimeZone.getTimeZone(influxProperties.getDatatimezone())); |
| 475 | + String startTime=myFormat.format(start.getTime()); |
| 476 | + |
| 477 | + start.add(Calendar.DATE,1); |
| 478 | + String endTime=myFormat.format(start.getTime()); |
| 479 | + |
| 480 | + String leakageval="select sum(value) from consumerleakage where time >='"+startTime+"' and time <'"+endTime+"' group by meter_id "; |
| 481 | + |
| 482 | + |
| 483 | + InfluxDB influxDB = InfluxDBFactory.connect(influxProperties.getUrl(),influxProperties.getUsername(),influxProperties.getPassword()); |
| 484 | + long startStarttime=System.currentTimeMillis(); |
| 485 | + QueryResult queryResult = influxDB.query(new Query(leakageval, influxProperties.getDbname())); |
| 486 | + long endtime=System.currentTimeMillis(); |
| 487 | + log.debug("Time After countOfSupply query execution:"+endtime); |
| 488 | + log.debug("Time Taken for query execution:"+(endtime-startStarttime)); |
| 489 | + |
| 490 | + |
| 491 | + List<ConsumptionLeakage> resultList=new ArrayList<ConsumptionLeakage>(); |
| 492 | + |
| 493 | + // leakageData.setMetricsDate((Calendar.getInstance(influxProperties.getDatatimezone()))); |
| 494 | + |
| 495 | + List<Result> results=queryResult.getResults(); |
| 496 | + if(results!=null && results.size()>0){ |
| 497 | + Result result=results.get(0); |
| 498 | + List<Series> serieslist=result.getSeries(); |
| 499 | + serieslist.forEach(series -> { |
| 500 | + ConsumptionLeakage leakage= new ConsumptionLeakage(); |
| 501 | + |
| 502 | + String meterId=series.getTags().get(METER_ID); |
| 503 | + leakage.setMeterId(meterId); |
| 504 | + List<List<Object>> values=series.getValues(); |
| 505 | + if(values !=null && values.size()>0){ |
| 506 | + List<Object> value=values.get(0); |
| 507 | + leakage.setLeakage( (Double)value.get(1)); |
| 508 | + |
| 509 | + String time=(String)value.get(0); |
| 510 | + Instant instant=Instant.parse(time); |
| 511 | + leakage.setDate(Date.from(instant)); |
| 512 | + |
| 513 | + |
| 514 | + } |
| 515 | + resultList.add(leakage); |
| 516 | + |
| 517 | + }); |
| 518 | + } |
| 519 | + |
| 520 | + |
| 521 | + log.info("Exiting SupplyAnalyticsService.getCurrentDayConsumerLeakage"); |
| 522 | + return resultList; |
| 523 | + |
| 524 | +} |
402 | 525 |
|
403 | 526 | }
|
0 commit comments