From 1420653dec4158c51e6431c9d314988120c56aff Mon Sep 17 00:00:00 2001
From: Fabian Schindler <fabian.schindler.strauss@gmail.com>
Date: Sun, 29 Mar 2020 13:55:34 +0200
Subject: [PATCH] Creat database views for mapcache items Allows smaller
 queries in tilesets

---
 cache/Dockerfile              |  1 +
 cache/configure.sh            |  5 +++--
 cache/install.sh              |  2 +-
 cache/mapcache_items_view.sql | 14 ++++++++++++++
 4 files changed, 19 insertions(+), 3 deletions(-)
 create mode 100644 cache/mapcache_items_view.sql

diff --git a/cache/Dockerfile b/cache/Dockerfile
index 5bbfe5b9..f415ab13 100644
--- a/cache/Dockerfile
+++ b/cache/Dockerfile
@@ -64,6 +64,7 @@ ADD configure.sh \
     seeder.py \
     mapcache-template.xml \
     entrypoint.sh \
+    mapcache_items_view.sql \
     /
 
 RUN chmod -v +x \
diff --git a/cache/configure.sh b/cache/configure.sh
index 34e81010..3e478fe9 100755
--- a/cache/configure.sh
+++ b/cache/configure.sh
@@ -108,5 +108,6 @@ else
     echo "Using existing index.html"
 fi
 
-echo "Store environment variables for cron."
-env > /etc/environment
+# create a database view for the mapcache items
+echo "Creating database view for mapcache items"
+psql "host=${DB_HOST} user=${DB_USER} password=${DB_PW} dbname=${DB_NAME} port=${DB_PORT}" -f /mapcache_items_view.sql
diff --git a/cache/install.sh b/cache/install.sh
index 1df036ae..1fd4d533 100755
--- a/cache/install.sh
+++ b/cache/install.sh
@@ -13,6 +13,6 @@ echo "Installing packages"
 VERSION=1.8.0-1~bionic1eox5
 DEBIAN_FRONTEND=noninteractive apt install -y \
     libmapcache1=${VERSION} libapache2-mod-mapcache=${VERSION} mapcache-tools=${VERSION} \
-    sqlite3 curl apache2 python3-dateutil python3-redis wait-for-it
+    sqlite3 curl apache2 python3-dateutil python3-redis wait-for-it postgresql-client
 
 rm -rf /var/lib/apt/lists/*
diff --git a/cache/mapcache_items_view.sql b/cache/mapcache_items_view.sql
new file mode 100644
index 00000000..4d429a79
--- /dev/null
+++ b/cache/mapcache_items_view.sql
@@ -0,0 +1,14 @@
+CREATE OR REPLACE VIEW mapcache_items AS
+    SELECT
+        product_eoobject."begin_time" AS "begin_time",
+        product_eoobject."end_time" AS "end_time",
+        product_eoobject."footprint" AS "footprint",
+        CONCAT(to_char(product_eoobject."begin_time", 'YYYY-MM-DD"T"HH24:MI:SS"Z"'), CONCAT('/', to_char(product_eoobject."end_time", 'YYYY-MM-DD"T"HH24:MI:SS"Z"'))) AS "interval",
+        collection_eoobject.identifier AS "collection"
+    FROM "coverages_product"
+        INNER JOIN "coverages_product_collections" ON ("coverages_product"."eoobject_ptr_id" = "coverages_product_collections"."product_id")
+        INNER JOIN "coverages_collection" collection ON ("coverages_product_collections"."collection_id" = collection."eoobject_ptr_id")
+        INNER JOIN "coverages_eoobject" ON (collection."eoobject_ptr_id" = "coverages_eoobject"."id")
+        INNER JOIN "coverages_eoobject" product_eoobject ON ("coverages_product"."eoobject_ptr_id" = product_eoobject."id")
+        INNER JOIN "coverages_eoobject" collection_eoobject ON (collection."eoobject_ptr_id" = collection_eoobject."id")
+;
-- 
GitLab