2024-08-20 23:56:48 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
// wave core application coordinator
|
|
|
|
package wcore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-09-23 22:59:55 +02:00
|
|
|
"log"
|
2024-08-20 23:56:48 +02:00
|
|
|
"time"
|
|
|
|
|
2024-08-27 00:17:37 +02:00
|
|
|
"github.com/google/uuid"
|
2024-12-04 23:16:50 +01:00
|
|
|
|
2024-09-05 23:25:45 +02:00
|
|
|
"github.com/wavetermdev/waveterm/pkg/waveobj"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/wstore"
|
2024-08-20 23:56:48 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// the wcore package coordinates actions across the storage layer
|
|
|
|
// orchestrating the wave object store, the wave pubsub system, and the wave rpc system
|
|
|
|
|
2024-12-02 20:44:08 +01:00
|
|
|
// Ensures that the initial data is present in the store, creates an initial window if needed
|
|
|
|
func EnsureInitialData() error {
|
2024-08-27 00:17:37 +02:00
|
|
|
// does not need to run in a transaction since it is called on startup
|
|
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), 2*time.Second)
|
|
|
|
defer cancelFn()
|
|
|
|
client, err := wstore.DBGetSingleton[*waveobj.Client](ctx)
|
|
|
|
if err == wstore.ErrNotFound {
|
|
|
|
client, err = CreateClient(ctx)
|
|
|
|
if err != nil {
|
2024-12-02 20:44:08 +01:00
|
|
|
return fmt.Errorf("error creating client: %w", err)
|
|
|
|
}
|
|
|
|
migrateErr := wstore.TryMigrateOldHistory()
|
|
|
|
if migrateErr != nil {
|
|
|
|
log.Printf("error migrating old history: %v\n", migrateErr)
|
2024-08-27 00:17:37 +02:00
|
|
|
}
|
|
|
|
}
|
2024-11-21 03:48:46 +01:00
|
|
|
if client.TempOID == "" {
|
2024-12-02 19:56:56 +01:00
|
|
|
log.Println("client.TempOID is empty")
|
2024-11-21 03:48:46 +01:00
|
|
|
client.TempOID = uuid.NewString()
|
|
|
|
err = wstore.DBUpdate(ctx, client)
|
|
|
|
if err != nil {
|
2024-12-02 20:44:08 +01:00
|
|
|
return fmt.Errorf("error updating client: %w", err)
|
2024-11-21 03:48:46 +01:00
|
|
|
}
|
|
|
|
}
|
2024-09-23 22:59:55 +02:00
|
|
|
log.Printf("clientid: %s\n", client.OID)
|
2024-09-26 22:29:07 +02:00
|
|
|
if len(client.WindowIds) == 1 {
|
2024-12-02 19:56:56 +01:00
|
|
|
log.Println("client has one window")
|
2024-12-02 20:44:08 +01:00
|
|
|
CheckAndFixWindow(ctx, client.WindowIds[0])
|
|
|
|
return nil
|
2024-09-26 22:29:07 +02:00
|
|
|
}
|
2024-08-27 00:17:37 +02:00
|
|
|
if len(client.WindowIds) > 0 {
|
2024-12-02 19:56:56 +01:00
|
|
|
log.Println("client has windows")
|
2024-12-02 20:44:08 +01:00
|
|
|
return nil
|
2024-08-27 00:17:37 +02:00
|
|
|
}
|
2024-12-07 00:50:52 +01:00
|
|
|
log.Println("client has no windows, creating starter workspace")
|
2024-12-13 02:21:09 +01:00
|
|
|
starterWs, err := CreateWorkspace(ctx, "Starter workspace", "custom@wave-logo-solid", "#58C142", false, true)
|
2024-12-02 19:56:56 +01:00
|
|
|
if err != nil {
|
2024-12-07 00:50:52 +01:00
|
|
|
return fmt.Errorf("error creating starter workspace: %w", err)
|
2024-12-02 19:56:56 +01:00
|
|
|
}
|
2024-12-07 00:50:52 +01:00
|
|
|
_, err = CreateWindow(ctx, nil, starterWs.OID)
|
2024-08-27 00:17:37 +02:00
|
|
|
if err != nil {
|
2024-12-02 20:44:08 +01:00
|
|
|
return fmt.Errorf("error creating window: %w", err)
|
2024-08-27 00:17:37 +02:00
|
|
|
}
|
2024-12-02 20:44:08 +01:00
|
|
|
return nil
|
2024-08-27 00:17:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func CreateClient(ctx context.Context) (*waveobj.Client, error) {
|
|
|
|
client := &waveobj.Client{
|
|
|
|
OID: uuid.NewString(),
|
|
|
|
WindowIds: []string{},
|
|
|
|
}
|
|
|
|
err := wstore.DBInsert(ctx, client)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error inserting client: %w", err)
|
|
|
|
}
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
2024-12-02 19:56:56 +01:00
|
|
|
func GetClientData(ctx context.Context) (*waveobj.Client, error) {
|
|
|
|
clientData, err := wstore.DBGetSingleton[*waveobj.Client](ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error getting client data: %w", err)
|
|
|
|
}
|
|
|
|
return clientData, nil
|
|
|
|
}
|