Skip to content

Commit 75330ba

Browse files
committed
✨ feat: Improvements from review
1 parent 9a09a94 commit 75330ba

File tree

12 files changed

+80
-38
lines changed

12 files changed

+80
-38
lines changed

chart/values.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,6 @@ microblogService:
279279
OPENTRACING_JAEGER_ENABLED: false
280280
REDIS_SERVICE_ADDRESS: unguard-redis
281281
USER_AUTH_SERVICE_ADDRESS: unguard-user-auth-service
282-
RAG_SERVICE_ENABLED: "{{ .Values.ragService.enabled }}"
283282
RAG_SERVICE_ADDRESS: unguard-rag-service
284283
RAG_SERVICE_PORT: 8000
285284

src/frontend-nextjs/app/api/post/[postId]/spam-prediction-user-rating/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ export async function GET(req: Request, { params }: { params: Promise<PostParams
1414
const { postId } = await params;
1515
const res = await fetchSpamPredictionUserRating(postId);
1616

17-
return NextResponse.json(res.data);
17+
return NextResponse.json(res.data, { status: res.status });
1818
}

src/frontend-nextjs/components/Timeline/PostSpamPrediction.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ export function PostSpamPrediction(props: Readonly<PostSpamPredictionProps>) {
2929
{props.isSpamPredictedLabel ? 'Potential Spam Detected' : 'No Spam Detected'}
3030
</div>
3131
{isLoggedIn && (
32-
<ErrorBoundary fallbackRender={(props) => <ErrorCard message={props.error.message} />}>
32+
<ErrorBoundary
33+
fallbackRender={(errorProps) => <ErrorCard message={errorProps.error.message} />}
34+
>
3335
<SpamPredictionUserRating
3436
isSpamPredictedLabel={props.isSpamPredictedLabel}
3537
postId={props.postId}

src/frontend-nextjs/hooks/queries/useSpamPredictionUserRating.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { BASE_PATH } from '@/constants';
77

88
type SpamPredictionUserRating = {
99
spamPredictionUserUpvotes: number;
10-
spamPredictionUserDownvotes: boolean;
10+
spamPredictionUserDownvotes: number;
1111
isUpvotedByUser?: boolean;
1212
isDownvotedByUser?: boolean;
1313
};

src/frontend-nextjs/services/api/SpamPredictionVotesService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export async function fetchSpamPredictionUserRating(postId: string): Promise<any
2626

2727
export async function addSpamPredictionUserRatingUpvote(postId: string): Promise<any> {
2828
return await getMicroblogApi()
29-
.post(`/spam-prediction-user-rating/${postId}/upvote/`, {}, await withJwtCookie())
29+
.post(`/spam-prediction-user-rating/${postId}/upvote`, {}, await withJwtCookie())
3030
.then((response) => {
3131
return response;
3232
})
@@ -37,7 +37,7 @@ export async function addSpamPredictionUserRatingUpvote(postId: string): Promise
3737

3838
export async function addSpamPredictionUserRatingDownvote(postId: string): Promise<any> {
3939
return await getMicroblogApi()
40-
.post(`/spam-prediction-user-rating/${postId}/downvote/`, {}, await withJwtCookie())
40+
.post(`/spam-prediction-user-rating/${postId}/downvote`, {}, await withJwtCookie())
4141
.then((response) => {
4242
return response;
4343
})

src/microblog-service/README.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,15 @@ Running the application should start a webserver accessible on [localhost:8080](
5151
5252
To get more information about the JAEGER config options, see https://www.jaegertracing.io/docs/1.19/client-features/
5353
54-
| Name | Example Value | Description |
55-
|---------------------------|---------------------------|--------------------------------------------------------------|
56-
| SERVER_PORT | 8080 | The port that the server will run on |
57-
| REDIS_SERVICE_ADDRESS | localhost | Change to hostname/IP of your Redis instance |
58-
| JAEGER_AGENT_HOST | localhost | Change to hostname/IP of your Jaeger agent |
59-
| JAEGER_SERVICE_NAME | microblog-service | Name that will be used for the service in the Jaeger traces |
60-
| JAEGER_SAMPLER_TYPE | const | (optional) Set to const to get all traces |
61-
| JAEGER_SAMPLER_PARAM | 1 | (optional) Set to 1 while sampler is const to get all traces |
62-
| USER_AUTH_SERVICE_ADDRESS | unguard-user-auth-service | Change to hostname/IP of user-auth-service instance |
63-
| RAG_SERVICE_ADDRESS | unguard-rag-service | Change to hostname/IP of rag-service instance |
64-
| RAG_SERVICE_PORT | 8000 | Change to port number of rag-service instance |
54+
| Name | Example Value | Description |
55+
|---------------------------|---------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
56+
| SERVER_PORT | 8080 | The port that the server will run on |
57+
| REDIS_SERVICE_ADDRESS | localhost | Change to hostname/IP of your Redis instance |
58+
| JAEGER_AGENT_HOST | localhost | Change to hostname/IP of your Jaeger agent |
59+
| JAEGER_SERVICE_NAME | microblog-service | Name that will be used for the service in the Jaeger traces |
60+
| JAEGER_SAMPLER_TYPE | const | (optional) Set to const to get all traces |
61+
| JAEGER_SAMPLER_PARAM | 1 | (optional) Set to 1 while sampler is const to get all traces |
62+
| USER_AUTH_SERVICE_ADDRESS | unguard-user-auth-service | Change to hostname/IP of user-auth-service instance |
63+
| RAG_SERVICE_ADDRESS | unguard-rag-service | Change to hostname/IP of rag-service instance |
64+
| RAG_SERVICE_PORT | 8000 | Change to port number of rag-service instance |
65+
| RAG_SERVICE_ENABLED | true | Configure whether the RAG service for Spam Detection should be deployed, including other necessary services such as the Ollama service and the Feedback Ingestion Service |

src/microblog-service/src/main/java/org/dynatrace/microblog/MicroblogController.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
import org.springframework.web.bind.annotation.RestController;
4444
import org.springframework.web.server.ResponseStatusException;
4545

46+
import javax.annotation.PreDestroy;
47+
import java.util.concurrent.TimeUnit;
4648
import java.io.IOException;
4749
import java.util.Collection;
4850
import java.util.List;
@@ -119,6 +121,19 @@ public MicroblogController(Tracer tracer, PostSerializer postSerializer) {
119121
this.ragServiceEnabled = isRagServiceEnabled;
120122
}
121123

124+
@PreDestroy
125+
public void shutdownRagExecutor() {
126+
ragExecutor.shutdown();
127+
try {
128+
if (!ragExecutor.awaitTermination(30, TimeUnit.SECONDS)) {
129+
ragExecutor.shutdownNow();
130+
}
131+
} catch (InterruptedException e) {
132+
ragExecutor.shutdownNow();
133+
Thread.currentThread().interrupt();
134+
}
135+
}
136+
122137
@RequestMapping("/timeline")
123138
public List<Post> timeline() {
124139
return redisClient.getTimeline();
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.dynatrace.microblog.exceptions;
2+
3+
public class EmptySpamClassificationException extends RuntimeException {
4+
public EmptySpamClassificationException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.dynatrace.microblog.exceptions;
2+
3+
public class InvalidPostException extends RuntimeException {
4+
public InvalidPostException(String message) {
5+
super(message);
6+
}
7+
}

src/microblog-service/src/main/java/org/dynatrace/microblog/ragservice/RAGServiceClient.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
import io.opentracing.contrib.okhttp3.TracingInterceptor;
66
import io.opentracing.util.GlobalTracer;
77
import okhttp3.*;
8-
import org.dynatrace.microblog.exceptions.InvalidJwtException;
9-
import org.dynatrace.microblog.exceptions.UserNotFoundException;
8+
import org.dynatrace.microblog.exceptions.EmptySpamClassificationException;
109
import org.json.JSONObject;
1110
import org.slf4j.Logger;
1211
import org.slf4j.LoggerFactory;
@@ -23,9 +22,9 @@ public class RAGServiceClient {
2322
private final String ragServiceHost;
2423
private final int ragServicePort;
2524

26-
public RAGServiceClient(String ragServiceHost, String ragsServicePort) {
25+
public RAGServiceClient(String ragServiceHost, String ragServicePort) {
2726
this.ragServiceHost = ragServiceHost;
28-
this.ragServicePort = Integer.parseInt(ragsServicePort);
27+
this.ragServicePort = Integer.parseInt(ragServicePort);
2928

3029
TracingInterceptor tracingInterceptor = new TracingInterceptor(
3130
GlobalTracer.get(),
@@ -41,7 +40,7 @@ public RAGServiceClient(String ragServiceHost, String ragsServicePort) {
4140

4241
}
4342

44-
public String getSpamClassification(String postText) throws InvalidJwtException, UserNotFoundException, IOException {
43+
public String getSpamClassification(String postText) throws IOException, EmptySpamClassificationException {
4544
JsonObject obj = new JsonObject();
4645
obj.addProperty("text", postText);
4746
String jsonRequest = obj.toString();
@@ -66,10 +65,12 @@ public String getSpamClassification(String postText) throws InvalidJwtException,
6665
try (Response response = call.execute()) {
6766

6867
if (response.code() == 200) {
69-
JSONObject responseObject = new JSONObject(response.body().string());
68+
ResponseBody responseBody = response.body();
69+
if (responseBody == null) {
70+
throw new EmptySpamClassificationException("Error retrieving spam classification from RAG service: empty response body");
71+
}
72+
JSONObject responseObject = new JSONObject(responseBody.string());
7073
return responseObject.getString("classification");
71-
} else if (response.code() == 401) {
72-
throw new InvalidJwtException();
7374
} else {
7475
throw new RuntimeException("Error retrieving spam classification from RAG service: " + response.code() + " - " + response.message());
7576
}

0 commit comments

Comments
 (0)