Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transformations: Use an explicit join seperator when converting from an array to string field #80169

Merged
merged 6 commits into from
Jan 9, 2024

Conversation

ryantxu
Copy link
Member

@ryantxu ryantxu commented Jan 8, 2024

This PR allows people more control over the object > string conversion function. Currently it is just JSON encoded, this PR allows specifying an explicit separator for arrays.

image image
Key Value
Panel table @ 10.3.0-pre
Grafana 10.3.0-pre (6fbd6e3) // Enterprise
Panel debug snapshot dashboard
{
  "panels": [
    {
      "datasource": {
        "type": "grafana",
        "uid": "grafana"
      },
      "fieldConfig": {
        "defaults": {
          "custom": {
            "align": "auto",
            "cellOptions": {
              "type": "auto"
            },
            "inspect": false
          },
          "unitScale": true,
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "green",
                "value": null
              },
              {
                "color": "red",
                "value": 80
              }
            ]
          },
          "color": {
            "mode": "thresholds"
          }
        },
        "overrides": []
      },
      "gridPos": {
        "h": 13,
        "w": 15,
        "x": 0,
        "y": 0
      },
      "id": 2,
      "options": {
        "showHeader": true,
        "cellHeight": "sm",
        "footer": {
          "show": false,
          "reducer": [
            "sum"
          ],
          "countRows": false,
          "fields": ""
        }
      },
      "pluginVersion": "10.3.0-pre",
      "targets": [
        {
          "refId": "A",
          "datasource": {
            "type": "grafana",
            "uid": "grafana"
          },
          "queryType": "snapshot",
          "snapshot": [
            {
              "schema": {
                "refId": "A",
                "fields": [
                  {
                    "name": "TheValues",
                    "type": "string",
                    "typeInfo": {
                      "frame": "string",
                      "nullable": true
                    },
                    "config": {}
                  }
                ]
              },
              "data": {
                "values": [
                  [
                    "aaa",
                    "bbb",
                    "aaa",
                    "ccc"
                  ]
                ]
              }
            }
          ]
        }
      ],
      "title": "Reproduced with embedded data",
      "transformations": [
        {
          "id": "reduce",
          "options": {
            "includeTimeField": false,
            "mode": "reduceFields",
            "reducers": [
              "uniqueValues"
            ]
          }
        },
        {
          "id": "convertFieldType",
          "options": {
            "fields": {},
            "conversions": [
              {
                "destinationType": "string",
                "joinWith": "|",
                "targetField": "TheValues"
              }
            ]
          }
        }
      ],
      "type": "table"
    },
    {
      "gridPos": {
        "h": 7,
        "w": 9,
        "x": 15,
        "y": 0
      },
      "id": 5,
      "options": {
        "content": "<table width=\"100%\">\n    <tr>\n      <th width=\"2%\">Panel</th>\n      <td >table @ 10.3.0-pre</td>\n    </tr>\n    <tr>\n      <th>Queries</th>\n      <td>A[grafana-testdata-datasource]</td>\n    </tr>\n    <tr>\n      <th>Transform</th>\n      <td>reduce, convertFieldType</td>\n  </tr>\n    <tr><th>Data</th><td> 1 frames, 1 fields, 4 rows </td></tr>\n    \n    <tr>\n      <th>Grafana</th>\n      <td>10.3.0-pre (6fbd6e3d310) // Enterprise</td>\n    </tr>\n  </table>",
        "mode": "html"
      },
      "title": "Debug info",
      "type": "text"
    },
    {
      "id": 6,
      "title": "Original Panel JSON",
      "type": "text",
      "gridPos": {
        "h": 13,
        "w": 9,
        "x": 15,
        "y": 7
      },
      "options": {
        "content": "{\n  \"datasource\": {\n    \"uid\": \"PD8C576611E62080A\",\n    \"type\": \"grafana-testdata-datasource\"\n  },\n  \"fieldConfig\": {\n    \"defaults\": {\n      \"custom\": {\n        \"align\": \"auto\",\n        \"cellOptions\": {\n          \"type\": \"auto\"\n        },\n        \"inspect\": false\n      },\n      \"unitScale\": true,\n      \"mappings\": [],\n      \"thresholds\": {\n        \"mode\": \"absolute\",\n        \"steps\": [\n          {\n            \"color\": \"green\",\n            \"value\": null\n          },\n          {\n            \"color\": \"red\",\n            \"value\": 80\n          }\n        ]\n      },\n      \"color\": {\n        \"mode\": \"thresholds\"\n      }\n    },\n    \"overrides\": []\n  },\n  \"gridPos\": {\n    \"h\": 8,\n    \"w\": 12,\n    \"x\": 0,\n    \"y\": 0\n  },\n  \"id\": 1,\n  \"options\": {\n    \"showHeader\": true,\n    \"cellHeight\": \"sm\",\n    \"footer\": {\n      \"show\": false,\n      \"reducer\": [\n        \"sum\"\n      ],\n      \"countRows\": false,\n      \"fields\": \"\"\n    }\n  },\n  \"pluginVersion\": \"10.3.0-pre\",\n  \"targets\": [\n    {\n      \"scenarioId\": \"csv_content\",\n      \"refId\": \"A\",\n      \"datasource\": {\n        \"type\": \"grafana-testdata-datasource\",\n        \"uid\": \"PD8C576611E62080A\"\n      },\n      \"csvContent\": \"TheValues\\naaa\\nbbb\\naaa\\nccc\"\n    }\n  ],\n  \"title\": \"Panel Title\",\n  \"transformations\": [\n    {\n      \"id\": \"reduce\",\n      \"options\": {\n        \"includeTimeField\": false,\n        \"mode\": \"reduceFields\",\n        \"reducers\": [\n          \"uniqueValues\"\n        ]\n      }\n    },\n    {\n      \"id\": \"convertFieldType\",\n      \"options\": {\n        \"fields\": {},\n        \"conversions\": [\n          {\n            \"destinationType\": \"string\",\n            \"joinWith\": \"|\",\n            \"targetField\": \"TheValues\"\n          }\n        ]\n      }\n    }\n  ],\n  \"type\": \"table\"\n}",
        "mode": "code",
        "code": {
          "language": "json",
          "showLineNumbers": true,
          "showMiniMap": true
        }
      }
    },
    {
      "id": 3,
      "title": "Data from panel above (after transformations)",
      "type": "table",
      "datasource": {
        "type": "datasource",
        "uid": "-- Dashboard --"
      },
      "gridPos": {
        "h": 7,
        "w": 15,
        "x": 0,
        "y": 13
      },
      "options": {
        "showTypeIcons": true
      },
      "targets": [
        {
          "datasource": {
            "type": "datasource",
            "uid": "-- Dashboard --"
          },
          "panelId": 2,
          "withTransforms": true,
          "refId": "A"
        }
      ]
    },
    {
      "id": 100,
      "title": "Data (before transformations)",
      "type": "table",
      "datasource": {
        "type": "datasource",
        "uid": "-- Dashboard --"
      },
      "gridPos": {
        "h": 7,
        "w": 24,
        "x": 0,
        "y": 13
      },
      "options": {
        "showTypeIcons": true
      },
      "targets": [
        {
          "datasource": {
            "type": "datasource",
            "uid": "-- Dashboard --"
          },
          "panelId": 2,
          "withTransforms": false,
          "refId": "A"
        }
      ]
    }
  ],
  "schemaVersion": 37,
  "title": "Debug: Panel Title // 2024-01-08 10:52:43",
  "tags": [
    "debug",
    "debug-table"
  ],
  "time": {
    "from": "2024-01-08T12:52:43.703Z",
    "to": "2024-01-08T18:52:43.703Z"
  }
}

