Skip to content

Commit

Permalink
stat_svc -> Реализация сервиса статистики
Browse files Browse the repository at this point in the history
  • Loading branch information
PavelSpectr committed Oct 16, 2023
1 parent 1b50d1b commit 5711829
Show file tree
Hide file tree
Showing 26 changed files with 702 additions and 1 deletion.
32 changes: 32 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,47 @@
version: '3.1'
services:
stats-server:
build: statistics-service/stats-server
image: statistics-server
container_name: stats-server-container
ports:
- "9090:9090"
depends_on:
- stats-db
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://stats-db:5432/ewm-stats
- SPRING_DATASOURCE_USERNAME=root
- SPRING_DATASOURCE_PASSWORD=root

stats-db:
image: postgres:14-alpine
container_name: stats-db-container
ports:
- "6541:5432"
environment:
- POSTGRES_DB=ewm-stats
- POSTGRES_USER=root
- POSTGRES_PASSWORD=root

ewm-service:
build: main-service
image: ewm-main-service
container_name: ewm-main-service-container
ports:
- "8080:8080"
depends_on:
- ewm-db
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://ewm-db:5432/ewm-db
- SPRING_DATASOURCE_USERNAME=root
- SPRING_DATASOURCE_PASSWORD=root

ewm-db:
image: postgres:14-alpine
container_name: ewm-db-container
ports:
- "6542:5432"
environment:
- POSTGRES_DB=ewm-db
- POSTGRES_USER=root
- POSTGRES_PASSWORD=root
3 changes: 3 additions & 0 deletions main-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM amazoncorretto:11
COPY target/*.jar main-service.jar
ENTRYPOINT ["java", "-jar", "/main-service.jar"]
30 changes: 30 additions & 0 deletions main-service/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>ru.practicum</groupId>
<artifactId>explore-with-me</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<artifactId>main-service</artifactId>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>

</project>
11 changes: 11 additions & 0 deletions main-service/src/main/java/ru/practicum/MainService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ru.practicum;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MainService {
public static void main(String[] args) {
SpringApplication.run(MainService.class, args);
}
}
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
Expand All @@ -11,6 +11,10 @@
</parent>

<name>Explore With Me</name>
<modules>
<module>main-service</module>
<module>stats-service</module>
</modules>

<groupId>ru.practicum</groupId>
<artifactId>explore-with-me</artifactId>
Expand All @@ -19,6 +23,8 @@

<properties>
<java.version>11</java.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

Expand Down
38 changes: 38 additions & 0 deletions stats-service/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>explore-with-me</artifactId>
<groupId>ru.practicum</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<artifactId>stats-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
<module>stats-dto</module>
<module>stats-server</module>
<module>stats-client</module>
</modules>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
42 changes: 42 additions & 0 deletions stats-service/stats-client/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>stats-service</artifactId>
<groupId>ru.practicum</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>stats-client</artifactId>
<version>0.0.1-SNAPSHOT</version>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>ru.practicum</groupId>
<artifactId>stats-dto</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package ru.practicum;

import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;

import java.util.List;

@Service
public class StatsClient {
private final WebClient client;

public StatsClient(String serverUrl) {
client = WebClient.create(serverUrl);
}

public ResponseEntity<List<ViewStatsDto>> getStats(String start, String end, String[] uris, Boolean unique) {
return client.get()
.uri(uriBuilder -> uriBuilder
.path("/stats")
.queryParam("start", start)
.queryParam("end", end)
.queryParam("uris", uris)
.queryParam("unique", unique)
.build())
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.toEntityList(ViewStatsDto.class)
.block();
}

public void addHit(EndpointHitDto endpointHitDto) {
client.post()
.uri("/hit")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.bodyValue(endpointHitDto)
.retrieve()
.toBodilessEntity()
.block();
}


}
45 changes: 45 additions & 0 deletions stats-service/stats-dto/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>stats-service</artifactId>
<groupId>ru.practicum</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>stats-dto</artifactId>
<version>0.0.1-SNAPSHOT</version>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ru.practicum;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class EndpointHitDto {
@NotBlank(message = "IP-адрес пользователя не может быть пустым")
private String ip;
@NotBlank(message = "ID сервиса не может быть пустым")
private String app;
@NotBlank(message = "URI не может быть пустым")
private String uri;
@NotNull
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime timestamp;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ru.practicum;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ViewStatsDto {
private String app;
private String uri;
private Long hits;
}
3 changes: 3 additions & 0 deletions stats-service/stats-server/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM amazoncorretto:11-alpine-jdk
COPY target/*.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
Loading

0 comments on commit 5711829

Please sign in to comment.