Skip to content

Commit 4fd32e3

Browse files
committed
REPORT-770 : Add a user interface component that enables viewing and searching logs, Add an automated task to delete these logs after a certain period of time and added Tests
1 parent 5a7f860 commit 4fd32e3

File tree

11 files changed

+253
-4
lines changed

11 files changed

+253
-4
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* This Source Code Form is subject to the terms of the Mozilla Public License,
3+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
4+
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
5+
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
6+
*
7+
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
8+
* graphic logo is a trademark of OpenMRS Inc.
9+
*/
10+
package org.openmrs.module.reporting.evaluation;
11+
12+
import java.io.File;
13+
import java.io.IOException;
14+
import java.nio.file.Path;
15+
import java.nio.file.Paths;
16+
17+
import org.junit.Assert;
18+
import org.junit.Before;
19+
import org.junit.Test;
20+
import org.openmrs.module.reporting.ReportingConstants;
21+
import org.openmrs.util.OpenmrsUtil;
22+
23+
public class DeleteOldLogsTest {
24+
25+
File baseDir = OpenmrsUtil.getDirectoryInApplicationDataDirectory(ReportingConstants.REPORT_RESULTS_DIRECTORY_NAME);
26+
27+
Path test= Paths.get(baseDir.getAbsolutePath(),"test.reportlog");
28+
File test_file = test.toFile();
29+
30+
@Before
31+
public void init() throws IOException {
32+
test_file.createNewFile();
33+
}
34+
35+
@Test
36+
public void Should_DeleteLogFlesExceedingSevendays() throws IOException {
37+
//Setting the file to exist over seven days
38+
long exceedingDays = 8 ;
39+
test_file.setLastModified(exceedingDays * (24 * 60 * 60 * 1000));
40+
41+
// asserting that the file exists befor the servlet DeleteOldLogs is called
42+
Assert.assertTrue(test_file.exists());
43+
44+
DeleteOldLogFiles delete = new DeleteOldLogFiles ();
45+
delete.deleteOldLogs();
46+
47+
//Asserting that the Log file will be deleted after seven days
48+
Assert.assertFalse(test_file.exists());
49+
}
50+
51+
@Test
52+
public void Should_NotDeleteLogFilesBeforeSevenDayPass() throws IOException {
53+
54+
// asserting that the file exists befor the servlet DeleteOldLogs is called
55+
Assert.assertTrue(test_file.exists());
56+
57+
DeleteOldLogFiles delete = new DeleteOldLogFiles ();
58+
delete.deleteOldLogs();
59+
60+
//Asserting that the Log file will Not be deleted after seven days
61+
Assert.assertTrue(test_file.exists());
62+
}
63+
64+
}
65+
66+
67+
68+
69+

api-tests/src/test/java/org/openmrs/module/reporting/report/service/ReportServiceTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,4 +521,10 @@ public void purgeReportDesignsForReportDefinition_shouldDeleteAllAssociatedRepor
521521
assertNull(rs.getReportDesignByUuid("d7a82b63-1066-4c1d-9b43-b405851fc467"));
522522
assertNull(rs.getReportDesignByUuid("e7a82b63-1066-4c1d-9b43-b405851fc467"));
523523
}
524+
525+
@Test
526+
public void shoild_returnAllReportRequests() {
527+
ReportService rs = Context.getService(ReportService.class);
528+
Assert.assertEquals(3, rs.getAllReportRequests().size());
529+
}
524530
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* This Source Code Form is subject to the terms of the Mozilla Public License,
3+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
4+
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
5+
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
6+
*
7+
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
8+
* graphic logo is a trademark of OpenMRS Inc.
9+
*/
10+
11+
package org.openmrs.module.reporting.evaluation;
12+
import java.io.File;
13+
import java.util.Date;
14+
15+
import javax.servlet.ServletException;
16+
import javax.servlet.http.HttpServlet;
17+
18+
import org.apache.commons.io.FilenameUtils;
19+
import org.apache.commons.lang3.StringUtils;
20+
import org.apache.commons.logging.Log;
21+
import org.apache.commons.logging.LogFactory;
22+
import org.openmrs.module.reporting.ReportingConstants;
23+
import org.openmrs.util.OpenmrsUtil;
24+
25+
26+
27+
public class DeleteOldLogFiles extends HttpServlet {
28+
29+
private static final long serialVersionUID = 1L;
30+
// Servlet to delete old Logs on module startup
31+
protected static final Log log = LogFactory.getLog(DeleteOldLogFiles.class);
32+
33+
34+
public void deleteOldLogs(){
35+
//maximum number of days to keep log files is 7
36+
long alloweddays = 7;
37+
File baseDir = OpenmrsUtil.getDirectoryInApplicationDataDirectory(ReportingConstants.REPORT_RESULTS_DIRECTORY_NAME);
38+
File[] fList = baseDir.listFiles();
39+
40+
if (fList != null){
41+
for (File file : fList){
42+
String fileExt = FilenameUtils.getExtension(file.toString());
43+
if (file.isFile() && StringUtils.equals(fileExt, "reportlog")){
44+
45+
long diff = new Date().getTime() - file.lastModified();
46+
long maximumTime = (alloweddays * (24 * 60 * 60 * 1000));
47+
48+
if (diff > maximumTime) {
49+
file.delete();
50+
}
51+
52+
}
53+
}
54+
55+
}
56+
}
57+
58+
@Override
59+
public void init() throws ServletException {
60+
deleteOldLogs();
61+
}
62+
63+
}
64+
65+

