Skip to content

Commit 6cda771

Browse files
committed
stage - add statistics service module
1 parent 8f59bf6 commit 6cda771

17 files changed

Lines changed: 466 additions & 9 deletions

File tree

docker-compose.yml

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
11
services:
2-
stats-server:
3-
ports:
4-
- "9090:9090"
5-
62
stats-db:
73
image: postgres:16.1
8-
9-
ewm-service:
4+
container_name: stats-db
105
ports:
11-
- "8080:8080"
6+
- "5432:5432"
7+
environment:
8+
POSTGRES_DB: statsdb
9+
POSTGRES_USER: postgres
10+
POSTGRES_PASSWORD: password
11+
healthcheck:
12+
test: ["CMD-SHELL", "pg_isready -U postgres"]
13+
interval: 10s
14+
timeout: 5s
15+
retries: 5
1216

13-
ewm-db:
14-
image: postgres:16.1
17+
stats-server:
18+
build: ./statistics/server
19+
container_name: stats-server
20+
ports:
21+
- "9090:9090"
22+
depends_on:
23+
stats-db:
24+
condition: service_healthy
25+
environment:
26+
STATS_DB_URL: jdbc:postgresql://stats-db:5432/statsdb
27+
STATS_DB_USER: postgres
28+
STATS_DB_PASSWORD: password

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2323
</properties>
2424

25+
<modules>
26+
<module>statistics/dto</module>
27+
<module>statistics/client</module>
28+
<module>statistics/server</module>
29+
</modules>
30+
2531
<build>
2632
<pluginManagement>
2733
<plugins>

statistics/client/pom.xml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>ru.practicum</groupId>
9+
<artifactId>explore-with-me</artifactId>
10+
<version>0.0.1-SNAPSHOT</version>
11+
<relativePath>../../pom.xml</relativePath>
12+
</parent>
13+
14+
<artifactId>statistics-client</artifactId>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>ru.practicum</groupId>
19+
<artifactId>statistics-dto</artifactId>
20+
<version>${project.version}</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-web</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.apache.httpcomponents.client5</groupId>
28+
<artifactId>httpclient5</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.projectlombok</groupId>
32+
<artifactId>lombok</artifactId>
33+
<optional>true</optional>
34+
</dependency>
35+
</dependencies>
36+
</project>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package ru.practicum.statistics.client;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.boot.web.client.RestTemplateBuilder;
5+
import org.springframework.http.*;
6+
import org.springframework.stereotype.Service;
7+
import org.springframework.web.client.RestTemplate;
8+
import org.springframework.web.util.UriComponentsBuilder;
9+
import ru.practicum.statistics.dto.EndpointHit;
10+
import ru.practicum.statistics.dto.ViewStats;
11+
12+
import java.net.URLEncoder;
13+
import java.nio.charset.StandardCharsets;
14+
import java.time.LocalDateTime;
15+
import java.time.format.DateTimeFormatter;
16+
import java.util.Arrays;
17+
import java.util.List;
18+
19+
@Service
20+
public class StatsClient {
21+
private final RestTemplate rest;
22+
private final String serverUrl;
23+
24+
public StatsClient(@Value("${stats-server.url:http://localhost:9090}") String serverUrl,
25+
RestTemplateBuilder builder) {
26+
this.serverUrl = serverUrl;
27+
this.rest = builder.build();
28+
}
29+
30+
public void sendHit(EndpointHit hit) {
31+
HttpEntity<EndpointHit> requestEntity = new HttpEntity<>(hit);
32+
rest.exchange(serverUrl + "/hit", HttpMethod.POST, requestEntity, Void.class);
33+
}
34+
35+
public List<ViewStats> getStats(LocalDateTime start, LocalDateTime end, List<String> uris, boolean unique) {
36+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
37+
String startStr = URLEncoder.encode(start.format(formatter), StandardCharsets.UTF_8);
38+
String endStr = URLEncoder.encode(end.format(formatter), StandardCharsets.UTF_8);
39+
40+
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(serverUrl + "/stats")
41+
.queryParam("start", startStr)
42+
.queryParam("end", endStr)
43+
.queryParam("unique", unique);
44+
if (uris != null && !uris.isEmpty()) {
45+
for (String uri : uris) {
46+
builder.queryParam("uris", uri);
47+
}
48+
}
49+
50+
ResponseEntity<ViewStats[]> response = rest.getForEntity(builder.build().toUriString(), ViewStats[].class);
51+
return Arrays.asList(response.getBody());
52+
}
53+
}

