Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/conluz-49 to main #51

Merged
merged 3 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-mail'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-quartz'

implementation 'org.liquibase:liquibase-core'
implementation 'org.influxdb:influxdb-java:2.23'
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/lucoenergia/conluz/ConLuzApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class ConLuzApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

public interface GetSupplyRepository {

long count();

Optional<Supply> findById(SupplyId id);

boolean existsById(SupplyId id);
Expand Down
120 changes: 99 additions & 21 deletions src/main/java/org/lucoenergia/conluz/domain/admin/supply/Supply.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,99 @@

import org.lucoenergia.conluz.domain.admin.user.User;

import java.time.LocalDate;

public class Supply {

private final String id;
private User user;
private String name;
private final String name;
private final String address;
private final Float partitionCoefficient;
private final Boolean enabled;
private final LocalDate validDateFrom;
private final String distributor;
private final String distributorCode;
private final String pointType;

public Supply(String id, String address, Float partitionCoefficient, Boolean enabled) {
this.id = id;
this.address = address;
this.partitionCoefficient = partitionCoefficient;
this.enabled = enabled;
private Supply(Builder builder) {
this.id = builder.id;
this.user = builder.user;
this.name = builder.name;
this.address = builder.address;
this.partitionCoefficient = builder.partitionCoefficient;
this.enabled = builder.enabled;
this.validDateFrom = builder.validDateFrom;
this.distributor = builder.distributor;
this.distributorCode = builder.distributorCode;
this.pointType = builder.pointType;
}

public Supply(String id, String name, String address, Float partitionCoefficient, Boolean enabled) {
this.id = id;
this.name = name;
this.address = address;
this.partitionCoefficient = partitionCoefficient;
this.enabled = enabled;
}
public static class Builder {
private String id;
private User user;
private String name;
private String address;
private Float partitionCoefficient;
private Boolean enabled;
private LocalDate validDateFrom;
private String distributor;
private String distributorCode;
private String pointType;

public Supply(String id, User user, String name, String address, Float partitionCoefficient, Boolean enabled) {
this.id = id;
this.user = user;
this.name = name;
this.address = address;
this.partitionCoefficient = partitionCoefficient;
this.enabled = enabled;
public Builder withId(String id) {
this.id = id;
return this;
}

public Builder withUser(User user) {
this.user = user;
return this;
}

public Builder withName(String name) {
this.name = name;
return this;
}

public Builder withAddress(String address) {
this.address = address;
return this;
}

public Builder withPartitionCoefficient(Float partitionCoefficient) {
this.partitionCoefficient = partitionCoefficient;
return this;
}

public Builder withEnabled(Boolean enabled) {
this.enabled = enabled;
return this;
}

public Builder withValidDateFrom(LocalDate validDateFrom) {
this.validDateFrom = validDateFrom;
return this;
}

public Builder withDistributor(String distributor) {
this.distributor = distributor;
return this;
}

public Builder withDistributorCode(String distributorCode) {
this.distributorCode = distributorCode;
return this;
}

public Builder withPointType(String pointType) {
this.pointType = pointType;
return this;
}

public Supply build() {
return new Supply(this);
}
}

public String getId() {
Expand Down Expand Up @@ -62,4 +124,20 @@ public Float getPartitionCoefficient() {
public Boolean getEnabled() {
return enabled;
}
}

public LocalDate getValidDateFrom() {
return validDateFrom;
}

public String getDistributor() {
return distributor;
}

public String getDistributorCode() {
return distributorCode;
}

public String getPointType() {
return pointType;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package org.lucoenergia.conluz.domain.admin.user;

import jakarta.validation.constraints.*;
import org.lucoenergia.conluz.infrastructure.shared.format.UUIDValidator;
import org.lucoenergia.conluz.infrastructure.shared.format.ValidUUID;
import org.lucoenergia.conluz.infrastructure.shared.uuid.ValidUUID;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.lucoenergia.conluz.domain.consumption.datadis;

public class Consumption {

private String cups;
private String date;
private String time;
private Float consumptionKWh;
private String obtainMethod;
private Float surplusEnergyKWh;

public String getCups() {
return cups;
}

public void setCups(String cups) {
this.cups = cups;
}

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}

public String getTime() {
return time;
}

public void setTime(String time) {
this.time = time;
}

public Float getConsumptionKWh() {
return consumptionKWh;
}

public void setConsumptionKWh(Float consumptionKWh) {
this.consumptionKWh = consumptionKWh;
}

public String getObtainMethod() {
return obtainMethod;
}

public void setObtainMethod(String obtainMethod) {
this.obtainMethod = obtainMethod;
}

public Float getSurplusEnergyKWh() {
return surplusEnergyKWh;
}

public void setSurplusEnergyKWh(Float surplusEnergyKWh) {
this.surplusEnergyKWh = surplusEnergyKWh;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.lucoenergia.conluz.domain.consumption.datadis;

import org.lucoenergia.conluz.domain.admin.supply.GetSupplyRepository;
import org.lucoenergia.conluz.domain.admin.supply.Supply;
import org.lucoenergia.conluz.domain.shared.pagination.PagedRequest;
import org.lucoenergia.conluz.domain.shared.pagination.PagedResult;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.Month;
import java.util.List;

@Service
public class DatadisConsumptionSyncService {

private final GetDatadisConsumptionRepository getDatadisConsumptionRepository;
private final GetSupplyRepository getSupplyRepository;
private final PersistDatadisConsumptionRepository persistDatadisConsumptionRepository;

public DatadisConsumptionSyncService(@Qualifier("getDatadisConsumptionRepositoryRest") GetDatadisConsumptionRepository getDatadisConsumptionRepository,
GetSupplyRepository getSupplyRepository,
PersistDatadisConsumptionRepository persistDatadisConsumptionRepository) {
this.getDatadisConsumptionRepository = getDatadisConsumptionRepository;
this.getSupplyRepository = getSupplyRepository;
this.persistDatadisConsumptionRepository = persistDatadisConsumptionRepository;
}

/**
* Synchronizes the consumptions for all supplies.
* It retrieves all supplies from the repository, and for each supply it retrieves the monthly consumptions
* based on the validity date of the supply. The method iterates through the validity dates until it reaches
* the current date, retrieving the monthly consumptions for each month and year.
*/
public void synchronizeConsumptions() {
// Get all supplies
long total = getSupplyRepository.count();
PagedResult<Supply> suppliesPageResult = getSupplyRepository.findAll(
PagedRequest.of(0, Long.valueOf(total).intValue())
);

LocalDate today = LocalDate.now();
LocalDate oneYearAgo = today.minusYears(1);

for (Supply supply : suppliesPageResult.getItems()) {

// Get validity date
LocalDate validDateFrom = supply.getValidDateFrom();

if (validDateFrom == null) {
continue;
}

if (validDateFrom.isBefore(oneYearAgo)) {
validDateFrom = oneYearAgo;
}

while (validDateFrom.isBefore(today)) {

Month month = validDateFrom.getMonth();
int year = validDateFrom.getYear();

List<Consumption> consumptions = getDatadisConsumptionRepository.getHourlyConsumptionsByMonth(supply, month, year);
if (!consumptions.isEmpty()) {
persistDatadisConsumptionRepository.persistConsumptions(consumptions);
}

validDateFrom = validDateFrom.plusMonths(1);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.lucoenergia.conluz.domain.consumption.datadis;

import jakarta.validation.constraints.NotNull;
import org.lucoenergia.conluz.domain.admin.supply.Supply;

import java.time.Month;
import java.util.List;

public interface GetDatadisConsumptionRepository {

List<Consumption> getHourlyConsumptionsByMonth(@NotNull Supply supply, @NotNull Month month, @NotNull int year);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.lucoenergia.conluz.domain.consumption.datadis;

import jakarta.validation.constraints.NotNull;

import java.util.List;

public interface PersistDatadisConsumptionRepository {

void persistConsumptions(@NotNull List<Consumption> consumptions);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ public class SupplyEntityMapper extends BaseMapper<SupplyEntity, Supply> {

@Override
public Supply map(SupplyEntity entity) {
return new Supply(entity.getId(), entity.getName(), entity.getAddress(), entity.getPartitionCoefficient(),
entity.getEnabled());
return new Supply.Builder()
.withId(entity.getId())
.withAddress(entity.getAddress())
.withPartitionCoefficient(entity.getPartitionCoefficient())
.withEnabled(entity.getEnabled())
.withName(entity.getName())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ public class CreateSupplyAssembler implements Assembler<CreateSupplyBody, Supply

@Override
public Supply assemble(CreateSupplyBody body) {
return new Supply(body.getId(), body.getAddress(), body.getPartitionCoefficient(), true);
return new Supply.Builder()
.withId(body.getId())
.withAddress(body.getAddress())
.withPartitionCoefficient(body.getPartitionCoefficient())
.withEnabled(true)
.build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public GetSupplyRepositoryImpl(SupplyRepository supplyRepository, SupplyEntityMa
this.paginationResultMapper = paginationResultMapper;
}

@Override
public long count() {
return supplyRepository.count();
}

@Override
public Optional<Supply> findById(SupplyId id) {
Optional<SupplyEntity> supplyEntity = supplyRepository.findById(id.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.lucoenergia.conluz.infrastructure.admin.user.UserEntity;
import org.lucoenergia.conluz.infrastructure.admin.user.UserRepository;
import org.lucoenergia.conluz.infrastructure.shared.format.UUIDValidator;
import org.lucoenergia.conluz.infrastructure.shared.uuid.UUIDValidator;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
Expand Down
Loading
Loading