go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/projects/nodes/pkg/dbmodel/graph.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package dbmodel
     9  
    10  import (
    11  	"time"
    12  
    13  	"github.com/wcharczuk/go-incr"
    14  	"go.charczuk.com/projects/nodes/pkg/types"
    15  	"go.charczuk.com/sdk/uuid"
    16  )
    17  
    18  // GraphFromIncr returns a db graph from an incremental graph.
    19  func GraphFromIncr(g *incr.Graph, vp types.Viewport) Graph {
    20  	eg := incr.ExpertGraph(g)
    21  	output := Graph{
    22  		ID:               uuid.UUID(g.ID()),
    23  		Label:            g.Label(),
    24  		StabilizationNum: eg.StabilizationNum(),
    25  		ViewportX:        vp.X,
    26  		ViewportY:        vp.Y,
    27  		ViewportZoom:     vp.Zoom,
    28  	}
    29  	return output
    30  }
    31  
    32  // GraphFromType returns a db graph from an api graph.
    33  func GraphFromType(g *types.Graph) *Graph {
    34  	if g == nil {
    35  		return NewGraph()
    36  	}
    37  	output := Graph{
    38  		ID:               uuid.UUID(g.ID),
    39  		Label:            g.Label,
    40  		UserID:           g.Metadata.UserID,
    41  		CreatedUTC:       g.Metadata.CreatedUTC,
    42  		UpdatedUTC:       g.Metadata.UpdatedUTC,
    43  		StabilizationNum: g.StabilizationNum,
    44  		ViewportX:        g.Metadata.ViewportX,
    45  		ViewportY:        g.Metadata.ViewportY,
    46  		ViewportZoom:     g.Metadata.ViewportZoom,
    47  	}
    48  	return &output
    49  }
    50  
    51  // TypeGraphFromGraph returns an api graph from an api graph.
    52  func TypeGraphFromGraph(g Graph) *types.Graph {
    53  	output := types.Graph{
    54  		ID:               incr.Identifier(g.ID),
    55  		Label:            g.Label,
    56  		StabilizationNum: g.StabilizationNum,
    57  		Metadata: types.GraphMetadata{
    58  			UserID:       g.UserID,
    59  			CreatedUTC:   g.CreatedUTC,
    60  			UpdatedUTC:   g.UpdatedUTC,
    61  			ViewportX:    g.ViewportX,
    62  			ViewportY:    g.ViewportY,
    63  			ViewportZoom: g.ViewportZoom,
    64  		},
    65  	}
    66  	return &output
    67  }
    68  
    69  // NewGraph returns a new, empty, graph.
    70  func NewGraph() *Graph {
    71  	return &Graph{
    72  		ID:               uuid.V4(),
    73  		CreatedUTC:       time.Now().UTC(),
    74  		StabilizationNum: 1,
    75  		ViewportZoom:     1,
    76  	}
    77  }
    78  
    79  // Graph is the root object for computations.
    80  type Graph struct {
    81  	ID               uuid.UUID `db:"id,pk"`
    82  	CreatedUTC       time.Time `db:"created_utc"`
    83  	UpdatedUTC       time.Time `db:"updated_utc"`
    84  	UserID           uuid.UUID `db:"user_id"`
    85  	Label            string    `db:"label"`
    86  	StabilizationNum uint64    `db:"stabilization_num"`
    87  	ViewportX        float64   `db:"viewport_x"`
    88  	ViewportY        float64   `db:"viewport_y"`
    89  	ViewportZoom     float64   `db:"viewport_zoom"`
    90  }
    91  
    92  // TableName returns the mapped table name.
    93  func (g Graph) TableName() string {
    94  	return "graph"
    95  }