github.com/status-im/status-go@v1.1.0/centralizedmetrics/common/structs.go (about)

     1  package common
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/google/uuid"
     7  )
     8  
     9  var (
    10  	ErrInvalidEventName = errors.New("centralized-metric: invalid-event-name")
    11  	ErrInvalidPlatform  = errors.New("centralized-metric: invalid-platform")
    12  	ErrInvalidVersion   = errors.New("centralized-metric: invalid-version")
    13  )
    14  
    15  type Metric struct {
    16  	ID         string         `json:"id"`
    17  	UserID     string         `json:"userId"`
    18  	EventName  string         `json:"eventName"`
    19  	EventValue map[string]any `json:"eventValue"`
    20  	Timestamp  int64          `json:"timestamp"`
    21  	Platform   string         `json:"platform"`
    22  	AppVersion string         `json:"appVersion"`
    23  }
    24  
    25  type MetricProcessor interface {
    26  	Process(metrics []Metric) error
    27  }
    28  
    29  func (m *Metric) Validate() error {
    30  	if len(m.EventName) == 0 {
    31  		return ErrInvalidEventName
    32  	}
    33  	if len(m.Platform) == 0 {
    34  		return ErrInvalidPlatform
    35  	}
    36  	if len(m.AppVersion) == 0 {
    37  		return ErrInvalidVersion
    38  	}
    39  	return nil
    40  }
    41  
    42  func (m *Metric) EnsureID() {
    43  
    44  	if len(m.ID) != 0 {
    45  		return
    46  	}
    47  	m.ID = uuid.New().String()
    48  }