VolkovLabs / business-forms

The Business Forms panel is a conceptually new plugin for Grafana. It is the first plugin that allows inserting and updating application data, as well as modifying configuration directly from your Grafana dashboard.
https://docs.volkovlabs.io
Apache License 2.0
86 stars 10 forks source link

Multiple initial fields from query source cannot be distinguished #505

Closed octal-ip closed 1 month ago

octal-ip commented 1 month ago

When using a query that returns multiple measurements as the source for initial field values, they cannot be distinguished in the initial query fields drop-down list.

For example, when using the following query:

from(bucket: "Inverter")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "Power_switch_enable" or
                       r["_measurement"] == "Factory_reset_enable" or
                       r["_measurement"] == "PV_curve_enable" or
                       r["_measurement"] == "Limiter_enable" or
                       r["_measurement"] == "RSD_enable")
  |> last()

The query fields drop-down list will just show "A:Time' and "A:value" for each measurement, making them indistinguishable from each other. They also can't be individually selected in the drop-down list. {B08E4093-ADE6-4B60-8E52-152146B52186}

The workaround for this would be to make a separate query for every field, however this would be very cumbersome for forms containing more than a few fields.

Either I'm trying to do this wrong, or a suitable fix would be to include the measurement name in the query field drop-down menu. This would result in: "A:Power_switch_enable:Time" and "A:Power_switch_enable:value", etc.

mikhail-vl commented 1 month ago

@octal-ip Please confirm the data source is Influx.

Could you please send us how the data looks like as Table View and in the inspector.

octal-ip commented 1 month ago

Yes, I'm using Flux to query InfluxDB.

Sure thing, here's the output of the inspector:

