From 475b7f3b3b07d07bbeecad87879911f74e8d700d Mon Sep 17 00:00:00 2001 From: jkuznik Date: Thu, 28 Nov 2024 11:58:03 +0100 Subject: [PATCH 1/9] Update application-prod to fix AI issue, prepare RISK_THRESHOLD value as a env --- .../RiskAssessmentService.java | 2 +- .../disasteralert/DisasterServiceImpl.java | 21 ++++++++----------- src/main/resources/application-prod.yml | 4 ++-- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/main/java/pl/ateam/disasteralerts/airiskassessment/RiskAssessmentService.java b/src/main/java/pl/ateam/disasteralerts/airiskassessment/RiskAssessmentService.java index 7dec2956..2cd76696 100644 --- a/src/main/java/pl/ateam/disasteralerts/airiskassessment/RiskAssessmentService.java +++ b/src/main/java/pl/ateam/disasteralerts/airiskassessment/RiskAssessmentService.java @@ -10,7 +10,7 @@ @Slf4j class RiskAssessmentService { - public static final double RISK_THRESHOLD = 0.7; + public static final double RISK_THRESHOLD = Double.parseDouble(System.getenv("RISK_THRESHOLD")); private final OpenAIClient openAIClient; boolean assessRisk(DisasterAddDTO disasterAddDTO) { diff --git a/src/main/java/pl/ateam/disasteralerts/disasteralert/DisasterServiceImpl.java b/src/main/java/pl/ateam/disasteralerts/disasteralert/DisasterServiceImpl.java index 93c80b95..8c8d6ef9 100644 --- a/src/main/java/pl/ateam/disasteralerts/disasteralert/DisasterServiceImpl.java +++ b/src/main/java/pl/ateam/disasteralerts/disasteralert/DisasterServiceImpl.java @@ -25,18 +25,15 @@ public DisasterDTO createDisaster(DisasterAddDTO disasterAddDTO, String source) Disaster disaster = mapper.mapDisasterAddDtoToDisaster(disasterAddDTO); disaster.setSource(source); //TODO: trzeba dopracować promt do czata bo przy obecnym trudno osiągnąć wynik pozwalający na uznanie zdarzenia za prawdziwe -// if (riskAssessment.assessRisk(disasterAddDTO)) { -// disaster.setStatus(DisasterStatus.ACTIVE); -// disasterRepository.save(disaster); -// generateAlert(disaster.getId()); -// } else { -// disaster.setStatus(DisasterStatus.FAKE); -// disasterRepository.save(disaster); -// } - - disaster.setStatus(DisasterStatus.ACTIVE); - disasterRepository.save(disaster); - generateAlert(disaster.getId()); + // edit: tymczasowo można po prostu zmniejszyć wymagany wynik zwracany przez AI aby zdarzenie zakwalifikować jako ACTIVE + if (riskAssessment.assessRisk(disasterAddDTO)) { + disaster.setStatus(DisasterStatus.ACTIVE); + disasterRepository.save(disaster); + generateAlert(disaster.getId()); + } else { + disaster.setStatus(DisasterStatus.FAKE); + disasterRepository.save(disaster); + } return mapper.mapDisasterToDisasterDto(disaster); } diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml index deab92fa..29ef6504 100644 --- a/src/main/resources/application-prod.yml +++ b/src/main/resources/application-prod.yml @@ -24,11 +24,11 @@ spring: ai: openai: - api-key: "${{ secrets.OPENAI_API_KEY }}" + api-key: ${OPENAI_API_KEY} chat: options: model: gpt-4o - temperature: 0.5 + temperature: 1.0 server: port: 8080 From 73e168645e5d854b3bb0c6ce80a0623b3ac28d03 Mon Sep 17 00:00:00 2001 From: jkuznik Date: Thu, 28 Nov 2024 12:00:44 +0100 Subject: [PATCH 2/9] Update CD script to hande risk threshold value --- .github/workflows/self-host-CD.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/self-host-CD.yml b/.github/workflows/self-host-CD.yml index 0a6452ad..5558844b 100644 --- a/.github/workflows/self-host-CD.yml +++ b/.github/workflows/self-host-CD.yml @@ -25,4 +25,5 @@ jobs: OPENAI_API_KEY: ${{secrets.OPENAI_API_KEY}} GOOGLE_API_KEY: ${{secrets.GOOGLE_API_KEY}} DAY_SMS_LIMIT: 5 - run: sudo docker run -d -p 8080:8080 --name disaster-alert-container --network disaster-net -e TWILIO_ACCOUNT_SID=$TWILIO_ACCOUNT_SID -e TWILIO_AUTH_TOKEN=$TWILIO_AUTH_TOKEN -e TWILIO_PHONE_NUMBER=$TWILIO_PHONE_NUMBER -e OPENAI_API_KEY=$OPENAI_API_KEY -e GOOGLE_API_KEY=$GOOGLE_API_KEY -e DAY_SMS_LIMIT=$DAY_SMS_LIMIT jkuznik/disaster-alert \ No newline at end of file + RISK_THRESHOLD: 0.5 + run: sudo docker run -d -p 8080:8080 --name disaster-alert-container --network disaster-net -e TWILIO_ACCOUNT_SID=$TWILIO_ACCOUNT_SID -e TWILIO_AUTH_TOKEN=$TWILIO_AUTH_TOKEN -e TWILIO_PHONE_NUMBER=$TWILIO_PHONE_NUMBER -e OPENAI_API_KEY=$OPENAI_API_KEY -e GOOGLE_API_KEY=$GOOGLE_API_KEY -e DAY_SMS_LIMIT=$DAY_SMS_LIMIT -e RISK_THRESHOLD=$RISK_THRESHOLD jkuznik/disaster-alert \ No newline at end of file From c5898d1ca4aaeddc015eb20ce1323e927f634d3c Mon Sep 17 00:00:00 2001 From: jkuznik Date: Thu, 28 Nov 2024 12:40:49 +0100 Subject: [PATCH 3/9] Update tests --- .../RiskAssessmentServiceTest.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/test/java/pl/ateam/disasteralerts/airiskassessment/RiskAssessmentServiceTest.java b/src/test/java/pl/ateam/disasteralerts/airiskassessment/RiskAssessmentServiceTest.java index 21dfa1a5..d0025ff0 100644 --- a/src/test/java/pl/ateam/disasteralerts/airiskassessment/RiskAssessmentServiceTest.java +++ b/src/test/java/pl/ateam/disasteralerts/airiskassessment/RiskAssessmentServiceTest.java @@ -6,6 +6,9 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import pl.ateam.disasteralerts.disasteralert.DisasterType; import pl.ateam.disasteralerts.disasteralert.dto.DisasterAddDTO; import pl.ateam.disasteralerts.util.CitiesInPoland; @@ -17,20 +20,19 @@ import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.when; -@ExtendWith(MockitoExtension.class) +@SpringJUnitConfig(classes = {OpenAIClient.class, RiskAssessmentService.class}) class RiskAssessmentServiceTest { + @Autowired + private RiskAssessmentService riskAssessmentService; + + @MockBean + private OpenAIClient openAIClient; + public static final String FLOOD_DESCRIPTION = "Duża powódź"; public static final String FIRE_DESCRIPTION = "Ognisko palone przez turystów nad rzeką"; public static final Double PROBABILITY_OF_RISK_HIGH = 0.8; public static final Double PROBABILITY_OF_RISK_LOW = 0.3; - - @Mock - private OpenAIClient openAIClient; - - @InjectMocks - private RiskAssessmentService riskAssessmentService; - private final List citiesInPoland = CitiesInPoland.getList(); @Test From 16572a2217aa4497b71a613055a8ea6e97466337 Mon Sep 17 00:00:00 2001 From: jkuznik Date: Thu, 28 Nov 2024 12:46:07 +0100 Subject: [PATCH 4/9] Rollback tests version, update build.yml --- .github/workflows/build.yml | 1 + .../RiskAssessmentServiceTest.java | 18 ++++++++---------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b2e04309..236bb4e4 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -45,4 +45,5 @@ jobs: - name: Build and Run Tests env: OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} + RISK_THRESHOLD: 0.5 run: mvn clean install diff --git a/src/test/java/pl/ateam/disasteralerts/airiskassessment/RiskAssessmentServiceTest.java b/src/test/java/pl/ateam/disasteralerts/airiskassessment/RiskAssessmentServiceTest.java index d0025ff0..21dfa1a5 100644 --- a/src/test/java/pl/ateam/disasteralerts/airiskassessment/RiskAssessmentServiceTest.java +++ b/src/test/java/pl/ateam/disasteralerts/airiskassessment/RiskAssessmentServiceTest.java @@ -6,9 +6,6 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import pl.ateam.disasteralerts.disasteralert.DisasterType; import pl.ateam.disasteralerts.disasteralert.dto.DisasterAddDTO; import pl.ateam.disasteralerts.util.CitiesInPoland; @@ -20,19 +17,20 @@ import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.when; -@SpringJUnitConfig(classes = {OpenAIClient.class, RiskAssessmentService.class}) +@ExtendWith(MockitoExtension.class) class RiskAssessmentServiceTest { - @Autowired - private RiskAssessmentService riskAssessmentService; - - @MockBean - private OpenAIClient openAIClient; - public static final String FLOOD_DESCRIPTION = "Duża powódź"; public static final String FIRE_DESCRIPTION = "Ognisko palone przez turystów nad rzeką"; public static final Double PROBABILITY_OF_RISK_HIGH = 0.8; public static final Double PROBABILITY_OF_RISK_LOW = 0.3; + + @Mock + private OpenAIClient openAIClient; + + @InjectMocks + private RiskAssessmentService riskAssessmentService; + private final List citiesInPoland = CitiesInPoland.getList(); @Test From 77904a165b457c13f16e95918e624859eda6ed95 Mon Sep 17 00:00:00 2001 From: jkuznik Date: Thu, 28 Nov 2024 12:51:09 +0100 Subject: [PATCH 5/9] Update build.yml and self-hosted-CD.yml to achieve maintain env in one place --- .github/workflows/build.yml | 4 ++-- .github/workflows/self-host-CD.yml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 236bb4e4..6873ffbe 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -44,6 +44,6 @@ jobs: - name: Build and Run Tests env: - OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} - RISK_THRESHOLD: 0.5 + OPENAI_API_KEY: ${{secrets.OPENAI_API_KEY}} + RISK_THRESHOLD: ${{secrets.RISK_THRESHOLD}} run: mvn clean install diff --git a/.github/workflows/self-host-CD.yml b/.github/workflows/self-host-CD.yml index 5558844b..f9af3014 100644 --- a/.github/workflows/self-host-CD.yml +++ b/.github/workflows/self-host-CD.yml @@ -24,6 +24,6 @@ jobs: TWILIO_PHONE_NUMBER: ${{secrets.TWILIO_PHONE_NUMBER}} OPENAI_API_KEY: ${{secrets.OPENAI_API_KEY}} GOOGLE_API_KEY: ${{secrets.GOOGLE_API_KEY}} - DAY_SMS_LIMIT: 5 - RISK_THRESHOLD: 0.5 + DAY_SMS_LIMIT: ${{secrets.DAY_SMS_LIMIT}} + RISK_THRESHOLD: ${{secrets.RISK_THRESHOLD}} run: sudo docker run -d -p 8080:8080 --name disaster-alert-container --network disaster-net -e TWILIO_ACCOUNT_SID=$TWILIO_ACCOUNT_SID -e TWILIO_AUTH_TOKEN=$TWILIO_AUTH_TOKEN -e TWILIO_PHONE_NUMBER=$TWILIO_PHONE_NUMBER -e OPENAI_API_KEY=$OPENAI_API_KEY -e GOOGLE_API_KEY=$GOOGLE_API_KEY -e DAY_SMS_LIMIT=$DAY_SMS_LIMIT -e RISK_THRESHOLD=$RISK_THRESHOLD jkuznik/disaster-alert \ No newline at end of file From ff6ae645679679cfdecd6a918e3394c2a63cf290 Mon Sep 17 00:00:00 2001 From: jkuznik Date: Fri, 29 Nov 2024 09:32:23 +0100 Subject: [PATCH 6/9] Increase self-host.yml readability --- .github/workflows/self-host-CD.yml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/.github/workflows/self-host-CD.yml b/.github/workflows/self-host-CD.yml index f9af3014..082b2d6d 100644 --- a/.github/workflows/self-host-CD.yml +++ b/.github/workflows/self-host-CD.yml @@ -26,4 +26,12 @@ jobs: GOOGLE_API_KEY: ${{secrets.GOOGLE_API_KEY}} DAY_SMS_LIMIT: ${{secrets.DAY_SMS_LIMIT}} RISK_THRESHOLD: ${{secrets.RISK_THRESHOLD}} - run: sudo docker run -d -p 8080:8080 --name disaster-alert-container --network disaster-net -e TWILIO_ACCOUNT_SID=$TWILIO_ACCOUNT_SID -e TWILIO_AUTH_TOKEN=$TWILIO_AUTH_TOKEN -e TWILIO_PHONE_NUMBER=$TWILIO_PHONE_NUMBER -e OPENAI_API_KEY=$OPENAI_API_KEY -e GOOGLE_API_KEY=$GOOGLE_API_KEY -e DAY_SMS_LIMIT=$DAY_SMS_LIMIT -e RISK_THRESHOLD=$RISK_THRESHOLD jkuznik/disaster-alert \ No newline at end of file + run: sudo docker run -d --name disaster-alert-container --network disaster-net -p 8080:8080 \ + -e TWILIO_ACCOUNT_SID=$TWILIO_ACCOUNT_SID \ + -e TWILIO_AUTH_TOKEN=$TWILIO_AUTH_TOKEN \ + -e TWILIO_PHONE_NUMBER=$TWILIO_PHONE_NUMBER \ + -e OPENAI_API_KEY=$OPENAI_API_KEY \ + -e GOOGLE_API_KEY=$GOOGLE_API_KEY \ + -e RISK_THRESHOLD=$RISK_THRESHOLD \ + -e DAY_SMS_LIMIT=5 \ + jkuznik/disaster-alert \ No newline at end of file From 356c4267b235d888b22ce9d4551e62461e0e6aed Mon Sep 17 00:00:00 2001 From: jkuznik Date: Fri, 29 Nov 2024 09:32:43 +0100 Subject: [PATCH 7/9] Fix time zone issue --- .github/workflows/self-host-CD.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/self-host-CD.yml b/.github/workflows/self-host-CD.yml index 082b2d6d..0407e932 100644 --- a/.github/workflows/self-host-CD.yml +++ b/.github/workflows/self-host-CD.yml @@ -32,6 +32,7 @@ jobs: -e TWILIO_PHONE_NUMBER=$TWILIO_PHONE_NUMBER \ -e OPENAI_API_KEY=$OPENAI_API_KEY \ -e GOOGLE_API_KEY=$GOOGLE_API_KEY \ + -e TZ=Europe/Warsaw \ -e RISK_THRESHOLD=$RISK_THRESHOLD \ -e DAY_SMS_LIMIT=5 \ jkuznik/disaster-alert \ No newline at end of file From 0cb917219335d6bf325bd95d165e42bd9616b90b Mon Sep 17 00:00:00 2001 From: Slawek84PL Date: Sat, 30 Nov 2024 18:22:39 +0100 Subject: [PATCH 8/9] Added message for FAKE --- .../disasteralerts/web/DisasterViewController.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/java/pl/ateam/disasteralerts/web/DisasterViewController.java b/src/main/java/pl/ateam/disasteralerts/web/DisasterViewController.java index 932cd9c7..39bd404f 100644 --- a/src/main/java/pl/ateam/disasteralerts/web/DisasterViewController.java +++ b/src/main/java/pl/ateam/disasteralerts/web/DisasterViewController.java @@ -59,8 +59,18 @@ public String createDisaster(Model model, @AuthenticationPrincipal AppUser userD return "addDisaster"; } - disasterAlertFacade.createDisaster(disasterAddDTO, USER_AS_DISASTER_SOURCE); - redirectAttributes.addFlashAttribute("message", "Dodano zdarzenie"); + DisasterDTO disasterDTO = disasterAlertFacade.createDisaster(disasterAddDTO, USER_AS_DISASTER_SOURCE); + + if (DisasterStatus.FAKE.equals(disasterDTO.status())) { + String message = String.format(""" + Zdarzenie zostało uznane za falszywe. + Jeśli chcesz je aktywować skontatuj się z adminiastratorem i podaj id zgłoszenia %s. + """, disasterDTO.id()); + redirectAttributes.addFlashAttribute("message", message); + } else { + redirectAttributes.addFlashAttribute("message", "Dodano zdarzenie"); + } + return "redirect:/disasters/add"; } From fc90b41adf525a9697faf22a386f04e8e5885079 Mon Sep 17 00:00:00 2001 From: Slawek84PL Date: Sat, 30 Nov 2024 19:26:32 +0100 Subject: [PATCH 9/9] Added message status --- .../disasteralerts/web/DisasterViewController.java | 3 ++- src/main/resources/templates/fragments.html | 10 ++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/main/java/pl/ateam/disasteralerts/web/DisasterViewController.java b/src/main/java/pl/ateam/disasteralerts/web/DisasterViewController.java index 39bd404f..c65d1f00 100644 --- a/src/main/java/pl/ateam/disasteralerts/web/DisasterViewController.java +++ b/src/main/java/pl/ateam/disasteralerts/web/DisasterViewController.java @@ -63,9 +63,10 @@ public String createDisaster(Model model, @AuthenticationPrincipal AppUser userD if (DisasterStatus.FAKE.equals(disasterDTO.status())) { String message = String.format(""" - Zdarzenie zostało uznane za falszywe. + Zdarzenie zostało uznane za fałszywe. Jeśli chcesz je aktywować skontatuj się z adminiastratorem i podaj id zgłoszenia %s. """, disasterDTO.id()); + redirectAttributes.addFlashAttribute("messageStatus", DisasterStatus.FAKE.toString()); redirectAttributes.addFlashAttribute("message", message); } else { redirectAttributes.addFlashAttribute("message", "Dodano zdarzenie"); diff --git a/src/main/resources/templates/fragments.html b/src/main/resources/templates/fragments.html index 464653b7..6c96890f 100644 --- a/src/main/resources/templates/fragments.html +++ b/src/main/resources/templates/fragments.html @@ -67,12 +67,10 @@