api/src/main/java/org/openmrs/module/reporting/report/service/ReportService.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ public interface ReportService extends OpenmrsService {
133133
@Transactional(readOnly = true)
134134
public List<ReportRequest> getReportRequests(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Status...statuses);
135135

136+
/**
137+
* @return all {@link ReportRequest} in the system
138+
* @should retrieve all report requests
139+
*/
140+
@Transactional(readOnly = true)
141+
public List<ReportRequest> getAllReportRequests();
142+
143+
136144
/**
137145
* @return all {@link ReportRequest} in the system that match the passed parameters
138146
* @should retrieve report requests by definition

api/src/main/java/org/openmrs/module/reporting/report/service/ReportServiceImpl.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,15 @@ public List<ReportRequest> getReportRequests(ReportDefinition reportDefinition,
223223
return reportDAO.getReportRequests(reportDefinition, requestOnOrAfter, requestOnOrBefore, mostRecentNum, statuses);
224224
}
225225

226+
227+
/**
228+
* @see ReportService#getReportRequests()
229+
*/
230+
@Transactional(readOnly=true)
231+
public List<ReportRequest> getAllReportRequests(){
232+
return reportDAO.getAllReportRequests();
233+
}
234+
226235
/**
227236
* @see ReportService#purgeReportRequest(ReportRequest)
228237
*/

api/src/main/java/org/openmrs/module/reporting/report/service/db/HibernateReportDAO.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,17 @@ public List<String> getReportRequestUuids(String reportDefinitionUuid) {
255255
query.setString("uuid", reportDefinitionUuid);
256256
return query.list();
257257
}
258+
259+
/**
260+
* @see ReportDAO#getReportAllRequests()
261+
*/
262+
public List<ReportRequest> getAllReportRequests(){
263+
String hql = "from ReportRequest";
264+
Query query = sessionFactory.getCurrentSession().createQuery(hql);
265+
return query.list();
266+
267+
}
268+
258269

259270
//***** PROPERTY ACCESS *****
260271

api/src/main/java/org/openmrs/module/reporting/report/service/db/ReportDAO.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ public List<ReportDesign> getReportDesigns(ReportDefinition reportDefinition, Cl
118118
*/
119119
public ReportRequest getReportRequestByUuid(String uuid);
120120

121+
/**
122+
* @returns all the {@link ReportRequestst}
123+
*/
124+
public List<ReportRequest> getAllReportRequests();
125+
121126
/**
122127
* @return all {@link ReportRequest} in the system that match the passed parameters
123128
*/

api/src/main/resources/messages.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ reporting.status.SCHEDULE_COMPLETED=Schedule Completed
9797
reporting.status.SAVED=Completed and Saved
9898
reporting.status.FAILED=Failed
9999
reporting.viewReportLog=View Log
100+
reporting.searchReportLog= view and search all logs
101+
reporting.viewLogs= All logs
100102

101103
reporting.viewError=View Error
102104
reporting.viewReport=View Report

omod/src/main/java/org/openmrs/module/reporting/web/reports/ReportHistoryController.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ public String loadReportStatus(ModelMap model, @RequestParam("uuid") String uuid
157157
return "/module/reporting/json";
158158
}
159159

160+
160161
@RequestMapping("/module/reporting/reports/viewErrorDetails")
161162
public void viewErrorDetails(HttpServletResponse response, @RequestParam("uuid") String uuid) throws IOException {
162163
ReportRequest rr = Context.getService(ReportService.class).getReportRequestByUuid(uuid);
@@ -187,7 +188,21 @@ public String openFromHistory(@RequestParam("uuid") String uuid, HttpServletResp
187188
req.setRenderingMode(mode);
188189
}
189190
}
191+
192+
List<ReportRequest> requests = rs.getAllReportRequests();
193+
194+
List <String> allLogs = new ArrayList<String>();
195+
for(ReportRequest reqst: requests) {
196+
try {
197+
List<String> logs = rs.loadReportLog(reqst);
198+
allLogs.addAll(logs);
199+
}catch(NullPointerException e) {
200+
}
201+
202+
}
203+
190204
model.addAttribute("request", req);
205+
model.addAttribute("allLogs", allLogs);
191206

192207
if (req.getStatus() == Status.REQUESTED) {
193208
model.addAttribute("positionInQueue", rs.getPositionInQueue(req));

omod/src/main/resources/config.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,15 @@
168168
ReportRequest.hbm.xml
169169
</mappingFiles>
170170

171+
172+
<!-- Servlets -->
173+
<servlet>
174+
<servlet-name>DeleteOldLogFiles</servlet-name>
175+
<servlet-class>@[email protected]</servlet-class>
176+
<load-on-startup>1</load-on-startup>
177+
</servlet>
178+
<!-- /Servlets -->
179+
171180
<conditionalResources>
172181
<conditionalResource>
173182
<path>/lib/reporting-api-1.9.*</path>

0 commit comments

Comments
 (0)