statistics/dto/pom.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>ru.practicum</groupId>
9+
<artifactId>explore-with-me</artifactId>
10+
<version>0.0.1-SNAPSHOT</version>
11+
<relativePath>../../pom.xml</relativePath>
12+
</parent>
13+
14+
<artifactId>statistics-dto</artifactId>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.projectlombok</groupId>
19+
<artifactId>lombok</artifactId>
20+
<optional>true</optional>
21+
</dependency>
22+
<dependency>
23+
<groupId>com.fasterxml.jackson.core</groupId>
24+
<artifactId>jackson-annotations</artifactId>
25+
</dependency>
26+
</dependencies>
27+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package ru.practicum.statistics.dto;
2+
3+
import com.fasterxml.jackson.annotation.JsonFormat;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
import java.time.LocalDateTime;
9+
10+
@Data
11+
@NoArgsConstructor
12+
@AllArgsConstructor
13+
public class EndpointHit {
14+
private Long id;
15+
private String app;
16+
private String uri;
17+
private String ip;
18+
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
19+
private LocalDateTime timestamp;
20+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package ru.practicum.statistics.dto;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
7+
@Data
8+
@NoArgsConstructor
9+
@AllArgsConstructor
10+
public class ViewStats {
11+
private String app;
12+
private String uri;
13+
private Long hits;
14+
}

statistics/server/Dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM eclipse-temurin:21-jre-jammy
2+
VOLUME /tmp
3+
ARG JAR_FILE=target/*.jar
4+
COPY ${JAR_FILE} app.jar
5+
ENTRYPOINT ["sh", "-c", "java ${JAVA_OPTS} -jar /app.jar"]

statistics/server/pom.xml

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>ru.practicum</groupId>
9+
<artifactId>explore-with-me</artifactId>
10+
<version>0.0.1-SNAPSHOT</version>
11+
<relativePath>../../pom.xml</relativePath>
12+
</parent>
13+
14+
<artifactId>statistics-server</artifactId>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>ru.practicum</groupId>
19+
<artifactId>statistics-dto</artifactId>
20+
<version>${project.version}</version>
21+
</dependency>
22+
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-web</artifactId>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-data-jpa</artifactId>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-actuator</artifactId>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.postgresql</groupId>
37+
<artifactId>postgresql</artifactId>
38+
<scope>runtime</scope>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.projectlombok</groupId>
42+
<artifactId>lombok</artifactId>
43+
<optional>true</optional>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-starter-test</artifactId>
49+
<scope>test</scope>
50+
</dependency>
51+
<dependency>
52+
<groupId>com.h2database</groupId>
53+
<artifactId>h2</artifactId>
54+
<scope>test</scope>
55+
</dependency>
56+
<dependency>
57+
<groupId>org.springframework.boot</groupId>
58+
<artifactId>spring-boot-starter-validation</artifactId>
59+
</dependency>
60+
</dependencies>
61+
62+
<build>
63+
<plugins>
64+
<plugin>
65+
<groupId>org.springframework.boot</groupId>
66+
<artifactId>spring-boot-maven-plugin</artifactId>
67+
</plugin>
68+
</plugins>
69+
</build>
70+
</project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ru.practicum.statistics;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class StatsServerApp {
8+
public static void main(String[] args) {
9+
SpringApplication.run(StatsServerApp.class, args);
10+
}
11+
}

0 commit comments

Comments
 (0)