Making task endpoint a bit more useful, including state changes and attempts
This commit is contained in:
@@ -541,16 +541,15 @@ namespace daggy::daggyd {
|
||||
auto runID = request.param(":runID").as<DAGRunID>();
|
||||
auto taskName = request.param(":taskName").as<std::string>();
|
||||
|
||||
std::stringstream ss;
|
||||
Task task;
|
||||
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) {
|
||||
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,
|
||||
|
||||
@@ -41,6 +41,8 @@ namespace daggy {
|
||||
|
||||
std::string tasksToJSON(const TaskSet &tasks);
|
||||
|
||||
std::string taskRecordToJSON(const loggers::dag_run::TaskRecord &taskRecord);
|
||||
|
||||
// Full specs
|
||||
DAGSpec dagFromJSON(const rj::Value &spec);
|
||||
DAGSpec dagFromJSON(const std::string &jsonSpec);
|
||||
|
||||
@@ -276,21 +276,60 @@ namespace daggy {
|
||||
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)
|
||||
{
|
||||
os << taskToJSON(task);
|
||||
return os;
|
||||
}
|
||||
|
||||
// From https://stackoverflow.com/questions/7724448/simple-json-string-escape-for-c
|
||||
enum State {ESCAPED, UNESCAPED};
|
||||
std::string escapeJSON(const std::string& input)
|
||||
// From
|
||||
// https://stackoverflow.com/questions/7724448/simple-json-string-escape-for-c
|
||||
enum State
|
||||
{
|
||||
ESCAPED,
|
||||
UNESCAPED
|
||||
};
|
||||
std::string escapeJSON(const std::string &input)
|
||||
{
|
||||
std::string output;
|
||||
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]) {
|
||||
case '"':
|
||||
output += "\\\"";
|
||||
@@ -320,7 +359,6 @@ namespace daggy {
|
||||
output += input[i];
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return output;
|
||||
@@ -344,7 +382,8 @@ namespace daggy {
|
||||
}
|
||||
*/
|
||||
|
||||
std::string attemptRecordToJSON(const AttemptRecord &record) {
|
||||
std::string attemptRecordToJSON(const AttemptRecord &record)
|
||||
{
|
||||
rj::Document doc;
|
||||
doc.SetObject();
|
||||
auto &alloc = doc.GetAllocator();
|
||||
|
||||
Reference in New Issue
Block a user