From d0cc05bf7ff3a7c1c6163c3d7f1f65aa2944b986 Mon Sep 17 00:00:00 2001
From: Martin Weise <martin.weise@tuwien.ac.at>
Date: Wed, 8 Nov 2023 15:55:12 +0100
Subject: [PATCH] Hotfix the metrics and swagger-ui

---
 dbrepo-metadata-db/setup-schema.sql         |  8 +---
 dbrepo-search-service/Dockerfile            |  3 +-
 dbrepo-search-service/app/__init__.py       | 52 ++++++++++++++++++++-
 dbrepo-search-service/app/api/__init__.py   |  2 +-
 dbrepo-search-service/app/api/routes.py     | 14 ++++--
 dbrepo-search-service/us-yml/get_health.yml | 24 ++++++++++
 6 files changed, 89 insertions(+), 14 deletions(-)
 create mode 100644 dbrepo-search-service/us-yml/get_health.yml

diff --git a/dbrepo-metadata-db/setup-schema.sql b/dbrepo-metadata-db/setup-schema.sql
index e9d96b7151..0c281ba9a0 100644
--- a/dbrepo-metadata-db/setup-schema.sql
+++ b/dbrepo-metadata-db/setup-schema.sql
@@ -279,10 +279,8 @@ CREATE TABLE IF NOT EXISTS `mdb_concepts`
     name        VARCHAR(255)          null,
     description TEXT                  null,
     created     timestamp             NOT NULL DEFAULT NOW(),
-    created_by  character varying(36) NOT NULL,
     PRIMARY KEY (id),
-    UNIQUE (uri(200)),
-    FOREIGN KEY (created_by) REFERENCES mdb_users (id)
+    UNIQUE (uri(200))
 ) WITH SYSTEM VERSIONING;
 
 CREATE TABLE IF NOT EXISTS `mdb_units`
@@ -292,10 +290,8 @@ CREATE TABLE IF NOT EXISTS `mdb_units`
     name        VARCHAR(255)          null,
     description TEXT                  null,
     created     timestamp             NOT NULL DEFAULT NOW(),
-    created_by  character varying(36) NOT NULL,
     PRIMARY KEY (id),
-    UNIQUE (uri(200)),
-    FOREIGN KEY (created_by) REFERENCES mdb_users (id)
+    UNIQUE (uri(200))
 ) WITH SYSTEM VERSIONING;
 
 CREATE TABLE IF NOT EXISTS `mdb_columns_concepts`
diff --git a/dbrepo-search-service/Dockerfile b/dbrepo-search-service/Dockerfile
index bbe8d7710b..5474f0500e 100644
--- a/dbrepo-search-service/Dockerfile
+++ b/dbrepo-search-service/Dockerfile
@@ -1,6 +1,6 @@
 FROM python:3.10-alpine
 
-RUN adduser -D alpine
+RUN apk add bash curl && adduser -D alpine
 
 WORKDIR /home/alpine
 
@@ -12,6 +12,7 @@ RUN pip install pipenv && \
 
 COPY ./app ./app
 COPY ./scripts ./scripts
+COPY ./us-yml ./us-yml
 COPY config.py wsgi.py ./
 
 ENV FLASK_APP=wsgi.py
diff --git a/dbrepo-search-service/app/__init__.py b/dbrepo-search-service/app/__init__.py
index 72c4679c9d..014b475338 100644
--- a/dbrepo-search-service/app/__init__.py
+++ b/dbrepo-search-service/app/__init__.py
@@ -2,7 +2,7 @@
 
 import os
 import logging
-from flasgger import LazyJSONEncoder
+from flasgger import LazyJSONEncoder, Swagger
 from flask import Flask
 from opensearchpy import OpenSearch
 from config import Config
@@ -41,7 +41,55 @@ def create_app(config_class=Config):
 
     metrics = PrometheusMetrics(app)
     metrics.info("app_info", "Application info", version="0.0.1")
