Skip to content

Commit

Permalink
REPORT-770 : Add a user interface component that enables viewing and …
Browse files Browse the repository at this point in the history
…searching logs, Add an automated task to delete these logs after a certain period of time and added Tests
  • Loading branch information
mozzy11 committed Apr 4, 2019
1 parent 5a7f860 commit 4fd32e3
Show file tree
Hide file tree
Showing 11 changed files with 253 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.module.reporting.evaluation;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openmrs.module.reporting.ReportingConstants;
import org.openmrs.util.OpenmrsUtil;

public class DeleteOldLogsTest {

File baseDir = OpenmrsUtil.getDirectoryInApplicationDataDirectory(ReportingConstants.REPORT_RESULTS_DIRECTORY_NAME);

Path test= Paths.get(baseDir.getAbsolutePath(),"test.reportlog");
File test_file = test.toFile();

@Before
public void init() throws IOException {
test_file.createNewFile();
}

@Test
public void Should_DeleteLogFlesExceedingSevendays() throws IOException {
//Setting the file to exist over seven days
long exceedingDays = 8 ;
test_file.setLastModified(exceedingDays * (24 * 60 * 60 * 1000));

// asserting that the file exists befor the servlet DeleteOldLogs is called
Assert.assertTrue(test_file.exists());

DeleteOldLogFiles delete = new DeleteOldLogFiles ();
delete.deleteOldLogs();

//Asserting that the Log file will be deleted after seven days
Assert.assertFalse(test_file.exists());
}

@Test
public void Should_NotDeleteLogFilesBeforeSevenDayPass() throws IOException {

// asserting that the file exists befor the servlet DeleteOldLogs is called
Assert.assertTrue(test_file.exists());

DeleteOldLogFiles delete = new DeleteOldLogFiles ();
delete.deleteOldLogs();

//Asserting that the Log file will Not be deleted after seven days
Assert.assertTrue(test_file.exists());
}

}





Original file line number Diff line number Diff line change
Expand Up @@ -521,4 +521,10 @@ public void purgeReportDesignsForReportDefinition_shouldDeleteAllAssociatedRepor
assertNull(rs.getReportDesignByUuid("d7a82b63-1066-4c1d-9b43-b405851fc467"));
assertNull(rs.getReportDesignByUuid("e7a82b63-1066-4c1d-9b43-b405851fc467"));
}

