Skip to content

Commit

Permalink
Merge pull request #26 from Pardus-LiderAhenk/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
tncyclk authored May 28, 2024
2 parents d24b6c9 + eaeb46c commit d229510
Show file tree
Hide file tree
Showing 83 changed files with 5,872 additions and 1,799 deletions.
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
96 changes: 96 additions & 0 deletions src/main/java/tr/org/lider/LiderCronJob.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package tr.org.lider;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import tr.org.lider.entities.AgentImpl;
import tr.org.lider.entities.AgentStatus;
import tr.org.lider.message.service.IMessagingService;
import tr.org.lider.repositories.AgentRepository;
import tr.org.lider.services.ConfigurationService;
import tr.org.lider.services.TaskSchedulerService;

/**
* @author <a href="mailto:[email protected]">Ebru Arslan</a>
*/

@Component
@EnableScheduling
public class LiderCronJob {

@Autowired
private AgentRepository agentRepository;

@Autowired
private IMessagingService messagingService;

@Autowired
private ConfigurationService configurationService;

@Autowired
private TaskSchedulerService taskScheduledService;

Logger logger = LoggerFactory.getLogger(this.getClass());


@Scheduled(cron = "0 55 10 * * ?")
public void dailyCronJob() {


if(configurationService.getMachineEventStatus() == true) {
Date today = new Date();
List<AgentImpl> agentsEventDate = agentRepository.findAll();

for(AgentImpl agent : agentsEventDate) {

if(!(messagingService.isRecipientOnline(agent.getJid()))) {

Date eventDate = agent.getEventDate();
if (eventDate != null) {
LocalDate todayLocalDate = today.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
LocalDate dbEventDate = eventDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

long daysDifference = ChronoUnit.DAYS.between(todayLocalDate, dbEventDate);

if (daysDifference > -configurationService.getMachineEventDay()) {

agent.setAgentStatus(AgentStatus.Active);
agentRepository.save(agent);
}
else {
agent.setAgentStatus(AgentStatus.Passive);
agentRepository.save(agent);
}
}
else {
logger.info("Event date is null for agent: " + agent.getId());
}
}
}

logger.info("Executed cron job for machine update");
}
}

@Scheduled(cron = "0 */20 * * * ?")
public void taskJob() {
try {
taskScheduledService.sendScheduledTaskMesasage();
} catch (IndexOutOfBoundsException e) {
logger.info("Array size is not enough" + e.getMessage());
} catch (Throwable t) {
logger.info(t.getMessage());
}

}
}
80 changes: 8 additions & 72 deletions src/main/java/tr/org/lider/controllers/AgentInfoController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
Expand All @@ -18,7 +17,6 @@
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -27,6 +25,7 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import tr.org.lider.dto.AgentDTO;
import tr.org.lider.entities.AgentImpl;
import tr.org.lider.services.AgentService;
import tr.org.lider.services.ExcelExportService;
Expand Down Expand Up @@ -55,50 +54,17 @@ public class AgentInfoController {
})
})
@PostMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<HashMap<String, Object>> findAllAgents(
@RequestParam (value = "pageNumber") int pageNumber,
@RequestParam (value = "pageSize") int pageSize,
@RequestParam (value = "sessionReportType") Optional<String> sessionReportType,
@RequestParam (value = "getFilterData") Optional<Boolean> getFilterData,
@RequestParam (value = "registrationStartDate") @DateTimeFormat(pattern="dd/MM/yyyy HH:mm:ss") Optional<Date> registrationStartDate,
@RequestParam (value = "registrationEndDate") @DateTimeFormat(pattern="dd/MM/yyyy HH:mm:ss") Optional<Date> registrationEndDate,
@RequestParam (value = "status") Optional<String> status,
@RequestParam (value = "dn") Optional<String> dn,
@RequestParam (value = "hostname") Optional<String> hostname,
@RequestParam (value = "macAddress") Optional<String> macAddress,
@RequestParam (value = "ipAddress") Optional<String> ipAddress,
@RequestParam (value = "brand") Optional<String> brand,
@RequestParam (value = "model") Optional<String> model,
@RequestParam (value = "processor") Optional<String> processor,
@RequestParam (value = "osVersion") Optional<String> osVersion,
@RequestParam (value = "agentVersion") Optional<String> agentVersion,
@RequestParam (value = "diskType") Optional<String> diskType) {
public ResponseEntity<HashMap<String, Object>> findAllAgents(AgentDTO agentDTO) {
HashMap<String, Object> resultMap = new HashMap<>();
if(getFilterData.isPresent() && getFilterData.get()) {
if(agentDTO.getGetFilterData() != null) {
resultMap.put("brands", agentService.getBrands());
resultMap.put("models", agentService.getmodels());
resultMap.put("processors", agentService.getProcessors());
resultMap.put("osVersions", agentService.getOSVersions());
resultMap.put("agentVersions", agentService.getAgentVersions());
resultMap.put("diskType", agentService.getDiskType());
}
Page<AgentImpl> listOfAgents = agentService.findAllAgents(
pageNumber,
pageSize,
sessionReportType,
registrationStartDate,
registrationEndDate,
status,
dn,
hostname,
macAddress,
ipAddress,
brand,
model,
processor,
osVersion,
agentVersion,
diskType);
Page<AgentImpl> listOfAgents = agentService.findAllAgents( agentDTO );

resultMap.put("agents", listOfAgents);
return ResponseEntity
Expand Down Expand Up @@ -136,38 +102,10 @@ public ResponseEntity<AgentImpl> findAgentByIDRest(@PathVariable Long agentID)
@ApiResponse(responseCode = "400", description = "Could not create client report.",
content = @Content(schema = @Schema(implementation = String.class))) })
@PostMapping(value = "/export", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> export(
@RequestParam (value = "registrationStartDate") @DateTimeFormat(pattern="dd/MM/yyyy HH:mm:ss") Optional<Date> registrationStartDate,
@RequestParam (value = "registrationEndDate") @DateTimeFormat(pattern="dd/MM/yyyy HH:mm:ss") Optional<Date> registrationEndDate,
@RequestParam (value = "status") Optional<String> status,
@RequestParam (value = "sessionReportType") Optional<String> sessionReportType,
@RequestParam (value = "dn") Optional<String> dn,
@RequestParam (value = "hostname") Optional<String> hostname,
@RequestParam (value = "macAddress") Optional<String> macAddress,
@RequestParam (value = "ipAddress") Optional<String> ipAddress,
@RequestParam (value = "brand") Optional<String> brand,
@RequestParam (value = "model") Optional<String> model,
@RequestParam (value = "processor") Optional<String> processor,
@RequestParam (value = "osVersion") Optional<String> osVersion,
@RequestParam (value = "agentVersion") Optional<String> agentVersion,
@RequestParam (value = "diskType") Optional<String> diskType){
Page<AgentImpl> listOfAgents = agentService.findAllAgents(
1,
agentService.count().intValue(),
sessionReportType,
registrationStartDate,
registrationEndDate,
status,
dn,
hostname,
macAddress,
ipAddress,
brand,
model,
processor,
osVersion,
agentVersion,
diskType);
public ResponseEntity<?> export(AgentDTO agentDTO){
agentDTO.setPageNumber(1);
agentDTO.setPageSize(agentService.count().intValue());
Page<AgentImpl> listOfAgents = agentService.findAllAgents(agentDTO);

try {
HttpHeaders headers = new HttpHeaders();
Expand All @@ -185,7 +123,5 @@ public ResponseEntity<?> export(
.headers(headers)
.build();
}


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package tr.org.lider.controllers;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import tr.org.lider.dto.AgentDTO;
import tr.org.lider.dto.AgentSessionDTO;
import tr.org.lider.entities.AgentImpl;
import tr.org.lider.services.AgentSessionReportService;
import tr.org.lider.services.ExcelExportService;
import tr.org.lider.services.UserSessionReportService;
import tr.org.lider.utils.IUserSessionReport;

@Secured({"ROLE_ADMIN", "ROLE_USER_SESSION_REPORT" })
@RestController
@RequestMapping("/api/lider/agent-session")
@Tag(name = "Agent session service", description = "Agent Sesion Report controller")
public class AgentSessionReportController {

private final Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass());

@Autowired
private AgentSessionReportService agentSessionReportService;

@Autowired
private UserSessionReportService userSessionReportService;

@Autowired
private ExcelExportService excelService;

@Operation(summary = "Find all agents", description = "", tags = { "agent-service" })
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Find all agents",
content = { @Content(schema = @Schema(implementation = AgentImpl.class))
}),
@ApiResponse(responseCode = "417", description = "Could not retrieve agents list. Unexpected error occured.",
content = { @Content(schema = @Schema(implementation = String.class))
})
})
@PostMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<HashMap<String, Object>> findAllAgents( AgentDTO agentDTO) {
HashMap<String, Object> resultMap = new HashMap<>();
if(agentDTO.getGetFilterData().isPresent() && agentDTO.getGetFilterData().get()) {
resultMap.put("brands", agentSessionReportService.getBrands());
resultMap.put("models", agentSessionReportService.getmodels());
resultMap.put("processors", agentSessionReportService.getProcessors());
resultMap.put("osVersions", agentSessionReportService.getOSVersions());
resultMap.put("agentVersions", agentSessionReportService.getAgentVersions());
resultMap.put("diskType", agentSessionReportService.getDiskType());
}
Page<AgentImpl> listOfAgents = agentSessionReportService.findAllAgents(agentDTO);

resultMap.put("agents", listOfAgents);
return ResponseEntity
.status(HttpStatus.OK)
.body(resultMap);
}

@Operation(summary = "Find agent session detail by id.", description = "", tags = { "agent-service" })
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Find agent detail by id.",
content = { @Content(schema = @Schema(implementation = AgentImpl.class)) }),
@ApiResponse(responseCode = "404", description = "Agent id not found.Not found.",
content = @Content(schema = @Schema(implementation = String.class))) })
@PostMapping(value = "/detail", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> getAgentSessionsDetail(AgentSessionDTO agentSessionDTO) {
logger.debug("Agent id: {} ", agentSessionDTO.getAgentID());
Page<IUserSessionReport> agentSessionList = agentSessionReportService.getSessionList(agentSessionDTO);

return ResponseEntity
.status(HttpStatus.OK)
.body(agentSessionList);
}

@Operation(summary = "Exports filtered agent session list to excel", description = "", tags = { "agent-service" })
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Created excel file successfully",
content = { @Content(schema = @Schema(implementation = AgentImpl.class)) }),
@ApiResponse(responseCode = "400", description = "Could not create client report.",
content = @Content(schema = @Schema(implementation = String.class))) })
@PostMapping(value = "/export", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> export(AgentSessionDTO agentSessionDTO){
agentSessionDTO.setPageNumber(1);
agentSessionDTO.setPageSize(userSessionReportService.count().intValue());
Page<IUserSessionReport> agentSessionList = agentSessionReportService.getSessionList(agentSessionDTO);
try {
if (agentSessionList != null) {
HttpHeaders headers = new HttpHeaders();
headers.add("fileName", "Oturum Raporu_" + new SimpleDateFormat("dd_MM_yyyy_HH:mm:ss.SSS").format(new Date()) + ".xlsx");
headers.setContentType(MediaType.parseMediaType("application/csv"));
headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
byte[] excelContent = excelService.generateUserSessionReport(agentSessionList.getContent());
return new ResponseEntity<byte[]>(excelContent, headers, HttpStatus.OK);
}
else {
HttpHeaders headers = new HttpHeaders();
headers.add("message", "Invalid session type: " + agentSessionDTO.getSessionType());
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.headers(headers)
.build();
}
} catch (Exception e) {
logger.error("Error occured while creating excel report Error: ." + e.getMessage());
HttpHeaders headers = new HttpHeaders();
headers.add("message", "Error occured while creating excel report. Error: " + e.getMessage());
return ResponseEntity
.status(HttpStatus.EXPECTATION_FAILED)
.headers(headers)
.build();
}
}
}
Loading

0 comments on commit d229510

Please sign in to comment.