Skip to content

Commit

Permalink
Merge pull request #791 from AtlasOfLivingAustralia/Feature/#728
Browse files Browse the repository at this point in the history
Feature/#728
  • Loading branch information
schoicsiro authored Oct 18, 2023
2 parents 0103d97 + 53bfa1f commit b9034fb
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ profileEditor.controller('ALAAdminController', function ($http, util, messageSer
};

self.tags = [];
self.cacheRegions = [];

loadBackupFileList();
loadOpusList();
loadPendingJobs();
loadTags();
cacheManagement();

self.reloadHelpUrls = function() {
var promise = $http.post(util.contextRoot() + "/admin/reloadHelpUrls");
Expand Down Expand Up @@ -225,4 +227,28 @@ profileEditor.controller('ALAAdminController', function ($http, util, messageSer
self.loadingTags = false;
});
}

function cacheManagement() {
self.loadingCacheManagement = true;
var promise = $http.get(util.contextRoot() + "/admin/cacheManagement");
promise.then(function (response) {
self.cacheRegions = response.data || [];
self.loadingCacheManagement = false;
}, function() {
self.loadingCacheManagement = false;
});
}

self.clearCache = function (cache) {
var promise = $http.get(util.contextRoot() + "/admin/clearCache/" + cache);
promise.then(function(response) {
if (response.data.error) {
messageService.alert(response.data.error);
} else {
messageService.success(response.data.resp);
}
}, function() {
messageService.alert("Failed to clear cache the job");
});
}
});
3 changes: 3 additions & 0 deletions grails-app/conf/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ grails:
profile: web
codegen:
defaultPackage: au.org.ala.profile.hub
cache:
ehcache:
ehcacheXmlLocation: 'classpath:profile-ehcache.xml'
gorm:
reactor:
# Whether to translate GORM events into Reactor events
Expand Down
43 changes: 43 additions & 0 deletions grails-app/conf/profile-ehcache.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns='http://www.ehcache.org/v3'
xmlns:jsr107='http://www.ehcache.org/v3/jsr107'>

<persistence directory="/data/profile-hub/ehcache"/>

<cache alias="userDetailsCache">
<expiry>
<ttl unit="days">1</ttl>
</expiry>
<resources>
<heap unit="entries">2000</heap>
</resources>
</cache>

<cache alias="userDetailsByIdCache">
<expiry>
<ttl unit="days">1</ttl>
</expiry>
<resources>
<heap unit="entries">2000</heap>
</resources>
</cache>

<cache alias="userListCache">
<expiry>
<ttl unit="days">1</ttl>
</expiry>
<resources>
<heap unit="entries">2000</heap>
</resources>
</cache>

<cache alias="vocabListCache">
<expiry>
<ttl unit="days">1</ttl>
</expiry>
<resources>
<heap unit="entries">20000</heap>
</resources>
</cache>

</config>
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ package au.org.ala.profile.hub
import au.org.ala.profile.security.Role
import au.org.ala.profile.security.Secured
import au.org.ala.ws.service.WebService
import grails.converters.JSON
import grails.util.Environment
import org.apache.http.HttpStatus
import org.apache.http.entity.ContentType
import org.springframework.core.io.support.PathMatchingResourcePatternResolver
import org.springframework.cache.Cache

import javax.validation.constraints.NotNull
import static groovy.io.FileType.DIRECTORIES
import org.grails.plugin.cache.GrailsCacheManager
class AdminController extends BaseController {

WebService webService
ProfileService profileService

GrailsCacheManager grailsCacheManager
@Secured(role = Role.ROLE_ADMIN, opusSpecific = false)
def index() {
render view: "admin.gsp"
Expand Down Expand Up @@ -120,4 +120,24 @@ class AdminController extends BaseController {
return grailsApplication.config.backupRestoreDir?: '/data/profile-service/backup/db'
}

@Secured(role = Role.ROLE_ADMIN, opusSpecific = false)
def cacheManagement() {
def cacheNames = grailsCacheManager.getCacheNames()
success cacheNames
}

@Secured(role = Role.ROLE_ADMIN, opusSpecific = false)
def clearCache() {
Map result = [:]
if (params.id) {
Cache cache = grailsCacheManager.getCache(params.id)
cache.clear()
result.resp = "Successfully cleared cache - " + params.id
result.statusCode = HttpStatus.SC_OK
success result
} else {
var message = "Failed to clear cache the job"
sendError(HttpStatus.SC_BAD_REQUEST, message ?:"");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ class UrlMappings {
"/admin/job/$jobType/$jobId" controller: "admin", action: [DELETE: "deleteJob"]
"/admin/job/" controller: "admin", action: [GET: "listPendingJobs"]
"/admin/tag/$tagId?" controller: "admin", action: [GET: "getTag", PUT: "createTag", POST: "updateTag", DELETE: "deleteTag"]
"/admin/cacheManagement" controller: "admin", action: [GET: "cacheManagement"]
"/admin/clearCache/$id" controller: "admin", action: [GET: "clearCache"]
"/admin/reloadHelpUrls" controller: "admin", action: [POST: "reloadHelpUrls"]
"/admin" controller: "admin", action: [GET: "index"]
"/alaAdmin/index" controller: "admin", action: [GET: "alaIndex"]
Expand Down
19 changes: 19 additions & 0 deletions grails-app/views/admin/admin.gsp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,25 @@
<button class="btn btn-primary" ng-click="adminCtrl.restoreCollections()">Restore collection</button><br><br>
</div>

<div class="panel panel-body">
<h4>Caches Management</h4>
<div class="table-responsive" ng-show="adminCtrl.cacheRegions.length > 0">
<table class="table table-striped">
<thead>
<tr>
<th>Cache Name</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="cache in adminCtrl.cacheRegions">
<td>{{ cache }}</td>
<td><button class="btn btn-sm btn-danger" ng-click="adminCtrl.clearCache(cache)"><i class="fa fa-times"></i>Clear</button></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</body>
Expand Down
49 changes: 49 additions & 0 deletions src/test/groovy/au/org/ala/profile/hub/AdminControllerSpec.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package au.org.ala.profile.hub

import grails.testing.web.controllers.ControllerUnitTest
import org.apache.http.HttpStatus
import org.grails.plugin.cache.GrailsCacheManager
import org.springframework.cache.Cache
import spock.lang.Specification

class AdminControllerSpec extends Specification implements ControllerUnitTest<AdminController> {

def setup() {
controller.grailsCacheManager = Mock(GrailsCacheManager)
}

def "cache management should display to the admin controller when admin mode"() {
when:
controller.grailsCacheManager.getCacheNames() >> ["Cache1","Cache2"]
controller.cacheManagement()

then:
def cacheNames = '["Cache1","Cache2"]'

assert response.getContentAsString() == cacheNames
}

def "clearCache() should return a 200 (OK_REQUEST) if id has been provided"() {
when:
params.id = "userDetailsCache"

Cache cache = Mock(Cache.class);
controller.grailsCacheManager.getCache(params.id) >> cache
controller.clearCache()

then:
response.status == HttpStatus.SC_OK
def successMessage = '{"resp":"Successfully cleared cache - userDetailsCache","statusCode":200}'
assert response.getContentAsString() == successMessage
}

def "clearCache() should return a 400 (BAD_REQUEST) if id has not been provided"() {
when:
params.id = ""
controller.clearCache()

then:
response.status != HttpStatus.SC_OK
assert response.errorMessage == "Failed to clear cache the job"
}
}

0 comments on commit b9034fb

Please sign in to comment.