Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
ck-c8y committed Nov 27, 2023
1 parent 583e9ac commit a4bc032
Showing 1 changed file with 36 additions and 28 deletions.
64 changes: 36 additions & 28 deletions analytics-service/flask_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,22 @@ def health():
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:
result_extension_file = ''
result_extension_absolute = ''
extension_name = data.get("extension_name", "")
monitors = data.get("monitors", False)

if extension_name != "":
with tempfile.TemporaryDirectory() as work_temp_dir:
try:
logger.info(
f"Create extension for: {extension_name} {monitors} "
)
logger.info(
f"Create extension POST for {work_temp_dir} {extension_name} {monitors} "
f"... in temp dir: {work_temp_dir}"
)
# step 1: download all monitors
for monitor in monitors:
Expand All @@ -60,14 +66,14 @@ def create_extension():
file_content = repo.get_contents(file_path).decoded_content

# Combine output directory and filename
logger.info(f"File downloaded and saved to: {file_name}")
logger.debug(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(
logger.error(
f"Error downloading file: {file_path} {monitor} {e}"
)

Expand All @@ -81,29 +87,31 @@ def create_extension():

# step 2: run analytics_builder script
# analytics_builder build extension --input work_temp_dir --output zip_name
zip_name = f"{extension_name}.zip"
result_extension_file = f"{extension_name}.zip"
result_extension_absolute = f"{work_temp_dir}/{result_extension_file}"
subprocess.call(
[
"/apama_work/apama-analytics-builder-block-sdk/analytics_builder",
"build extension",
f"--input {work_temp_dir}",
f"--output {zip_name}",
"build", "extension",
"--input",
f"{work_temp_dir}",
"--output",
f"{result_extension_absolute}",
]
)

# 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",
)

size = os.path.getsize(result_extension_absolute)
logger.info(f"Returning: {result_extension_absolute} with size {size}")

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

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

def extract_path(path):
# Extract information from the API URL
Expand All @@ -117,5 +125,5 @@ def extract_path(path):


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

0 comments on commit a4bc032

Please sign in to comment.