Skip to content

Commit b27c4ea

Browse files
committed
goals
1 parent 74802cf commit b27c4ea

File tree

10 files changed

+270
-119
lines changed

10 files changed

+270
-119
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
meta {
2+
name: Delete goal for room
3+
type: http
4+
seq: 4
5+
}
6+
7+
delete {
8+
url: {{baseUrl}}/{{roomId}}/goal
9+
body: json
10+
auth: none
11+
}
12+
13+
body:json {
14+
{
15+
"user": "{{user}}"
16+
}
17+
}
18+
19+
vars:pre-request {
20+
roomId: test123
21+
user: TestUser
22+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
meta {
2+
name: Get goal for room
3+
type: http
4+
seq: 5
5+
}
6+
7+
get {
8+
url: {{baseUrl}}/{{roomId}}/goal
9+
body: none
10+
auth: none
11+
}
12+
13+
vars:pre-request {
14+
roomId: test123
15+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
meta {
2+
name: Set goal for room
3+
type: http
4+
seq: 3
5+
}
6+
7+
put {
8+
url: {{baseUrl}}/{{roomId}}/goal
9+
body: json
10+
auth: none
11+
}
12+
13+
body:json {
14+
{
15+
"goal": "{{goal}}",
16+
"user": "{{user}}"
17+
}
18+
}
19+
20+
vars:pre-request {
21+
roomId: test123
22+
user: TestUser
23+
goal: This is an awesome goal!
24+
}

src/main/java/sh/mob/timer/web/Room.java

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ final class Room {
2121
public static final TimerRequest NULL_TIMER_REQUEST =
2222
new TimerRequest(0L, null, null, null, null);
2323
private final String name;
24+
2425
private final List<TimerRequest> timerRequests = new CopyOnWriteArrayList<>();
25-
private final Sinks.Many<TimerRequest> sink =
26+
private final Sinks.Many<TimerRequest> timerRequestSink =
2627
Sinks.many().replay().latestOrDefault(NULL_TIMER_REQUEST);
27-
private Goal currentGoal = Goal.NO_GOAL;
2828

29+
private Goal currentGoal = Goal.NO_GOAL;
2930
private final Sinks.Many<Goal> goalRequestSink =
3031
Sinks.many().replay().latestOrDefault(Goal.NO_GOAL);
3132

@@ -37,19 +38,19 @@ public void addTimer(Long timer, String user, Instant requested) {
3738
var nextUser = findNextUser(user);
3839
var timerRequest = new TimerRequest(timer, requested, user, nextUser, TimerType.TIMER);
3940
timerRequests.add(timerRequest);
40-
sink.tryEmitNext(timerRequest);
41+
timerRequestSink.tryEmitNext(timerRequest);
4142
}
4243

4344
public void setGoal(String text, String user, Instant requested) {
44-
var newGoal = new Goal(text, requested, user);
45+
var newGoal = new Goal(text, user, requested);
4546
currentGoal = newGoal ;
4647
goalRequestSink.tryEmitNext(newGoal);
4748
}
4849

49-
public void deleteGoal(String user) {
50-
if(currentGoal != Goal.NO_GOAL){
51-
currentGoal = Goal.NO_GOAL;
52-
goalRequestSink.tryEmitNext(Goal.NO_GOAL);
50+
public void deleteGoal(String user, Instant requested) {
51+
if(currentGoal.goal() != null){
52+
currentGoal = Goal.deleted(user, requested);
53+
goalRequestSink.tryEmitNext(currentGoal);
5354
log.info(
5455
"Delete current goal by user {} for room {}",
5556
user,
@@ -93,11 +94,11 @@ public void addBreaktimer(Long breaktimer, String user) {
9394
lastTimerRequest().map(TimerRequest::getNextUser).orElse(null),
9495
TimerType.BREAKTIMER);
9596
timerRequests.add(timerRequest);
96-
sink.tryEmitNext(timerRequest);
97+
timerRequestSink.tryEmitNext(timerRequest);
9798
}
9899

99100
public Sinks.Many<TimerRequest> timerRequestSink() {
100-
return sink;
101+
return timerRequestSink;
101102
}
102103

103104
public Sinks.Many<Goal> goalRequestSink() {
@@ -116,7 +117,7 @@ public void removeOldTimerRequests() {
116117
this.timerRequests.removeIf(
117118
timerRequest -> now.minus(24, HOURS).isAfter(timerRequest.getRequested()));
118119
if (timerRequests.isEmpty()) {
119-
sink.tryEmitNext(NULL_TIMER_REQUEST);
120+
timerRequestSink.tryEmitNext(NULL_TIMER_REQUEST);
120121
log.info("Emptied room {}", name);
121122
}
122123
}
@@ -129,6 +130,10 @@ public Goal currentGoal() {
129130
return currentGoal;
130131
}
131132

133+
public boolean hasGoal() {
134+
return currentGoal.goal != null;
135+
}
136+
132137
public List<TimerRequest> historyWithoutLatest() {
133138
if (timerRequests.isEmpty()) {
134139
return List.of();
@@ -148,8 +153,12 @@ private static boolean isTimerActive(TimerRequest timerRequest, Instant now) {
148153
&& timerRequest.getRequested().plus(timerRequest.getTimer(), MINUTES).isAfter(now);
149154
}
150155

151-
public record Goal(String goal, Instant requested, String user){
156+
public record Goal(String goal, String user, Instant requested){
152157
public static final Goal NO_GOAL = new Goal(null, null, null);
158+
159+
public static Goal deleted(String user, Instant requested){
160+
return new Goal(null, user, requested);
161+
}
153162
}
154163

155164
public static final class TimerRequest {

src/main/java/sh/mob/timer/web/RoomApiController.java

Lines changed: 10 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
@RequestMapping()
2020
public class RoomApiController {
2121

22-
private static final String SMOKETEST_ROOM_NAME = "testroom-310a9c47-515c-4ad7-a229-ae8efbab7387";
2322
private static final Logger log = LoggerFactory.getLogger(RoomApiController.class);
2423
private final RoomRepository roomRepository;
2524
private final Clock clock;
@@ -84,7 +83,7 @@ public void publishEvent(@PathVariable String roomId, @RequestBody PutTimerReque
8483
timerRequest.timer,
8584
timerRequest.user,
8685
room.name());
87-
incrementTimerStatsExceptForTestRoom(room, timer);
86+
stats.incrementTimer(room.name(), timer);
8887
} else if (timerRequest.breaktimer() != null) {
8988
long breaktimer = truncateTooLongTimers(timerRequest.breaktimer());
9089
room.addBreaktimer(breaktimer, timerRequest.user());
@@ -93,7 +92,7 @@ public void publishEvent(@PathVariable String roomId, @RequestBody PutTimerReque
9392
timerRequest.breaktimer(),
9493
timerRequest.user,
9594
room.name());
96-
incrementBreakTimerStatsExceptForTestRoom(room, breaktimer);
95+
stats.incrementBreaktimer(room.name(), breaktimer);
9796
} else {
9897
log.warn("Could not understand PUT request for room {}", roomId);
9998
}
@@ -112,35 +111,27 @@ public void putGoal(@PathVariable String roomId, @RequestBody PutGoalRequest goa
112111
goalRequest.goal(),
113112
goalRequest.user(),
114113
room.name());
114+
stats.incrementGoalCount(room.name());
115115
} else {
116116
log.warn("Could not understand PUT goal request for room {}", roomId);
117117
}
118118
}
119119

120120
@DeleteMapping("/{roomId:[A-Za-z0-9-_]+}/goal")
121121
@ResponseStatus(HttpStatus.ACCEPTED)
122-
public void putGoal(@PathVariable String roomId, @RequestBody DeleteGoalRequest deleteGoalRequest) {
122+
public void deleteGoal(@PathVariable String roomId, @RequestBody DeleteGoalRequest deleteGoalRequest) {
123123
var room = roomRepository.get(roomId);
124-
room.deleteGoal(deleteGoalRequest.user());
124+
room.deleteGoal(deleteGoalRequest.user(), Instant.now(clock));
125125
}
126126

127127
@GetMapping("/{roomId:[A-Za-z0-9-_]+}/goal")
128-
@ResponseStatus(HttpStatus.ACCEPTED)
128+
@ResponseStatus(HttpStatus.NO_CONTENT)
129129
public ResponseEntity<GoalResponse> getGoal(@PathVariable String roomId) {
130130
var room = roomRepository.get(roomId);
131-
return ResponseEntity.ofNullable(GoalResponse.of(room.currentGoal()));
132-
}
133-
134-
private void incrementBreakTimerStatsExceptForTestRoom(Room room, long breaktimer) {
135-
if (!Objects.equals(room.name(), SMOKETEST_ROOM_NAME)) {
136-
stats.incrementBreaktimer(breaktimer);
137-
}
138-
}
139-
140-
private void incrementTimerStatsExceptForTestRoom(Room room, long timer) {
141-
if (!Objects.equals(room.name(), SMOKETEST_ROOM_NAME)) {
142-
stats.incrementTimer(timer);
131+
if (!room.hasGoal()){
132+
return ResponseEntity.noContent().build();
143133
}
134+
return ResponseEntity.ofNullable(GoalResponse.of(room.currentGoal()));
144135
}
145136

146137
private static long truncateTooLongTimers(Long timer) {
@@ -159,69 +150,5 @@ public static GoalResponse of(Room.Goal goal){
159150

160151
public record PutGoalRequest(String goal, String user){}
161152
public record DeleteGoalRequest(String user){}
162-
static final class PutTimerRequest {
163-
164-
private final Long timer;
165-
private final Long breaktimer;
166-
private final String user;
167-
168-
PutTimerRequest(Long timer, Long breaktimer, String user) {
169-
this.timer = timer;
170-
this.user = user;
171-
this.breaktimer = breaktimer;
172-
}
173-
174-
public Long timer() {
175-
return timer;
176-
}
177-
178-
public Long breaktimer() {
179-
return breaktimer;
180-
}
181-
182-
public String user() {
183-
return user;
184-
}
185-
186-
public Long getTimer() {
187-
return timer;
188-
}
189-
190-
public Long getBreaktimer() {
191-
return breaktimer;
192-
}
193-
194-
public String getUser() {
195-
return user;
196-
}
197-
198-
@Override
199-
public boolean equals(Object obj) {
200-
if (obj == this) return true;
201-
if (obj == null || obj.getClass() != this.getClass()) return false;
202-
var that = (PutTimerRequest) obj;
203-
return Objects.equals(this.timer, that.timer)
204-
&& Objects.equals(this.breaktimer, that.breaktimer)
205-
&& Objects.equals(this.user, that.user);
206-
}
207-
208-
@Override
209-
public int hashCode() {
210-
return Objects.hash(timer, breaktimer, user);
211-
}
212-
213-
@Override
214-
public String toString() {
215-
return "PutTimerRequest["
216-
+ "timer="
217-
+ timer
218-
+ ", "
219-
+ "breaktimer="
220-
+ breaktimer
221-
+ ", "
222-
+ "user="
223-
+ user
224-
+ ']';
225-
}
226-
}
153+
public record PutTimerRequest(Long timer, Long breaktimer, String user){}
227154
}

src/main/java/sh/mob/timer/web/RoomRepository.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ Room get(String room) {
3131
});
3232
}
3333

34+
void deleteAll(){
35+
repository.clear();
36+
}
37+
3438
@Scheduled(fixedRateString = "PT1M")
3539
void cleanUpUnusedRooms() {
3640
repository.forEach((key, room) -> room.removeOldTimerRequests());

src/main/java/sh/mob/timer/web/Stats.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
package sh.mob.timer.web;
22

33
import java.time.Instant;
4+
import java.util.Objects;
45
import java.util.SortedMap;
56
import java.util.TreeMap;
67
import java.util.concurrent.ConcurrentHashMap;
8+
import java.util.concurrent.atomic.AtomicLong;
9+
710
import org.springframework.stereotype.Component;
811

912
@Component
1013
public class Stats {
1114

15+
private static final String SMOKETEST_ROOM_NAME = "testroom-310a9c47-515c-4ad7-a229-ae8efbab7387";
1216
private final Instant statisticsSince = Instant.now();
1317
private ConcurrentHashMap<Long, Long> timerCounts = new ConcurrentHashMap<Long, Long>();
1418
private ConcurrentHashMap<Long, Long> breaktimerCounts = new ConcurrentHashMap<Long, Long>();
19+
private AtomicLong goalCount = new AtomicLong(0);
1520

16-
public void incrementTimer(long timer) {
21+
public void incrementTimer(String roomname, long timer) {
22+
if (Objects.equals(roomname, SMOKETEST_ROOM_NAME)) {
23+
return;
24+
}
1725
Long count = timerCounts.get(timer);
1826
if (count == null) {
1927
count = 0L;
@@ -22,7 +30,10 @@ public void incrementTimer(long timer) {
2230
timerCounts.put(timer, count);
2331
}
2432

25-
public void incrementBreaktimer(long breakTimer) {
33+
public void incrementBreaktimer(String roomname, long breakTimer) {
34+
if (Objects.equals(roomname, SMOKETEST_ROOM_NAME)) {
35+
return;
36+
}
2637
Long count = breaktimerCounts.get(breakTimer);
2738
if (count == null) {
2839
count = 0L;
@@ -31,6 +42,17 @@ public void incrementBreaktimer(long breakTimer) {
3142
breaktimerCounts.put(breakTimer, count);
3243
}
3344

45+
public void incrementGoalCount(String roomname) {
46+
if(Objects.equals(roomname, SMOKETEST_ROOM_NAME)){
47+
return;
48+
}
49+
goalCount.incrementAndGet();
50+
}
51+
52+
public long getGoalCount() {
53+
return goalCount.get();
54+
}
55+
3456
public SortedMap<Long, Long> getTimer() {
3557
return new TreeMap<>(timerCounts);
3658
}

0 commit comments

Comments
 (0)