From e8dfb323da6f01bfd06ea68c32cc8f68bb27a526 Mon Sep 17 00:00:00 2001
From: Riccardo Boero <ribo@nilu.no>
Date: Tue, 10 Dec 2024 15:29:42 +0100
Subject: [PATCH] Chnage results to system service

---
 CHANGELOG                     |   4 ++
 Project.toml                  |   2 +-
 src/FACT_unified_data_IO.jl   |   2 +-
 src/utils/db.jl               | 113 +++++++++++++++++-----------------
 src/{ => utils}/manifestIO.jl |   0
 5 files changed, 61 insertions(+), 60 deletions(-)
 rename src/{ => utils}/manifestIO.jl (100%)

diff --git a/CHANGELOG b/CHANGELOG
index 4810c7c..fb9f3b7 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -9,6 +9,10 @@ Removed: For features removed in this release.
 Fixed: For any bug fixes.
 Security: For vulnerabilities.
 
+## [0.0.2] - 12-10-2024
+Fixed:
+- use of system services for results
+
 ## [0.0.1] - 12-09-2024
 Added:
 - preliminary but full version of the package
diff --git a/Project.toml b/Project.toml
index 3804041..38fe1a6 100644
--- a/Project.toml
+++ b/Project.toml
@@ -1,7 +1,7 @@
 name = "FACT_unified_data_IO"
 uuid = "ec8d5dc6-0dfe-41d7-8c2c-855ff50b7b42"
 authors = ["Riccardo Boero <ribo@nilu.no>"]
-version = "0.0.1"
+version = "0.0.2"
 
 [deps]
 DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
diff --git a/src/FACT_unified_data_IO.jl b/src/FACT_unified_data_IO.jl
index 1905ca0..5fbb6f7 100644
--- a/src/FACT_unified_data_IO.jl
+++ b/src/FACT_unified_data_IO.jl
@@ -38,7 +38,7 @@ module FACT_unified_data_IO
     include("utils/db.jl")
     export get_connection, close_connection
 
-    include("manifestIO.jl")
+    include("utils/manifestIO.jl")
     export get_table, write_table, overwrite_table, does_table_exist_for_method
 
 end
diff --git a/src/utils/db.jl b/src/utils/db.jl
index 3ff6e23..edb483d 100644
--- a/src/utils/db.jl
+++ b/src/utils/db.jl
@@ -31,28 +31,37 @@ function create_manifest_table(conn::MySQL.Connection)
 end
 
 """
-    create_database(host::String, user::String, password::String; port::Int=3306)
+create_database(service::OrchestratorRegistry.Service)
 
-Create a new MySQL database named 'FACT_Results' if it does not already exist.
+Ensures the `FACT_results` database exists for the specified service.
 
-This function establishes a connection to a MySQL server and creates the 'FACT_Results' database. The database creation is performed using the 'CREATE DATABASE IF NOT EXISTS' SQL command.
+This function connects to the database server using the provided `Service` instance, checks for the 
+existence of the `FACT_results` database, and creates it if it does not already exist.
 
-# Arguments
-- `host::String`: The hostname or IP address of the MySQL server.
-- `user::String`: The username for the MySQL server.
-- `password::String`: The password for the MySQL server.
-- `port::Int` (optional): The port number of the MySQL server, defaulting to 3306.
+Arguments:
+  - `service::OrchestratorRegistry.Service`: The `Service` instance containing the host and port 
+    details required to connect to the database server.
 
-# Example
-> host = "localhost"
-> user = "root"
-> password = "your_password"
-> create_database(host, user, password)
+Behavior:
+  - Connects to the database server using the service's host and port.
+  - Executes a `CREATE DATABASE IF NOT EXISTS` query to ensure the `FACT_results` database exists.
+  - Closes the connection to the database server after executing the query.
+
+Raises:
+  - `Error`: If the connection to the database server fails or the query execution encounters an error.
+
+Example:
 
-In this example, `create_database` is called with the host, user, and password for the MySQL server. This will attempt to create the 'FACT_Results' database on the specified MySQL server.
+```julia
+# Define a service instance
+service = Service("123", "auth-service", "http://localhost", 3306)
+
+# Ensure the database exists
+create_database(service)
+```
 """
-function create_database(host::String, user::String, password::String; port::Int=3306)
-    conn = establish_connection(host, user, password, ""; port=port)
+function create_database(service::OrchestratorRegistry.Service)
+    conn = establish_connection(service.host, "root", "devops", ""; port=service.port)
 
     # Prepare query
     query = "CREATE DATABASE IF NOT EXISTS FACT_results;"
@@ -65,62 +74,50 @@ function create_database(host::String, user::String, password::String; port::Int
 end  
 
 """
-    get_connection(connection_config::Dict)
-
-Establish a connection to a MySQL database using the provided configuration.
-
-# Arguments
-- `connection_config::Dict`: A dictionary containing the MySQL connection parameters.
-  - `"host"` (String): The hostname or IP address of the MySQL server.
-  - `"user"` (String): The username for authenticating with the MySQL server.
-  - `"password"` (String): The password for authenticating with the MySQL server.
-  - `"port"` (Int, optional): The port number for the MySQL server (default is 3306).
+get_connection(service::OrchestratorRegistry.Service)
 
-# Returns
-- A `MySQL.Connection` object representing the database connection.
+Establishes and returns a database connection for the specified service.
 
-This function takes the MySQL connection parameters from the `connection_config` dictionary and establishes a connection to the MySQL database using the `MySQL.jl` package. It returns the `MySQL.Connection` object for further database operations.
+This function ensures that the database associated with the given `Service` instance is ready for use.
+It performs the following steps:
+  1. Creates the database if it does not already exist.
+  2. Establishes a connection to the database using the service's host and port.
+  3. Ensures the `manifest` table is present in the database, creating it if necessary.
 
-"""
-function get_connection(connection_config::Dict)
-    host = connection_config["host"]
-    user = connection_config["user"]
-    password = connection_config["password"]
-    port = connection_config["port"]
-    conn = get_connection(host, user, password, port=port)
-    return conn
-end
+Arguments:
+  - `service::OrchestratorRegistry.Service`: The `Service` instance containing the details 
+    (host and port) required to connect to the database.
 
-"""
-    get_connection(host::String, user::String, password::String; port::Int=3306) -> MySQL.Connection
+Returns:
+  - `conn`: The database connection object established for the specified service.
 
-Establish a connection to a MySQL database named 'FACT_Results', creating the database and a 'manifest' table if they do not exist.
+Raises:
+  - `Error`: If the database connection cannot be established or if any database operation fails.
 
-This function first calls `create_database` to ensure that the 'FACT_Results' database exists on the MySQL server. It then establishes and returns a connection to this database. Lastly, it calls `create_table` to ensure that the 'manifest' table exists within the 'FACT_Results' database.
+Behavior:
+  - Uses the host and port of the service to connect.
+  - Assumes the database credentials are `root` and `devops`.
+  - The database name used is `FACT_results`.
 
-# Arguments
-- `host::String`: The hostname or IP address of the MySQL server.
-- `user::String`: The username for the MySQL server.
-- `password::String`: The password for the MySQL server.
-- `port::Int` (optional): The port number of the MySQL server, defaulting to 3306.
+Example:
 
-# Returns
-- `MySQL.Connection`: A connection object to the 'FACT_Results' database.
+```julia
+# Define a service instance
+service = Service("123", "auth-service", "http://localhost", 3306)
 
-# Example
-> host = "localhost"
-> user = "root"
-> password = "your_password"
-> conn = get_connection(host, user, password)
+# Establish a database connection
+conn = get_connection(service)
 
-In this example, `get_connection` is called with the host, user, and password for the MySQL server. It ensures the 'FACT_Results' database and the 'manifest' table are created and then establishes a connection to the database.
+# Perform operations using the connection
+execute_query(conn, "SELECT * FROM manifest")
+```
 """
-function get_connection(host::String, user::String, password::String; port::Int=3306)
+function get_connection(service::OrchestratorRegistry.Service)
     # Create the databse if it does not exist
-    create_database(host, user, password, port=port)
+    create_database(service)
 
     # Connect to the database
-    conn = establish_connection(host, user, password, "FACT_results"; port=port)
+    conn = establish_connection(service.host, "root", "devops", "FACT_results"; port=service.port)
 
     # Create the 'manifest' table if it does not exist
     create_manifest_table(conn)
diff --git a/src/manifestIO.jl b/src/utils/manifestIO.jl
similarity index 100%
rename from src/manifestIO.jl
rename to src/utils/manifestIO.jl
-- 
GitLab