-
Notifications
You must be signed in to change notification settings - Fork 23
/
router.go
77 lines (65 loc) · 2.55 KB
/
router.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package models
import (
"database/sql"
"encoding/json"
"fmt"
"strings"
)
type RouterStatus string
const (
RouterStatusPending RouterStatus = "pending"
RouterStatusFailed RouterStatus = "failed"
RouterStatusDeployed RouterStatus = "deployed"
RouterStatusUndeployed RouterStatus = "undeployed"
)
// Router holds the information as to which versions of Router have been deployed,
// and acts as the top level object for an instance of the Turing Router.
type Router struct {
Model
// Project id of the project this router belongs to, as retrieved from
// the MLP API.
ProjectID ID `json:"project_id"`
// Environment name of the environment this router belongs to, as retrieved
// from the MLP API.
EnvironmentName string `json:"environment_name"`
// Name of the router. Must be unique within the given project and environment.
Name string `json:"name"`
// Status of the Router. Indicates the deployment status of the router.
Status RouterStatus `json:"status"`
// Endpoint URL where the currently deployed router version is accessible at
Endpoint string `json:"endpoint,omitempty"`
// The current version (may be deployed or undeployed)
CurrRouterVersionID sql.NullInt32 `json:"-"`
CurrRouterVersion *RouterVersion `json:"config,omitempty" gorm:"foreignkey:CurrRouterVersionID"`
// MonitoringURL is for all router versions
MonitoringURL string `json:"monitoring_url" gorm:"-"`
}
func (r *Router) SetCurrRouterVersionID(routerVersionID ID) {
r.CurrRouterVersionID = sql.NullInt32{Int32: int32(routerVersionID), Valid: true}
}
func (r *Router) ClearCurrRouterVersionID() {
r.CurrRouterVersionID = sql.NullInt32{Int32: 0, Valid: false}
}
// SetCurrRouterVersion sets the currently version for this router to the provided
// routerVersion.
func (r *Router) SetCurrRouterVersion(routerVersion *RouterVersion) {
r.SetCurrRouterVersionID(routerVersion.ID)
r.CurrRouterVersion = routerVersion
}
// ClearCurrRouterVersion clears the current version for this router.
func (r *Router) ClearCurrRouterVersion() {
r.ClearCurrRouterVersionID()
r.CurrRouterVersion = nil
}
// RouterResponse is an alias for the Router, to enable custom marshaling
type RouterResponse Router
// MarshalJSON is a custom marshaling function for the Router, to manipulate the
// endpoint.
func (r *Router) MarshalJSON() ([]byte, error) {
response := RouterResponse(*r)
// Append the path to the endpoint
if response.Endpoint != "" && strings.HasPrefix(response.Endpoint, "http") {
response.Endpoint = fmt.Sprintf("%s/v1/predict", response.Endpoint)
}
return json.Marshal(response)
}