@ryantxu ryantxu added add to changelog no-backport Skip backport of PR labels Jan 8, 2024
@ryantxu ryantxu added this to the 10.3.x milestone Jan 8, 2024
@ryantxu ryantxu requested review from a team, codeincarnate and oscarkilhed and removed request for a team January 8, 2024 18:53
@ryantxu ryantxu modified the milestones: 10.3.x, 10.4.x Jan 8, 2024
@grafana-pr-automation grafana-pr-automation bot requested review from a team and oshirohugo and removed request for a team January 8, 2024 19:05
@@ -224,6 +224,7 @@ export function reduceFields(data: DataFrame[], matcher: FieldMatcher, reducerId
const value = results[reducer];
const copy = {
...field,
type: guessFieldTypeFromValue(value),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

matches the behavior we have in reduceSeriesToRows(...) rather than always keeping the same type!

@grafana-pr-automation grafana-pr-automation bot requested review from a team and removed request for a team January 9, 2024 02:12
Copy link
Contributor

@codeincarnate codeincarnate left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looked though and other than a minor nit looks good. Tested out and appears to be working fine as well 😄

<>
{(c.joinWith?.length || targetField?.type === FieldType.other) && (
<InlineField label="Join with" tooltip="Use an explicit separator when joining array values">
<Input value={c.joinWith} placeholder={'json'} onChange={onJoinWithChange(idx)} width={9} />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JSON I believe is all caps.

@ryantxu ryantxu enabled auto-merge (squash) January 9, 2024 16:14
@grafana-pr-automation grafana-pr-automation bot requested review from a team and removed request for a team January 9, 2024 16:18
@ryantxu ryantxu merged commit 114845a into main Jan 9, 2024
17 checks passed
@ryantxu ryantxu deleted the convert-join-with-xform branch January 9, 2024 16:24
@grafana-delivery-bot grafana-delivery-bot bot removed this from the 10.4.x milestone Jan 9, 2024
@grafana-delivery-bot grafana-delivery-bot bot added this to the 10.3.x milestone Jan 9, 2024
@ifrost ifrost modified the milestones: 10.3.x, 10.4.x Jan 12, 2024
zserge pushed a commit that referenced this pull request Jan 22, 2024
@aangelisc aangelisc modified the milestones: 10.4.x, 10.4.0 Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants