Skip to content

Commit 479e70d

Browse files
authored
Refactor result retrieval, unify solution logic, and improve Docker deployment workflow
- Refactor `getResults` method in `DBService` to return a more detailed result set. - Refactor `getApplications` and `getResults` endpoints in `SolverController` for simplification and improved readability. - Unify and simplify solution retrieval logic by renaming `get_solution` to `get_results` in both server and client code. - Update and standardize `docker-compose.yml` configuration, including service names and dependencies. - Set up and improve GitHub Actions workflows for Docker deployment. - Remove unnecessary `.env` file copying from Dockerfiles. - Update scheduled task to run cleanup of old applications at midnight. - Minor improvements to log file paths and project versioning.
2 parents f069c35 + b5febc6 commit 479e70d

File tree

10 files changed

+112
-178
lines changed

10 files changed

+112
-178
lines changed

.github/workflows/build-and-push.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Build and Push Docker Images
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
workflow_dispatch:
8+
9+
jobs:
10+
build-and-push:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v4
15+
16+
- name: Verify DockerHub credentials
17+
run: |
18+
if [ -z "${{ secrets.DOCKERHUB_USERNAME }}" ] || [ -z "${{ secrets.DOCKERHUB_PASSWORD }}" ]; then
19+
echo "Missing DockerHub credentials"
20+
exit 1
21+
fi
22+
23+
- name: Log in to Docker Hub
24+
uses: docker/login-action@v3
25+
with:
26+
registry: docker.io
27+
username: ${{ secrets.DOCKERHUB_USERNAME }}
28+
password: ${{ secrets.DOCKERHUB_PASSWORD }}
29+
30+
- name: Build and push diffy-bot-client
31+
uses: docker/build-push-action@v4
32+
with:
33+
context: .
34+
file: solver-bot/Dockerfile
35+
push: true
36+
tags: ${{ secrets.DOCKERHUB_USERNAME }}/diffy-bot-client:latest
37+
38+
- name: Build and push diffy-bot-server
39+
uses: docker/build-push-action@v4
40+
with:
41+
context: .
42+
file: solver-common/Dockerfile
43+
push: true
44+
tags: ${{ secrets.DOCKERHUB_USERNAME }}/diffy-bot-server:latest

docker-compose.yml

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,37 @@
11
services:
2-
solver-bot:
2+
client:
33
build:
44
context: .
55
dockerfile: solver-bot/Dockerfile
6-
container_name: diffy-bot-client-container
6+
container_name: diffy-bot-client
77
working_dir: /solver-client
88
env_file:
99
- .env
1010
environment:
1111
- CLIENT_API_KEY=${CLIENT_API_KEY}
12-
- CLIENT_API_URL=http://solver-common:8080/api/solver
12+
- CLIENT_API_URL=http://server:8080/api/solver
1313
ports:
1414
- "8001:8000"
1515
volumes:
1616
- ./solver-bot/src:/solver-client/src
17+
- .env:/solver-client/.env
1718
depends_on:
18-
- solver-db
19-
- solver-common
19+
- database
20+
- server
2021
networks:
21-
- solver-network
22+
- net
2223

23-
solver-common:
24+
server:
2425
build:
2526
context: .
2627
dockerfile: solver-common/Dockerfile
27-
container_name: diffy-bot-server-container
28+
container_name: diffy-bot-server
2829
working_dir: /solver-server
2930
env_file:
3031
- .env
3132
environment:
3233
- DB_CONNECTION=${DB_CONNECTION}
33-
- DB_HOST=diffy-bot-db-container
34+
- DB_HOST=database
3435
- DB_PORT=${DB_PORT}
3536
- DB_DATABASE=${DB_DATABASE}
3637
- DB_USERNAME=${DB_USERNAME}
@@ -39,14 +40,15 @@ services:
3940
- "8081:8080"
4041
volumes:
4142
- ./solver-common/src:/solver-server/src
43+
- .env:/solver-server/.env
4244
depends_on:
43-
- solver-db
45+
- database
4446
networks:
45-
- solver-network
47+
- net
4648

47-
solver-db:
49+
database:
4850
image: postgres:17.5-alpine
49-
container_name: diffy-bot-db-container
51+
container_name: diffy-bot-database
5052
environment:
5153
POSTGRES_USER: ${DB_USERNAME}
5254
POSTGRES_PASSWORD: ${DB_PASSWORD}
@@ -57,11 +59,11 @@ services:
5759
- pgdata:/var/lib/postgresql/data
5860
- ./database/schema.sql:/docker-entrypoint-initdb.d/init.sql:ro
5961
networks:
60-
- solver-network
62+
- net
6163