@Test
public void shoild_returnAllReportRequests() {
ReportService rs = Context.getService(ReportService.class);
Assert.assertEquals(3, rs.getAllReportRequests().size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/

package org.openmrs.module.reporting.evaluation;
import java.io.File;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.module.reporting.ReportingConstants;
import org.openmrs.util.OpenmrsUtil;



public class DeleteOldLogFiles extends HttpServlet {

private static final long serialVersionUID = 1L;
// Servlet to delete old Logs on module startup
protected static final Log log = LogFactory.getLog(DeleteOldLogFiles.class);


public void deleteOldLogs(){
//maximum number of days to keep log files is 7
long alloweddays = 7;
File baseDir = OpenmrsUtil.getDirectoryInApplicationDataDirectory(ReportingConstants.REPORT_RESULTS_DIRECTORY_NAME);
File[] fList = baseDir.listFiles();

if (fList != null){
for (File file : fList){
String fileExt = FilenameUtils.getExtension(file.toString());
if (file.isFile() && StringUtils.equals(fileExt, "reportlog")){

long diff = new Date().getTime() - file.lastModified();
long maximumTime = (alloweddays * (24 * 60 * 60 * 1000));

if (diff > maximumTime) {
file.delete();
}

}
}

}
}

@Override
public void init() throws ServletException {
deleteOldLogs();
}

}


Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ public interface ReportService extends OpenmrsService {
@Transactional(readOnly = true)
public List<ReportRequest> getReportRequests(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Status...statuses);

/**
* @return all {@link ReportRequest} in the system
* @should retrieve all report requests
*/
@Transactional(readOnly = true)
public List<ReportRequest> getAllReportRequests();


/**
* @return all {@link ReportRequest} in the system that match the passed parameters
* @should retrieve report requests by definition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,15 @@ public List<ReportRequest> getReportRequests(ReportDefinition reportDefinition,
return reportDAO.getReportRequests(reportDefinition, requestOnOrAfter, requestOnOrBefore, mostRecentNum, statuses);
}


/**
* @see ReportService#getReportRequests()
*/
@Transactional(readOnly=true)
public List<ReportRequest> getAllReportRequests(){
return reportDAO.getAllReportRequests();
}

/**
* @see ReportService#purgeReportRequest(ReportRequest)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,17 @@ public List<String> getReportRequestUuids(String reportDefinitionUuid) {
query.setString("uuid", reportDefinitionUuid);
return query.list();
}

/**
* @see ReportDAO#getReportAllRequests()
*/
public List<ReportRequest> getAllReportRequests(){
String hql = "from ReportRequest";
Query query = sessionFactory.getCurrentSession().createQuery(hql);
return query.list();

}


//***** PROPERTY ACCESS *****

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ public List<ReportDesign> getReportDesigns(ReportDefinition reportDefinition, Cl
*/
public ReportRequest getReportRequestByUuid(String uuid);

/**
* @returns all the {@link ReportRequestst}
*/
public List<ReportRequest> getAllReportRequests();

/**
* @return all {@link ReportRequest} in the system that match the passed parameters
*/
Expand Down
2 changes: 2 additions & 0 deletions api/src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ reporting.status.SCHEDULE_COMPLETED=Schedule Completed
reporting.status.SAVED=Completed and Saved
reporting.status.FAILED=Failed
reporting.viewReportLog=View Log
reporting.searchReportLog= view and search all logs
reporting.viewLogs= All logs

reporting.viewError=View Error
reporting.viewReport=View Report
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ public String loadReportStatus(ModelMap model, @RequestParam("uuid") String uuid
return "/module/reporting/json";
}


@RequestMapping("/module/reporting/reports/viewErrorDetails")
public void viewErrorDetails(HttpServletResponse response, @RequestParam("uuid") String uuid) throws IOException {
ReportRequest rr = Context.getService(ReportService.class).getReportRequestByUuid(uuid);
Expand Down Expand Up @@ -187,7 +188,21 @@ public String openFromHistory(@RequestParam("uuid") String uuid, HttpServletResp
req.setRenderingMode(mode);
}
}

List<ReportRequest> requests = rs.getAllReportRequests();

List <String> allLogs = new ArrayList<String>();
for(ReportRequest reqst: requests) {
try {
List<String> logs = rs.loadReportLog(reqst);
allLogs.addAll(logs);
}catch(NullPointerException e) {
}

}

model.addAttribute("request", req);
model.addAttribute("allLogs", allLogs);

if (req.getStatus() == Status.REQUESTED) {
model.addAttribute("positionInQueue", rs.getPositionInQueue(req));
Expand Down
9 changes: 9 additions & 0 deletions omod/src/main/resources/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,15 @@
ReportRequest.hbm.xml
</mappingFiles>


<!-- Servlets -->
<servlet>
<servlet-name>DeleteOldLogFiles</servlet-name>
<servlet-class>@[email protected]</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- /Servlets -->

<conditionalResources>
<conditionalResource>
<path>/lib/reporting-api-1.9.*</path>
Expand Down
58 changes: 54 additions & 4 deletions omod/src/main/webapp/reports/reportHistoryOpen.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
});
}
function deleteRequest(uuid) {
if (confirm('<spring:message code="reporting.reportHistory.confirmDelete"/>')) {
document.location.href='${pageContext.request.contextPath}/module/reporting/reports/deleteReportRequest.form?uuid='+uuid+'&returnUrl=/module/reporting/dashboard/index.form';
Expand All @@ -37,8 +38,7 @@
$j(document).ready(function() {
loadReportStatus();
loadReportStatus();
$j("#errorDetailsLink").click(function(event) {
showReportingDialog({
title: '<spring:message code="reporting.errorDetails"/>',
Expand All @@ -47,9 +47,26 @@
});
$j(".logDiv").hide();
$j(".logsDiv").hide();
$j("#viewReportLogLink").click(function(event) {
$j(".logDiv").toggle();
});
$j("#searchReportLogLink").click(function(event) {
$j(".logsDiv").toggle();
});
$j(".reporting-data-table").dataTable( {
"bPaginate": true,
"iDisplayLength": 15,
"bLengthChange": false,
"bFilter": true,
"bSort": true,
"bInfo": true,
"bAutoWidth": false
} );
$j(".reporting-data-table").css({"table-layout":"fixed","width":"1100px"});
} );
</script>

Expand Down Expand Up @@ -152,12 +169,45 @@
</fieldset>
</c:if>
<div class="reportAction">
<a href="#" id="viewReportLogLink">
<span>

<a style="margin-right: 100px" href="#" id="viewReportLogLink">
<img src="<c:url value="/images/info.gif"/>" border="0" style="vertical-align:middle"/>
<spring:message code="reporting.viewReportLog"/>
</a>
<div class="logDiv"></div>


<a style="margin-right: 20px" href="#" id="searchReportLogLink">
<img src="<c:url value="/images/info.gif"/>" border="0" style="vertical-align:middle"/>
<spring:message code="reporting.searchReportLog"/>
</a>

</span>
<span> <div class="logDiv"></div>

<div class="logsDiv">

<table id = "logsAll" class="reporting-data-table display">
<thead>
<tr>
<th> <spring:message code="reporting.viewLogs"/></th>
</tr>
</thead>
<tbody>
<c:forEach var="Logs" items="${allLogs}">
<tr>
<td>
<c:out value = "${Logs}" />
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</span>
</div>


</td>
<td valign="top" style="width:50%;">
<fieldset>
Expand Down

0 comments on commit 4fd32e3

Please sign in to comment.