Allow editting query-to-run from the automation config section

This commit is contained in:
Debanjum Singh Solanky 2024-04-30 02:40:02 +05:30
parent cb2b1dccc5
commit 1238cadd31
3 changed files with 60 additions and 1 deletions

View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?><!-- Uploaded to: SVG Repo, www.svgrepo.com, Generator: SVG Repo Mixer Tools -->
<svg width="800px" height="800px" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" fill="none">
<path fill="#000000" fill-rule="evenodd" d="M15.198 3.52a1.612 1.612 0 012.223 2.336L6.346 16.421l-2.854.375 1.17-3.272L15.197 3.521zm3.725-1.322a3.612 3.612 0 00-5.102-.128L3.11 12.238a1 1 0 00-.253.388l-1.8 5.037a1 1 0 001.072 1.328l4.8-.63a1 1 0 00.56-.267L18.8 7.304a3.612 3.612 0 00.122-5.106zM12 17a1 1 0 100 2h6a1 1 0 100-2h-6z"/>
</svg>

After

Width:  |  Height:  |  Size: 571 B

View file

@ -689,10 +689,11 @@
<tr id="automation-item-${automationId}">
<td><b>${automationObj.subject}</b></td>
<td><b>${automationObj.scheduling_request}</b></td>
<td><b>${automationObj.query_to_run}</b></td>
<td id="automation-query-to-run-${automationId}"><b>${automationObj.query_to_run}</b></td>
<td id="automation-${automationId}" title="${automationNextRun}">${automationObj.schedule}</td>
<td>
<img onclick="deleteAutomation('${automationId}')" class="automation-row-icon api-key-action enabled" src="/static/assets/icons/delete.svg" alt="Delete Automation" title="Delete Automation">
<img onclick="editAutomation('${automationId}')" class="automation-row-icon api-key-action enabled" src="/static/assets/icons/edit.svg" alt="Edit Automation" title="Edit Automation">
</td>
</tr>
`;
@ -730,6 +731,23 @@
}
document.getElementById("create-automation").addEventListener("click", async () => { await createAutomation(); });
function editAutomation(automationId) {
const query_to_run = window.prompt("What is the query you want to run on this automation's schedule?");
if (!query_to_run) return;
fetch(`/api/automation?automation_id=${automationId}&query_to_run=${query_to_run}`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
},
}).then(response => {
if (response.ok) {
const automationQueryToRunColumn = document.getElementById(`automation-query-to-run-${automationId}`);
automationQueryToRunColumn.innerHTML = `<b>${query_to_run}</b>`;
}
});
}
function getIndexedDataSize() {
document.getElementById("indexed-data-size").innerHTML = "Calculating...";
fetch('/api/config/index/size')

View file

@ -487,3 +487,40 @@ async def make_automation(
}
# Return information about the created automation as a JSON response
return Response(content=json.dumps(automation_info), media_type="application/json", status_code=200)
@api.patch("/automation", response_class=Response)
@requires(["authenticated"])
def edit_job(
request: Request, automation_id: str, query_to_run: Optional[str] = None, crontime: Optional[str] = None
) -> Response:
user: KhojUser = request.user.object
# Perform validation checks
# Check at least one of query or crontime is provided
if not query_to_run and not crontime:
return Response(content="A query or crontime is required", status_code=400)
# Check if user is allowed to edit this automation id
if not automation_id.startswith(f"automation_{user.uuid}_"):
return Response(content="Unauthorized automation deletion request", status_code=403)
# Check if automation with this id exist
automation: Job = state.scheduler.get_job(job_id=automation_id)
if not automation:
return Response(content="Invalid automation", status_code=403)
if not query_to_run.startswith("/automated_task"):
query_to_run = f"/automated_task {query_to_run}"
# Update automation with new query
automation_metadata = json.loads(automation.name)
automation_metadata["query_to_run"] = query_to_run
automation.modify(kwargs={"query_to_run": query_to_run}, name=json.dumps(automation_metadata))
# Collate info about the modified user automation
automation_info = {
"id": automation.id,
"name": automation.name,
"next": automation.next_run_time.strftime("%Y-%m-%d %H:%MS"),
}
# Return modified automation information as a JSON response
return Response(content=json.dumps(automation_info), media_type="application/json", status_code=200)