Skip to content

Commit

Permalink
Feat: Agent log is cleared after Agent been disabled (#830)
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyplayy authored Sep 19, 2023
1 parent b807902 commit c11d2cb
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 4 deletions.
5 changes: 5 additions & 0 deletions web/api/maestro_api/api_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ def agent_log_create_one(*args, **kwargs):
def agent_log_all(*args, **kwargs):
return agent_log_controller.all(*args, **kwargs)

@flask_app.route("/agent_log/<agent_id>", methods=["DELETE"])
@requires_auth()
def agent_log_delete(*args, **kwargs):
return agent_log_controller.delete(*args, **kwargs)

# /custom_data routes
@flask_app.route("/custom_data", methods=["GET"])
@requires_auth()
Expand Down
9 changes: 9 additions & 0 deletions web/api/maestro_api/controllers/agent_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,12 @@ def all(self, data, user):
agent_logs = AgentLog.objects.filter(filter_query).order_by(sort)

return jsonify_list_of_docs(agent_logs)

def delete(self, agent_id, user):
"""
Delete all AgentLog entries for a specific agent.
"""

deleted_count = AgentLog.objects(agent_id=agent_id).delete()

return jsonify(deleted_count)
17 changes: 17 additions & 0 deletions web/api/tests/routes/test_agent_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,20 @@ def test_create_agent_log_bad_response(client, request_data, expected_error_mess

assert response.status_code == 400
assert response_text == expected_error_message


def test_delete_agent_log(client):
agent_log_data = dict(
agent_id="6076d69ba216ff15b6e95ea7",
log_message="test agent log message",
level="INFO",
)
AgentLog(**agent_log_data).save()

response = client.delete(f"/agent_log/{agent_log_data['agent_id']}")

assert response.status_code == 200
assert AgentLog.objects(agent_id=agent_log_data["agent_id"]).count() == 0

res_json = json.loads(response.data)
assert res_json == 1
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import React, { useState } from "react";
import { Link } from "react-router-dom";

import { updateAgent } from "../../../lib/api/endpoints/agent";
import { clearAgentLog } from "../../../lib/api/endpoints/agentLog";
import { agentStatus as agentStatusModel } from "../../../lib/api/models";
import { agentLogsUrl } from "../../../lib/routes";
import AgentStatusBadge from "../../badge/AgentStatusBadge";
Expand Down Expand Up @@ -35,7 +36,11 @@ const AgentsListTable = ({ agents, isLoading, updateTestPlans }) => {
await updateAgent(agentId, {
agent_status: "DISABLED"
});
message.success({ content: "Agent Disabled", duration: 5 });
const deletedLogs = await clearAgentLog(agentId);
message.success({
content: `Agent Disabled \n${deletedLogs} Log entries cleared`,
duration: 3
});
updateTestPlans();
};

Expand All @@ -56,7 +61,7 @@ const AgentsListTable = ({ agents, isLoading, updateTestPlans }) => {
title: "IP",
dataIndex: "ip",
key: "ip",
width: 100
width: 150
},
{
title: "Status",
Expand Down

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions web/frontend/src/lib/api/endpoints/agentLog.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,16 @@ export const fetchAgentLogs = async ({ dateFrom, level, agentIds = [] }) => {
throw error;
}
};

export const clearAgentLog = async (agentId) => {
try {
const res = await maestroClient.delete(`/api/agent_log/${agentId}`);

const numDeleteLogs = res.data;

return numDeleteLogs;
} catch (error) {
ErrorHandler.handleError(error, "agent logs");
throw error;
}
};

0 comments on commit c11d2cb

Please sign in to comment.