Skip to content

Commit

Permalink
Changed data frame name
Browse files Browse the repository at this point in the history
  • Loading branch information
coderReview committed Apr 5, 2024
1 parent 88f0ddf commit 039c3b4
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 62 deletions.
70 changes: 58 additions & 12 deletions pkg/plugin/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"net/http"
"reflect"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -351,6 +352,55 @@ func compatible(actual reflect.Type, expected reflect.Type) bool {
a == reflect.Float64)
}

func getDataLabels(useNewFormat bool, q *PiProcessedQuery, pointType string, summaryLabel string) map[string]string {
var frameLabel map[string]string
summaryNewFormat := ""

if summaryLabel != "" {
summaryNewFormat = "\" summaryType=\"" + summaryLabel
summaryLabel = "[" + summaryLabel + "]"
}

var label string
if useNewFormat {
label = q.Label
} else {
label = q.Label + summaryLabel
}

if q.IsPIPoint {
// New format returns the full path with metadata
// PiPoint {element="PISERVER", name="Attribute", type="Float32"}
targetParts := strings.Split(q.FullTargetPath, `\`)
frameLabel = map[string]string{
"element": targetParts[0],
"name": label,
"type": pointType + summaryNewFormat,
}
} else {
// New format returns the full path with metadata
// Element|Attribute {element="Element", name="Attribute", type="Single"}
targetParts := strings.Split(q.FullTargetPath, `\`)
labelParts := strings.SplitN(targetParts[len(targetParts)-1], "|", 2)
frameLabel = map[string]string{
"element": labelParts[0],
"name": label,
"type": pointType + summaryNewFormat,
}
}

// Use ReplaceAllString to replace all instances of the search pattern with the replacement string
// FIXME: This is working, but graph panels seem to not render the trend.
if q.isRegexQuery() {
regex := regexp.MustCompile(*q.Regex.Search)
frameLabel["name"] = regex.ReplaceAllString(frameLabel["name"], *q.Regex.Replace)
} else if q.Display != nil && strings.TrimSpace(*q.Display) != "" {
// Old format with display name
frameLabel["name"] = strings.TrimSpace(*q.Display)
}
return frameLabel
}

func convertItemsToDataFrame(processedQuery *PiProcessedQuery, d *Datasource, SummaryType string) (*data.Frame, error) {
items := *processedQuery.Response.getItems(SummaryType)
webID := processedQuery.WebID
Expand All @@ -371,18 +421,15 @@ func convertItemsToDataFrame(processedQuery *PiProcessedQuery, d *Datasource, Su

// get frame name
frameLabel := getDataLabels(d.isUsingNewFormat(), processedQuery, d.getPointTypeForWebID(webID), SummaryType)
dataFrameName := frameLabel["name"]

var labels map[string]string
var digitalState = d.getDigitalStateForWebID(webID)

frame := data.NewFrame(frameLabel["element"])
frame := data.NewFrame("")
if d.isUsingNewFormat() {
labels = frameLabel
}

backend.Logger.Debug("Convert", "frame", dataFrameName, "labels", labels, "type", sliceType.Elem().String())

for i, item := range items {
if item.Value == nil {
backend.Logger.Warn("item.Value is nil", "value", item.Value, "item", item)
Expand Down Expand Up @@ -464,21 +511,20 @@ func convertItemsToDataFrame(processedQuery *PiProcessedQuery, d *Datasource, Su
// in the slice type, or values that are not "good"
valuepointers := convertSliceToPointers(fP.values, fP.badValues)

fieldConfig := &data.FieldConfig{}
if includeMetaData {
fieldConfig.Unit = d.getUnitsForWebID(webID)
fieldConfig.Description = d.getDescriptionForWebID(webID)
}

timeField := data.NewField("time", nil, fP.timestamps)
if !digitalState || !digitalStates {
valueField := data.NewField(dataFrameName, labels, valuepointers)
valueField := data.NewField(frameLabel["name"], labels, valuepointers)
frame.Fields = append(frame.Fields,
timeField,
valueField,
)
} else {
valueField := data.NewField(dataFrameName, labels, digitalStateValues)
fieldConfig := &data.FieldConfig{}
if includeMetaData {
fieldConfig.Unit = d.getUnitsForWebID(webID)
fieldConfig.Description = d.getDescriptionForWebID(webID)
}
valueField := data.NewField(frameLabel["name"], labels, digitalStateValues)
valueField.SetConfig(fieldConfig)
frame.Fields = append(frame.Fields,
timeField,
Expand Down
50 changes: 0 additions & 50 deletions pkg/plugin/timeseries_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,56 +319,6 @@ func (q *PIWebAPIQuery) isSummary() bool {
return *q.Summary.Basis != "" && len(*q.Summary.Types) > 0
}

func getDataLabels(useNewFormat bool, q *PiProcessedQuery, pointType string, summaryLabel string) map[string]string {
var frameLabel map[string]string
summaryNewFormat := ""

if summaryLabel != "" {
summaryNewFormat = "\" summaryType=\"" + summaryLabel
summaryLabel = "[" + summaryLabel + "]"
}

if useNewFormat {
if q.IsPIPoint {
// New format returns the full path with metadata
// PiPoint {element="PISERVER", name="Attribute", type="Float32"}
targetParts := strings.Split(q.FullTargetPath, `\`)
frameLabel = map[string]string{
"element": targetParts[0],
"name": q.Label,
"type": pointType + summaryNewFormat,
}
} else {
// New format returns the full path with metadata
// Element|Attribute {element="Element", name="Attribute", type="Single"}
targetParts := strings.Split(q.FullTargetPath, `\`)
labelParts := strings.SplitN(targetParts[len(targetParts)-1], "|", 2)
frameLabel = map[string]string{
"element": labelParts[0],
"name": q.Label,
"type": pointType + summaryNewFormat,
}
}
} else {
// Old format returns just the tag/attribute name
frameLabel = map[string]string{
"element": q.Label,
"name": q.Label + summaryLabel,
}
}

// Use ReplaceAllString to replace all instances of the search pattern with the replacement string
// FIXME: This is working, but graph panels seem to not render the trend.
if q.isRegexQuery() {
regex := regexp.MustCompile(*q.Regex.Search)
frameLabel["name"] = regex.ReplaceAllString(frameLabel["name"], *q.Regex.Replace)
} else if q.Display != nil && strings.TrimSpace(*q.Display) != "" {
// Old format with display name
frameLabel["name"] = strings.TrimSpace(*q.Display)
}
return frameLabel
}

// PiProcessedQuery isRegex returns true if the query is a regex query and is enabled
func (q *PiProcessedQuery) isRegex() bool {
if q.Regex == nil {
Expand Down

0 comments on commit 039c3b4

Please sign in to comment.