Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Stoffel committed Nov 27, 2023
2 parents 081e6f2 + 576e498 commit 6824a92
Show file tree
Hide file tree
Showing 16 changed files with 649 additions and 300 deletions.
24 changes: 22 additions & 2 deletions analytics-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
FROM python:3.8-slim
# Use the base image
FROM softwareag/apama-cumulocity-builder

RUN apt-get update && apt-get -y upgrade

# Set the working directory
WORKDIR /usr/src/app

# Clone the apama-analytics-builder-block-sdk repository
RUN git clone https://github.com/SoftwareAG/apama-analytics-builder-block-sdk.git

COPY requirements.txt /usr/src/app
COPY flask_wrapper.py /usr/src/app
# Used for tes purposes
RUN mkdir -p /tmp/builder
COPY Math_AB_Extension.zip /tmp/builder/

RUN pip install --upgrade pip
RUN pip install -r requirements.txt

ENTRYPOINT ["python"FROM python:3.8-slim

RUN apt-get update && apt-get -y upgrade

Expand All @@ -10,4 +30,4 @@ RUN pip install --upgrade pip
RUN pip install -r requirements.txt


ENTRYPOINT ["python", "-u", "/flask_wrapper.py"]
ENTRYPOINT ["python"
15 changes: 15 additions & 0 deletions analytics-service/Dockerfile_Standard
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM python:3.8-slim

RUN apt-get update && apt-get -y upgrade

COPY requirements.txt /
COPY flask_wrapper.py /
RUN mkdir -p /tmp/builder

COPY Math_AB_Extension.zip /tmp/builder/

RUN pip install --upgrade pip
RUN pip install -r requirements.txt


ENTRYPOINT ["python", "-u", "/flask_wrapper.py"]
21 changes: 20 additions & 1 deletion analytics-service/cumulocity.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
{
"apiVersion": "2",
"version": "{VERSION}",
"provider": {
"name": "Software AG"
},
"isolation": "PER_TENANT",
"requiredRoles": [
"ROLE_INVENTORY_READ",
"ROLE_INVENTORY_ADMIN",
"ROLE_OPTION_MANAGEMENT_READ",
"ROLE_OPTION_MANAGEMENT_ADMIN",
"ROLE_USER_MANAGEMENT_ADMIN",
"ROLE_USER_MANAGEMENT_READ"
],
"roles":[
"ROLE_ANALYTICSBUILDER_ADMIN",
"ROLE_ANALYTICSBUILDER_CREATE"
]
{
"apiVersion": "2",
"version": "{VERSION}",
"provider": {
Expand All @@ -17,4 +36,4 @@
"ROLE_TWINMAKER_ADMIN",
"ROLE_TWINMAKER_CREATE"
]
}

114 changes: 114 additions & 0 deletions analytics-service/flask_wrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
from requests import HTTPError
from flask import Flask, request, jsonify, send_file
import logging
from github import Github
import tempfile
import os
import re
import io
import subprocess

app = Flask(__name__)
# initialize github endpoint
gh = Github()
# define work directory
WORK_DIR_BASE = "/tmp/builder"
# define logging
logging.basicConfig(
level=logging.INFO,
format="[%(asctime)s] %(levelname)s [%(name)s.%(funcName)s:%(lineno)d] %(message)s",
datefmt="%d/%b/%Y %H:%M:%S",
)
logger = logging.getLogger("flask_wrapper")
logger.setLevel(logging.INFO)


@app.route("/health")
def health():
return '{"status":"UP"}'


@app.route("/extension", methods=["POST"])
def create_extension():
# sample url
# "https://api.github.com/repos/SoftwareAG/apama-analytics-builder-block-sdk/contents/samples/blocks/CreateEvent.mon?ref=rel/10.18.0.x"

# extract parameter
data = request.get_json()
try:
extension_name = data.get("extension_name", "")
monitors = data.get("monitors", False)
if extension_name != "":
with tempfile.TemporaryDirectory() as work_temp_dir:
logger.info(
f"Create extension POST for {work_temp_dir} {extension_name} {monitors} "
)
# step 1: download all monitors
for monitor in monitors:
organization, repository_name, file_path, file_name = extract_path(
monitor
)
logger.info(
f"File ( path / file_name ) : ({file_path} / {file_name} )"
)

# get repository
repo = gh.get_repo(f"{organization}/{repository_name}")

# get the contents of the file
try:
file_content = repo.get_contents(file_path).decoded_content

# Combine output directory and filename
logger.info(f"File downloaded and saved to: {file_name}")

named_file = open(os.path.join(work_temp_dir, file_name), "wb")
named_file.write(file_content)
named_file.close()

except Exception as e:
logger.info(
f"Error downloading file: {file_path} {monitor} {e}"
)

files_in_directory = [
f
for f in os.listdir(work_temp_dir)
if os.path.isfile(os.path.join(work_temp_dir, f))
]
for file_name in files_in_directory:
logger.info(f"File in work_temp_dir: {file_name}")

# step 2: run analytics_builder script
# analytics_builder build extension --input work_temp_dir --output zip_name
zip_name = f"{extension_name}.zip"
# subprocess.call(["analytics_builder", "build extension", f"--input {work_temp_dir}", f"--output {zip_name}"])

# step 3 : return result
# extension_result = f"{work_temp_dir}/{zip_name}"

except Exception as e:
logger.error(f"Exception synchronisation!", exc_info=True)

with open(f"/tmp/builder/Math_AB_Extension.zip", "rb") as extension_zip:
return send_file(
io.BytesIO(extension_zip.read()),
# attachment_filename=f"{extension_name}.zip",
mimetype="zip",
)


def extract_path(path):
# Extract information from the API URL
delimiters = r"[/?]"
parts = re.split(delimiters, path)
organization = parts[4]
repository_name = parts[5]
file_path = "/".join(parts[7:-2]) # Extract path excluding "contents" and "ref"
file_name = parts[-3] # Extract path excluding "contents" and "ref"
return organization, repository_name, file_path, file_name


if __name__ == "__main__":
# app.run(host="0.0.0.0", port=80, debug=False)
app.run(host="127.0.0.1", port=9080, debug=False)
Loading

0 comments on commit 6824a92

Please sign in to comment.