Making task endpoint a bit more useful, including state changes and attempts

This commit is contained in:
Ian Roddis
2022-02-25 14:17:36 -04:00
parent 7510fc288d
commit 197af4c76a
3 changed files with 52 additions and 12 deletions

View File

@@ -541,16 +541,15 @@ namespace daggy::daggyd {
auto runID = request.param(":runID").as<DAGRunID>(); auto runID = request.param(":runID").as<DAGRunID>();
auto taskName = request.param(":taskName").as<std::string>(); auto taskName = request.param(":taskName").as<std::string>();
std::stringstream ss;
Task task;
try { try {
task = logger_.getTask(runID, taskName); auto taskRecord = logger_.getTaskRecord(runID, taskName);
std::stringstream ss;
ss << taskRecordToJSON(taskRecord) << '\n';
response.send(Pistache::Http::Code::Ok, ss.str());
} }
catch (std::exception &e) { catch (std::exception &e) {
REQ_RESPONSE(Not_Found, e.what()); REQ_RESPONSE(Not_Found, e.what());
} }
ss << taskToJSON(task);
response.send(Pistache::Http::Code::Ok, ss.str());
} }
void Server::handleStopTask(const Pistache::Rest::Request &request, void Server::handleStopTask(const Pistache::Rest::Request &request,

View File

@@ -41,6 +41,8 @@ namespace daggy {
std::string tasksToJSON(const TaskSet &tasks); std::string tasksToJSON(const TaskSet &tasks);
std::string taskRecordToJSON(const loggers::dag_run::TaskRecord &taskRecord);
// Full specs // Full specs
DAGSpec dagFromJSON(const rj::Value &spec); DAGSpec dagFromJSON(const rj::Value &spec);
DAGSpec dagFromJSON(const std::string &jsonSpec); DAGSpec dagFromJSON(const std::string &jsonSpec);

View File

@@ -276,21 +276,60 @@ namespace daggy {
return ss.str(); return ss.str();
} }
std::string taskRecordToJSON(const loggers::dag_run::TaskRecord &tr)
{
std::stringstream ss;
ss << "{"
<< R"("state": )" << std::quoted(tr.state._to_string())
<< R"(, "task": )" << taskToJSON(tr.task) << R"(, "stateChanges": [)";
bool first = true;
for (const auto &sc : tr.stateChanges) {
if (first) {
first = false;
}
else {
ss.put(',');
}
ss << R"( { "time": )" << sc.time << R"(, "state": )"
<< std::quoted(sc.state._to_string()) << "}";
}
ss.put(']');
ss << R"(, "attempts": [)";
first = true;
for (const auto &attempt : tr.attempts) {
if (first) {
first = false;
}
else {
ss.put(',');
}
ss << attemptRecordToJSON(attempt);
}
ss << "]}";
return ss.str();
}
std::ostream &operator<<(std::ostream &os, const Task &task) std::ostream &operator<<(std::ostream &os, const Task &task)
{ {
os << taskToJSON(task); os << taskToJSON(task);
return os; return os;
} }
// From https://stackoverflow.com/questions/7724448/simple-json-string-escape-for-c // From
enum State {ESCAPED, UNESCAPED}; // https://stackoverflow.com/questions/7724448/simple-json-string-escape-for-c
std::string escapeJSON(const std::string& input) enum State
{
ESCAPED,
UNESCAPED
};
std::string escapeJSON(const std::string &input)
{ {
std::string output; std::string output;
output.reserve(input.length()); output.reserve(input.length());
for (std::string::size_type i = 0; i < input.length(); ++i) for (std::string::size_type i = 0; i < input.length(); ++i) {
{
switch (input[i]) { switch (input[i]) {
case '"': case '"':
output += "\\\""; output += "\\\"";
@@ -320,7 +359,6 @@ namespace daggy {
output += input[i]; output += input[i];
break; break;
} }
} }
return output; return output;
@@ -344,7 +382,8 @@ namespace daggy {
} }
*/ */
std::string attemptRecordToJSON(const AttemptRecord &record) { std::string attemptRecordToJSON(const AttemptRecord &record)
{
rj::Document doc; rj::Document doc;
doc.SetObject(); doc.SetObject();
auto &alloc = doc.GetAllocator(); auto &alloc = doc.GetAllocator();