2022-07-25 09:19:07 -05:00
|
|
|
package correlations
|
|
|
|
|
|
|
|
import (
|
2022-10-04 03:39:55 -05:00
|
|
|
"encoding/json"
|
2022-07-25 09:19:07 -05:00
|
|
|
"errors"
|
2022-10-04 03:39:55 -05:00
|
|
|
"fmt"
|
2022-07-25 09:19:07 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrSourceDataSourceReadOnly = errors.New("source data source is read only")
|
|
|
|
ErrSourceDataSourceDoesNotExists = errors.New("source data source does not exist")
|
|
|
|
ErrTargetDataSourceDoesNotExists = errors.New("target data source does not exist")
|
|
|
|
ErrCorrelationFailedGenerateUniqueUid = errors.New("failed to generate unique correlation UID")
|
2022-07-27 03:07:58 -05:00
|
|
|
ErrCorrelationNotFound = errors.New("correlation not found")
|
2022-08-03 08:18:51 -05:00
|
|
|
ErrUpdateCorrelationEmptyParams = errors.New("not enough parameters to edit correlation")
|
2022-10-04 03:39:55 -05:00
|
|
|
ErrInvalidConfigType = errors.New("invalid correlation config type")
|
2022-07-25 09:19:07 -05:00
|
|
|
)
|
|
|
|
|
2022-10-04 03:39:55 -05:00
|
|
|
type CorrelationConfigType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
ConfigTypeQuery CorrelationConfigType = "query"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (t CorrelationConfigType) Validate() error {
|
|
|
|
if t != ConfigTypeQuery {
|
|
|
|
return fmt.Errorf("%s: \"%s\"", ErrInvalidConfigType, t)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-09-27 04:08:02 -05:00
|
|
|
|
|
|
|
// swagger:model
|
|
|
|
type CorrelationConfig struct {
|
|
|
|
// Field used to attach the correlation link
|
|
|
|
// required:true
|
2022-10-11 08:04:55 -05:00
|
|
|
// example: message
|
2022-10-04 03:39:55 -05:00
|
|
|
Field string `json:"field" binding:"Required"`
|
|
|
|
// Target type
|
|
|
|
// required:true
|
|
|
|
Type CorrelationConfigType `json:"type" binding:"Required"`
|
2022-09-27 04:08:02 -05:00
|
|
|
// Target data query
|
|
|
|
// required:true
|
2022-10-11 08:04:55 -05:00
|
|
|
// example: { "expr": "job=app" }
|
2022-10-04 03:39:55 -05:00
|
|
|
Target map[string]interface{} `json:"target" binding:"Required"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c CorrelationConfig) MarshalJSON() ([]byte, error) {
|
|
|
|
target := c.Target
|
|
|
|
if target == nil {
|
|
|
|
target = map[string]interface{}{}
|
|
|
|
}
|
|
|
|
return json.Marshal(struct {
|
|
|
|
Type CorrelationConfigType `json:"type"`
|
|
|
|
Field string `json:"field"`
|
|
|
|
Target map[string]interface{} `json:"target"`
|
|
|
|
}{
|
|
|
|
Type: ConfigTypeQuery,
|
|
|
|
Field: c.Field,
|
|
|
|
Target: target,
|
|
|
|
})
|
2022-09-27 04:08:02 -05:00
|
|
|
}
|
|
|
|
|
2022-07-25 09:19:07 -05:00
|
|
|
// Correlation is the model for correlations definitions
|
2022-09-27 04:08:02 -05:00
|
|
|
// swagger:model
|
2022-07-25 09:19:07 -05:00
|
|
|
type Correlation struct {
|
|
|
|
// Unique identifier of the correlation
|
|
|
|
// example: 50xhMlg9k
|
|
|
|
UID string `json:"uid" xorm:"pk 'uid'"`
|
|
|
|
// UID of the data source the correlation originates from
|
|
|
|
// example:d0oxYRg4z
|
2022-07-27 01:01:46 -05:00
|
|
|
SourceUID string `json:"sourceUID" xorm:"pk 'source_uid'"`
|
2022-07-25 09:19:07 -05:00
|
|
|
// UID of the data source the correlation points to
|
|
|
|
// example:PE1C5CBDA0504A6A3
|
2022-10-04 03:39:55 -05:00
|
|
|
TargetUID *string `json:"targetUID" xorm:"target_uid"`
|
2022-07-25 09:19:07 -05:00
|
|
|
// Label identifying the correlation
|
|
|
|
// example: My Label
|
|
|
|
Label string `json:"label" xorm:"label"`
|
|
|
|
// Description of the correlation
|
|
|
|
// example: Logs to Traces
|
|
|
|
Description string `json:"description" xorm:"description"`
|
2022-09-27 04:08:02 -05:00
|
|
|
// Correlation Configuration
|
|
|
|
Config CorrelationConfig `json:"config" xorm:"jsonb config"`
|
2022-07-25 09:19:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateCorrelationResponse is the response struct for CreateCorrelationCommand
|
|
|
|
// swagger:model
|
2022-07-27 08:54:37 -05:00
|
|
|
type CreateCorrelationResponseBody struct {
|
2022-07-25 09:19:07 -05:00
|
|
|
Result Correlation `json:"result"`
|
|
|
|
// example: Correlation created
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateCorrelationCommand is the command for creating a correlation
|
|
|
|
// swagger:model
|
|
|
|
type CreateCorrelationCommand struct {
|
|
|
|
// UID of the data source for which correlation is created.
|
|
|
|
SourceUID string `json:"-"`
|
|
|
|
OrgId int64 `json:"-"`
|
|
|
|
SkipReadOnlyCheck bool `json:"-"`
|
2022-10-11 08:04:55 -05:00
|
|
|
// Target data source UID to which the correlation is created. required if config.type = query
|
2022-07-25 09:19:07 -05:00
|
|
|
// example:PE1C5CBDA0504A6A3
|
2022-10-04 03:39:55 -05:00
|
|
|
TargetUID *string `json:"targetUID"`
|
2022-07-25 09:19:07 -05:00
|
|
|
// Optional label identifying the correlation
|
|
|
|
// example: My label
|
|
|
|
Label string `json:"label"`
|
|
|
|
// Optional description of the correlation
|
|
|
|
// example: Logs to Traces
|
|
|
|
Description string `json:"description"`
|
2022-09-27 04:08:02 -05:00
|
|
|
// Arbitrary configuration object handled in frontend
|
2022-10-04 03:39:55 -05:00
|
|
|
Config CorrelationConfig `json:"config" binding:"Required"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c CreateCorrelationCommand) Validate() error {
|
|
|
|
if err := c.Config.Type.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if c.TargetUID == nil && c.Config.Type == ConfigTypeQuery {
|
|
|
|
return fmt.Errorf("correlations of type \"%s\" must have a targetUID", ConfigTypeQuery)
|
|
|
|
}
|
|
|
|
return nil
|
2022-07-25 09:19:07 -05:00
|
|
|
}
|
|
|
|
|
2022-07-27 03:07:58 -05:00
|
|
|
// swagger:model
|
2022-07-27 08:54:37 -05:00
|
|
|
type DeleteCorrelationResponseBody struct {
|
2022-07-27 03:07:58 -05:00
|
|
|
// example: Correlation deleted
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteCorrelationCommand is the command for deleting a correlation
|
|
|
|
type DeleteCorrelationCommand struct {
|
|
|
|
// UID of the correlation to be deleted.
|
|
|
|
UID string
|
|
|
|
SourceUID string
|
|
|
|
OrgId int64
|
|
|
|
}
|
|
|
|
|
2022-08-03 08:18:51 -05:00
|
|
|
// swagger:model
|
|
|
|
type UpdateCorrelationResponseBody struct {
|
|
|
|
Result Correlation `json:"result"`
|
|
|
|
// example: Correlation updated
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
2022-10-11 08:04:55 -05:00
|
|
|
// swagger:model
|
|
|
|
type CorrelationConfigUpdateDTO struct {
|
|
|
|
// Field used to attach the correlation link
|
|
|
|
// example: message
|
|
|
|
Field *string `json:"field"`
|
|
|
|
// Target type
|
|
|
|
Type *CorrelationConfigType `json:"type"`
|
|
|
|
// Target data query
|
|
|
|
// example: { "expr": "job=app" }
|
|
|
|
Target *map[string]interface{} `json:"target"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c CorrelationConfigUpdateDTO) Validate() error {
|
|
|
|
if c.Type != nil {
|
|
|
|
if err := c.Type.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-03 08:18:51 -05:00
|
|
|
// UpdateCorrelationCommand is the command for updating a correlation
|
2022-10-11 08:04:55 -05:00
|
|
|
// swagger:model
|
2022-08-03 08:18:51 -05:00
|
|
|
type UpdateCorrelationCommand struct {
|
2022-10-11 08:04:55 -05:00
|
|
|
// UID of the correlation to be updated.
|
2022-08-03 08:18:51 -05:00
|
|
|
UID string `json:"-"`
|
|
|
|
SourceUID string `json:"-"`
|
|
|
|
OrgId int64 `json:"-"`
|
|
|
|
|
|
|
|
// Optional label identifying the correlation
|
|
|
|
// example: My label
|
|
|
|
Label *string `json:"label"`
|
|
|
|
// Optional description of the correlation
|
|
|
|
// example: Logs to Traces
|
|
|
|
Description *string `json:"description"`
|
2022-10-10 08:36:14 -05:00
|
|
|
// Correlation Configuration
|
|
|
|
Config *CorrelationConfigUpdateDTO `json:"config"`
|
2022-08-03 08:18:51 -05:00
|
|
|
}
|
|
|
|
|
2022-10-11 08:04:55 -05:00
|
|
|
func (c UpdateCorrelationCommand) Validate() error {
|
|
|
|
if c.Config != nil {
|
|
|
|
if err := c.Config.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Label == nil && c.Description == nil && (c.Config == nil || (c.Config.Field == nil && c.Config.Type == nil && c.Config.Target == nil)) {
|
|
|
|
return ErrUpdateCorrelationEmptyParams
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-11 10:58:11 -05:00
|
|
|
// GetCorrelationQuery is the query to retrieve a single correlation
|
|
|
|
type GetCorrelationQuery struct {
|
|
|
|
// UID of the correlation
|
|
|
|
UID string `json:"-"`
|
|
|
|
// UID of the source data source
|
|
|
|
SourceUID string `json:"-"`
|
|
|
|
OrgId int64 `json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCorrelationsBySourceUIDQuery is the query to retrieve all correlations originating by the given Data Source
|
|
|
|
type GetCorrelationsBySourceUIDQuery struct {
|
|
|
|
SourceUID string `json:"-"`
|
|
|
|
OrgId int64 `json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCorrelationsQuery is the query to retrieve all correlations
|
|
|
|
type GetCorrelationsQuery struct {
|
|
|
|
OrgId int64 `json:"-"`
|
|
|
|
}
|
|
|
|
|
2022-07-25 09:19:07 -05:00
|
|
|
type DeleteCorrelationsBySourceUIDCommand struct {
|
|
|
|
SourceUID string
|
|
|
|
}
|
|
|
|
|
|
|
|
type DeleteCorrelationsByTargetUIDCommand struct {
|
|
|
|
TargetUID string
|
|
|
|
}
|