-    app.config["SWAGGER"] = {"openapi": "3.0.0", "title": "Swagger UI", "uiversion": 3}
+    app.config["SWAGGER"] = {"openapi": "3.0.1", "title": "Swagger UI", "uiversion": 3}
+
+    swagger_config = {
+        "headers": [],
+        "specs": [
+            {
+                "endpoint": "api-search",
+                "route": "/api-search.json",
+                "rule_filter": lambda rule: rule.endpoint.startswith('actuator'),
+                "model_filter": lambda tag: True,  # all in
+            }
+        ],
+        "static_url_path": "/flasgger_static",
+        "swagger_ui": True,
+        "specs_route": "/swagger-ui/",
+    }
+
+    template = {
+        "openapi": "3.0.0",
+        "info": {
+            "title": "Database Repository Search Service API",
+            "description": "Service that searches the search database",
+            "version": "1.3.0",
+            "contact": {
+                "name": "Prof. Andreas Rauber",
+                "email": "andreas.rauber@tuwien.ac.at"
+            },
+            "license": {
+                "name": "Apache 2.0",
+                "url": "https://www.apache.org/licenses/LICENSE-2.0"
+            },
+        },
+        "externalDocs": {
+            "description": "Sourcecode Documentation",
+            "url": "https://gitlab.phaidra.org/fair-data-austria-db-repository/fda-services"
+        },
+        "servers": [
+            {
+                "url": "http://localhost:4000",
+                "description": "Generated server url"
+            },
+            {
+                "url": "https://test.dbrepo.tuwien.ac.at",
+                "description": "Sandbox"
+            }
+        ]
+    }
+
+    swagger = Swagger(app, config=swagger_config, template=template)
     # https://flask-jwt-extended.readthedocs.io/en/stable/options/
     app.config["JWT_ALGORITHM"] = "HS256"
     app.config["JWT_DECODE_ISSUER"] = os.getenv("JWT_ISSUER")
diff --git a/dbrepo-search-service/app/api/__init__.py b/dbrepo-search-service/app/api/__init__.py
index f799e8b525..256d62b9bf 100644
--- a/dbrepo-search-service/app/api/__init__.py
+++ b/dbrepo-search-service/app/api/__init__.py
@@ -1,5 +1,5 @@
 from flask import Blueprint
 
-api_bp = Blueprint("api", __name__, url_prefix="/api/search")
+api_bp = Blueprint("api", __name__)
 
 from app.api import routes
diff --git a/dbrepo-search-service/app/api/routes.py b/dbrepo-search-service/app/api/routes.py
index 48625f6f2d..695e50e043 100644
--- a/dbrepo-search-service/app/api/routes.py
+++ b/dbrepo-search-service/app/api/routes.py
@@ -56,7 +56,13 @@ def general_filter(index, results):
     return results
 
 
-@api_bp.route("<string:index>", methods=["GET"], endpoint="endpoint")
+@api_bp.route("/health", methods=["GET"], endpoint="actuator_health")
+@swag_from("us-yml/get_health")  # ToDo: get the SWAG right
+def health():
+    return {"status": "UP"}
+
+
+@api_bp.route("/api/search/<string:index>", methods=["GET"], endpoint="search_get_index")
 @swag_from("us-yml")  # ToDo: get the SWAG right
 def get_index(index):
     """
@@ -87,11 +93,11 @@ def get_index(index):
     results_per_page = min(request.args.get("results_per_page", 50, type=int), 500)
     max_pages = math.ceil(len(results) / results_per_page)
     page = min(request.args.get("page", 1, type=int), max_pages)
-    results = results[(results_per_page * (page - 1)) : (results_per_page * page)]
+    results = results[(results_per_page * (page - 1)): (results_per_page * page)]
     return {"results": results, "total": total_number_of_results, "status": 200}
 
 
-@api_bp.route("<string:index>/fields", methods=["GET"], endpoint="blabla")
+@api_bp.route("/api/search/<string:index>/fields", methods=["GET"], endpoint="search_get_index_fields")
 def get_fields(index):
     """
     returns a list of attributes of the data for a specific index.
@@ -120,7 +126,7 @@ def get_fields(index):
     return {"fields": fields, "status": 200}
 
 
-@api_bp.route("", methods=["POST"], endpoint="endpoint2")
+@api_bp.route("/api/search", methods=["POST"], endpoint="search_fuzzy_search")
 def search():
     """
     Main endpoint for general searching.
diff --git a/dbrepo-search-service/us-yml/get_health.yml b/dbrepo-search-service/us-yml/get_health.yml
new file mode 100644
index 0000000000..a4b273a2bf
--- /dev/null
+++ b/dbrepo-search-service/us-yml/get_health.yml
@@ -0,0 +1,24 @@
+summary: Return a healthcheck
+description: |
+    Return UP if the instance is ready to serve connections.
+consumes:
+  - application/json
+produces:
+  - application/json
+parameters: []
+definitions:
+  Health:
+    type: object
+    properties:
+      status:
+        type: string
+        description: UP
+responses:
+  200:
+    description: OK, service is up and running
+    schema:
+      $ref: "#/definitions/Column"
+  404:
+    description: Service is not yet ready
+tags:
+  - actuator
\ No newline at end of file
-- 
GitLab