mirror of
https://github.com/grafana/grafana.git
synced 2024-11-29 12:14:08 -06:00
4cd2575ad0
Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
42 lines
1.0 KiB
Go
42 lines
1.0 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
)
|
|
|
|
type AutoJsonConverter struct {
|
|
config AutoJsonConverterConfig
|
|
nowTimeFunc func() time.Time
|
|
}
|
|
|
|
func NewAutoJsonConverter(c AutoJsonConverterConfig) *AutoJsonConverter {
|
|
return &AutoJsonConverter{config: c}
|
|
}
|
|
|
|
const ConverterTypeJsonAuto = "jsonAuto"
|
|
|
|
func (c *AutoJsonConverter) Type() string {
|
|
return ConverterTypeJsonAuto
|
|
}
|
|
|
|
// Automatic conversion works this way:
|
|
// * Time added automatically
|
|
// * Nulls dropped
|
|
// To preserve nulls we need FieldTips from a user.
|
|
// Custom time can be injected on FrameProcessor stage theoretically.
|
|
// Custom labels can be injected on FrameProcessor stage theoretically.
|
|
func (c *AutoJsonConverter) Convert(_ context.Context, vars Vars, body []byte) ([]*ChannelFrame, error) {
|
|
nowTimeFunc := c.nowTimeFunc
|
|
if nowTimeFunc == nil {
|
|
nowTimeFunc = time.Now
|
|
}
|
|
frame, err := jsonDocToFrame(vars.Path, body, c.config.FieldTips, nowTimeFunc)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return []*ChannelFrame{
|
|
{Channel: "", Frame: frame},
|
|
}, nil
|
|
}
|