Skip to content

Commit

Permalink
Added more traces
Browse files Browse the repository at this point in the history
  • Loading branch information
viktorKhan committed Apr 29, 2024
1 parent 504d9f0 commit 60ad443
Show file tree
Hide file tree
Showing 17 changed files with 418 additions and 143 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.lucoenergia.conluz.domain.production.huawei.get;

import org.lucoenergia.conluz.domain.production.huawei.RealTimeProduction;

import java.time.OffsetDateTime;
import java.util.List;

public interface GetHuaweiProductionRepository {

List<RealTimeProduction> getRealTimeProductionByRangeOfDates(OffsetDateTime startDate, OffsetDateTime endDate);
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ public void syncRealTimeProduction() {
// Get Huawei configuration
Optional<HuaweiConfig> huaweiConfig = getHuaweiConfigRepository.getHuaweiConfig();
if (huaweiConfig.isEmpty()) {
LOGGER.debug("No Huawei config found");
LOGGER.info("No Huawei config found");
return;
}

// Get all energy stations with Huawei inverter
List<Plant> huaweiStations = getEnergyStationRepository.findAllByInverterProvider(InverterProvider.HUAWEI);
if (huaweiStations.isEmpty()) {
LOGGER.debug("No Huawei stations found");
LOGGER.info("No Huawei stations found");
return;
}

Expand All @@ -73,14 +73,14 @@ public void syncHourlyProduction() {
// Get Huawei configuration
Optional<HuaweiConfig> huaweiConfig = getHuaweiConfigRepository.getHuaweiConfig();
if (huaweiConfig.isEmpty()) {
LOGGER.debug("No Huawei config found");
LOGGER.info("No Huawei config found");
return;
}

// Get all energy stations with Huawei inverter
List<Plant> huaweiStations = getEnergyStationRepository.findAllByInverterProvider(InverterProvider.HUAWEI);
if (huaweiStations.isEmpty()) {
LOGGER.debug("No Huawei stations found");
LOGGER.info("No Huawei stations found");
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import org.lucoenergia.conluz.domain.consumption.datadis.DatadisConsumption;
import org.lucoenergia.conluz.domain.consumption.datadis.get.GetDatadisConsumptionRepository;
import org.lucoenergia.conluz.infrastructure.consumption.datadis.DatadisConsumptionPoint;
import org.lucoenergia.conluz.infrastructure.consumption.datadis.config.DatadisConfigEntity;
import org.lucoenergia.conluz.infrastructure.consumption.datadis.DatadisDateTimeConverter;
import org.lucoenergia.conluz.infrastructure.shared.db.influxdb.DateToInfluxDbDateFormatConverter;
import org.lucoenergia.conluz.infrastructure.consumption.datadis.config.DatadisConfigEntity;
import org.lucoenergia.conluz.infrastructure.shared.db.influxdb.InfluxDbConnectionManager;
import org.lucoenergia.conluz.infrastructure.shared.time.DateConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

Expand All @@ -23,11 +23,11 @@
public class GetDatadisConsumptionRepositoryInflux implements GetDatadisConsumptionRepository {

private final InfluxDbConnectionManager influxDbConnectionManager;
private final DateToInfluxDbDateFormatConverter dateConverter;
private final DateConverter dateConverter;
private final DatadisDateTimeConverter datadisDateTimeConverter;

public GetDatadisConsumptionRepositoryInflux(InfluxDbConnectionManager influxDbConnectionManager,
DateToInfluxDbDateFormatConverter dateConverter, DatadisDateTimeConverter datadisDateTimeConverter) {
DateConverter dateConverter, DatadisDateTimeConverter datadisDateTimeConverter) {
this.influxDbConnectionManager = influxDbConnectionManager;
this.dateConverter = dateConverter;
this.datadisDateTimeConverter = datadisDateTimeConverter;
Expand All @@ -36,8 +36,8 @@ public GetDatadisConsumptionRepositoryInflux(InfluxDbConnectionManager influxDbC
@Override
public List<DatadisConsumption> getHourlyConsumptionsByMonth(Supply supply, Month month, int year) {

String startDate = dateConverter.convertToFirstDayOfTheMonth(month, year);
String endDate = dateConverter.convertToLastDayOfTheMonth(month, year);
String startDate = dateConverter.convertToFirstDayOfTheMonthAsString(month, year);
String endDate = dateConverter.convertToLastDayOfTheMonthAsString(month, year);

try (InfluxDB connection = influxDbConnectionManager.getConnection()) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import org.lucoenergia.conluz.domain.consumption.shelly.ShellyConsumption;
import org.lucoenergia.conluz.domain.consumption.shelly.persist.PersistShellyConsumptionRepository;
import org.lucoenergia.conluz.infrastructure.consumption.shelly.ShellyInstantConsumptionPoint;
import org.lucoenergia.conluz.infrastructure.shared.db.influxdb.DateToInfluxDbDateFormatConverter;
import org.lucoenergia.conluz.infrastructure.shared.db.influxdb.InfluxDbConnectionManager;
import org.lucoenergia.conluz.infrastructure.shared.time.DateConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
Expand All @@ -25,12 +25,12 @@ public class ShellyConsumptionsHourlyAggregator {
private static final Logger LOGGER = LoggerFactory.getLogger(ShellyConsumptionsHourlyAggregator.class);

private final InfluxDbConnectionManager influxDbConnectionManager;
private final DateToInfluxDbDateFormatConverter dateConverter;
private final DateConverter dateConverter;
private final GetSupplyRepository getSupplyRepository;
private final PersistShellyConsumptionRepository persistShellyConsumptionRepository;

public ShellyConsumptionsHourlyAggregator(InfluxDbConnectionManager influxDbConnectionManager,
DateToInfluxDbDateFormatConverter dateConverter,
DateConverter dateConverter,
GetSupplyRepository getSupplyRepository,
PersistShellyConsumptionRepository persistShellyConsumptionRepository) {
this.influxDbConnectionManager = influxDbConnectionManager;
Expand All @@ -41,15 +41,19 @@ public ShellyConsumptionsHourlyAggregator(InfluxDbConnectionManager influxDbConn

public void aggregate(OffsetDateTime startDate, OffsetDateTime endDate) {

String startDateAsString = dateConverter.convert(startDate);
String endDateAsString = dateConverter.convert(endDate);
String startDateAsString = dateConverter.convertToString(startDate);
String endDateAsString = dateConverter.convertToString(endDate);

LOGGER.info("Aggregating Shelly consumption from {} to {}", startDateAsString, endDateAsString);

List<Supply> supplies = getSupplyRepository.findAll();

try (InfluxDB connection = influxDbConnectionManager.getConnection()) {

for (Supply supply : supplies) {

LOGGER.info("Aggregating Shelly consumption from supply {}", supply.getShellyId());

try {
Query query = new Query(String.format(
"SELECT INTEGRAL(%s, 1h) AS %s FROM \"%s\" WHERE %s = '%s' AND time >= '%s' AND time <= '%s' GROUP BY time(1h)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import org.lucoenergia.conluz.domain.consumption.shelly.get.GetShellyConsumptionRepository;
import org.lucoenergia.conluz.infrastructure.consumption.shelly.ShellyConsumptionPoint;
import org.lucoenergia.conluz.infrastructure.consumption.shelly.ShellyInstantConsumptionPoint;
import org.lucoenergia.conluz.infrastructure.shared.db.influxdb.DateToInfluxDbDateFormatConverter;
import org.lucoenergia.conluz.infrastructure.shared.db.influxdb.InfluxDbConnectionManager;
import org.lucoenergia.conluz.infrastructure.shared.time.DateConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

Expand All @@ -23,19 +23,19 @@
public class GetShellyConsumptionRepositoryInflux implements GetShellyConsumptionRepository {

private final InfluxDbConnectionManager influxDbConnectionManager;
private final DateToInfluxDbDateFormatConverter dateConverter;
private final DateConverter dateConverter;

public GetShellyConsumptionRepositoryInflux(InfluxDbConnectionManager influxDbConnectionManager,
DateToInfluxDbDateFormatConverter dateConverter) {
DateConverter dateConverter) {
this.influxDbConnectionManager = influxDbConnectionManager;
this.dateConverter = dateConverter;
}

@Override
public List<ShellyInstantConsumption> getHourlyConsumptionsByRangeOfDatesAndSupply(Supply supply, OffsetDateTime startDate,
OffsetDateTime endDate) {
String startDateAsString = dateConverter.convert(startDate);
String endDateAsString = dateConverter.convert(endDate);
String startDateAsString = dateConverter.convertToString(startDate);
String endDateAsString = dateConverter.convertToString(endDate);

try (InfluxDB connection = influxDbConnectionManager.getConnection()) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public PersistShellyConsumptionRepositoryInflux(InfluxDbConnectionManager influx
@Override
public void persistInstantConsumptions(List<ShellyInstantConsumption> consumptions) {

LOGGER.info("Persisting Shelly instant consumptions data");

try (InfluxDB connection = influxDbConnectionManager.getConnection()) {

// Create new batch points object
Expand All @@ -47,13 +49,15 @@ public void persistInstantConsumptions(List<ShellyInstantConsumption> consumptio
}
connection.write(batchPoints);
} catch (Exception e) {
LOGGER.error("Unable to persist Shelly consumptions", e);
LOGGER.error("Unable to persist Shelly instant consumptions", e);
}
}

@Override
public void persistConsumptions(List<ShellyConsumption> consumptions) {

LOGGER.info("Persisting Shelly consumptions data");

try (InfluxDB connection = influxDbConnectionManager.getConnection()) {

// Create new batch points object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import org.influxdb.dto.Query;
import org.influxdb.dto.QueryResult;
import org.influxdb.impl.InfluxDBResultMapper;
import org.lucoenergia.conluz.domain.price.get.GetPriceRepository;
import org.lucoenergia.conluz.domain.price.PriceByHour;
import org.lucoenergia.conluz.domain.price.get.GetPriceRepository;
import org.lucoenergia.conluz.infrastructure.price.PriceByHourInfluxMapper;
import org.lucoenergia.conluz.infrastructure.price.PriceByHourPoint;
import org.lucoenergia.conluz.infrastructure.price.omie.OmieConfig;
import org.lucoenergia.conluz.infrastructure.shared.db.influxdb.DateToInfluxDbDateFormatConverter;
import org.lucoenergia.conluz.infrastructure.shared.db.influxdb.InfluxDbConnectionManager;
import org.lucoenergia.conluz.infrastructure.shared.time.DateConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

Expand All @@ -24,9 +24,10 @@ public class GetPriceRepositoryInflux implements GetPriceRepository {

private final InfluxDbConnectionManager influxDbConnectionManager;
private final PriceByHourInfluxMapper priceByHourInfluxMapper;
private final DateToInfluxDbDateFormatConverter dateConverter;
private final DateConverter dateConverter;

public GetPriceRepositoryInflux(InfluxDbConnectionManager influxDbConnectionManager, PriceByHourInfluxMapper priceByHourInfluxMapper, DateToInfluxDbDateFormatConverter dateConverter) {
public GetPriceRepositoryInflux(InfluxDbConnectionManager influxDbConnectionManager,
PriceByHourInfluxMapper priceByHourInfluxMapper, DateConverter dateConverter) {
this.influxDbConnectionManager = influxDbConnectionManager;
this.priceByHourInfluxMapper = priceByHourInfluxMapper;
this.dateConverter = dateConverter;
Expand All @@ -40,8 +41,8 @@ public List<PriceByHour> getPricesByRangeOfDates(OffsetDateTime startDate, Offse
Query query = new Query(String.format(
"SELECT * FROM %s WHERE time >= '%s' AND time <= '%s'",
OmieConfig.PRICES_KWH_MEASUREMENT,
dateConverter.convert(startDate),
dateConverter.convert(endDate)));
dateConverter.convertToString(startDate),
dateConverter.convertToString(endDate)));

QueryResult queryResult = connection.query(query);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package org.lucoenergia.conluz.infrastructure.production;

import org.influxdb.annotation.Column;
import org.influxdb.annotation.Measurement;
import org.lucoenergia.conluz.domain.production.huawei.HuaweiConfig;

import java.time.Instant;

@Measurement(name = HuaweiConfig.HUAWEI_REAL_TIME_PRODUCTION_MEASUREMENT)
public class RealTimeProductionPoint {

@Column(name = "time")
private Instant time;
/**
* Plant ID
*/
@Column(name = "station_code")
private String stationCode;
/**
* Plant health status.
* The following plant health states are supported:
* 1: disconnected
* 2: faulty
* 3: healthy
*/
@Column(name = "real_health_state")
private int realHealthState;
/**
* Yield today (kWh)
*/
@Column(name = "day_power")
private double dayPower;
/**
* Total yield (kWh)
*/
@Column(name = "total_power")
private double totalPower;
/**
* Revenue today, in the currency specified in the management system.
*/
@Column(name = "day_income")
private double dayIncome;
/**
* Yield this month (kWh)
*/
@Column(name = "month_power")
private double monthPower;
/**
* Total revenue, in the currency specified in the management system.
*/
@Column(name = "total_income")
private double totalIncome;

public Instant getTime() {
return time;
}

public String getStationCode() {
return stationCode;
}

public int getRealHealthState() {
return realHealthState;
}

public double getDayPower() {
return dayPower;
}

public double getTotalPower() {
return totalPower;
}

public double getDayIncome() {
return dayIncome;
}

public double getMonthPower() {
return monthPower;
}

public double getTotalIncome() {
return totalIncome;
}


public static class Builder {
private Instant time;
private String stationCode;
private int realHealthState;
private double dayPower;
private double totalPower;
private double dayIncome;
private double monthPower;
private double totalIncome;

public Builder setTime(Instant time) {
this.time = time;
return this;
}

public Builder setStationCode(String stationCode) {
this.stationCode = stationCode;
return this;
}

public Builder setRealHealthState(int realHealthState) {
this.realHealthState = realHealthState;
return this;
}

public Builder setDayPower(double dayPower) {
this.dayPower = dayPower;
return this;
}

public Builder setTotalPower(double totalPower) {
this.totalPower = totalPower;
return this;
}

public Builder setDayIncome(double dayIncome) {
this.dayIncome = dayIncome;
return this;
}

public Builder setMonthPower(double monthPower) {
this.monthPower = monthPower;
return this;
}

public Builder setTotalIncome(double totalIncome) {
this.totalIncome = totalIncome;
return this;
}

public RealTimeProductionPoint build() {
RealTimeProductionPoint realTimeProduction = new RealTimeProductionPoint();
realTimeProduction.time = this.time;
realTimeProduction.stationCode = this.stationCode;
realTimeProduction.realHealthState = this.realHealthState;
realTimeProduction.dayPower = this.dayPower;
realTimeProduction.totalPower = this.totalPower;
realTimeProduction.dayIncome = this.dayIncome;
realTimeProduction.monthPower = this.monthPower;
realTimeProduction.totalIncome = this.totalIncome;
return realTimeProduction;
}
}
}
Loading

0 comments on commit 60ad443

Please sign in to comment.