Skip to content

Commit

Permalink
Updated helper API functions
Browse files Browse the repository at this point in the history
  • Loading branch information
coderReview committed Apr 5, 2024
1 parent 1e8afab commit 8fcd832
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 61 deletions.
4 changes: 2 additions & 2 deletions pkg/plugin/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func (d *Datasource) QueryAnnotations(ctx context.Context, req *backend.QueryDat

span.AddEvent("Generated PI API URL for annotation query")

r, err := d.apiBatchRequest(ctx, batchReq)
r, err := apiBatchRequest(ctx, d, batchReq)
if err != nil {
return nil, fmt.Errorf("error getting data from PI Web API: %w", err)
}
Expand Down Expand Up @@ -280,7 +280,7 @@ func (d *Datasource) CallResource(ctx context.Context, req *backend.CallResource
})
}

r, err := d.apiGet(ctx, req.URL)
r, err := apiGet(ctx, d, req.URL)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/plugin/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func replaceAccentsWithEscape(s string) string {

// apiGet performs a GET request against the PI Web API. It returns the response body as a byte slice.
// If the request fails, an error is returned.
func (d Datasource) apiGet(ctx context.Context, path string) ([]byte, error) {
func apiGet(ctx context.Context, d *Datasource, path string) ([]byte, error) {
var uri = d.settings.URL
var pathEscaped = replaceAccentsWithEscape(path)
if strings.HasSuffix(uri, "/") {
Expand Down Expand Up @@ -86,7 +86,7 @@ func (d Datasource) apiGet(ctx context.Context, path string) ([]byte, error) {

// apiBatchRequest performs a batch request against the PI Web API. It returns the response body as a byte slice.
// If the request fails, an error is returned.
func (d *Datasource) apiBatchRequest(ctx context.Context, BatchSubRequests interface{}) ([]byte, error) {
func apiBatchRequest(ctx context.Context, d *Datasource, BatchSubRequests interface{}) ([]byte, error) {
var uri = d.settings.URL
if strings.HasSuffix(uri, "/") {
uri = uri + "batch"
Expand Down
2 changes: 1 addition & 1 deletion pkg/plugin/timeseries_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (d *Datasource) batchRequest(ctx context.Context, PIWebAPIQueries map[strin
}

// request the data from the PI Web API
batchRequestResponse, err := d.apiBatchRequest(ctx, batchRequest)
batchRequestResponse, err := apiBatchRequest(ctx, d, batchRequest)

// process response
if err != nil {
Expand Down
56 changes: 0 additions & 56 deletions pkg/plugin/webidcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,62 +173,6 @@ func (d *Datasource) _saveWebID(data interface{}, path string, isPiPoint bool) s
return entry.WebID
}

// func (d *Datasource) getWebID(ctx context.Context, path string, isPiPoint bool) (WebIDCacheEntry, error) {
// entry, ok := d.webIDCache.webIDCache[path]

// if ok && time.Now().Before(entry.ExpTime) {
// log.DefaultLogger.Debug("WebID cache hit", "path", path)
// d.webIDCache.webIDPaths[entry.WebID] = path
// return entry, nil
// }
// entry = WebIDCacheEntry{}
// c, err := d.requestWebID(ctx, path, isPiPoint)
// if err != nil {
// log.DefaultLogger.Error("WebID cache error", "path", path, "error", err)
// return entry, err
// }
// if c.WebID == "" {
// err = errors.New("WebID not found")
// log.DefaultLogger.Error("WebID cache error", "path", path, "error", err)
// return entry, err
// }
// d.webIDCache.webIDCache[path] = c
// d.webIDCache.webIDPaths[c.WebID] = path
// return c, nil
// }

// func (d *Datasource) requestWebID(ctx context.Context, path string, isPiPoint bool) (WebIDCacheEntry, error) {
// uri := ""
// if isPiPoint {
// uri = `points?selectedFields=WebId;Name;Path;PointType;DigitalSetName;Descriptor;EngineeringUnits&path=\\`
// uri += strings.Replace(strings.Replace(path, "|", `\`, -1), ";", `\`, -1)
// } else {
// uri = `attributes?selectedFields=WebId;Name;Path;Type;DigitalSetName;Description;DefaultUnitsName&path=\\`
// uri += path
// }
// r, e := d.apiGet(ctx, uri)
// if e != nil {
// return WebIDCacheEntry{}, e
// }
// var response WebIDResponse
// if isPiPoint {
// response = &WebIDResponsePiPoint{}
// } else {
// response = &WebIDResponseAttribute{}
// }
// json.Unmarshal(r, &response)

// return WebIDCacheEntry{
// Path: path,
// WebID: response.getWebID(),
// Type: getValueType(response.getType()),
// DigitalState: response.getDigitalSetName() != "",
// ExpTime: time.Now().Add(d.webIDCache.duration),
// PointType: response.getType(),
// Units: response.getUnits(),
// }, nil
// }

func getValueType(Type string) reflect.Type {
var dataType reflect.Type
switch Type {
Expand Down

0 comments on commit 8fcd832

Please sign in to comment.