monitor_dashboards.go raw

   1  package linodego
   2  
   3  import (
   4  	"context"
   5  	"encoding/json"
   6  	"time"
   7  
   8  	"github.com/linode/linodego/internal/parseabletime"
   9  )
  10  
  11  // MonitorDashboard represents an ACLP Dashboard object
  12  type MonitorDashboard struct {
  13  	ID          int               `json:"id"`
  14  	Type        DashboardType     `json:"type"`
  15  	ServiceType ServiceType       `json:"service_type"`
  16  	Label       string            `json:"label"`
  17  	Created     *time.Time        `json:"-"`
  18  	Updated     *time.Time        `json:"-"`
  19  	Widgets     []DashboardWidget `json:"widgets"`
  20  }
  21  
  22  // ServiceType is an enum object for serviceType
  23  type ServiceType string
  24  
  25  const (
  26  	ServiceTypeLinode          ServiceType = "linode"
  27  	ServiceTypeLKE             ServiceType = "lke"
  28  	ServiceTypeDBaaS           ServiceType = "dbaas"
  29  	ServiceTypeACLB            ServiceType = "aclb"
  30  	ServiceTypeNodeBalancer    ServiceType = "nodebalancer"
  31  	ServiceTypeObjectStorage   ServiceType = "object_storage"
  32  	ServiceTypeVPC             ServiceType = "vpc"
  33  	ServiceTypeFirewallService ServiceType = "firewall"
  34  	ServiceTypeNetLoadBalancer ServiceType = "netloadbalancer"
  35  )
  36  
  37  // DashboardType is an enum object for DashboardType
  38  type DashboardType string
  39  
  40  const (
  41  	DashboardTypeStandard DashboardType = "standard"
  42  	DashboardTypeCustom   DashboardType = "custom"
  43  )
  44  
  45  // DashboardWidget represents an ACLP DashboardWidget object
  46  type DashboardWidget struct {
  47  	Metric            string            `json:"metric"`
  48  	Unit              string            `json:"unit"`
  49  	Label             string            `json:"label"`
  50  	Color             string            `json:"color"`
  51  	Size              int               `json:"size"`
  52  	ChartType         ChartType         `json:"chart_type"`
  53  	YLabel            string            `json:"y_label"`
  54  	AggregateFunction AggregateFunction `json:"aggregate_function"`
  55  	GroupBy           []string          `json:"group_by"`
  56  	Filters           []DashboardFilter `json:"filters"`
  57  }
  58  
  59  // DashboardFilter represents a filter for dashboard widgets
  60  type DashboardFilter struct {
  61  	DimensionLabel string `json:"dimension_label"`
  62  	Operator       string `json:"operator"`
  63  	Value          string `json:"value"`
  64  }
  65  
  66  // AggregateFunction is an enum object for AggregateFunction
  67  type AggregateFunction string
  68  
  69  const (
  70  	AggregateFunctionMin      AggregateFunction = "min"
  71  	AggregateFunctionMax      AggregateFunction = "max"
  72  	AggregateFunctionAvg      AggregateFunction = "avg"
  73  	AggregateFunctionSum      AggregateFunction = "sum"
  74  	AggregateFunctionRate     AggregateFunction = "rate"
  75  	AggregateFunctionIncrease AggregateFunction = "increase"
  76  	AggregateFunctionCount    AggregateFunction = "count"
  77  	AggregateFunctionLast     AggregateFunction = "last"
  78  )
  79  
  80  // ChartType is an enum object for Chart type
  81  type ChartType string
  82  
  83  const (
  84  	ChartTypeLine ChartType = "line"
  85  	ChartTypeArea ChartType = "area"
  86  )
  87  
  88  // ListMonitorDashboards lists all the ACLP Monitor Dashboards
  89  func (c *Client) ListMonitorDashboards(ctx context.Context, opts *ListOptions) ([]MonitorDashboard, error) {
  90  	return getPaginatedResults[MonitorDashboard](ctx, c, "monitor/dashboards", opts)
  91  }
  92  
  93  // GetMonitorDashboard gets an ACLP Monitor Dashboard for a given dashboardID
  94  func (c *Client) GetMonitorDashboard(ctx context.Context, dashboardID int) (*MonitorDashboard, error) {
  95  	e := formatAPIPath("monitor/dashboards/%d", dashboardID)
  96  	return doGETRequest[MonitorDashboard](ctx, c, e)
  97  }
  98  
  99  // ListMonitorDashboardsByServiceType lists ACLP Monitor Dashboards for a given serviceType
 100  func (c *Client) ListMonitorDashboardsByServiceType(ctx context.Context, serviceType string, opts *ListOptions) ([]MonitorDashboard, error) {
 101  	e := formatAPIPath("monitor/services/%s/dashboards", serviceType)
 102  	return getPaginatedResults[MonitorDashboard](ctx, c, e, opts)
 103  }
 104  
 105  // UnmarshalJSON implements the json.Unmarshaler interface
 106  func (i *MonitorDashboard) UnmarshalJSON(b []byte) error {
 107  	type Mask MonitorDashboard
 108  
 109  	p := struct {
 110  		*Mask
 111  
 112  		Created *parseabletime.ParseableTime `json:"created"`
 113  		Updated *parseabletime.ParseableTime `json:"updated"`
 114  	}{
 115  		Mask: (*Mask)(i),
 116  	}
 117  
 118  	if err := json.Unmarshal(b, &p); err != nil {
 119  		return err
 120  	}
 121  
 122  	i.Created = (*time.Time)(p.Created)
 123  	i.Updated = (*time.Time)(p.Updated)
 124  
 125  	return nil
 126  }
 127