feat: Add streaming endpoint for workspace history (#157)

* feat: Add parameter querying to the API

* feat: Add streaming endpoint for workspace history

Enables a buildlog-like flow for reading job output.

* Fix empty parameter source and destination

* Add comment for usage of workspace history logs endpoint
This commit is contained in:
Kyle Carberry
2022-02-04 19:36:43 +00:00
committed by GitHub
parent 430341b7da
commit 65de6eef9c
22 changed files with 688 additions and 115 deletions
+10 -4
View File
@@ -224,7 +224,7 @@ func (q *fakeQuerier) GetWorkspaceHistoryByWorkspaceIDWithoutAfter(_ context.Con
return database.WorkspaceHistory{}, sql.ErrNoRows
}
func (q *fakeQuerier) GetWorkspaceHistoryLogsByIDBefore(_ context.Context, arg database.GetWorkspaceHistoryLogsByIDBeforeParams) ([]database.WorkspaceHistoryLog, error) {
func (q *fakeQuerier) GetWorkspaceHistoryLogsByIDBetween(_ context.Context, arg database.GetWorkspaceHistoryLogsByIDBetweenParams) ([]database.WorkspaceHistoryLog, error) {
q.mutex.Lock()
defer q.mutex.Unlock()
@@ -233,7 +233,10 @@ func (q *fakeQuerier) GetWorkspaceHistoryLogsByIDBefore(_ context.Context, arg d
if workspaceHistoryLog.WorkspaceHistoryID.String() != arg.WorkspaceHistoryID.String() {
continue
}
if workspaceHistoryLog.CreatedAt.After(arg.CreatedAt) {
if workspaceHistoryLog.CreatedAt.After(arg.CreatedBefore) {
continue
}
if workspaceHistoryLog.CreatedAt.Before(arg.CreatedAfter) {
continue
}
logs = append(logs, workspaceHistoryLog)
@@ -440,7 +443,7 @@ func (q *fakeQuerier) GetProjectHistoryByProjectIDAndName(_ context.Context, arg
return database.ProjectHistory{}, sql.ErrNoRows
}
func (q *fakeQuerier) GetProjectHistoryLogsByIDBefore(_ context.Context, arg database.GetProjectHistoryLogsByIDBeforeParams) ([]database.ProjectHistoryLog, error) {
func (q *fakeQuerier) GetProjectHistoryLogsByIDBetween(_ context.Context, arg database.GetProjectHistoryLogsByIDBetweenParams) ([]database.ProjectHistoryLog, error) {
q.mutex.Lock()
defer q.mutex.Unlock()
@@ -449,7 +452,10 @@ func (q *fakeQuerier) GetProjectHistoryLogsByIDBefore(_ context.Context, arg dat
if projectHistoryLog.ProjectHistoryID.String() != arg.ProjectHistoryID.String() {
continue
}
if projectHistoryLog.CreatedAt.After(arg.CreatedAt) {
if projectHistoryLog.CreatedAt.After(arg.CreatedBefore) {
continue
}
if projectHistoryLog.CreatedAt.Before(arg.CreatedAfter) {
continue
}
logs = append(logs, projectHistoryLog)
+3
View File
@@ -20,6 +20,7 @@ CREATE TYPE login_type AS ENUM (
);
CREATE TYPE parameter_destination_scheme AS ENUM (
'none',
'environment_variable',
'provisioner_variable'
);
@@ -32,10 +33,12 @@ CREATE TYPE parameter_scope AS ENUM (
);
CREATE TYPE parameter_source_scheme AS ENUM (
'none',
'data'
);
CREATE TYPE parameter_type_system AS ENUM (
'none',
'hcl'
);
+3 -3
View File
@@ -46,13 +46,13 @@ CREATE TABLE project_history (
);
-- Types of parameters the automator supports.
CREATE TYPE parameter_type_system AS ENUM ('hcl');
CREATE TYPE parameter_type_system AS ENUM ('none', 'hcl');
-- Supported schemes for a parameter source.
CREATE TYPE parameter_source_scheme AS ENUM('data');
CREATE TYPE parameter_source_scheme AS ENUM('none', 'data');
-- Supported schemes for a parameter destination.
CREATE TYPE parameter_destination_scheme AS ENUM('environment_variable', 'provisioner_variable');
CREATE TYPE parameter_destination_scheme AS ENUM('none', 'environment_variable', 'provisioner_variable');
-- Stores project version parameters parsed on import.
-- No secrets are stored here.
+4 -1
View File
@@ -75,6 +75,7 @@ func (e *LoginType) Scan(src interface{}) error {
type ParameterDestinationScheme string
const (
ParameterDestinationSchemeNone ParameterDestinationScheme = "none"
ParameterDestinationSchemeEnvironmentVariable ParameterDestinationScheme = "environment_variable"
ParameterDestinationSchemeProvisionerVariable ParameterDestinationScheme = "provisioner_variable"
)
@@ -115,6 +116,7 @@ func (e *ParameterScope) Scan(src interface{}) error {
type ParameterSourceScheme string
const (
ParameterSourceSchemeNone ParameterSourceScheme = "none"
ParameterSourceSchemeData ParameterSourceScheme = "data"
)
@@ -133,7 +135,8 @@ func (e *ParameterSourceScheme) Scan(src interface{}) error {
type ParameterTypeSystem string
const (
ParameterTypeSystemHCL ParameterTypeSystem = "hcl"
ParameterTypeSystemNone ParameterTypeSystem = "none"
ParameterTypeSystemHCL ParameterTypeSystem = "hcl"
)
func (e *ParameterTypeSystem) Scan(src interface{}) error {
+2 -2
View File
@@ -21,7 +21,7 @@ type querier interface {
GetProjectHistoryByID(ctx context.Context, id uuid.UUID) (ProjectHistory, error)
GetProjectHistoryByProjectID(ctx context.Context, projectID uuid.UUID) ([]ProjectHistory, error)
GetProjectHistoryByProjectIDAndName(ctx context.Context, arg GetProjectHistoryByProjectIDAndNameParams) (ProjectHistory, error)
GetProjectHistoryLogsByIDBefore(ctx context.Context, arg GetProjectHistoryLogsByIDBeforeParams) ([]ProjectHistoryLog, error)
GetProjectHistoryLogsByIDBetween(ctx context.Context, arg GetProjectHistoryLogsByIDBetweenParams) ([]ProjectHistoryLog, error)
GetProjectParametersByHistoryID(ctx context.Context, projectHistoryID uuid.UUID) ([]ProjectParameter, error)
GetProjectsByOrganizationIDs(ctx context.Context, ids []string) ([]Project, error)
GetProvisionerDaemonByID(ctx context.Context, id uuid.UUID) (ProvisionerDaemon, error)
@@ -37,7 +37,7 @@ type querier interface {
GetWorkspaceHistoryByWorkspaceID(ctx context.Context, workspaceID uuid.UUID) ([]WorkspaceHistory, error)
GetWorkspaceHistoryByWorkspaceIDAndName(ctx context.Context, arg GetWorkspaceHistoryByWorkspaceIDAndNameParams) (WorkspaceHistory, error)
GetWorkspaceHistoryByWorkspaceIDWithoutAfter(ctx context.Context, workspaceID uuid.UUID) (WorkspaceHistory, error)
GetWorkspaceHistoryLogsByIDBefore(ctx context.Context, arg GetWorkspaceHistoryLogsByIDBeforeParams) ([]WorkspaceHistoryLog, error)
GetWorkspaceHistoryLogsByIDBetween(ctx context.Context, arg GetWorkspaceHistoryLogsByIDBetweenParams) ([]WorkspaceHistoryLog, error)
GetWorkspaceResourcesByHistoryID(ctx context.Context, workspaceHistoryID uuid.UUID) ([]WorkspaceResource, error)
GetWorkspacesByProjectAndUserID(ctx context.Context, arg GetWorkspacesByProjectAndUserIDParams) ([]Workspace, error)
GetWorkspacesByUserID(ctx context.Context, ownerID string) ([]Workspace, error)
+12 -6
View File
@@ -188,14 +188,17 @@ FROM
WHERE
id = $1;
-- name: GetProjectHistoryLogsByIDBefore :many
-- name: GetProjectHistoryLogsByIDBetween :many
SELECT
*
FROM
project_history_log
WHERE
project_history_id = $1
AND created_at <= $2
project_history_id = @project_history_id
AND (
created_at >= @created_after
OR created_at <= @created_before
)
ORDER BY
created_at;
@@ -295,14 +298,17 @@ WHERE
LIMIT
1;
-- name: GetWorkspaceHistoryLogsByIDBefore :many
-- name: GetWorkspaceHistoryLogsByIDBetween :many
SELECT
*
FROM
workspace_history_log
WHERE
workspace_history_id = $1
AND created_at <= $2
workspace_history_id = @workspace_history_id
AND (
created_at >= @created_after
OR created_at <= @created_before
)
ORDER BY
created_at;
+20 -12
View File
@@ -450,25 +450,29 @@ func (q *sqlQuerier) GetProjectHistoryByProjectIDAndName(ctx context.Context, ar
return i, err
}
const getProjectHistoryLogsByIDBefore = `-- name: GetProjectHistoryLogsByIDBefore :many
const getProjectHistoryLogsByIDBetween = `-- name: GetProjectHistoryLogsByIDBetween :many
SELECT
id, project_history_id, created_at, source, level, output
FROM
project_history_log
WHERE
project_history_id = $1
AND created_at <= $2
AND (
created_at >= $2
OR created_at <= $3
)
ORDER BY
created_at
`
type GetProjectHistoryLogsByIDBeforeParams struct {
type GetProjectHistoryLogsByIDBetweenParams struct {
ProjectHistoryID uuid.UUID `db:"project_history_id" json:"project_history_id"`
CreatedAt time.Time `db:"created_at" json:"created_at"`
CreatedAfter time.Time `db:"created_after" json:"created_after"`
CreatedBefore time.Time `db:"created_before" json:"created_before"`
}
func (q *sqlQuerier) GetProjectHistoryLogsByIDBefore(ctx context.Context, arg GetProjectHistoryLogsByIDBeforeParams) ([]ProjectHistoryLog, error) {
rows, err := q.db.QueryContext(ctx, getProjectHistoryLogsByIDBefore, arg.ProjectHistoryID, arg.CreatedAt)
func (q *sqlQuerier) GetProjectHistoryLogsByIDBetween(ctx context.Context, arg GetProjectHistoryLogsByIDBetweenParams) ([]ProjectHistoryLog, error) {
rows, err := q.db.QueryContext(ctx, getProjectHistoryLogsByIDBetween, arg.ProjectHistoryID, arg.CreatedAfter, arg.CreatedBefore)
if err != nil {
return nil, err
}
@@ -1007,25 +1011,29 @@ func (q *sqlQuerier) GetWorkspaceHistoryByWorkspaceIDWithoutAfter(ctx context.Co
return i, err
}
const getWorkspaceHistoryLogsByIDBefore = `-- name: GetWorkspaceHistoryLogsByIDBefore :many
const getWorkspaceHistoryLogsByIDBetween = `-- name: GetWorkspaceHistoryLogsByIDBetween :many
SELECT
id, workspace_history_id, created_at, source, level, output
FROM
workspace_history_log
WHERE
workspace_history_id = $1
AND created_at <= $2
AND (
created_at >= $2
OR created_at <= $3
)
ORDER BY
created_at
`
type GetWorkspaceHistoryLogsByIDBeforeParams struct {
type GetWorkspaceHistoryLogsByIDBetweenParams struct {
WorkspaceHistoryID uuid.UUID `db:"workspace_history_id" json:"workspace_history_id"`
CreatedAt time.Time `db:"created_at" json:"created_at"`
CreatedAfter time.Time `db:"created_after" json:"created_after"`
CreatedBefore time.Time `db:"created_before" json:"created_before"`
}
func (q *sqlQuerier) GetWorkspaceHistoryLogsByIDBefore(ctx context.Context, arg GetWorkspaceHistoryLogsByIDBeforeParams) ([]WorkspaceHistoryLog, error) {
rows, err := q.db.QueryContext(ctx, getWorkspaceHistoryLogsByIDBefore, arg.WorkspaceHistoryID, arg.CreatedAt)
func (q *sqlQuerier) GetWorkspaceHistoryLogsByIDBetween(ctx context.Context, arg GetWorkspaceHistoryLogsByIDBetweenParams) ([]WorkspaceHistoryLog, error) {
rows, err := q.db.QueryContext(ctx, getWorkspaceHistoryLogsByIDBetween, arg.WorkspaceHistoryID, arg.CreatedAfter, arg.CreatedBefore)
if err != nil {
return nil, err
}