diff --git a/sql/init_data.h2.sql b/sql/init_data.h2.sql index c2d3e4c52680f0bbb85187f7047d801588dd618d..c89864532000e5c3071d864534c454efa80ad4bc 100644 --- a/sql/init_data.h2.sql +++ b/sql/init_data.h2.sql @@ -123,3 +123,7 @@ INSERT INTO normalvalue (indicator, cell, doy, medianvalue, q5, q95) INSERT INTO simulation (date, simulationid, started, ended) VALUES ('2024-02-19', 1, '2024-02-20 12:00:00', '2024-02-20 12:30:00'), ('2024-02-20', 2, '2024-02-21 13:00:00', NULL); + +-- +REFRESH MATERIALIZED VIEW v_pra_dailyvalue; + diff --git a/sql/migration.sql b/sql/migration.sql index 28523590aa79a3a2e37ccc761f0ede5d77513ffe..e3e2b7c1b9c88fdc1563302f7613d3a114ec48fc 100644 --- a/sql/migration.sql +++ b/sql/migration.sql @@ -208,14 +208,6 @@ BEGIN ALTER TABLE indicator DROP CONSTRAINT "FK_indicator_i18n_description"; ALTER TABLE indicator ADD CONSTRAINT "FK_indicator_i18n_description" FOREIGN KEY (description) REFERENCES i18nkey (id); CREATE INDEX "IX_dailyvalue_doy" ON dailyvalue (EXTRACT(DOY FROM date), indicator); - UPDATE i18n SET translation=REPLACE(translation, ' (tmax', ' (Tmax') - RETURN true; -END -$BODY$ -language plpgsql; - -CREATE OR REPLACE FUNCTION upgrade20240426() RETURNS boolean AS $BODY$ -BEGIN CREATE INDEX IF NOT EXISTS "IX_cellpra" ON cellpra (cell,pra); UPDATE i18n SET translation=REPLACE(translation, 'tmax', 'Tmax'); RETURN true; @@ -223,10 +215,12 @@ END $BODY$ language plpgsql; -CREATE OR REPLACE FUNCTION upgrade20240426() RETURNS boolean AS $BODY$ +-- +-- #87 +-- +CREATE OR REPLACE FUNCTION upgrade20240507() RETURNS boolean AS $BODY$ BEGIN - CREATE INDEX IF NOT EXISTS "IX_cellpra" ON cellpra (cell,pra); - UPDATE i18n SET translation=REPLACE(translation, 'tmax', 'Tmax'); + DROP VIEW IF EXISTS v_pra_dailyvalue CASCADE; RETURN true; END $BODY$ diff --git a/sql/schema.tables.sql b/sql/schema.tables.sql index 03ddaca73fdd2e4363d8041eb168aa9daefdfdbb..77eff33edcc7ade9a4851183ae2f56ac837bca37 100644 --- a/sql/schema.tables.sql +++ b/sql/schema.tables.sql @@ -221,8 +221,7 @@ AS SELECT l.languagetag, FROM i18n i JOIN locale l ON l.id = i.locale; -DROP VIEW IF EXISTS v_pra_dailyvalue; -CREATE VIEW v_pra_dailyvalue AS +CREATE MATERIALIZED VIEW IF NOT EXISTS v_pra_dailyvalue AS SELECT MIN(d.id) AS id, d.indicator, @@ -237,4 +236,4 @@ FROM dailyvalue AS d JOIN cellpra AS cp ON cp.cell=d.cell LEFT JOIN normalvalue AS n ON n.cell=d.cell AND n.doy=EXTRACT(DOY FROM d.date) AND n.indicator=d.indicator GROUP BY pra, d.indicator, date; -COMMENT ON VIEW v_pra_dailyvalue IS 'Daily values weighted by PRA.' +COMMENT ON VIEW v_pra_dailyvalue IS 'Daily values weighted by PRA.'; diff --git a/www-server/src/main/java/fr/agrometinfo/www/server/rs/ApplicationResource.java b/www-server/src/main/java/fr/agrometinfo/www/server/rs/ApplicationResource.java index 8a7b184e3ff956b1718537e2854a12b8a95c4c6e..4b13b1ae25e591209d2766977247b1bf05e68523 100644 --- a/www-server/src/main/java/fr/agrometinfo/www/server/rs/ApplicationResource.java +++ b/www-server/src/main/java/fr/agrometinfo/www/server/rs/ApplicationResource.java @@ -8,6 +8,7 @@ import fr.agrometinfo.www.server.AgroMetInfoConfiguration.ConfigurationKey; import fr.agrometinfo.www.server.dao.DailyVisitDao; import fr.agrometinfo.www.server.exception.AgroMetInfoException; import fr.agrometinfo.www.server.model.DailyVisit; +import fr.agrometinfo.www.server.service.CacheService; import fr.agrometinfo.www.server.service.MailService; import fr.agrometinfo.www.server.service.MailService.Mail; import fr.agrometinfo.www.server.util.AppVersion; @@ -68,6 +69,19 @@ public class ApplicationResource implements ApplicationService { */ private Date applicationDate; + /** + * Cache service for server-side and browser-side. + */ + @Inject + private CacheService cacheService; + + @GET + @Path(ApplicationService.PATH_EMPTY_CACHE) + @Override + public void emptyCache() { + cacheService.emptyCacheDirectory(); + } + @GET @Path(ApplicationService.PATH_APPLICATION_DATE) @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) diff --git a/www-server/src/main/java/fr/agrometinfo/www/server/service/CacheService.java b/www-server/src/main/java/fr/agrometinfo/www/server/service/CacheService.java index f34521eb6c75df6e3ffa2decf9ab50fbed8b0c71..568c9d67d66a84335fd653959488a0fc7de95769 100644 --- a/www-server/src/main/java/fr/agrometinfo/www/server/service/CacheService.java +++ b/www-server/src/main/java/fr/agrometinfo/www/server/service/CacheService.java @@ -4,6 +4,8 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; import java.time.LocalDateTime; import java.time.temporal.ChronoUnit; @@ -80,6 +82,17 @@ public class CacheService { @Setter private int nbOfDays = 1; + /** + * Remove all files in the cache directory. + */ + public void emptyCacheDirectory() { + try (var dirStream = Files.walk(Paths.get(cacheDirectory))) { + dirStream.filter(Files::isRegularFile).map(Path::toFile).forEach(File::delete); + } catch (final IOException e) { + LOGGER.error("Strange, looping on {} should not fail!", e); + } + } + /** * @return object from cache or null. */ diff --git a/www-shared/src/main/java/fr/agrometinfo/www/shared/service/ApplicationService.java b/www-shared/src/main/java/fr/agrometinfo/www/shared/service/ApplicationService.java index 4d3facdccfbc24fce3ded03b8471b20721ab5bec..695df5e1d4826db9671887e709f5e6947ce738a9 100644 --- a/www-shared/src/main/java/fr/agrometinfo/www/shared/service/ApplicationService.java +++ b/www-shared/src/main/java/fr/agrometinfo/www/shared/service/ApplicationService.java @@ -34,6 +34,18 @@ public interface ApplicationService { */ String PATH_SEND_EMAIL = "send"; + /** + * Path for {@link ApplicationService#emptyCache()}. + */ + String PATH_EMPTY_CACHE = "empty_cache"; + + /** + * Remove all files in the cache directory. + */ + @GET + @Path(PATH_EMPTY_CACHE) + void emptyCache(); + /** * @return date of application build */