feat: add labels to NodeInfo
This commit is contained in:
@@ -95,6 +95,16 @@ void ApiServer::onSystemStatusRequest(AsyncWebServerRequest *request) {
|
||||
apiObj["uri"] = std::get<0>(entry);
|
||||
apiObj["method"] = std::get<1>(entry);
|
||||
}
|
||||
// Include local node labels if present
|
||||
if (ctx.memberList) {
|
||||
auto it = ctx.memberList->find(ctx.hostname);
|
||||
if (it != ctx.memberList->end()) {
|
||||
JsonObject labelsObj = doc["labels"].to<JsonObject>();
|
||||
for (const auto& kv : it->second.labels) {
|
||||
labelsObj[kv.first.c_str()] = kv.second;
|
||||
}
|
||||
}
|
||||
}
|
||||
String json;
|
||||
serializeJson(doc, json);
|
||||
request->send(200, "application/json", json);
|
||||
@@ -122,6 +132,13 @@ void ApiServer::onClusterMembersRequest(AsyncWebServerRequest *request) {
|
||||
apiObj["uri"] = std::get<0>(endpoint);
|
||||
methodToStr(endpoint, apiObj);
|
||||
}
|
||||
// Add labels if present
|
||||
if (!node.labels.empty()) {
|
||||
JsonObject labelsObj = obj["labels"].to<JsonObject>();
|
||||
for (const auto& kv : node.labels) {
|
||||
labelsObj[kv.first.c_str()] = kv.second;
|
||||
}
|
||||
}
|
||||
}
|
||||
String json;
|
||||
serializeJson(doc, json);
|
||||
|
||||
@@ -118,6 +118,16 @@ void ClusterManager::fetchNodeInfo(const IPAddress& ip) {
|
||||
node.apiEndpoints.push_back(std::make_tuple(uri, method));
|
||||
}
|
||||
}
|
||||
// Parse labels if present
|
||||
node.labels.clear();
|
||||
if (doc["labels"].is<JsonObject>()) {
|
||||
JsonObject labelsObj = doc["labels"].as<JsonObject>();
|
||||
for (JsonPair kvp : labelsObj) {
|
||||
String k = String(kvp.key().c_str());
|
||||
String v = String(labelsObj[kvp.key()]);
|
||||
node.labels[k] = v;
|
||||
}
|
||||
}
|
||||
Serial.printf("[Cluster] Fetched info for node: %s @ %s\n", node.hostname.c_str(), ip.toString().c_str());
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -54,5 +54,7 @@ void NetworkManager::setupWiFi() {
|
||||
}
|
||||
self.lastSeen = millis();
|
||||
self.status = NodeInfo::ACTIVE;
|
||||
// Initialize a default label for demonstration; users can modify at runtime
|
||||
self.labels["hostname"] = ctx.hostname;
|
||||
ctx.fire("node_discovered", &self);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user