<commit_before>//
// Last.Backend LLC CONFIDENTIAL
// __________________
//
// [2014] - [2017] Last.Backend LLC
// All Rights Reserved.
//
// NOTICE:  All information contained herein is, and remains
// the property of Last.Backend LLC and its suppliers,
// if any.  The intellectual and technical concepts contained
// herein are proprietary to Last.Backend LLC
// and its suppliers and may be covered by Russian Federation and Foreign Patents,
// patents in process, and are protected by trade secret or copyright law.
// Dissemination of this information or reproduction of this material
// is strictly forbidden unless prior written permission is obtained
// from Last.Backend LLC.
//

package storage

import (
	"context"
	"fmt"
	"github.com/lastbackend/lastbackend/pkg/apis/types"
	"github.com/lastbackend/lastbackend/pkg/daemon/storage/store"
	"github.com/satori/go.uuid"
	"time"
)

const nodeStorage = "node"
const nodeStateTTL = 3000

// Namespace Service type for interface in interfaces folder
type NodeStorage struct {
	INode
	util   IUtil
	Client func() (store.IStore, store.DestroyFunc, error)
}

func (s *NodeStorage) List(ctx context.Context) (*types.NodeList, error) {
	const filter = `\b(.+)projects\/[a-z0-9-]{36}\/meta\b`

	client, destroy, err := s.Client()
	if err != nil {
		return nil, err
	}
	defer destroy()

	key := s.util.Key(ctx, nodeStorage)
	metaList := []types.NodeMeta{}
	if err := client.List(ctx, key, filter, &metaList); err != nil {
		if err.Error() == store.ErrKeyNotFound {
			return nil, nil
		}
		return nil, err
	}

	if metaList == nil {
		return nil, nil
	}

	nodeList := new(types.NodeList)
	for _, meta := range metaList {
		node := types.Node{}
		node.Meta = meta
		*nodeList = append(*nodeList, node)
	}

	return nodeList, nil
}

func (s *NodeStorage) Get(ctx context.Context, hostname string) (*types.Node, error) {
	node := new(types.Node)

	client, destroy, err := s.Client()
	if err != nil {
		return nil, err
	}
	defer destroy()

	keyMeta := s.util.Key(ctx, nodeStorage, hostname, "meta")
	if err := client.Get(ctx, keyMeta, &node.Meta); err != nil {
		if err.Error() == store.ErrKeyNotFound {
			return nil, nil
		}
		return nil, err
	}

	keyState := s.util.Key(ctx, nodeStorage, hostname, "state")
	fmt.Println(keyState)
	if err := client.Get(ctx, keyState, &node.State); err != nil {
		if err.Error() == store.ErrKeyNotFound {
			return nil, nil
		}
		return nil, err
	}

	keySpec := s.util.Key(ctx, nodeStorage, hostname, "spec")
	if err := client.List(ctx, keySpec, "", &node.Spec.Pods); err != nil {
		if err.Error() == store.ErrKeyNotFound {
			return nil, nil
		}
		return nil, err
	}

	fmt.Println(node)

	return node, nil
}

func (s *NodeStorage) Insert(ctx context.Context, meta *types.NodeMeta, state *types.NodeState) (*types.Node, error) {

	var (
		id   = uuid.NewV4().String()
		node = new(types.Node)
	)

	client, destroy, err := s.Client()
	if err != nil {
		return nil, err
	}
	defer destroy()

	tx := client.Begin(ctx)

	node.Meta = *meta
	node.Meta.ID = id
	node.Meta.Labels = map[string]string{"tier": "node"}
	node.Meta.Updated = time.Now()
	node.Meta.Created = time.Now()

	node.State = *state
<commit_msg>remove fmt debug info<commit_after>//
// Last.Backend LLC CONFIDENTIAL
// __________________
//
// [2014] - [2017] Last.Backend LLC
// All Rights Reserved.
//
// NOTICE:  All information contained herein is, and remains
// the property of Last.Backend LLC and its suppliers,
// if any.  The intellectual and technical concepts contained
// herein are proprietary to Last.Backend LLC
// and its suppliers and may be covered by Russian Federation and Foreign Patents,
// patents in process, and are protected by trade secret or copyright law.
// Dissemination of this information or reproduction of this material
// is strictly forbidden unless prior written permission is obtained
// from Last.Backend LLC.
//

package storage

import (
	"context"
	"fmt"
	"github.com/lastbackend/lastbackend/pkg/apis/types"
	"github.com/lastbackend/lastbackend/pkg/daemon/storage/store"
	"github.com/satori/go.uuid"
	"time"
)

const nodeStorage = "node"
const nodeStateTTL = 3000

// Namespace Service type for interface in interfaces folder
type NodeStorage struct {
	INode
	util   IUtil
	Client func() (store.IStore, store.DestroyFunc, error)
}

func (s *NodeStorage) List(ctx context.Context) (*types.NodeList, error) {
	const filter = `\b(.+)projects\/[a-z0-9-]{36}\/meta\b`

	client, destroy, err := s.Client()
	if err != nil {
		return nil, err
	}
	defer destroy()

	key := s.util.Key(ctx, nodeStorage)
	metaList := []types.NodeMeta{}
	if err := client.List(ctx, key, filter, &metaList); err != nil {
		if err.Error() == store.ErrKeyNotFound {
			return nil, nil
		}
		return nil, err
	}

	if metaList == nil {
		return nil, nil
	}

	nodeList := new(types.NodeList)
	for _, meta := range metaList {
		node := types.Node{}
		node.Meta = meta
		*nodeList = append(*nodeList, node)
	}

	return nodeList, nil
}

func (s *NodeStorage) Get(ctx context.Context, hostname string) (*types.Node, error) {
	node := new(types.Node)

	client, destroy, err := s.Client()
	if err != nil {
		return nil, err
	}
	defer destroy()

	keyMeta := s.util.Key(ctx, nodeStorage, hostname, "meta")
	if err := client.Get(ctx, keyMeta, &node.Meta); err != nil {
		if err.Error() == store.ErrKeyNotFound {
			return nil, nil
		}
		return nil, err
	}

	keyState := s.util.Key(ctx, nodeStorage, hostname, "state")
	if err := client.Get(ctx, keyState, &node.State); err != nil {
		if err.Error() == store.ErrKeyNotFound {
			return nil, nil
		}
		return nil, err
	}

	keySpec := s.util.Key(ctx, nodeStorage, hostname, "spec")
	if err := client.List(ctx, keySpec, "", &node.Spec.Pods); err != nil {
		if err.Error() == store.ErrKeyNotFound {
			return nil, nil
		}
		return nil, err
	}

	return node, nil
}

func (s *NodeStorage) Insert(ctx context.Context, meta *types.NodeMeta, state *types.NodeState) (*types.Node, error) {

	var (
		id   = uuid.NewV4().String()
		node = new(types.Node)
	)

	client, destroy, err := s.Client()
	if err != nil {
		return nil, err
	}
	defer destroy()

	tx := client.Begin(ctx)

	node.Meta = *meta
	node.Meta.ID = id
	node.Meta.Labels = map[string]string{"tier": "node"}
	node.Meta.Updated = time.Now()
	node.Meta.Created = time.Now()

	node.State = *state

{'fraction_non_alphanumeric': 0.1409385368936641, 'fraction_numerical': 0.00537209669774056, 'mean_word_length': 3.5902828136330673, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7119270', 'n_tokens_mistral': 2246, 'n_tokens_neox': 2126, 'n_words': 643}