2024-05-27 08:05:11 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package objectservice
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2024-06-12 22:47:13 +02:00
|
|
|
"github.com/wavetermdev/thenextwave/pkg/tsgen/tsgenmeta"
|
2024-05-27 08:05:11 +02:00
|
|
|
"github.com/wavetermdev/thenextwave/pkg/waveobj"
|
2024-08-20 23:56:48 +02:00
|
|
|
"github.com/wavetermdev/thenextwave/pkg/wcore"
|
2024-08-28 03:38:57 +02:00
|
|
|
"github.com/wavetermdev/thenextwave/pkg/wlayout"
|
2024-05-27 08:05:11 +02:00
|
|
|
"github.com/wavetermdev/thenextwave/pkg/wstore"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ObjectService struct{}
|
|
|
|
|
|
|
|
const DefaultTimeout = 2 * time.Second
|
2024-09-05 09:21:08 +02:00
|
|
|
const ConnContextTimeout = 60 * time.Second
|
2024-05-27 08:05:11 +02:00
|
|
|
|
|
|
|
func parseORef(oref string) (*waveobj.ORef, error) {
|
|
|
|
fields := strings.Split(oref, ":")
|
|
|
|
if len(fields) != 2 {
|
|
|
|
return nil, fmt.Errorf("invalid object reference: %q", oref)
|
|
|
|
}
|
|
|
|
return &waveobj.ORef{OType: fields[0], OID: fields[1]}, nil
|
|
|
|
}
|
|
|
|
|
2024-06-12 22:47:13 +02:00
|
|
|
func (svc *ObjectService) GetObject_Meta() tsgenmeta.MethodMeta {
|
|
|
|
return tsgenmeta.MethodMeta{
|
2024-06-12 02:42:10 +02:00
|
|
|
Desc: "get wave object by oref",
|
|
|
|
ArgNames: []string{"oref"},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (svc *ObjectService) GetObject(orefStr string) (waveobj.WaveObj, error) {
|
2024-05-27 08:05:11 +02:00
|
|
|
oref, err := parseORef(orefStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout)
|
|
|
|
defer cancelFn()
|
|
|
|
obj, err := wstore.DBGetORef(ctx, *oref)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error getting object: %w", err)
|
|
|
|
}
|
2024-06-12 02:42:10 +02:00
|
|
|
return obj, nil
|
2024-05-27 08:05:11 +02:00
|
|
|
}
|
|
|
|
|
2024-06-12 22:47:13 +02:00
|
|
|
func (svc *ObjectService) GetObjects_Meta() tsgenmeta.MethodMeta {
|
|
|
|
return tsgenmeta.MethodMeta{
|
2024-06-12 02:42:10 +02:00
|
|
|
ArgNames: []string{"orefs"},
|
|
|
|
ReturnDesc: "objects",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (svc *ObjectService) GetObjects(orefStrArr []string) ([]waveobj.WaveObj, error) {
|
2024-05-27 08:05:11 +02:00
|
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout)
|
|
|
|
defer cancelFn()
|
|
|
|
|
|
|
|
var orefArr []waveobj.ORef
|
|
|
|
for _, orefStr := range orefStrArr {
|
|
|
|
orefObj, err := parseORef(orefStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
orefArr = append(orefArr, *orefObj)
|
|
|
|
}
|
|
|
|
return wstore.DBSelectORefs(ctx, orefArr)
|
|
|
|
}
|
2024-05-27 22:59:58 +02:00
|
|
|
|
2024-06-12 22:47:13 +02:00
|
|
|
func (svc *ObjectService) AddTabToWorkspace_Meta() tsgenmeta.MethodMeta {
|
|
|
|
return tsgenmeta.MethodMeta{
|
2024-06-12 02:42:10 +02:00
|
|
|
ArgNames: []string{"uiContext", "tabName", "activateTab"},
|
|
|
|
ReturnDesc: "tabId",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-20 23:56:48 +02:00
|
|
|
func (svc *ObjectService) AddTabToWorkspace(uiContext waveobj.UIContext, tabName string, activateTab bool) (string, waveobj.UpdatesRtnType, error) {
|
2024-05-27 22:59:58 +02:00
|
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout)
|
|
|
|
defer cancelFn()
|
2024-08-20 23:56:48 +02:00
|
|
|
ctx = waveobj.ContextWithUpdates(ctx)
|
2024-08-27 00:17:37 +02:00
|
|
|
tabId, err := wcore.CreateTab(ctx, uiContext.WindowId, tabName, activateTab)
|
2024-05-27 22:59:58 +02:00
|
|
|
if err != nil {
|
2024-06-12 02:42:10 +02:00
|
|
|
return "", nil, fmt.Errorf("error creating tab: %w", err)
|
2024-05-27 22:59:58 +02:00
|
|
|
}
|
2024-08-28 03:38:57 +02:00
|
|
|
|
|
|
|
err = wlayout.ApplyPortableLayout(ctx, tabId, wlayout.GetNewTabLayout())
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, fmt.Errorf("error applying new tab layout: %w", err)
|
|
|
|
}
|
2024-08-27 00:17:37 +02:00
|
|
|
return tabId, waveobj.ContextGetUpdatesRtn(ctx), nil
|
2024-06-12 02:42:10 +02:00
|
|
|
}
|
|
|
|
|
2024-06-18 06:50:33 +02:00
|
|
|
func (svc *ObjectService) UpdateWorkspaceTabIds_Meta() tsgenmeta.MethodMeta {
|
|
|
|
return tsgenmeta.MethodMeta{
|
|
|
|
ArgNames: []string{"uiContext", "workspaceId", "tabIds"},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-20 23:56:48 +02:00
|
|
|
func (svc *ObjectService) UpdateWorkspaceTabIds(uiContext waveobj.UIContext, workspaceId string, tabIds []string) (waveobj.UpdatesRtnType, error) {
|
2024-06-18 06:50:33 +02:00
|
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout)
|
|
|
|
defer cancelFn()
|
2024-08-20 23:56:48 +02:00
|
|
|
ctx = waveobj.ContextWithUpdates(ctx)
|
2024-06-18 06:50:33 +02:00
|
|
|
err := wstore.UpdateWorkspaceTabIds(ctx, workspaceId, tabIds)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error updating workspace tab ids: %w", err)
|
|
|
|
}
|
2024-08-20 23:56:48 +02:00
|
|
|
return waveobj.ContextGetUpdatesRtn(ctx), nil
|
2024-06-18 06:50:33 +02:00
|
|
|
}
|
|
|
|
|
2024-06-12 22:47:13 +02:00
|
|
|
func (svc *ObjectService) SetActiveTab_Meta() tsgenmeta.MethodMeta {
|
|
|
|
return tsgenmeta.MethodMeta{
|
2024-06-12 02:42:10 +02:00
|
|
|
ArgNames: []string{"uiContext", "tabId"},
|
|
|
|
}
|
2024-05-27 22:59:58 +02:00
|
|
|
}
|
|
|
|
|
2024-08-20 23:56:48 +02:00
|
|
|
func (svc *ObjectService) SetActiveTab(uiContext waveobj.UIContext, tabId string) (waveobj.UpdatesRtnType, error) {
|
2024-05-27 22:59:58 +02:00
|
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout)
|
|
|
|
defer cancelFn()
|
2024-08-20 23:56:48 +02:00
|
|
|
ctx = waveobj.ContextWithUpdates(ctx)
|
2024-05-27 22:59:58 +02:00
|
|
|
err := wstore.SetActiveTab(ctx, uiContext.WindowId, tabId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error setting active tab: %w", err)
|
|
|
|
}
|
2024-05-29 06:08:00 +02:00
|
|
|
// check all blocks in tab and start controllers (if necessary)
|
2024-08-20 23:56:48 +02:00
|
|
|
tab, err := wstore.DBMustGet[*waveobj.Tab](ctx, tabId)
|
2024-05-29 06:08:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error getting tab: %w", err)
|
|
|
|
}
|
2024-06-12 02:42:10 +02:00
|
|
|
blockORefs := tab.GetBlockORefs()
|
|
|
|
blocks, err := wstore.DBSelectORefs(ctx, blockORefs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error getting tab blocks: %w", err)
|
|
|
|
}
|
2024-08-20 23:56:48 +02:00
|
|
|
updates := waveobj.ContextGetUpdatesRtn(ctx)
|
|
|
|
updates = append(updates, waveobj.MakeUpdate(tab))
|
|
|
|
updates = append(updates, waveobj.MakeUpdates(blocks)...)
|
2024-06-12 02:42:10 +02:00
|
|
|
return updates, nil
|
|
|
|
}
|
|
|
|
|
2024-06-21 19:23:04 +02:00
|
|
|
func (svc *ObjectService) UpdateTabName_Meta() tsgenmeta.MethodMeta {
|
|
|
|
return tsgenmeta.MethodMeta{
|
|
|
|
ArgNames: []string{"uiContext", "tabId", "name"},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-20 23:56:48 +02:00
|
|
|
func (svc *ObjectService) UpdateTabName(uiContext waveobj.UIContext, tabId, name string) (waveobj.UpdatesRtnType, error) {
|
2024-06-21 19:23:04 +02:00
|
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout)
|
|
|
|
defer cancelFn()
|
2024-08-20 23:56:48 +02:00
|
|
|
ctx = waveobj.ContextWithUpdates(ctx)
|
2024-06-21 19:23:04 +02:00
|
|
|
err := wstore.UpdateTabName(ctx, tabId, name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error updating tab name: %w", err)
|
|
|
|
}
|
2024-08-20 23:56:48 +02:00
|
|
|
return waveobj.ContextGetUpdatesRtn(ctx), nil
|
2024-06-21 19:23:04 +02:00
|
|
|
}
|
|
|
|
|
2024-06-12 22:47:13 +02:00
|
|
|
func (svc *ObjectService) CreateBlock_Meta() tsgenmeta.MethodMeta {
|
|
|
|
return tsgenmeta.MethodMeta{
|
2024-06-12 02:42:10 +02:00
|
|
|
ArgNames: []string{"uiContext", "blockDef", "rtOpts"},
|
|
|
|
ReturnDesc: "blockId",
|
|
|
|
}
|
2024-05-27 22:59:58 +02:00
|
|
|
}
|
2024-05-28 00:44:57 +02:00
|
|
|
|
2024-08-20 23:56:48 +02:00
|
|
|
func (svc *ObjectService) CreateBlock(uiContext waveobj.UIContext, blockDef *waveobj.BlockDef, rtOpts *waveobj.RuntimeOpts) (string, waveobj.UpdatesRtnType, error) {
|
2024-05-28 00:44:57 +02:00
|
|
|
if uiContext.ActiveTabId == "" {
|
2024-06-12 02:42:10 +02:00
|
|
|
return "", nil, fmt.Errorf("no active tab")
|
2024-05-28 00:44:57 +02:00
|
|
|
}
|
|
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout)
|
|
|
|
defer cancelFn()
|
2024-08-20 23:56:48 +02:00
|
|
|
ctx = waveobj.ContextWithUpdates(ctx)
|
2024-08-01 06:27:46 +02:00
|
|
|
|
2024-08-28 03:38:57 +02:00
|
|
|
blockData, err := wcore.CreateBlock(ctx, uiContext.ActiveTabId, blockDef, rtOpts)
|
2024-05-28 00:44:57 +02:00
|
|
|
if err != nil {
|
2024-08-01 06:27:46 +02:00
|
|
|
return "", nil, err
|
2024-05-28 00:44:57 +02:00
|
|
|
}
|
2024-08-01 06:27:46 +02:00
|
|
|
|
2024-08-20 23:56:48 +02:00
|
|
|
return blockData.OID, waveobj.ContextGetUpdatesRtn(ctx), nil
|
2024-05-28 00:44:57 +02:00
|
|
|
}
|
2024-05-28 01:33:31 +02:00
|
|
|
|
2024-06-12 22:47:13 +02:00
|
|
|
func (svc *ObjectService) DeleteBlock_Meta() tsgenmeta.MethodMeta {
|
|
|
|
return tsgenmeta.MethodMeta{
|
2024-06-12 02:42:10 +02:00
|
|
|
ArgNames: []string{"uiContext", "blockId"},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-20 23:56:48 +02:00
|
|
|
func (svc *ObjectService) DeleteBlock(uiContext waveobj.UIContext, blockId string) (waveobj.UpdatesRtnType, error) {
|
2024-05-28 01:33:31 +02:00
|
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout)
|
|
|
|
defer cancelFn()
|
2024-08-20 23:56:48 +02:00
|
|
|
ctx = waveobj.ContextWithUpdates(ctx)
|
|
|
|
err := wcore.DeleteBlock(ctx, uiContext.ActiveTabId, blockId)
|
2024-05-28 01:33:31 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error deleting block: %w", err)
|
|
|
|
}
|
2024-08-20 23:56:48 +02:00
|
|
|
return waveobj.ContextGetUpdatesRtn(ctx), nil
|
2024-05-28 01:33:31 +02:00
|
|
|
}
|
|
|
|
|
2024-06-12 22:47:13 +02:00
|
|
|
func (svc *ObjectService) CloseTab_Meta() tsgenmeta.MethodMeta {
|
|
|
|
return tsgenmeta.MethodMeta{
|
2024-06-12 02:42:10 +02:00
|
|
|
ArgNames: []string{"uiContext", "tabId"},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-12 22:47:13 +02:00
|
|
|
func (svc *ObjectService) UpdateObjectMeta_Meta() tsgenmeta.MethodMeta {
|
|
|
|
return tsgenmeta.MethodMeta{
|
2024-06-12 02:42:10 +02:00
|
|
|
ArgNames: []string{"uiContext", "oref", "meta"},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-20 23:56:48 +02:00
|
|
|
func (svc *ObjectService) UpdateObjectMeta(uiContext waveobj.UIContext, orefStr string, meta waveobj.MetaMapType) (waveobj.UpdatesRtnType, error) {
|
2024-05-28 21:18:26 +02:00
|
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout)
|
|
|
|
defer cancelFn()
|
2024-08-20 23:56:48 +02:00
|
|
|
ctx = waveobj.ContextWithUpdates(ctx)
|
2024-05-29 00:41:03 +02:00
|
|
|
oref, err := parseORef(orefStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error parsing object reference: %w", err)
|
|
|
|
}
|
|
|
|
err = wstore.UpdateObjectMeta(ctx, *oref, meta)
|
2024-05-28 21:18:26 +02:00
|
|
|
if err != nil {
|
2024-05-29 00:41:03 +02:00
|
|
|
return nil, fmt.Errorf("error updateing %q meta: %w", orefStr, err)
|
2024-05-28 21:18:26 +02:00
|
|
|
}
|
2024-08-20 23:56:48 +02:00
|
|
|
return waveobj.ContextGetUpdatesRtn(ctx), nil
|
2024-06-12 02:42:10 +02:00
|
|
|
}
|
|
|
|
|
2024-06-12 22:47:13 +02:00
|
|
|
func (svc *ObjectService) UpdateObject_Meta() tsgenmeta.MethodMeta {
|
|
|
|
return tsgenmeta.MethodMeta{
|
2024-06-12 02:42:10 +02:00
|
|
|
ArgNames: []string{"uiContext", "waveObj", "returnUpdates"},
|
|
|
|
}
|
2024-05-28 21:18:26 +02:00
|
|
|
}
|
2024-05-29 00:41:03 +02:00
|
|
|
|
2024-08-20 23:56:48 +02:00
|
|
|
func (svc *ObjectService) UpdateObject(uiContext waveobj.UIContext, waveObj waveobj.WaveObj, returnUpdates bool) (waveobj.UpdatesRtnType, error) {
|
2024-05-29 00:41:03 +02:00
|
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout)
|
|
|
|
defer cancelFn()
|
2024-08-20 23:56:48 +02:00
|
|
|
ctx = waveobj.ContextWithUpdates(ctx)
|
2024-06-12 02:42:10 +02:00
|
|
|
if waveObj == nil {
|
|
|
|
return nil, fmt.Errorf("update wavobj is nil")
|
2024-05-29 00:41:03 +02:00
|
|
|
}
|
2024-06-12 02:42:10 +02:00
|
|
|
oref := waveobj.ORefFromWaveObj(waveObj)
|
2024-05-29 00:41:03 +02:00
|
|
|
found, err := wstore.DBExistsORef(ctx, *oref)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error getting object: %w", err)
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return nil, fmt.Errorf("object not found: %s", oref)
|
|
|
|
}
|
2024-06-12 02:42:10 +02:00
|
|
|
err = wstore.DBUpdate(ctx, waveObj)
|
2024-05-29 00:41:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error updating object: %w", err)
|
|
|
|
}
|
|
|
|
if returnUpdates {
|
2024-08-20 23:56:48 +02:00
|
|
|
return waveobj.ContextGetUpdatesRtn(ctx), nil
|
2024-05-29 00:41:03 +02:00
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|