6264
volumes:
6365
pgdata:
6466

6567
networks:
66-
solver-network:
68+
net:
6769
driver: bridge

solver-bot/Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ FROM python:3.13.3-slim
33
WORKDIR /solver-client
44

55
COPY solver-bot .
6-
COPY .env .
76

87
RUN pip install --no-cache-dir -r requirements.txt
98

solver-bot/src/main/python/shell.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010
set_parameters,
1111
set_user_settings,
1212
get_user_settings,
13-
get_x_values,
14-
get_y_values,
15-
get_solution,
1613
get_recent_applications,
14+
get_results,
1715
wait_for_application_completion)
1816
from equation.equation_parser import format_equation
1917
from equation.equation_validator import (
@@ -450,6 +448,7 @@ async def solve_history_details(update: Update, context: ContextTypes.DEFAULT_TY
450448

451449
application = recent_applications[application_index]
452450
application_id = application.get('id')
451+
results = await get_results(application_id)
453452

454453
try:
455454
parameters = json.loads(application.get("parameters", "{}"))
@@ -462,9 +461,10 @@ async def solve_history_details(update: Update, context: ContextTypes.DEFAULT_TY
462461
reach_point = parameters.get("reachPoint", "")
463462
step_size = parameters.get("stepSize", "")
464463

465-
x_values = await get_x_values(application_id)
466-
y_values = await get_y_values(application_id)
467-
solution = await get_solution(application_id)
464+
data = json.loads(results[0].get("data", "{}"))
465+
x_values = data.get("xvalues", [])
466+
y_values = data.get("yvalues", [])
467+
solution = data.get("solution", "")
468468

469469
plot_graph = plot_solution(x_values, y_values, order)
470470

@@ -839,10 +839,13 @@ async def solution(update: Update, context: ContextTypes.DEFAULT_TYPE):
839839
)
840840
return MENU
841841

842+
results = await get_results(application_id)
843+
842844
try:
843-
result = await get_solution(application_id)
844-
x_values = await get_x_values(application_id)
845-
y_values = await get_y_values(application_id)
845+
data = json.loads(results[0].get("data", "{}"))
846+
x_values = data.get("xvalues", [])
847+
y_values = data.get("yvalues", [])
848+
solution = data.get("solution", "")
846849
except Exception:
847850
logger.error("Error while getting solution for application %s", application_id)
848851
await save_user_settings(context)
@@ -853,7 +856,7 @@ async def solution(update: Update, context: ContextTypes.DEFAULT_TYPE):
853856
)
854857
return MENU
855858

856-
if result is None:
859+
if solution is None:
857860
logger.info("User %s used unsupported symbols", user.id)
858861
await save_user_settings(context)
859862
await processing_message.edit_text(
@@ -870,7 +873,7 @@ async def solution(update: Update, context: ContextTypes.DEFAULT_TYPE):
870873
)
871874

872875
print_result = print_solution(
873-
result,
876+
solution,
874877
context.user_data['order'],
875878
context.user_data['rounding']
876879
)

solver-bot/src/main/python/spring_client.py

Lines changed: 9 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ async def set_parameters(
3838
timeout = ClientTimeout(total=REQUEST_TIMEOUT)
3939
async with ClientSession(timeout=timeout) as session:
4040
async with session.post(
41-
f"{os.getenv("CLIENT_API_URL")}/solve/{user_id}",
41+
f"{os.getenv('CLIENT_API_URL')}/solve/{user_id}",
4242
json=payload
4343
) as response:
4444
response.raise_for_status()
@@ -61,7 +61,7 @@ async def set_user_settings(user_id, method, rounding, language, hints):
6161
timeout = ClientTimeout(total=REQUEST_TIMEOUT)
6262
async with ClientSession(timeout=timeout) as session:
6363
async with session.post(
64-
f"{os.getenv("CLIENT_API_URL")}/users/{user_id}",
64+
f"{os.getenv('CLIENT_API_URL')}/users/{user_id}",
6565
params=payload
6666
) as response:
6767
response.raise_for_status()
@@ -72,7 +72,7 @@ async def get_user_settings(user_id, method, rounding, language, hints):
7272
timeout = ClientTimeout(total=REQUEST_TIMEOUT)
7373
async with ClientSession(timeout=timeout) as session:
7474
async with session.get(
75-
f"{os.getenv("CLIENT_API_URL")}/users/{user_id}"
75+
f"{os.getenv('CLIENT_API_URL')}/users/{user_id}"
7676
) as response:
7777
if response.status == 500:
7878
return {
@@ -95,7 +95,7 @@ async def get_recent_applications(user_id):
9595
timeout = ClientTimeout(total=REQUEST_TIMEOUT)
9696
async with ClientSession(timeout=timeout) as session:
9797
async with session.get(
98-
f"{os.getenv("CLIENT_API_URL")}/applications/list/{user_id}"
98+
f"{os.getenv('CLIENT_API_URL')}/applications/{user_id}"
9999
) as response:
100100
if response.status == 500:
101101
return []
@@ -132,65 +132,26 @@ async def get_recent_applications(user_id):
132132
return []
133133

134134

135-
async def get_solution(application_id):
135+
async def get_results(application_id):
136136
timeout = ClientTimeout(total=REQUEST_TIMEOUT)
137137
async with ClientSession(timeout=timeout) as session:
138138
async with session.get(
139-
f"{os.getenv("CLIENT_API_URL")}/results/{application_id}/solution"
139+
f"{os.getenv('CLIENT_API_URL')}/results/{application_id}"
140140
) as response:
141-
if response.status == 500:
142-
return []
143-
144-
response.raise_for_status()
145-
text = await response.text()
146-
try:
147-
data = json.loads(text)
148-
except json.JSONDecodeError:
149-
raise ValueError(f"Failed to parse solution data: {text}")
150-
return np.array(data)
151-
152-
153-
async def get_x_values(application_id):
154-
timeout = ClientTimeout(total=REQUEST_TIMEOUT)
155-
async with ClientSession(timeout=timeout) as session:
156-
async with session.get(
157-
f"{os.getenv("CLIENT_API_URL")}/results/{application_id}/xvalues"
158-
) as response:
159-
if response.status == 500:
160-
return []
161-
162141
response.raise_for_status()
163142
text = await response.text()
164143
try:
165144
data = json.loads(text)
166145
except json.JSONDecodeError:
167-
raise ValueError(f"Failed to parse x-values data: {text}")
168-
return np.array(data)
169-
170-
171-
async def get_y_values(application_id):
172-
timeout = ClientTimeout(total=REQUEST_TIMEOUT)
173-
async with ClientSession(timeout=timeout) as session:
174-
async with session.get(
175-
f"{os.getenv("CLIENT_API_URL")}/results/{application_id}/yvalues"
176-
) as response:
177-
if response.status == 500:
178-
return []
179-
180-
response.raise_for_status()
181-
text = await response.text()
182-
try:
183-
data = json.loads(text)
184-
except json.JSONDecodeError:
185-
raise ValueError(f"Failed to parse y-values data: {text}")
186-
return np.array(data)
146+
return text
147+
return data
187148

188149

189150
async def get_application_status(application_id):
190151
timeout = ClientTimeout(total=REQUEST_TIMEOUT)
191152
async with ClientSession(timeout=timeout) as session:
192153
async with session.get(
193-
f"{os.getenv("CLIENT_API_URL")}/applications/{application_id}/status"
154+
f"{os.getenv('CLIENT_API_URL')}/applications/{application_id}/status"
194155
) as response:
195156
response.raise_for_status()
196157
return await response.text()

solver-common/Dockerfile

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@ COPY solver-common/pom.xml .
1111
RUN mvn dependency:go-offline
1212

1313
COPY solver-common .
14-
COPY .env .
1514
RUN mvn clean package -DskipTests -B
1615

1716
FROM openjdk:21-slim
1817

1918
WORKDIR /solver-server
2019

21-
COPY --from=builder /solver-server/target/solver-0.1.0-alpha.jar /solver-server/target/solver-0.1.0-alpha.jar
22-
COPY --from=builder /solver-server/.env /solver-server/.env
20+
COPY --from=builder /solver-server/target/solver-0.1.1-alpha.jar /solver-server/target/solver-0.1.1-alpha.jar
2321

24-
CMD ["java", "-jar", "target/solver-0.1.0-alpha.jar"]
22+
CMD ["java", "-jar", "target/solver-0.1.1-alpha.jar"]

solver-common/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
<artifactId>solver</artifactId>
99
<groupId>com.solver</groupId>
10-
<version>0.1.0-alpha</version>
10+
<version>0.1.1-alpha</version>
1111

1212
<properties>
1313
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

0 commit comments

Comments
 (0)