Skip to content

Commit

Permalink
Add Metrics sets and Job definitions (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
HuwCampbell authored Feb 21, 2024
2 parents 57acea2 + 2bd89e5 commit ae6f4a2
Show file tree
Hide file tree
Showing 30 changed files with 2,059 additions and 444 deletions.
90 changes: 90 additions & 0 deletions client/metrics_job.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package anaml

import (
"encoding/json"
"fmt"
"net/http"
"strings"
)

func (c *Client) GetMetricsJob(MetricsJobID string) (*MetricsJob, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/metrics-job/%s", c.HostURL, MetricsJobID), nil)
if err != nil {
return nil, err
}

body, err := c.doRequest(req)
if err != nil {
return nil, err
}
if body == nil {
return nil, nil
}

MetricsJob := MetricsJob{}
err = json.Unmarshal(body, &MetricsJob)
if err != nil {
return nil, err
}

return &MetricsJob, nil
}

func (c *Client) CreateMetricsJob(creationRequest MetricsJob) (*MetricsJob, error) {
rb, err := json.Marshal(creationRequest)
if err != nil {
return nil, err
}

req, err := http.NewRequest("POST", fmt.Sprintf("%s/metrics-job", c.HostURL), strings.NewReader(string(rb)))
if err != nil {
return nil, err
}

body, err := c.doRequest(req)
if err != nil {
return nil, err
}

var V int
err = json.Unmarshal(body, &V)
if err != nil {
return nil, err
}

creationRequest.ID = V
return &creationRequest, nil
}

func (c *Client) UpdateMetricsJob(MetricsJobID string, creationRequest MetricsJob) error {
rb, err := json.Marshal(creationRequest)
if err != nil {
return err
}

req, err := http.NewRequest("PUT", fmt.Sprintf("%s/metrics-job/%s", c.HostURL, MetricsJobID), strings.NewReader(string(rb)))
if err != nil {
return err
}

_, err = c.doRequest(req)
if err != nil {
return err
}

return nil
}

func (c *Client) DeleteMetricsJob(MetricsJobID string) error {
req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/metrics-job/%s", c.HostURL, MetricsJobID), nil)
if err != nil {
return err
}

_, err = c.doRequest(req)
if err != nil {
return err
}

return nil
}
90 changes: 90 additions & 0 deletions client/metrics_set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package anaml

import (
"encoding/json"
"fmt"
"net/http"
"strings"
)

func (c *Client) GetMetricsSet(MetricsSetID string) (*MetricsSet, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/metrics-set/%s", c.HostURL, MetricsSetID), nil)
if err != nil {
return nil, err
}

body, err := c.doRequest(req)
if err != nil {
return nil, err
}
if body == nil {
return nil, nil
}

MetricsSet := MetricsSet{}
err = json.Unmarshal(body, &MetricsSet)
if err != nil {
return nil, err
}

return &MetricsSet, nil
}

func (c *Client) CreateMetricsSet(creationRequest MetricsSet) (*MetricsSet, error) {
rb, err := json.Marshal(creationRequest)
if err != nil {
return nil, err
}

req, err := http.NewRequest("POST", fmt.Sprintf("%s/metrics-set", c.HostURL), strings.NewReader(string(rb)))
if err != nil {
return nil, err
}

body, err := c.doRequest(req)
if err != nil {
return nil, err
}

var V int
err = json.Unmarshal(body, &V)
if err != nil {
return nil, err
}

creationRequest.ID = V
return &creationRequest, nil
}

func (c *Client) UpdateMetricsSet(MetricsSetID string, creationRequest MetricsSet) error {
rb, err := json.Marshal(creationRequest)
if err != nil {
return err
}

req, err := http.NewRequest("PUT", fmt.Sprintf("%s/metrics-set/%s", c.HostURL, MetricsSetID), strings.NewReader(string(rb)))
if err != nil {
return err
}

_, err = c.doRequest(req)
if err != nil {
return err
}

return nil
}

func (c *Client) DeleteMetricsSet(MetricsSetID string) error {
req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/metrics-set/%s", c.HostURL, MetricsSetID), nil)
if err != nil {
return err
}

_, err = c.doRequest(req)
if err != nil {
return err
}

return nil
}
107 changes: 95 additions & 12 deletions client/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,42 @@ type EventDescription struct {
TimestampInfo *TimestampInfo `json:"timestampInfo"`
}

// ColumnRepresentation ..
type ColumnRepresentation struct {
Type string `json:"adt_type"`
Expression *string `json:"expression,omitempty"`
}

// ColumnKind ..
type ColumnKind struct {
Type string `json:"adt_type"`
Units *string `json:"units,omitempty"`
}

// ColumnInfo ..
type ColumnInfo struct {
Description string `json:"description"`
Column *ColumnRepresentation `json:"column"`
Kind *ColumnKind `json:"kind,omitempty"`
}

// Table ...
type Table struct {
ID int `json:"id,omitempty"`
Name string `json:"name"`
Description string `json:"description"`
Type string `json:"adt_type"`
Sources []int `json:"sources,omitempty"`
Source *SourceReference `json:"source,omitempty"`
Expression string `json:"expression,omitempty"`
EventInfo *EventDescription `json:"eventDescription,omitempty"`
EntityMapping int `json:"entityMapping,omitempty"`
ExtraFeatures []int `json:"extraFeatures,omitempty"`
Labels []string `json:"labels"`
Attributes []Attribute `json:"attributes"`
ID int `json:"id,omitempty"`
Name string `json:"name"`
Description string `json:"description"`
Type string `json:"adt_type"`
Sources []int `json:"sources,omitempty"`
Source *SourceReference `json:"source,omitempty"`
Expression string `json:"expression,omitempty"`
EventInfo *EventDescription `json:"eventDescription,omitempty"`
EntityMapping int `json:"entityMapping,omitempty"`
ExtraFeatures []int `json:"extraFeatures,omitempty"`
Base *int `json:"base,omitempty"`
Joins []int `json:"joins,omitempty"`
Columns map[string]ColumnInfo `json:"columns,omitempty"`
Labels []string `json:"labels"`
Attributes []Attribute `json:"attributes"`
}

// EventWindow ...
Expand Down Expand Up @@ -139,6 +161,50 @@ type FeatureSet struct {
Attributes []Attribute `json:"attributes"`
}

// MetricsSource ...
type MetricsSource struct {
Type string `json:"adt_type"`
FeatureSet *int `json:"featureset,omitempty"`
Table *int `json:"table,omitempty"`
Joins []int `json:"joins,omitempty"`
}

type TypeTag struct {
Type string `json:"adt_type"`
}

// Dimension ...
type Dimension struct {
Type string `json:"adt_type"`
Granularity *TypeTag `json:"granularity,omitempty"`
Edge *TypeTag `json:"edgeSemantics,omitempty"`
Back *int `json:"back,omitempty"`
Name *string `json:"name,omitempty"`
Expression *string `json:"expression,omitempty"`
Filter *string `json:"filterExpression,omitempty"`
}

// Dimension ...
type Metric struct {
Name *string `json:"name,omitempty"`
Select SQLExpression `json:"select"`
Filter *SQLExpression `json:"filter"`
Aggregate *AggregateExpression `json:"aggregate,omitempty"`
PostAggExpr *SQLExpression `json:"postAggregateExpr"`
}

// MetricsSet ...
type MetricsSet struct {
ID int `json:"id,omitempty"`
Name string `json:"name"`
Description string `json:"description"`
Labels []string `json:"labels"`
Attributes []Attribute `json:"attributes"`
Source MetricsSource `json:"source"`
Dimensions []Dimension `json:"dimensions"`
Metrics []Metric `json:"metrics"`
}

// VersionTarget ...
type VersionTarget struct {
Type string `json:"adt_type"`
Expand Down Expand Up @@ -171,6 +237,23 @@ type FeatureStore struct {
VersionTarget *VersionTarget `json:"versionTarget,omitempty"`
}

// MetricsJob ...
type MetricsJob struct {
ID int `json:"id,omitempty"`
Name string `json:"name"`
Description string `json:"description"`
Labels []string `json:"labels"`
Attributes []Attribute `json:"attributes"`
MetricsSet int `json:"metricsSet"`
Enabled bool `json:"enabled"`
Schedule *Schedule `json:"schedule"`
Destinations []DestinationReference `json:"destinations"`
Cluster int `json:"cluster"`
ClusterPropertySets []int `json:"clusterPropertySets"`
Principal int `json:"principal"`
VersionTarget *VersionTarget `json:"versionTarget,omitempty"`
}

type Schedule struct {
Type string `json:"adt_type"`
StartTimeOfDay *string `json:"startTimeOfDay,omitempty"`
Expand Down
33 changes: 33 additions & 0 deletions client/resource_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,39 @@ import (
"strconv"
)

func getAnamlId(d *schema.ResourceData, key string) (int, error) {
if raw, ok := d.GetOk(key); ok {
if i, err := strconv.Atoi(raw.(string)); err == nil {
return i, nil
} else {
return 0, err
}
}
return 0, fmt.Errorf("Required Identifier %s is missing", key)
}

func getAnamlIdPointer(d *schema.ResourceData, key string) *int {
if raw, ok := d.GetOk(key); ok {
if i, err := strconv.Atoi(raw.(string)); err == nil {
return &i
} else {
return nil
}
}
return nil
}

func getStringPointer(d *schema.ResourceData, key string) *string {
if raw, ok := d.GetOk(key); ok {
if i, ok := raw.(string); ok {
return &i
} else {
return nil
}
}
return nil
}

func labelSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeString,
Expand Down
1 change: 1 addition & 0 deletions client/resource_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func ResourceEntity() *schema.Resource {
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"default_column", "entities"},
ValidateFunc: validation.StringIsNotWhiteSpace,
},
"required_type": {
Type: schema.TypeString,
Expand Down
12 changes: 6 additions & 6 deletions client/resource_entity_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ func resourceEntityMappingRead(d *schema.ResourceData, m interface{}) error {

func resourceEntityMappingCreate(d *schema.ResourceData, m interface{}) error {
c := m.(*Client)
from, _ := strconv.Atoi(d.Get("from").(string))
to, _ := strconv.Atoi(d.Get("to").(string))
feat, _ := strconv.Atoi(d.Get("mapping").(string))
from, _ := getAnamlId(d, "from")
to, _ := getAnamlId(d, "to")
feat, _ := getAnamlId(d, "mapping")

mapping := EntityMapping{
From: from,
Expand All @@ -131,9 +131,9 @@ func resourceEntityMappingCreate(d *schema.ResourceData, m interface{}) error {
func resourceEntityMappingUpdate(d *schema.ResourceData, m interface{}) error {
c := m.(*Client)
mappingID := d.Id()
from, _ := strconv.Atoi(d.Get("from").(string))
to, _ := strconv.Atoi(d.Get("to").(string))
feat, _ := strconv.Atoi(d.Get("mapping").(string))
from, _ := getAnamlId(d, "from")
to, _ := getAnamlId(d, "to")
feat, _ := getAnamlId(d, "mapping")

mapping := EntityMapping{
From: from,
Expand Down
2 changes: 1 addition & 1 deletion client/resource_entity_population.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func resourceEntityPopulationDelete(d *schema.ResourceData, m interface{}) error
}

func buildPopulation(d *schema.ResourceData) EntityPopulation {
entity, _ := strconv.Atoi(d.Get("entity").(string))
entity, _ := getAnamlId(d, "entity")
return EntityPopulation{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Expand Down
Loading

0 comments on commit ae6f4a2

Please sign in to comment.