mirror of
https://github.com/coder/coder.git
synced 2025-07-03 16:13:58 +00:00
chore: refactor time.Duration -> int64 milliseconds for FE consumption (#1944)
* Changes all public-facing codersdk types to use a plain int64 (milliseconds) instead of time.Duration. * Makes autostart_schedule a *string as it may not be present. * Adds a utils/ptr package with some useful methods.
This commit is contained in:
@ -25,6 +25,7 @@ import (
|
||||
"github.com/coder/coder/coderd/httpapi"
|
||||
"github.com/coder/coder/coderd/httpmw"
|
||||
"github.com/coder/coder/coderd/rbac"
|
||||
"github.com/coder/coder/coderd/util/ptr"
|
||||
"github.com/coder/coder/codersdk"
|
||||
)
|
||||
|
||||
@ -345,7 +346,7 @@ func (api *API) postWorkspacesByOrganization(rw http.ResponseWriter, r *http.Req
|
||||
dbAutostartSchedule.String = *createWorkspace.AutostartSchedule
|
||||
}
|
||||
|
||||
dbTTL, err := validWorkspaceTTL(createWorkspace.TTL)
|
||||
dbTTL, err := validWorkspaceTTLMillis(createWorkspace.TTLMillis)
|
||||
if err != nil {
|
||||
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
|
||||
Message: "validate workspace ttl",
|
||||
@ -527,20 +528,15 @@ func (api *API) putWorkspaceAutostart(rw http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
var dbSched sql.NullString
|
||||
if req.Schedule != "" {
|
||||
validSched, err := schedule.Weekly(req.Schedule)
|
||||
if err != nil {
|
||||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
||||
Message: fmt.Sprintf("invalid autostart schedule: %s", err),
|
||||
})
|
||||
return
|
||||
}
|
||||
dbSched.String = validSched.String()
|
||||
dbSched.Valid = true
|
||||
dbSched, err := validWorkspaceSchedule(req.Schedule)
|
||||
if err != nil {
|
||||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
||||
Message: fmt.Sprintf("invalid autostart schedule: %s", err),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
err := api.Database.UpdateWorkspaceAutostart(r.Context(), database.UpdateWorkspaceAutostartParams{
|
||||
err = api.Database.UpdateWorkspaceAutostart(r.Context(), database.UpdateWorkspaceAutostartParams{
|
||||
ID: workspace.ID,
|
||||
AutostartSchedule: dbSched,
|
||||
})
|
||||
@ -564,7 +560,7 @@ func (api *API) putWorkspaceTTL(rw http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
dbTTL, err := validWorkspaceTTL(req.TTL)
|
||||
dbTTL, err := validWorkspaceTTLMillis(req.TTLMillis)
|
||||
if err != nil {
|
||||
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
|
||||
Message: "validate workspace ttl",
|
||||
@ -830,13 +826,18 @@ func convertWorkspaces(ctx context.Context, db database.Store, workspaces []data
|
||||
}
|
||||
return apiWorkspaces, nil
|
||||
}
|
||||
|
||||
func convertWorkspace(
|
||||
workspace database.Workspace,
|
||||
workspaceBuild database.WorkspaceBuild,
|
||||
job database.ProvisionerJob,
|
||||
template database.Template,
|
||||
owner database.User) codersdk.Workspace {
|
||||
var autostartSchedule *string
|
||||
if workspace.AutostartSchedule.Valid {
|
||||
autostartSchedule = &workspace.AutostartSchedule.String
|
||||
}
|
||||
|
||||
ttlMillis := convertWorkspaceTTLMillis(workspace.Ttl)
|
||||
return codersdk.Workspace{
|
||||
ID: workspace.ID,
|
||||
CreatedAt: workspace.CreatedAt,
|
||||
@ -848,25 +849,27 @@ func convertWorkspace(
|
||||
TemplateName: template.Name,
|
||||
Outdated: workspaceBuild.TemplateVersionID.String() != template.ActiveVersionID.String(),
|
||||
Name: workspace.Name,
|
||||
AutostartSchedule: workspace.AutostartSchedule.String,
|
||||
TTL: convertSQLNullInt64(workspace.Ttl),
|
||||
AutostartSchedule: autostartSchedule,
|
||||
TTLMillis: ttlMillis,
|
||||
}
|
||||
}
|
||||
|
||||
func convertSQLNullInt64(i sql.NullInt64) *time.Duration {
|
||||
func convertWorkspaceTTLMillis(i sql.NullInt64) *int64 {
|
||||
if !i.Valid {
|
||||
return nil
|
||||
}
|
||||
|
||||
return (*time.Duration)(&i.Int64)
|
||||
millis := time.Duration(i.Int64).Milliseconds()
|
||||
return &millis
|
||||
}
|
||||
|
||||
func validWorkspaceTTL(ttl *time.Duration) (sql.NullInt64, error) {
|
||||
if ttl == nil {
|
||||
func validWorkspaceTTLMillis(millis *int64) (sql.NullInt64, error) {
|
||||
if ptr.NilOrZero(millis) {
|
||||
return sql.NullInt64{}, nil
|
||||
}
|
||||
|
||||
truncated := ttl.Truncate(time.Minute)
|
||||
dur := time.Duration(*millis) * time.Millisecond
|
||||
truncated := dur.Truncate(time.Minute)
|
||||
if truncated < time.Minute {
|
||||
return sql.NullInt64{}, xerrors.New("ttl must be at least one minute")
|
||||
}
|
||||
@ -902,3 +905,19 @@ func validWorkspaceDeadline(old, new time.Time) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func validWorkspaceSchedule(s *string) (sql.NullString, error) {
|
||||
if ptr.NilOrEmpty(s) {
|
||||
return sql.NullString{}, nil
|
||||
}
|
||||
|
||||
_, err := schedule.Weekly(*s)
|
||||
if err != nil {
|
||||
return sql.NullString{}, err
|
||||
}
|
||||
|
||||
return sql.NullString{
|
||||
Valid: true,
|
||||
String: *s,
|
||||
}, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user