diff --git a/src/main/java/com/olympus/hermione/controllers/ExecutionController.java b/src/main/java/com/olympus/hermione/controllers/ExecutionController.java index 6961a44..55af9ab 100644 --- a/src/main/java/com/olympus/hermione/controllers/ExecutionController.java +++ b/src/main/java/com/olympus/hermione/controllers/ExecutionController.java @@ -20,8 +20,12 @@ public class ExecutionController { ScenarioExecutionService scenarioExecutionService; @GetMapping("/execution") - public ScenarioExecution getOldExections(@RequestParam String id){ - return scenarioExecutionRepository.findById(id).get(); + public ScenarioExecution getOldExections(@RequestParam String id){ + ScenarioExecution scenarioExecution = scenarioExecutionRepository.findById(id).get(); + scenarioExecution.getScenario().getAiModel().setApiKey("**********"); + scenarioExecution.getScenario().getAiModel().setEndpoint("**********"); + scenarioExecution.getScenario().getAiModel().setFull_path_endpoint("**********"); + return scenarioExecution; } } diff --git a/src/main/java/com/olympus/hermione/controllers/dashboard/DashboardChatController.java b/src/main/java/com/olympus/hermione/controllers/dashboard/DashboardChatController.java new file mode 100644 index 0000000..a3ea7ff --- /dev/null +++ b/src/main/java/com/olympus/hermione/controllers/dashboard/DashboardChatController.java @@ -0,0 +1,46 @@ +package com.olympus.hermione.controllers.dashboard; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import com.olympus.hermione.repository.dashboard.DashboardChatModel; +import com.olympus.hermione.repository.dashboard.DashboardScenarioChatRepository; + +@RestController +public class DashboardChatController { + + @Autowired + private DashboardScenarioChatRepository scenarioTestChatRepository; + + // Recupera tutti i messaggi + @GetMapping("/chat/all") + public List getAllMessages() { + return scenarioTestChatRepository.findAll(); + } + + // Filtra per userId + @GetMapping("/chat/byUser") + public List getByUserId(@RequestParam String userId) { + return scenarioTestChatRepository.findByUserId(userId); + } + + // Filtra per conversationId + @GetMapping("/chat/byConversation") + public List getByConversationId(@RequestParam String conversationId) { + return scenarioTestChatRepository.findByConversationId(conversationId); + } + + // Filtra per scenarioId + @GetMapping("/chat/byScenario") + public List getByScenarioId(@RequestParam String scenarioId) { + return scenarioTestChatRepository.findByScenarioId(scenarioId); + } + + @PostMapping("/save") + public DashboardChatModel saveMessage(@RequestBody DashboardChatModel message) { //salva i nuovi messaggi + return scenarioTestChatRepository.save(message); + } + +} diff --git a/src/main/java/com/olympus/hermione/controllers/dashboard/DashboardExecutionController.java b/src/main/java/com/olympus/hermione/controllers/dashboard/DashboardExecutionController.java new file mode 100644 index 0000000..11c3ff8 --- /dev/null +++ b/src/main/java/com/olympus/hermione/controllers/dashboard/DashboardExecutionController.java @@ -0,0 +1,205 @@ +package com.olympus.hermione.controllers.dashboard; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Arrays; +import java.util.Date; +import java.util.HashMap; +import java.util.List; + + +import org.slf4j.Logger; +import java.util.stream.Collectors; +import org.springframework.http.ResponseEntity; +import org.springframework.http.HttpStatus; +import org.bson.types.ObjectId; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import com.olympus.hermione.dto.DashboardChatInput; +import com.olympus.hermione.dto.DashboardInput; +import com.olympus.hermione.dto.DashboardResponse; +import com.olympus.hermione.models.Scenario; +import com.olympus.hermione.models.ScenarioExecution; +import com.olympus.hermione.repository.dashboard.DashboardScenarioExecutionRepository; +import com.olympus.hermione.repository.dashboard.DashboardScenariosRepository; +import com.olympus.hermione.repository.dashboard.DashboardUserRepository; +import com.olympus.hermione.security.entity.User; +import com.olympus.model.Project; + +@RestController +public class DashboardExecutionController { + + @Autowired + DashboardScenarioExecutionRepository dashboardScenarioExecutionRepository; + + @Autowired + DashboardUserRepository dashboardUserRepository; + + @Autowired + DashboardScenariosRepository dashboardScenariosRepository; + + private Logger logger = LoggerFactory.getLogger(DashboardExecutionController.class); + + @GetMapping("/getExecScenarioByProject") + public List getExecScenarioByProject(@RequestParam String project) { + return dashboardScenarioExecutionRepository.findByExecutedByUserId(project); + } + + @GetMapping("/projects") + public List getProjects() { + return dashboardScenarioExecutionRepository.findAllProjects(); + } + + @PostMapping("/executions-dash") + public ResponseEntity> getExecutions(@RequestBody DashboardInput filters) { + try { + Date fromDate = null; + Date toDate = null; + + // Parsing delle date + if (filters.getDateFrom() != null && !filters.getDateFrom().isEmpty()) { + fromDate = new SimpleDateFormat("yyyy-MM-dd").parse(filters.getDateFrom()); + } + if (filters.getDateTo() != null && !filters.getDateTo().isEmpty()) { + toDate = new SimpleDateFormat("yyyy-MM-dd").parse(filters.getDateTo()); + } + + // Conversione delle stringhe in liste + List projectNameList = filters.getProjectNameList(); + List scenarioNameList = filters.getScenarioNameList(); + + List executions = dashboardScenarioExecutionRepository.findByFilters( + fromDate, toDate, projectNameList, scenarioNameList + ); + + + return ResponseEntity.ok(executions); + + } catch (ParseException e) { + logger.error("Error parsing date: ", e); + return ResponseEntity.badRequest().body(null); + } catch (Exception e) { + logger.error("Error fetching executions: ", e); + return ResponseEntity.internalServerError().body(null); + } + } + + + @PostMapping("/executions-stats-dash") + public ResponseEntity> getExecutionsStats (@RequestBody DashboardInput filters) { + try { + Date fromDate = null; + Date toDate = null; + + // Parsing delle date + if (filters.getDateFrom() != null && !filters.getDateFrom().isEmpty()) { + fromDate = new SimpleDateFormat("yyyy-MM-dd").parse(filters.getDateFrom()); + } + if (filters.getDateTo() != null && !filters.getDateTo().isEmpty()) { + toDate = new SimpleDateFormat("yyyy-MM-dd").parse(filters.getDateTo()); + } + + // Conversione delle stringhe in liste + List projectNameList = filters.getProjectNameList(); + List scenarioNameList = filters.getScenarioNameList(); + + + List execStatByFilters = dashboardScenarioExecutionRepository.execStatsByFilters( + projectNameList, scenarioNameList, fromDate, toDate + ); + for (DashboardResponse r : execStatByFilters) { + logger.info("Query Response: {}", r); + + } + + + return ResponseEntity.ok(execStatByFilters); + + + + } catch (ParseException e) { + logger.error("Error parsing date: ", e); + return ResponseEntity.badRequest().body(null); + } catch (Exception e) { + logger.error("Error fetching executions: ", e); + return ResponseEntity.internalServerError().body(null); + } + } + + + + @GetMapping("/users-by-projects") + public ResponseEntity> getUsersByProjects(@RequestParam String projectIdsArr) { + try { + + List ids = Arrays.asList(projectIdsArr.split(",")); + List objectIds = ids.stream() + .map(ObjectId::new) + .collect(Collectors.toList()); + + List users = dashboardUserRepository.findUsersByLstProjectsIn(objectIds); + return ResponseEntity.ok(users); + } catch (Exception e) { + // Log dell'errore + logger.error("Error fetching users by projects: ", e); + return ResponseEntity.internalServerError().body(null); + } + } + + @GetMapping("/scenarios-filter") + public ResponseEntity> getScenarios(@RequestParam String selectedAccount) { + try { + List scenarios = dashboardScenariosRepository.findAllForAccount(selectedAccount); + return ResponseEntity.ok(scenarios); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); + } + } + + @PostMapping("/dashboard-chat-stats") + public ResponseEntity> getChatStats (@RequestBody DashboardChatInput filters) { + try { + Date fromDate = null; + Date toDate = null; + + if (filters.getDateFrom() != null && !filters.getDateFrom().isEmpty()) { + fromDate = new SimpleDateFormat("yyyy-MM-dd").parse(filters.getDateFrom()); + } + if (filters.getDateTo() != null && !filters.getDateTo().isEmpty()) { + toDate = new SimpleDateFormat("yyyy-MM-dd").parse(filters.getDateTo()); + } + + // Conversione delle stringhe in liste + List projectNameList = filters.getProjectNameList(); + List scenarioIdList = filters.getScenarioIdList(); + + + List execStatByFilters = dashboardScenarioExecutionRepository.execStatsByFilters( + projectNameList, scenarioIdList, fromDate, toDate + ); + for (DashboardResponse r : execStatByFilters) { + logger.info("Query Response: {}", r); + + } + + + return ResponseEntity.ok(execStatByFilters); + + + + } catch (ParseException e) { + logger.error("Error parsing date: ", e); + return ResponseEntity.badRequest().body(null); + } catch (Exception e) { + logger.error("Error fetching executions: ", e); + return ResponseEntity.internalServerError().body(null); + } + } + +} diff --git a/src/main/java/com/olympus/hermione/dto/DashboardChatInput.java b/src/main/java/com/olympus/hermione/dto/DashboardChatInput.java new file mode 100644 index 0000000..05d3ee0 --- /dev/null +++ b/src/main/java/com/olympus/hermione/dto/DashboardChatInput.java @@ -0,0 +1,19 @@ +package com.olympus.hermione.dto; + +import java.util.List; + +import lombok.Getter; +import lombok.Setter; + + +@Getter +@Setter +public class DashboardChatInput { + + public String dateFrom; + public String dateTo; + public List projectNameList; + public List scenarioIdList; + +} + diff --git a/src/main/java/com/olympus/hermione/dto/DashboardInput.java b/src/main/java/com/olympus/hermione/dto/DashboardInput.java new file mode 100644 index 0000000..10d4311 --- /dev/null +++ b/src/main/java/com/olympus/hermione/dto/DashboardInput.java @@ -0,0 +1,18 @@ +package com.olympus.hermione.dto; + +import java.util.List; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class DashboardInput { + public String dateFrom; + public String dateTo; + public List projectNameList; + public List applicationNameList; + public List userNameList; + public List scenarioNameList; + +} diff --git a/src/main/java/com/olympus/hermione/dto/DashboardResponse.java b/src/main/java/com/olympus/hermione/dto/DashboardResponse.java new file mode 100644 index 0000000..7eb5c7b --- /dev/null +++ b/src/main/java/com/olympus/hermione/dto/DashboardResponse.java @@ -0,0 +1,23 @@ + +package com.olympus.hermione.dto; + +import java.util.List; + +import org.springframework.data.annotation.PersistenceConstructor; + +import com.olympus.hermione.models.ScenarioExecution; + +import lombok.ToString; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +@ToString +public class DashboardResponse { + private long totalTokens; + private long totalExecutions; + private String projectName; + private String scenarioInternalName; + +} diff --git a/src/main/java/com/olympus/hermione/repository/dashboard/DashboardChatHistoryRepository.java b/src/main/java/com/olympus/hermione/repository/dashboard/DashboardChatHistoryRepository.java new file mode 100644 index 0000000..eece1f7 --- /dev/null +++ b/src/main/java/com/olympus/hermione/repository/dashboard/DashboardChatHistoryRepository.java @@ -0,0 +1,82 @@ +package com.olympus.hermione.repository.dashboard; + +import java.util.Date; +import java.util.List; + +import org.springframework.data.mongodb.repository.Aggregation; +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.data.rest.core.annotation.RepositoryRestResource; +import org.springframework.web.bind.annotation.CrossOrigin; + +import com.olympus.hermione.dto.DashboardResponse; +import com.olympus.model.ChatHistory; + +@RepositoryRestResource(collectionResourceRel = "ChatHistory", path = "ChatHistory") +@CrossOrigin +public interface DashboardChatHistoryRepository extends MongoRepository { + + + @Aggregation(pipeline = { + + "{ '$match': { " + + + "'$and': [" + + + "{ 'conversationId': { '$regex': ?1 } }, " + + + "{ 'lastMessageDate': { '$gte': ?2 } }, " + + + "{ 'lastMessageDate': { '$lte': ?3 } }" + + + "]" + + + "} }", + + "{ '$group': { " + + + "'_id': { " + + + "'project': '$execSharedMap.user_input.selected_project', " + + + "'scenario': '$scenario.name'" + + + "}, " + + + "'totalTokens': { '$sum': '$usedTokens' }, " + + + "'totalExecutions': { '$sum': 1 }" + + + "} }", + + "{ '$project': { " + + + "'projectName': '$_id.project', " + + + "'scenarioInternalName': '$_id.scenario', " + + + "'totalTokens': 1, " + + + "'totalExecutions': 1" + + + "} }" + +}) + +List execStatsByFilters( + + List projectNames, + + List scenarioNames, + + Date fromDate, + + Date toDate); + +} + + + + + + + diff --git a/src/main/java/com/olympus/hermione/repository/dashboard/DashboardChatModel.java b/src/main/java/com/olympus/hermione/repository/dashboard/DashboardChatModel.java new file mode 100644 index 0000000..70f2401 --- /dev/null +++ b/src/main/java/com/olympus/hermione/repository/dashboard/DashboardChatModel.java @@ -0,0 +1,112 @@ +package com.olympus.hermione.repository.dashboard; + +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + +import java.util.Date; +import java.util.List; + +@Document(collection = "ChatHistory") +public class DashboardChatModel { + + @Id + private String id; + private String userId; + private String conversationId; + private String scenarioId; + private String scenarioName; + private List messages; + + // Getters e Setters + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } + + public String getConversationId() { + return conversationId; + } + + public void setConversationId(String conversationId) { + this.conversationId = conversationId; + } + + public String getScenarioId() { + return scenarioId; + } + + public void setScenarioId(String scenarioId) { + this.scenarioId = scenarioId; + } + + public String getScenarioName() { + return scenarioName; + } + + public void setScenarioName(String scenarioName) { + this.scenarioName = scenarioName; + } + + public List getMessages() { + return messages; + } + + public void setMessages(List messages) { + this.messages = messages; + } + + + + + //Classe interna per i messaggi + public static class Message { + private String message; + private String type; + private Date timestamp; + private String conversationId; + + // Getters e Setters + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public Date getTimestamp() { + return timestamp; + } + + public void setTimestamp(Date timestamp) { + this.timestamp = timestamp; + } + + public String getConversationId() { + return conversationId; + } + + public void setConversationId(String conversationId) { + this.conversationId = conversationId; + } + } +} \ No newline at end of file diff --git a/src/main/java/com/olympus/hermione/repository/dashboard/DashboardScenarioChatRepository.java b/src/main/java/com/olympus/hermione/repository/dashboard/DashboardScenarioChatRepository.java new file mode 100644 index 0000000..5f5ca40 --- /dev/null +++ b/src/main/java/com/olympus/hermione/repository/dashboard/DashboardScenarioChatRepository.java @@ -0,0 +1,22 @@ +package com.olympus.hermione.repository.dashboard; +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.data.rest.core.annotation.RepositoryRestResource; +import org.springframework.stereotype.Repository; +import org.springframework.web.bind.annotation.CrossOrigin; + +import java.util.List; + + + +@RepositoryRestResource(collectionResourceRel = "ChatModels", path = "ChatModels") +@CrossOrigin +public interface DashboardScenarioChatRepository extends MongoRepository { + + List findByUserId(String userId); + List findByConversationId(String conversationId); + List findByScenarioId(String scenarioId); + + +} + + diff --git a/src/main/java/com/olympus/hermione/repository/dashboard/DashboardScenarioExecutionRepository.java b/src/main/java/com/olympus/hermione/repository/dashboard/DashboardScenarioExecutionRepository.java new file mode 100644 index 0000000..51a32d9 --- /dev/null +++ b/src/main/java/com/olympus/hermione/repository/dashboard/DashboardScenarioExecutionRepository.java @@ -0,0 +1,101 @@ +package com.olympus.hermione.repository.dashboard; + +import java.util.Date; +import java.util.HashMap; +import java.util.List; + +import org.bson.types.ObjectId; +import org.bson.Document; +import org.springframework.data.mongodb.repository.Aggregation; +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.data.mongodb.repository.Query; +import org.springframework.data.rest.core.annotation.RepositoryRestResource; +import org.springframework.web.bind.annotation.CrossOrigin; + +import com.olympus.hermione.dto.DashboardResponse; +import com.olympus.hermione.models.ScenarioExecution; +import com.olympus.hermione.security.entity.User; +import com.olympus.model.Project; + +//query per trovare esecuzioni tramite IDUtente e progetti +@RepositoryRestResource(collectionResourceRel = "scenario_executions", path = "scenario_executions") +@CrossOrigin +public interface DashboardScenarioExecutionRepository extends MongoRepository { + List findByExecutedByUserId(String project); + + List findByExecutedByUserIdOrderByStartDateDesc(String userId); + + @Query(value = "{}", fields = "{ 'projectId': 1, 'FE_name': 1 }") + List findAllProjects(); + + @Query("{ 'execSharedMap.user_input.selected_project': ?0 }") + List getExecutionByProjectName(String projectName); + + @Query("{'execSharedMap.user_input.selected_project': { $in: ?2 },'endDate': { $gte: ?0, $lte: ?1 }, 'scenario.name' : { $in: ?3} }") + List findByFilters(Date fromDate, Date toDate, List projectNames, + List scenarioNames); + + @Query("{ 'lstProjects': { $in: ?0 }}") + List findUsersByLstProjectsIn(List projectIds); + + @Aggregation(pipeline = { + + "{ '$match': { " + + + "'$and': [" + + + "{ 'execSharedMap.user_input.selected_project': { '$in': ?0 } }, " + + + "{ 'scenario.name': { '$in': ?1 } }, " + + + "{ 'endDate': { '$gte': ?2 } }, " + + + "{ 'endDate': { '$lte': ?3 } }" + + + "]" + + + "} }", + + "{ '$group': { " + + + "'_id': { " + + + "'project': '$execSharedMap.user_input.selected_project', " + + + "'scenario': '$scenario.name'" + + + "}, " + + + "'totalTokens': { '$sum': '$usedTokens' }, " + + + "'totalExecutions': { '$sum': 1 }" + + + "} }", + + "{ '$project': { " + + + "'projectName': '$_id.project', " + + + "'scenarioInternalName': '$_id.scenario', " + + + "'totalTokens': 1, " + + + "'totalExecutions': 1" + + + "} }" + +}) + +List execStatsByFilters( + + List projectNames, + + List scenarioNames, + + Date fromDate, + + Date toDate); + +} + + diff --git a/src/main/java/com/olympus/hermione/repository/dashboard/DashboardScenariosRepository.java b/src/main/java/com/olympus/hermione/repository/dashboard/DashboardScenariosRepository.java new file mode 100644 index 0000000..9a14831 --- /dev/null +++ b/src/main/java/com/olympus/hermione/repository/dashboard/DashboardScenariosRepository.java @@ -0,0 +1,29 @@ +package com.olympus.hermione.repository.dashboard; + +import java.util.Date; +import java.util.List; + +import org.bson.types.ObjectId; +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.data.mongodb.repository.Query; +import org.springframework.data.rest.core.annotation.RepositoryRestResource; +import org.springframework.stereotype.Repository; +import org.springframework.web.bind.annotation.CrossOrigin; + +import com.olympus.hermione.models.Scenario; +import com.olympus.hermione.models.ScenarioExecution; +import com.olympus.hermione.security.entity.User; +import com.olympus.model.Project; + + + +@RepositoryRestResource(collectionResourceRel = "scenarios", path = "scenarios") +@CrossOrigin +public interface DashboardScenariosRepository extends MongoRepository +{ + @Query("{ 'visible': 'Y', 'account': ?0 }") + List findAllForAccount(String account); + + +} + diff --git a/src/main/java/com/olympus/hermione/repository/dashboard/DashboardUserRepository.java b/src/main/java/com/olympus/hermione/repository/dashboard/DashboardUserRepository.java new file mode 100644 index 0000000..1dab4ec --- /dev/null +++ b/src/main/java/com/olympus/hermione/repository/dashboard/DashboardUserRepository.java @@ -0,0 +1,28 @@ +package com.olympus.hermione.repository.dashboard; + +import java.util.Date; +import java.util.List; + +import org.bson.types.ObjectId; +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.data.mongodb.repository.Query; +import org.springframework.data.rest.core.annotation.RepositoryRestResource; +import org.springframework.stereotype.Repository; +import org.springframework.web.bind.annotation.CrossOrigin; + +import com.olympus.hermione.models.ScenarioExecution; +import com.olympus.hermione.security.entity.User; +import com.olympus.model.Project; + + + +@RepositoryRestResource(collectionResourceRel = "users", path = "users") +@CrossOrigin +public interface DashboardUserRepository extends MongoRepository + { + + @Query("{ 'lstProjects': { $in: ?0 }}") + List findUsersByLstProjectsIn(List projectIds); + +} +