Skip to content

Commit

Permalink
fixed minor issues
Browse files Browse the repository at this point in the history
  • Loading branch information
fedorovychh committed Feb 29, 2024
1 parent 02fd484 commit 196d30c
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

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

@SpringBootApplication
@EnableScheduling
@EnableCaching
public class SellerRetailReportsApplication {
public static void main(String[] args) {
SpringApplication.run(SellerRetailReportsApplication.class, args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,25 @@

@Component
public class SpringCacheCustomizer implements CacheManagerCustomizer<ConcurrentMapCacheManager> {
private static final String SALES_AND_TRAFFIC_BY_DATES_BETWEEN =
"salesAndTrafficByDatesBetween";
private static final String SALES_AND_TRAFFIC_BY_SPECIFIED_DATE =
"salesAndTrafficBySpecifiedDate";
private static final String SALES_AND_TRAFFIC_BY_SPECIFIED_ASINS =
"salesAndTrafficBySpecifiedAsins";
private static final String SALES_AND_TRAFFIC_BY_ASINS =
"salesAndTrafficByAsins";
private static final String SALES_AND_TRAFFIC_BY_DATES =
"salesAndTrafficByDates";

@Override
public void customize(ConcurrentMapCacheManager cacheManager) {
cacheManager.setCacheNames(List.of(
"salesAndTrafficByDatesBetween",
"salesAndTrafficBySpecifiedDate",
"salesAndTrafficBySpecifiedAsins",
"salesAndTrafficByAsins",
"salesAndTrafficByDates"
SALES_AND_TRAFFIC_BY_DATES_BETWEEN,
SALES_AND_TRAFFIC_BY_SPECIFIED_DATE,
SALES_AND_TRAFFIC_BY_SPECIFIED_ASINS,
SALES_AND_TRAFFIC_BY_ASINS,
SALES_AND_TRAFFIC_BY_DATES
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/report")
@Tag(name = "Report management",
description = "Endpoints for managing reports.")
description = "Endpoints for getting reports.")
public class ReportController {
private final ReportService reportService;

@GetMapping(value = "/all")
@GetMapping(value = "/reports")
@Operation(summary = "Get reports by all dates")
public List<ReportResponseDto> getAll() {
return reportService.findAll();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,13 @@

public class FileReaderService {
public static String readFile(String fileName) {

try {
BufferedReader bufferedReader = Files.newBufferedReader(Paths.get(fileName));

try {
String str = bufferedReader.lines()
.collect(Collectors.joining(System.lineSeparator()));
bufferedReader.close();
return str;
} catch (Throwable throwable) {
try {
bufferedReader.close();
} catch (Throwable throwable1) {
throwable.addSuppressed(throwable1);
}
throw throwable;
}
try (BufferedReader bufferedReader = Files.newBufferedReader(Paths.get(fileName))) {
String str = bufferedReader.lines()
.collect(Collectors.joining(System.lineSeparator()));
bufferedReader.close();
return str;
} catch (IOException e) {
throw new RuntimeException("Cannot read data from the file " + fileName, e);
}

}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,62 @@
package com.app.sellerretailreports.service.initializer;

public interface ReportInitializerService {
void initialize();
import com.app.sellerretailreports.entity.report.SalesAndTrafficReport;
import com.app.sellerretailreports.repository.report.ReportRepository;
import com.app.sellerretailreports.service.file.FileReaderService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import jakarta.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

void update();
@Service
@RequiredArgsConstructor
class ReportInitializerService {
private static final String INITIALIZE_JSON_PATH =
"src\\main\\resources\\db\\json\\test_report.json";
private static final String UPDATE_JSON_PATH =
"src\\main\\resources\\db\\json\\db_report.json";
private final ReportRepository repository;

@PostConstruct
public void initialize() {
Path path = Paths.get(UPDATE_JSON_PATH);
if (Files.exists(path)) {
update();
return;
}
SalesAndTrafficReport report = getReport(INITIALIZE_JSON_PATH);
SalesAndTrafficReport saved = repository.save(report);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
try {
objectMapper.writeValue(new File(UPDATE_JSON_PATH), saved);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Scheduled(fixedRate = 900_000) /* updates data in db every 15 minutes */
public void update() {
SalesAndTrafficReport report = getReport("src\\main\\resources\\db\\json\\db_report.json");
repository.save(report);
}

private SalesAndTrafficReport getReport(String fileName) {
String jsonString = FileReaderService.readFile(fileName);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
try {
return objectMapper.readValue(jsonString, SalesAndTrafficReport.class);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@
@Service
@RequiredArgsConstructor
public class SalesAndTrafficByDateServiceImpl implements SalesAndTrafficByDateService {
private static final String SALES_AND_TRAFFIC_BY_DATES_BETWEEN =
"salesAndTrafficByDatesBetween";
private static final String SALES_AND_TRAFFIC_BY_SPECIFIED_DATE =
"salesAndTrafficBySpecifiedDate";
private static final String SALES_AND_TRAFFIC_BY_SPECIFIED_ASINS =
"salesAndTrafficBySpecifiedAsins";
private static final String SALES_AND_TRAFFIC_BY_ASINS =
"salesAndTrafficByAsins";
private static final String SALES_AND_TRAFFIC_BY_DATES =
"salesAndTrafficByDates";
private static final int FIRST_ELEMENT_INDEX = 0;
private final ReportRepository repository;
private final SalesAndTrafficByDateMapper salesAndTrafficByDateMapper;
Expand Down Expand Up @@ -78,16 +88,16 @@ public List<SalesAndTrafficByDateDto> findAllByDates() {

@CacheEvict(
value = {
"salesAndTrafficByDatesBetween",
"salesAndTrafficBySpecifiedDate",
"salesAndTrafficBySpecifiedAsins",
"salesAndTrafficByAsins",
"salesAndTrafficByDates"
SALES_AND_TRAFFIC_BY_DATES_BETWEEN,
SALES_AND_TRAFFIC_BY_SPECIFIED_DATE,
SALES_AND_TRAFFIC_BY_SPECIFIED_ASINS,
SALES_AND_TRAFFIC_BY_ASINS,
SALES_AND_TRAFFIC_BY_DATES
},
allEntries = true
)
@Scheduled(fixedRateString = "${caching.spring.TTL}")
public void emptyHotelsCache() {
public void emptyCache() {
}

private SalesAndTrafficReport findReport() {
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ caching.spring.TTL=420000

jwt.expiration=${JWT_EXPIRATION}
jwt.secret=${JWT_SECRET}

logging.level.org.springframework.data.mongodb.core.MongoTemplate=DEBUG

0 comments on commit 196d30c

Please sign in to comment.