Add versions for http_functions, whenever script is updated a new version is logged.

This commit is contained in:
Peter Stockings
2024-04-06 19:49:46 +11:00
parent 4b30deb709
commit b4c67d8346
7 changed files with 37 additions and 24 deletions

11
app.py
View File

@@ -158,10 +158,11 @@ def get_http_function_edit_form(function_id):
is_public = http_function['is_public']
log_request = http_function['log_request']
log_response = http_function['log_response']
version_number = http_function['version_number']
if htmx:
return render_block(app.jinja_env, 'dashboard/http_functions/edit.html', 'page', user_id=user_id, function_id=function_id, name=name, script=script, environment_info=environment_info, is_public=is_public, log_request=log_request, log_response=log_response)
return render_template("dashboard/http_functions/edit.html", user_id=user_id, name=name, function_id=function_id, script=script, environment_info=environment_info, is_public=is_public, log_request=log_request, log_response=log_response)
return render_block(app.jinja_env, 'dashboard/http_functions/edit.html', 'page', user_id=user_id, function_id=function_id, name=name, script=script, environment_info=environment_info, is_public=is_public, log_request=log_request, log_response=log_response, version_number=version_number)
return render_template("dashboard/http_functions/edit.html", user_id=user_id, name=name, function_id=function_id, script=script, environment_info=environment_info, is_public=is_public, log_request=log_request, log_response=log_response, version_number=version_number)
@ app.route("/dashboard/http_functions/<int:function_id>/edit", methods=["POST"])
@login_required
@@ -175,7 +176,7 @@ def edit_http_function(function_id):
log_request = request.json.get('log_request')
log_response = request.json.get('log_response')
db.edit_http_function(user_id, function_id, name, script_content, environment_info, is_public, log_request, log_response)
updated_version = db.edit_http_function(user_id, function_id, name, script_content, environment_info, is_public, log_request, log_response)
return { "status": "success", "message": f'{name} updated' }
except Exception as e:
@@ -275,6 +276,7 @@ def execute_http_function(user_id, function):
is_public = http_function['is_public']
log_request = http_function['log_request']
log_response = http_function['log_response']
version_number = http_function['version_number']
# Check if the function is public, if not check if the user is authenticated and owns the function
if not is_public:
@@ -323,7 +325,8 @@ def execute_http_function(user_id, function):
response_data['status'],
request_data if log_request else {},
response_data['result'] if (log_response or response_data['status'] != 'SUCCESS') else {},
response_data['logs'])
response_data['logs'],
version_number)
if response_data['status'] != 'SUCCESS':
return render_template("function_error.html", function_name=function_name ,error=response_data['result'], logs=response_data['logs'])

19
db.py
View File

