diff --git a/include/WebApi_prometheus.h b/include/WebApi_prometheus.h index 4a77acd..b03f817 100644 --- a/include/WebApi_prometheus.h +++ b/include/WebApi_prometheus.h @@ -13,33 +13,38 @@ public: private: void onPrometheusMetricsGet(AsyncWebServerRequest* request); - void addField(AsyncResponseStream* stream, String& serial, uint8_t idx, std::shared_ptr 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 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 inv, ChannelType_t type, ChannelNum_t channel); AsyncWebServer* _server; - enum { - METRIC_TYPE_NONE = 0, - METRIC_TYPE_GAUGE, - METRIC_TYPE_COUNTER, + enum MetricType_t { + NONE = 0, + GAUGE, + COUNTER, }; const char* _metricTypes[3] = { 0, "gauge", "counter" }; - std::map _fieldMetricAssignment { - { FLD_UDC, METRIC_TYPE_GAUGE }, - { FLD_IDC, METRIC_TYPE_GAUGE }, - { FLD_PDC, METRIC_TYPE_GAUGE }, - { FLD_YD, METRIC_TYPE_COUNTER }, - { FLD_YT, METRIC_TYPE_COUNTER }, - { FLD_UAC, METRIC_TYPE_GAUGE }, - { FLD_IAC, METRIC_TYPE_GAUGE }, - { FLD_PAC, METRIC_TYPE_GAUGE }, - { FLD_F, METRIC_TYPE_GAUGE }, - { FLD_T, METRIC_TYPE_GAUGE }, - { FLD_PF, METRIC_TYPE_GAUGE }, - { FLD_EFF, METRIC_TYPE_GAUGE }, - { FLD_IRR, METRIC_TYPE_GAUGE }, - { FLD_Q, METRIC_TYPE_GAUGE } + struct publish_type_t { + FieldId_t field; + MetricType_t type; + }; + + const publish_type_t _publishFields[14] = { + { FLD_PAC, MetricType_t::GAUGE }, + { FLD_UAC, MetricType_t::GAUGE }, + { FLD_IAC, MetricType_t::GAUGE }, + { FLD_PDC, MetricType_t::GAUGE }, + { FLD_UDC, MetricType_t::GAUGE }, + { FLD_IDC, MetricType_t::GAUGE }, + { FLD_YD, MetricType_t::COUNTER }, + { FLD_YT, MetricType_t::COUNTER }, + { 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 }, }; }; \ No newline at end of file diff --git a/src/WebApi_prometheus.cpp b/src/WebApi_prometheus.cpp index 968438c..3decacf 100644 --- a/src/WebApi_prometheus.cpp +++ b/src/WebApi_prometheus.cpp @@ -74,24 +74,13 @@ void WebApiPrometheusClass::onPrometheusMetricsGet(AsyncWebServerRequest* reques for (auto& t : inv->Statistics()->getChannelTypes()) { for (auto& c : inv->Statistics()->getChannelsByType(t)) { addPanelInfo(stream, serial, i, inv, t, c); - addField(stream, serial, i, inv, t, c, FLD_PAC); - addField(stream, serial, i, inv, t, c, FLD_UAC); - addField(stream, serial, i, inv, t, c, FLD_IAC); - if (t == TYPE_AC) { - addField(stream, serial, i, inv, t, c, FLD_PDC, "PowerDC"); - } else { - addField(stream, serial, i, inv, t, c, FLD_PDC); + for (uint8_t f = 0; f < sizeof(_publishFields) / sizeof(_publishFields[0]); f++) { + if (t == TYPE_AC && _publishFields[f].field == FLD_PDC) { + addField(stream, serial, i, inv, t, c, _publishFields[f].field, _metricTypes[_publishFields[f].type], "PowerDC"); + } else { + 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 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 inv, ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId, const char* metricName, const char* channelName) { if (inv->Statistics()->hasChannelFieldValue(type, channel, fieldId)) { const char* chanName = (channelName == NULL) ? inv->Statistics()->getChannelFieldName(type, channel, fieldId) : channelName; 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("# 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", chanName,