{
  "request": {
    "url": "api/ds/query?ds_type=influxdb",
    "method": "POST",
    "data": {
      "queries": [
        {
          "query": "from(bucket: \"Inverter\")\r\n  |> range(start: -7d, stop: now())\r\n  |> filter(fn: (r) => r[\"_measurement\"] == \"Power_switch_enable\" or\r\n                       r[\"_measurement\"] == \"Factory_reset_enable\" or\r\n                       r[\"_measurement\"] == \"Island_protection_enable\" or\r\n                       r[\"_measurement\"] == \"MTTP_enable\" or\r\n                       r[\"_measurement\"] == \"GFDI_enable\" or\r\n                       r[\"_measurement\"] == \"RCD_enable\" or\r\n                       r[\"_measurement\"] == \"RISO_enable\" or\r\n                       r[\"_measurement\"] == \"PV_curve_enable\" or\r\n                       r[\"_measurement\"] == \"Limiter_enable\" or\r\n                       r[\"_measurement\"] == \"RSD_enable\")\r\n  |> filter(fn: (r) => r[\"MAC\"] =~ /d83addd8d2ab/)\r\n  |> last()",
          "datasource": {
            "type": "influxdb",
            "uid": "ddxu7noheujggc"
          },
          "datasourceId": 1
        }
      ]
    },
    "hideFromInspector": false
  },
  "response": {
    "results": {
      "A": {
        "status": 200,
        "frames": [
          {
            "schema": {
              "name": "Factory_reset_enable",
              "refId": "A",
              "meta": {
                "typeVersion": [
                  0,
                  0
                ],
                "executedQueryString": "from(bucket: \"Inverter\")\r\n  |> range(start: -7d, stop: now())\r\n  |> filter(fn: (r) => r[\"_measurement\"] == \"Power_switch_enable\" or\r\n                       r[\"_measurement\"] == \"Factory_reset_enable\" or\r\n                       r[\"_measurement\"] == \"Island_protection_enable\" or\r\n                       r[\"_measurement\"] == \"MTTP_enable\" or\r\n                       r[\"_measurement\"] == \"GFDI_enable\" or\r\n                       r[\"_measurement\"] == \"RCD_enable\" or\r\n                       r[\"_measurement\"] == \"RISO_enable\" or\r\n                       r[\"_measurement\"] == \"PV_curve_enable\" or\r\n                       r[\"_measurement\"] == \"Limiter_enable\" or\r\n                       r[\"_measurement\"] == \"RSD_enable\")\r\n  |> filter(fn: (r) => r[\"MAC\"] =~ /d83addd8d2ab/)\r\n  |> last()"
              },
              "fields": [
                {
                  "name": "Time",
                  "type": "time",
                  "typeInfo": {
                    "frame": "time.Time",
                    "nullable": true
                  }
                },
                {
                  "name": "value",
                  "type": "number",
                  "typeInfo": {
                    "frame": "float64",
                    "nullable": true
                  },
                  "labels": {
                    "MAC": "d83addffffff",
                    "sensor": "Inverter"
                  }
                }
              ]
            },
            "data": {
              "values": [
                [
                  1727419794084
                ],
                [
                  0
                ]
              ],
              "nanos": [
                [
                  788710
                ],
                null
              ]
            }
          },
          {
            "schema": {
              "name": "GFDI_enable",
              "refId": "A",
              "fields": [
                {
                  "name": "Time",
                  "type": "time",
                  "typeInfo": {
                    "frame": "time.Time",
                    "nullable": true
                  }
                },
                {
                  "name": "value",
                  "type": "number",
                  "typeInfo": {
                    "frame": "float64",
                    "nullable": true
                  },
                  "labels": {
                    "MAC": "d83addffffff",
                    "sensor": "Inverter"
                  }
                }
              ]
            },
            "data": {
              "values": [
                [
                  1727419794084
                ],
                [
                  0
                ]
              ],
              "nanos": [
                [
                  788710
                ],
                null
              ]
            }
          },
          {
            "schema": {
              "name": "Island_protection_enable",
              "refId": "A",
              "fields": [
                {
                  "name": "Time",
                  "type": "time",
                  "typeInfo": {
                    "frame": "time.Time",
                    "nullable": true
                  }
                },
                {
                  "name": "value",
                  "type": "number",
                  "typeInfo": {
                    "frame": "float64",
                    "nullable": true
                  },
                  "labels": {
                    "MAC": "d83addffffff",
                    "sensor": "Inverter"
                  }
                }
              ]
            },
            "data": {
              "values": [
                [
                  1727419794084
                ],
                [
                  0
                ]
              ],
              "nanos": [
                [
                  788710
                ],
                null
              ]
            }
          },
          {
            "schema": {
              "name": "Limiter_enable",
              "refId": "A",
              "fields": [
                {
                  "name": "Time",
                  "type": "time",
                  "typeInfo": {
                    "frame": "time.Time",
                    "nullable": true
                  }
                },
                {
                  "name": "value",
                  "type": "number",
                  "typeInfo": {
                    "frame": "float64",
                    "nullable": true
                  },
                  "labels": {
                    "MAC": "d83addffffff",
                    "sensor": "Inverter"
                  }
                }
              ]
            },
            "data": {
              "values": [
                [
                  1727419794084
                ],
                [
                  0
                ]
              ],
              "nanos": [
                [
                  788710
                ],
                null
              ]
            }
          },
          {
            "schema": {
              "name": "MTTP_enable",
              "refId": "A",
              "fields": [
                {
                  "name": "Time",
                  "type": "time",
                  "typeInfo": {
                    "frame": "time.Time",
                    "nullable": true
                  }
                },
                {
                  "name": "value",
                  "type": "number",
                  "typeInfo": {
                    "frame": "float64",
                    "nullable": true
                  },
                  "labels": {
                    "MAC": "d83addffffff",
                    "sensor": "Inverter"
                  }
                }
              ]
            },
            "data": {
              "values": [
                [
                  1727419794084
                ],
                [
                  0
                ]
              ],
              "nanos": [
                [
                  788710
                ],
                null
              ]
            }
          },
          {
            "schema": {
              "name": "PV_curve_enable",
              "refId": "A",
              "fields": [
                {
                  "name": "Time",
                  "type": "time",
                  "typeInfo": {
                    "frame": "time.Time",
                    "nullable": true
                  }
                },
                {
                  "name": "value",
                  "type": "number",
                  "typeInfo": {
                    "frame": "float64",
                    "nullable": true
                  },
                  "labels": {
                    "MAC": "d83addffffff",
                    "sensor": "Inverter"
                  }
                }
              ]
            },
            "data": {
              "values": [
                [
                  1727419794084
                ],
                [
                  0
                ]
              ],
              "nanos": [
                [
                  788710
                ],
                null
              ]
            }
          },
          {
            "schema": {
              "name": "Power_switch_enable",
              "refId": "A",
              "fields": [
                {
                  "name": "Time",
                  "type": "time",
                  "typeInfo": {
                    "frame": "time.Time",
                    "nullable": true
                  }
                },
                {
                  "name": "value",
                  "type": "number",
                  "typeInfo": {
                    "frame": "float64",
                    "nullable": true
                  },
                  "labels": {
                    "MAC": "d83addffffff",
                    "sensor": "Inverter"
                  }
                }
              ]
            },
            "data": {
              "values": [
                [
                  1727419794084
                ],
                [
                  1
                ]
              ],
              "nanos": [
                [
                  788710
                ],
                null
              ]
            }
          },
          {
            "schema": {
              "name": "RCD_enable",
              "refId": "A",
              "fields": [
                {
                  "name": "Time",
                  "type": "time",
                  "typeInfo": {
                    "frame": "time.Time",
                    "nullable": true
                  }
                },
                {
                  "name": "value",
                  "type": "number",
                  "typeInfo": {
                    "frame": "float64",
                    "nullable": true
                  },
                  "labels": {
                    "MAC": "d83addffffff",
                    "sensor": "Inverter"
                  }
                }
              ]
            },
            "data": {
              "values": [
                [
                  1727419794084
                ],
                [
                  0
                ]
              ],
              "nanos": [
                [
                  788710
                ],
                null
              ]
            }
          },
          {
            "schema": {
              "name": "RISO_enable",
              "refId": "A",
              "fields": [
                {
                  "name": "Time",
                  "type": "time",
                  "typeInfo": {
                    "frame": "time.Time",
                    "nullable": true
                  }
                },
                {
                  "name": "value",
                  "type": "number",
                  "typeInfo": {
                    "frame": "float64",
                    "nullable": true
                  },
                  "labels": {
                    "MAC": "d83addffffff",
                    "sensor": "Inverter"
                  }
                }
              ]
            },
            "data": {
              "values": [
                [
                  1727419794084
                ],
                [
                  0
                ]
              ],
              "nanos": [
                [
                  788710
                ],
                null
              ]
            }
          },
          {
            "schema": {
              "name": "RSD_enable",
              "refId": "A",
              "fields": [
                {
                  "name": "Time",
                  "type": "time",
                  "typeInfo": {
                    "frame": "time.Time",
                    "nullable": true
                  }
                },
                {
                  "name": "value",
                  "type": "number",
                  "typeInfo": {
                    "frame": "float64",
                    "nullable": true
                  },
                  "labels": {
                    "MAC": "d83addffffff",
                    "sensor": "Inverter"
                  }
                }
              ]
            },
            "data": {
              "values": [
                [
                  1727419794084
                ],
                [
                  0.01
                ]
              ],
              "nanos": [
                [
                  788710
                ],
                null
              ]
            }
          }
        ]
      }
    }
  }
}

And as a table: image

octal-ip commented 1 month ago

Thank you very much for this fix - I've updated to 4.7.0 today and everything seems to be working.

Note that a variable I'm using for the dashboard (MAC) gets included with the query field selection (e.g. "A:Exporttime value {MAC=\"d83addffffff\", sensor=\"Inverter\"}") which ends up hard coded into the dashboard, and the initial values won't adjust to a change in the dashboard variable. I've managed to work around this by adding "keep(columns: ["_measurement", "value"])" to the flux query which excludes these unnecessary columns. This seems to work OK in my quick testing.

mikhail-vl commented 1 month ago

@octal-ip Thank you for confirming.

We use Grafana data frame's ids as they retrieved from the data source. Business Forms supports any data source and it's up to the user to name them properly.