Commit 9539f38b authored by Filip Bugoš's avatar Filip Bugoš
Browse files

fix: statistics 16/04/23

parent 94017967
Loading
Loading
Loading
Loading
+17 −18
Original line number Diff line number Diff line
@@ -23,8 +23,9 @@ import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
@Component

@RequiredArgsConstructor
@Component
public class DataInitializer implements ApplicationRunner {
  private final UserService userService;
  private final DeviceService deviceService;
@@ -80,8 +81,18 @@ public class DataInitializer implements ApplicationRunner {
                    .build();
    userService.create(user);

    House house = House.builder()
            .address("lol")
            .city("lol")
            .state("lol")
            .zipcode("lol")
            .build();
    house.setId("b256ac78-dc47-11ed-afa1-0242ac120002");
    houseService.create(house);

    HouseRole role = HouseRole.builder()
                    .houseRole(HouseRoleEnum.Owner)
                    .house(house)
                    .build();
    role.setUser(user);
    roleService.create(role);
@@ -89,24 +100,12 @@ public class DataInitializer implements ApplicationRunner {
    Device device = Device.builder().name("device03").build();
    deviceService.create(device);

    SmartMeter smartMeter = SmartMeter.builder().name("smartMeter01").device(device).build();
    smartMeterService.create(smartMeter);

    List<SmartMeter> smartMeterList = new ArrayList<SmartMeter>();
    smartMeterList.add(smartMeter);
    ArrayList<HouseRole> owners = new ArrayList<>();
    owners.add(role);
    House house = House.builder()
            .smartMeterList(smartMeterList)
            .address("lol")
            .city("lol")
            .state("lol")
            .zipcode("lol")
            .ownerList(owners)
    SmartMeter smartMeter = SmartMeter
            .builder()
            .device(device)
            .house(house)
            .build();
    houseService.create(house);


    smartMeterService.create(smartMeter);

    Metrics metrics = Metrics.builder()
            .consumptionKWH(10)
+1 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ public interface UserRepository extends JpaRepository<User, String> {

	@Query("Select SUM(m.consumptionKWH) " +
			"From User u " +
			"INNER JOIN Role r " +
			"INNER JOIN HouseRole hr " +
			"INNER JOIN House h " +
			"INNER JOIN SmartMeter s " +
+6 −13
Original line number Diff line number Diff line
package cz.muni.fi.pa165.statistics.statistics;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.HttpClientBuilder;
@@ -12,12 +10,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@@ -35,7 +28,7 @@ public class StatisticService {
    }

    @Transactional
    public UserStatistic CreateStatistic(UserStatistic userStatistic) throws IOException {
    public UserStatistic createStatistic(UserStatistic userStatistic) throws IOException {
        UserStatistic statistic = repository.findUserHouseDateStatistics(userStatistic.getUserId(), userStatistic.getHouseId(), userStatistic.getMonthNum(), userStatistic.getYearNum());

        if (statistic == null){
@@ -45,7 +38,7 @@ public class StatisticService {
                    userStatistic.getHouseId(),
                    userStatistic.getMonthNum(),
                    userStatistic.getYearNum()));
            userStatistic.setMonthlyConsumption(GenerateStatistic(userStatistic));
            userStatistic.setMonthlyConsumption(generateStatistic(userStatistic));
            repository.save(userStatistic);
            return userStatistic;
        }
@@ -65,7 +58,7 @@ public class StatisticService {
        return repository.findAllByUserAndHouse(userId, houseId);
    }

    private double GenerateStatistic(UserStatistic userStatistic) throws IOException {
    public double generateStatistic(UserStatistic userStatistic) throws IOException {

        LocalDateTime startDateTime = GetFirstDayOfMonth(userStatistic.getYearNum(), userStatistic.getMonthNum());
        LocalDateTime lastDateTime = GetLastDayOfMonth(userStatistic.getYearNum(), userStatistic.getMonthNum());
@@ -102,12 +95,12 @@ public class StatisticService {
    private LocalDateTime GetLastDayOfMonth(int year, int month){
        LocalDateTime date = LocalDateTime.of(year,
                month,1,0,0,0,0);
        date.plusMonths(1);
        date.minusDays(1);
        LocalDateTime date_pom = date.plusMonths(1);
        LocalDateTime lastDateOfMonth = date_pom.minusDays(1);

        return LocalDateTime.of(year,
                month,
                date.getDayOfMonth(),
                lastDateOfMonth.getDayOfMonth(),
                23,59,59,999999);
    }

+1 −3
Original line number Diff line number Diff line
package cz.muni.fi.pa165.statistics.statistics;

import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;
import java.util.List;
@@ -24,7 +22,7 @@ public class StatisticsFacade {

    @Transactional
    public UserStatisticsDto CreateStatistics(StatisticCreateDto entity) throws IOException {
        return mapper.toDto(statisticService.CreateStatistic(mapper.fromDto(entity)));
        return mapper.toDto(statisticService.createStatistic(mapper.fromDto(entity)));
    }

    @Transactional