From 3eadef7ea5574d983c32b069bb26e89a14f750e8 Mon Sep 17 00:00:00 2001 From: Anne Marsan Date: Mon, 25 Nov 2024 13:59:56 -0500 Subject: [PATCH] Add endpoint for clearing individual source caches --- .../service/AchillesCacheService.java | 3 ++- .../cdmresults/service/CDMCacheService.java | 3 ++- .../model/SourcePermissionSchema.java | 1 + .../webapi/service/CDMResultsService.java | 19 +++++++++++++++++++ ...41113115700__add_clearcache_permission.sql | 17 ++++++++++++++++- 5 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/ohdsi/webapi/achilles/service/AchillesCacheService.java b/src/main/java/org/ohdsi/webapi/achilles/service/AchillesCacheService.java index 4502dd07fb..023f6d7d69 100644 --- a/src/main/java/org/ohdsi/webapi/achilles/service/AchillesCacheService.java +++ b/src/main/java/org/ohdsi/webapi/achilles/service/AchillesCacheService.java @@ -102,7 +102,8 @@ public void clearCache() { sources.stream().forEach(this::clearCache); } - private void clearCache(Source source) { + @Transactional() + public void clearCache(Source source) { if (sourceAccessor.hasAccess(source)) { cacheRepository.deleteBySource(source); } diff --git a/src/main/java/org/ohdsi/webapi/cdmresults/service/CDMCacheService.java b/src/main/java/org/ohdsi/webapi/cdmresults/service/CDMCacheService.java index 14855bc5f4..8b7a6e520b 100644 --- a/src/main/java/org/ohdsi/webapi/cdmresults/service/CDMCacheService.java +++ b/src/main/java/org/ohdsi/webapi/cdmresults/service/CDMCacheService.java @@ -107,7 +107,8 @@ public void clearCache() { sources.stream().forEach(this::clearCache); } - private void clearCache(Source source) { + @Transactional() + public void clearCache(Source source) { if (sourceAccessor.hasAccess(source)) { cdmCacheRepository.deleteBySource(source.getSourceId()); } diff --git a/src/main/java/org/ohdsi/webapi/security/model/SourcePermissionSchema.java b/src/main/java/org/ohdsi/webapi/security/model/SourcePermissionSchema.java index c510776be8..ae457d252f 100644 --- a/src/main/java/org/ohdsi/webapi/security/model/SourcePermissionSchema.java +++ b/src/main/java/org/ohdsi/webapi/security/model/SourcePermissionSchema.java @@ -33,6 +33,7 @@ public class SourcePermissionSchema extends EntityPermissionSchema { put("cdmresults:%s:*:get", "Get Achilles reports on Source with SourceKey = %s"); put("cdmresults:%s:conceptRecordCount:post", "Get Achilles concept counts on Source with SourceKey = %s"); put("cdmresults:%s:*:*:get", "Get Achilles reports details on Source with SourceKey = %s"); + put("cdmresults:%s:clearcache:post", "Clear the Achilles and CDM results caches on Source with SourceKey = %s"); put("cohortresults:%s:*:*:get", "Get cohort results on Source with SourceKey = %s"); put("cohortresults:%s:*:*:*:get", "Get cohort results details on Source with SourceKey = %s"); put("cohortresults:%s:*:healthcareutilization:*:*:get", "Get cohort results baseline on period for Source with SourceKey = %s"); diff --git a/src/main/java/org/ohdsi/webapi/service/CDMResultsService.java b/src/main/java/org/ohdsi/webapi/service/CDMResultsService.java index f44353a017..c887962d1c 100644 --- a/src/main/java/org/ohdsi/webapi/service/CDMResultsService.java +++ b/src/main/java/org/ohdsi/webapi/service/CDMResultsService.java @@ -284,6 +284,25 @@ public JobExecutionResource refreshCache(@PathParam("sourceKey") final String so } return new JobExecutionResource(); } + + /** + * Clear the cdm_cache and achilles_cache for all sources + * + * @summary Clear the cdm_cache and achilles_cache for all sources + * @return void + * @throws ForbiddenException if the user is not an admin + */ + @POST + @Path("{sourceKey}/clearCache") + @Transactional() + public void clearCacheForSource(@PathParam("sourceKey") final String sourceKey) { + if (!isSecured() || !isAdmin()) { + throw new ForbiddenException(); + } + Source source = getSourceRepository().findBySourceKey(sourceKey); + cacheService.clearCache(source); + cdmCacheService.clearCache(source); + } /** * Clear the cdm_cache and achilles_cache for all sources diff --git a/src/main/resources/db/migration/postgresql/V2.15.0.20241113115700__add_clearcache_permission.sql b/src/main/resources/db/migration/postgresql/V2.15.0.20241113115700__add_clearcache_permission.sql index b2ba6fc697..080e455933 100644 --- a/src/main/resources/db/migration/postgresql/V2.15.0.20241113115700__add_clearcache_permission.sql +++ b/src/main/resources/db/migration/postgresql/V2.15.0.20241113115700__add_clearcache_permission.sql @@ -1,7 +1,7 @@ INSERT INTO ${ohdsiSchema}.sec_permission (id, value, description) SELECT nextval('${ohdsiSchema}.sec_permission_id_seq'), 'cdmresults:clearcache:post', - 'Clear the achilles and cdm results caches'; + 'Clear the Achilles and CDM results caches'; INSERT INTO ${ohdsiSchema}.sec_role_permission (role_id, permission_id) SELECT sr.id, sp.id @@ -12,3 +12,18 @@ WHERE sp."value" in 'cdmresults:clearcache:post' ) AND sr.name IN ('admin'); + +INSERT INTO ${ohdsiSchema}.sec_permission (id, value, description) +SELECT nextval('${ohdsiSchema}.sec_permission_id_seq'), + 'cdmresults:*:clearcache:post', + 'Clear the Achilles and CDM results caches'; + +INSERT INTO ${ohdsiSchema}.sec_role_permission (role_id, permission_id) +SELECT sr.id, sp.id +FROM ${ohdsiSchema}.sec_permission sp, + ${ohdsiSchema}.sec_role sr +WHERE sp."value" in + ( + 'cdmresults:*:clearcache:post' + ) + AND sr.name IN ('admin');