Prometheus Endpoint: Simplify code by looping over fields instead of duplicated code
This commit is contained in:
parent
4bf9083b23
commit
6429d64062
@ -13,33 +13,38 @@ public:
|
|||||||
private:
|
private:
|
||||||
void onPrometheusMetricsGet(AsyncWebServerRequest* request);
|
void onPrometheusMetricsGet(AsyncWebServerRequest* request);
|
||||||
|
|
||||||
void addField(AsyncResponseStream* stream, String& serial, uint8_t idx, std::shared_ptr<InverterAbstract> inv, ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId, const char* channelName = NULL);
|
void addField(AsyncResponseStream* stream, String& serial, uint8_t idx, std::shared_ptr<InverterAbstract> inv, ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId, const char* metricName, const char* channelName = NULL);
|
||||||
|
|
||||||
void addPanelInfo(AsyncResponseStream* stream, String& serial, uint8_t idx, std::shared_ptr<InverterAbstract> inv, ChannelType_t type, ChannelNum_t channel);
|
void addPanelInfo(AsyncResponseStream* stream, String& serial, uint8_t idx, std::shared_ptr<InverterAbstract> inv, ChannelType_t type, ChannelNum_t channel);
|
||||||
|
|
||||||
AsyncWebServer* _server;
|
AsyncWebServer* _server;
|
||||||
|
|
||||||
enum {
|
enum MetricType_t {
|
||||||
METRIC_TYPE_NONE = 0,
|
NONE = 0,
|
||||||
METRIC_TYPE_GAUGE,
|
GAUGE,
|
||||||
METRIC_TYPE_COUNTER,
|
COUNTER,
|
||||||
};
|
};
|
||||||
const char* _metricTypes[3] = { 0, "gauge", "counter" };
|
const char* _metricTypes[3] = { 0, "gauge", "counter" };
|
||||||
|
|
||||||
std::map<FieldId_t, uint8_t> _fieldMetricAssignment {
|
struct publish_type_t {
|
||||||
{ FLD_UDC, METRIC_TYPE_GAUGE },
|
FieldId_t field;
|
||||||
{ FLD_IDC, METRIC_TYPE_GAUGE },
|
MetricType_t type;
|
||||||
{ FLD_PDC, METRIC_TYPE_GAUGE },
|
};
|
||||||
{ FLD_YD, METRIC_TYPE_COUNTER },
|
|
||||||
{ FLD_YT, METRIC_TYPE_COUNTER },
|
const publish_type_t _publishFields[14] = {
|
||||||
{ FLD_UAC, METRIC_TYPE_GAUGE },
|
{ FLD_PAC, MetricType_t::GAUGE },
|
||||||
{ FLD_IAC, METRIC_TYPE_GAUGE },
|
{ FLD_UAC, MetricType_t::GAUGE },
|
||||||
{ FLD_PAC, METRIC_TYPE_GAUGE },
|
{ FLD_IAC, MetricType_t::GAUGE },
|
||||||
{ FLD_F, METRIC_TYPE_GAUGE },
|
{ FLD_PDC, MetricType_t::GAUGE },
|
||||||
{ FLD_T, METRIC_TYPE_GAUGE },
|
{ FLD_UDC, MetricType_t::GAUGE },
|
||||||
{ FLD_PF, METRIC_TYPE_GAUGE },
|
{ FLD_IDC, MetricType_t::GAUGE },
|
||||||
{ FLD_EFF, METRIC_TYPE_GAUGE },
|
{ FLD_YD, MetricType_t::COUNTER },
|
||||||
{ FLD_IRR, METRIC_TYPE_GAUGE },
|
{ FLD_YT, MetricType_t::COUNTER },
|
||||||
{ FLD_Q, METRIC_TYPE_GAUGE }
|
{ FLD_F, MetricType_t::GAUGE },
|
||||||
|
{ FLD_T, MetricType_t::GAUGE },
|
||||||
|
{ FLD_PF, MetricType_t::GAUGE },
|
||||||
|
{ FLD_Q, MetricType_t::GAUGE },
|
||||||
|
{ FLD_EFF, MetricType_t::GAUGE },
|
||||||
|
{ FLD_IRR, MetricType_t::GAUGE },
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@ -74,24 +74,13 @@ void WebApiPrometheusClass::onPrometheusMetricsGet(AsyncWebServerRequest* reques
|
|||||||
for (auto& t : inv->Statistics()->getChannelTypes()) {
|
for (auto& t : inv->Statistics()->getChannelTypes()) {
|
||||||
for (auto& c : inv->Statistics()->getChannelsByType(t)) {
|
for (auto& c : inv->Statistics()->getChannelsByType(t)) {
|
||||||
addPanelInfo(stream, serial, i, inv, t, c);
|
addPanelInfo(stream, serial, i, inv, t, c);
|
||||||
addField(stream, serial, i, inv, t, c, FLD_PAC);
|
for (uint8_t f = 0; f < sizeof(_publishFields) / sizeof(_publishFields[0]); f++) {
|
||||||
addField(stream, serial, i, inv, t, c, FLD_UAC);
|
if (t == TYPE_AC && _publishFields[f].field == FLD_PDC) {
|
||||||
addField(stream, serial, i, inv, t, c, FLD_IAC);
|
addField(stream, serial, i, inv, t, c, _publishFields[f].field, _metricTypes[_publishFields[f].type], "PowerDC");
|
||||||
if (t == TYPE_AC) {
|
|
||||||
addField(stream, serial, i, inv, t, c, FLD_PDC, "PowerDC");
|
|
||||||
} else {
|
} else {
|
||||||
addField(stream, serial, i, inv, t, c, FLD_PDC);
|
addField(stream, serial, i, inv, t, c, _publishFields[f].field, _metricTypes[_publishFields[f].type]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
addField(stream, serial, i, inv, t, c, FLD_UDC);
|
|
||||||
addField(stream, serial, i, inv, t, c, FLD_IDC);
|
|
||||||
addField(stream, serial, i, inv, t, c, FLD_YD);
|
|
||||||
addField(stream, serial, i, inv, t, c, FLD_YT);
|
|
||||||
addField(stream, serial, i, inv, t, c, FLD_F);
|
|
||||||
addField(stream, serial, i, inv, t, c, FLD_T);
|
|
||||||
addField(stream, serial, i, inv, t, c, FLD_PF);
|
|
||||||
addField(stream, serial, i, inv, t, c, FLD_Q);
|
|
||||||
addField(stream, serial, i, inv, t, c, FLD_EFF);
|
|
||||||
addField(stream, serial, i, inv, t, c, FLD_IRR);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -106,13 +95,13 @@ void WebApiPrometheusClass::onPrometheusMetricsGet(AsyncWebServerRequest* reques
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebApiPrometheusClass::addField(AsyncResponseStream* stream, String& serial, uint8_t idx, std::shared_ptr<InverterAbstract> inv, ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId, const char* channelName)
|
void WebApiPrometheusClass::addField(AsyncResponseStream* stream, String& serial, uint8_t idx, std::shared_ptr<InverterAbstract> inv, ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId, const char* metricName, const char* channelName)
|
||||||
{
|
{
|
||||||
if (inv->Statistics()->hasChannelFieldValue(type, channel, fieldId)) {
|
if (inv->Statistics()->hasChannelFieldValue(type, channel, fieldId)) {
|
||||||
const char* chanName = (channelName == NULL) ? inv->Statistics()->getChannelFieldName(type, channel, fieldId) : channelName;
|
const char* chanName = (channelName == NULL) ? inv->Statistics()->getChannelFieldName(type, channel, fieldId) : channelName;
|
||||||
if (idx == 0 && type == TYPE_AC && channel == 0) {
|
if (idx == 0 && type == TYPE_AC && channel == 0) {
|
||||||
stream->printf("# HELP opendtu_%s in %s\n", chanName, inv->Statistics()->getChannelFieldUnit(type, channel, fieldId));
|
stream->printf("# HELP opendtu_%s in %s\n", chanName, inv->Statistics()->getChannelFieldUnit(type, channel, fieldId));
|
||||||
stream->printf("# TYPE opendtu_%s %s\n", chanName, _metricTypes[_fieldMetricAssignment[fieldId]]);
|
stream->printf("# TYPE opendtu_%s %s\n", chanName, metricName);
|
||||||
}
|
}
|
||||||
stream->printf("opendtu_%s{serial=\"%s\",unit=\"%d\",name=\"%s\",type=\"%s\",channel=\"%d\"} %s\n",
|
stream->printf("opendtu_%s{serial=\"%s\",unit=\"%d\",name=\"%s\",type=\"%s\",channel=\"%d\"} %s\n",
|
||||||
chanName,
|
chanName,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user