@@ -49,17 +49,17 @@ class DataBase():
def get_http_functions_for_user(self, user_id):
http_functions = self.execute(
'SELECT id, user_id, NAME, script_content, invoked_count, environment_info, is_public, log_request, log_response FROM http_functions WHERE user_id=%s ORDER by id DESC', [user_id])
'SELECT id, user_id, NAME, script_content, invoked_count, environment_info, is_public, log_request, log_response, version_number FROM http_functions WHERE user_id=%s ORDER by id DESC', [user_id])
return http_functions
def get_http_function(self, user_id, name):
http_function = self.execute(
'SELECT id, user_id, NAME, script_content, invoked_count, environment_info, is_public, log_request, log_response FROM http_functions WHERE user_id=%s AND NAME=%s', [user_id, name], one=True)
'SELECT id, user_id, NAME, script_content, invoked_count, environment_info, is_public, log_request, log_response, version_number FROM http_functions WHERE user_id=%s AND NAME=%s', [user_id, name], one=True)
return http_function
def get_http_function_by_id(self, user_id, http_function_id):
http_function = self.execute(
'SELECT id, user_id, NAME, script_content, invoked_count, environment_info, is_public, log_request, log_response FROM http_functions WHERE user_id=%s AND id=%s', [user_id, http_function_id], one=True)
'SELECT id, user_id, NAME, script_content, invoked_count, environment_info, is_public, log_request, log_response, version_number FROM http_functions WHERE user_id=%s AND id=%s', [user_id, http_function_id], one=True)
return http_function
def create_new_http_function(self, user_id, name, script_content, environment_info, is_public, log_request, log_response):
@@ -70,11 +70,12 @@ class DataBase():
)
def edit_http_function(self, user_id, function_id, name, script_content, environment_info, is_public, log_request, log_response):
self.execute(
'UPDATE http_functions SET NAME=%s, script_content=%s, environment_info=%s, is_public=%s, log_request=%s, log_response=%s WHERE user_id=%s AND id=%s',
updated_version = self.execute(
'UPDATE http_functions SET NAME=%s, script_content=%s, environment_info=%s, is_public=%s, log_request=%s, log_response=%s WHERE user_id=%s AND id=%s RETURNING version_number',
[name, script_content, environment_info, is_public, log_request, log_response, user_id, function_id],
commit=True
commit=True, one=True
)
return updated_version
def update_http_function_environment_info_and_invoked_count(self, user_id, name, environment_info):
self.execute(
@@ -84,13 +85,13 @@ class DataBase():
self.execute(
'DELETE FROM http_functions WHERE user_id=%s AND id=%s', [user_id, function_id], commit=True)
def add_http_function_invocation(self, http_function_id, status, request_data, response_data, logs):
def add_http_function_invocation(self, http_function_id, status, request_data, response_data, logs, version_number):
self.execute(
'INSERT INTO http_function_invocations (http_function_id, status, request_data, response_data, logs) VALUES (%s, %s, %s, %s, %s)', [http_function_id, status, json.dumps(request_data), json.dumps(response_data), json.dumps(logs)], commit=True)
'INSERT INTO http_function_invocations (http_function_id, status, request_data, response_data, logs, version_number) VALUES (%s, %s, %s, %s, %s, %s)', [http_function_id, status, json.dumps(request_data), json.dumps(response_data), json.dumps(logs), version_number], commit=True)
def get_http_function_invocations(self, http_function_id):
http_function_invocations = self.execute(
"""SELECT id, http_function_id, STATUS, invocation_time, request_data, response_data, LOGS
"""SELECT id, http_function_id, STATUS, invocation_time, request_data, response_data, LOGS, version_number
FROM http_function_invocations
WHERE http_function_id=%s
ORDER BY invocation_time DESC""", [http_function_id])

View File

@@ -9,6 +9,7 @@ def create_http_function_view_model(http_function):
"is_public": http_function['is_public'],
"log_request": http_function['log_request'],
"log_response": http_function['log_response'],
"version_number": http_function['version_number']
}
return function_view_model

View File

@@ -8,6 +8,6 @@ refresh_url=url_for('get_http_function_edit_form', function_id=function_id), sho
{{ render_partial('function_editor.html', function_id=function_id, name=name, script=script,
environment_info=environment_info,
is_public=is_public, log_request=log_request, log_response=log_response, is_edit=True) }}
is_public=is_public, log_request=log_request, log_response=log_response, version_number=version_number, is_edit=True) }}
{% endblock %}

View File

@@ -25,6 +25,9 @@ refresh_url=url_for('get_http_function_logs', function_id=function_id), show_edi
</svg>
</button>
<span>{{ invocation.invocation_time.strftime('%Y-%m-%d %H:%M:%S') }}</span>
<span class="bg-blue-500 text-white text-xs font-semibold px-2 py-1 rounded">
v{{ invocation.version_number }}
</span>
</div>
<!-- Request Data for Mobile and Desktop -->

View File

@@ -32,11 +32,14 @@
<tbody class="[&amp;_tr:last-child]:border-0" data-id="62">
{% for function in http_functions %}
<tr class="border-b transition-colors hover:bg-muted/50 data-[state=selected]:bg-muted" data-id="63">
<td class="p-4 align-middle [&amp;:has([role=checkbox])]:pr-0 font-medium flex items-center">
<td
class="p-4 align-middle [&amp;:has([role=checkbox])]:pr-0 font-medium flex items-center space-x-1">
<span class="text-gray-800 text-base text-md">{{ function.name }}</span>
<span
class="inline-flex items-center justify-center w-5 h-5 ms-2 text-xs font-semibold text-blue-800 bg-blue-200 rounded-full ml-1">
{{ function.invoked_count }}
<span class="bg-green-500 text-white text-xs font-semibold px-2 py-1 rounded flex items-center">
#{{ function.invoked_count }}
</span>
<span class="bg-blue-500 text-white text-xs font-semibold px-2 py-1 rounded">
v{{ function.version_number }}
</span>
{% if function.is_public %}
<span class="ml-1">

View File

@@ -17,6 +17,10 @@
</path>
</svg>
<span class="bg-blue-500 text-white text-xs font-semibold px-2 py-1 rounded ml-2">
v{{ version_number }}
</span>
</div>
<script>
function toggleInput() {
@@ -305,12 +309,10 @@
})
.then(response => response.json())
.then(json => {
if ("{{ name }}" != name) {
htmx.ajax('GET', "{{ url_for('get_http_function_edit_form', function_id=function_id) }}", {
target: '#container',
swap: 'innerHTML'
});
}
showAlert(json.message, json.status)
})
})