-
Notifications
You must be signed in to change notification settings - Fork 0
12_Обслуживание #10
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
Open
Kama-Pushka
wants to merge
1
commit into
main
Choose a base branch
from
12task
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
12_Обслуживание #10
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/main/java/org/javaspringcourse/actuator/TimeActuator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package org.javaspringcourse.actuator; | ||
|
|
||
| import lombok.extern.log4j.Log4j2; | ||
| import org.springframework.boot.actuate.endpoint.annotation.Endpoint; | ||
| import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| @Log4j2 | ||
| @Component | ||
| @Endpoint(id = "time") | ||
| public class TimeActuator { | ||
|
|
||
| @ReadOperation | ||
| public String time() { | ||
| log.info("Current time: {}", LocalDateTime.now()); | ||
| return LocalDateTime.now().toString(); | ||
| } | ||
| } |
24 changes: 24 additions & 0 deletions
24
src/main/java/org/javaspringcourse/controller/ChocolateShopController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package org.javaspringcourse.controller; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import org.javaspringcourse.dto.OrderIn; | ||
| import org.javaspringcourse.dto.OrderOut; | ||
| import org.javaspringcourse.metric.counter.CounterMetric; | ||
| import org.javaspringcourse.metric.timer.TimerMetric; | ||
| import org.javaspringcourse.service.ChocolateShopService; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RestController | ||
| @RequiredArgsConstructor | ||
| public class ChocolateShopController { | ||
| private final ChocolateShopService chocolateShopService; | ||
|
|
||
| @PostMapping("/order") | ||
| @CounterMetric(name = "createOrder") | ||
| @TimerMetric(name = "createOrder") | ||
| public OrderOut createOrder(@RequestBody OrderIn order) { | ||
| return chocolateShopService.createOrder(order); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package org.javaspringcourse.dto; | ||
|
|
||
| import org.javaspringcourse.model.Chocolate; | ||
|
|
||
| public record OrderIn(Chocolate chocolate, int count) { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package org.javaspringcourse.dto; | ||
|
|
||
| import org.javaspringcourse.model.Chocolate; | ||
|
|
||
| public record OrderOut(int orderId, Chocolate chocolate, int count) { | ||
| } |
20 changes: 20 additions & 0 deletions
20
src/main/java/org/javaspringcourse/metric/counter/CounterAspect.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package org.javaspringcourse.metric.counter; | ||
|
|
||
| import io.micrometer.core.instrument.MeterRegistry; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.aspectj.lang.annotation.After; | ||
| import org.aspectj.lang.annotation.Aspect; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Aspect | ||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class CounterAspect { | ||
| private final MeterRegistry meterRegistry; | ||
|
|
||
| @After("@annotation(counter)") | ||
| public void counterMetric(CounterMetric counter) { | ||
| meterRegistry.counter("shop.api-request.counter.all").increment(); | ||
| meterRegistry.counter("shop.api-request.counter." + counter.name()).increment(); | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/main/java/org/javaspringcourse/metric/counter/CounterMetric.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package org.javaspringcourse.metric.counter; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Target(ElementType.METHOD) | ||
| public @interface CounterMetric { | ||
| String name() default ""; | ||
| } |
27 changes: 27 additions & 0 deletions
27
src/main/java/org/javaspringcourse/metric/timer/TimerAspect.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package org.javaspringcourse.metric.timer; | ||
|
|
||
| import io.micrometer.core.instrument.MeterRegistry; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.aspectj.lang.ProceedingJoinPoint; | ||
| import org.aspectj.lang.annotation.Around; | ||
| import org.aspectj.lang.annotation.Aspect; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Aspect | ||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class TimerAspect { | ||
| private final MeterRegistry meterRegistry; | ||
|
|
||
| @Around("@annotation(timer)") | ||
| public Object timer(ProceedingJoinPoint jp, TimerMetric timer) { | ||
| return meterRegistry.timer("shop.api-request.timer." + timer.name()).record(() -> | ||
| { | ||
| try { | ||
| return jp.proceed(); | ||
| } catch (Throwable e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
12 changes: 12 additions & 0 deletions
12
src/main/java/org/javaspringcourse/metric/timer/TimerMetric.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package org.javaspringcourse.metric.timer; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Target(ElementType.METHOD) | ||
| public @interface TimerMetric { | ||
| String name() default ""; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package org.javaspringcourse.model; | ||
|
|
||
| public enum Chocolate { | ||
| KitKat, | ||
| Snickers, | ||
| Twix | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package org.javaspringcourse.model; | ||
|
|
||
| import lombok.Builder; | ||
| import lombok.Data; | ||
|
|
||
| @Data | ||
| @Builder | ||
| public class OrderEntity { | ||
| private int orderId; | ||
| private Chocolate chocolateType; | ||
| private int count; | ||
| } |
35 changes: 35 additions & 0 deletions
35
src/main/java/org/javaspringcourse/service/ChocolateShopService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package org.javaspringcourse.service; | ||
|
|
||
| import io.micrometer.core.instrument.MeterRegistry; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.javaspringcourse.dto.OrderIn; | ||
| import org.javaspringcourse.dto.OrderOut; | ||
| import org.javaspringcourse.model.OrderEntity; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class ChocolateShopService { | ||
| private final List<OrderEntity> orders = new ArrayList<OrderEntity>(); | ||
| private final MeterRegistry meterRegistry; | ||
|
|
||
| public OrderOut createOrder(OrderIn orderIn) { | ||
| var order = OrderEntity.builder() | ||
| .orderId(orders.size()) | ||
| .chocolateType(orderIn.chocolate()) | ||
| .count(orderIn.count()) | ||
| .build(); | ||
| orders.add(order); | ||
|
|
||
| meterRegistry.counter("shop.order.counter." + orderIn.chocolate()).increment(orderIn.count()); // TODO стоит выделить в ивент | ||
|
|
||
| return toDto(order); | ||
| } | ||
|
|
||
| private OrderOut toDto(OrderEntity order) { | ||
| return new OrderOut(order.getOrderId(), order.getChocolateType(), order.getCount()); | ||
| } | ||
| } |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| management: | ||
| endpoints: | ||
| web: | ||
| exposure: | ||
| include: '*' |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: существует встроенная
@Timedаннотация, почитай про нее