utils

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 30, 2024 License: Apache-2.0 Imports: 25 Imported by: 0

README

utils

Table of Contents

  1. Description
  2. Structure and Organisation
  3. Class Diagram
  4. Functionality
  5. Data Types
  6. Testing
  7. Proposed Functionality/Requirements
  8. References

Specification

Description

This package contains utility tools and functionalities used by other packages

Structure and Organisation

Here is quick overview of the contents of this directory:

  • README: Current file which is aimed towards developers who wish to use and modify the package functionality.

  • blockchain: This file contains methods and data types related to interaction with blockchain.

  • file_system: This file contains a method to retrieve the size of the volume.

  • init: This file initializes an Open Telemetry logger for this package. It also defines constants to reflect the status of transaction.

  • network: This file contains helper methods for DMS API calls and responses.

  • progress_io: This file defines wrapper functions for readers and writers with progress tracking capabilities.

  • syncmap: This file defines a SyncMap type which is a thread-safe version of the standard Go map with strongly-typed methods and functions for managing key-value pairs concurrently.

  • utils: This file contains various utility functions for the DMS functionality.

  • validate: This contains helper functions that perform different kinds of validation checks and numeric conversions.

  • specs: This folder contains the class diagram for the package.

Files with *_test.go naming contains unit tests of the specified functionality.

Class Diagram
Source File

utils Class Diagram

Rendered from source file
!$rootUrlGitlab = "https://gitlab.com/nunet/device-management-service/-/raw/main"
!$packageRelativePath = "/utils"
!$packageUrlGitlab = $rootUrlGitlab + $packageRelativePath
 
!include $packageUrlGitlab/specs/class_diagram.puml
Functionality

utils package defines various helper methods for functionality defined in the different packages of DMS. Refer to utils.go for details.

Data Types

Blockchain data models

  • utils.UTXOs: TBD
type UTXOs struct {
	TxHash  string `json:"tx_hash"`
	IsSpent bool   `json:"is_spent"`
}
  • utils.TxHashResp: TBD
type TxHashResp struct {
	TxHash          string `json:"tx_hash"`
	TransactionType string `json:"transaction_type"`
	DateTime        string `json:"date_time"`
}
  • utils.ClaimCardanoTokenBody: TBD
type ClaimCardanoTokenBody struct {
	ComputeProviderAddress string `json:"compute_provider_address"`
	TxHash                 string `json:"tx_hash"`
}
  • utils.rewardRespToCPD: TBD
type rewardRespToCPD struct {
	ServiceProviderAddr string `json:"service_provider_addr"`
	ComputeProviderAddr string `json:"compute_provider_addr"`
	RewardType          string `json:"reward_type,omitempty"`
	SignatureDatum      string `json:"signature_datum,omitempty"`
	MessageHashDatum    string `json:"message_hash_datum,omitempty"`
	Datum               string `json:"datum,omitempty"`
	SignatureAction     string `json:"signature_action,omitempty"`
	MessageHashAction   string `json:"message_hash_action,omitempty"`
	Action              string `json:"action,omitempty"`
}
  • utils.UpdateTxStatusBody: TBD
type UpdateTxStatusBody struct {
	Address string `json:"address,omitempty"`
}

progress_io data models

  • utils.IOProgress: TBD
type IOProgress struct {
	n         float64
	size      float64
	started   time.Time
	estimated time.Time
	err       error
}
  • utils.Reader: TBD
type Reader struct {
	reader   io.Reader
	lock     sync.RWMutex
	Progress IOProgress
}
  • utils.Writer: TBD
type Writer struct {
	writer   io.Writer
	lock     sync.RWMutex
	Progress IOProgress
}

syncmap data models

  • utils.SyncMap: a concurrency-safe sync.Map that uses strongly-typed method signatures to ensure the types of its stored data are known.
type SyncMap[K comparable, V any] struct {
	sync.Map
}
Testing

The unit tests for the functionality are defined in network_test.go and utils_test.go files.

Proposed Functionality / Requirements

List of issues related to the implementation of the utils package can be found below. These include proposals for modifications to the package or new functionality needed to cover the requirements of other packages.

References

Documentation

Index

Constants

View Source
const (
	KernelFileURL  = "https://d.nunet.io/fc/vmlinux"
	KernelFilePath = "/etc/nunet/vmlinux"
	FilesystemURL  = "https://d.nunet.io/fc/nunet-fc-ubuntu-20.04-0.ext4"
	FilesystemPath = "/etc/nunet/nunet-fc-ubuntu-20.04-0.ext4"
)

Variables

This section is empty.

Functions

func CalculateSHA256Checksum added in v0.4.159

func CalculateSHA256Checksum(filePath string) (string, error)

CalculateSHA256Checksum calculates the SHA256 checksum of a file

func CheckWSL added in v0.4.159

func CheckWSL(afs afero.Afero) (bool, error)

CheckWSL check if running in WSL

func ConvertTypedSliceToUntypedSlice added in v0.5.0

func ConvertTypedSliceToUntypedSlice(typedSlice interface{}) []interface{}

func CreateCheckSumFile added in v0.4.159

func CreateCheckSumFile(filePath string, checksum string) (string, error)

put checksum in file

func CreateDirectoryIfNotExists added in v0.4.159

func CreateDirectoryIfNotExists(path string) error

CreateDirectoryIfNotExists creates a directory if it does not exist

func DeleteFile added in v0.4.159

func DeleteFile(path string, backup bool) (err error)

DeleteFile deletes a file, with or without a backup

func DownloadFile

func DownloadFile(url string, filepath string, maxBytes int64) (err error)

DownloadFile downloads a file from a url and saves it to a filepath

func ExtractTarGzToPath added in v0.4.159

func ExtractTarGzToPath(tarGzFilePath, extractedPath string, maxBytes int64) error

ExtractTarGzToPath extracts a tar.gz file to a specified path

func FileExists added in v0.5.0

func FileExists(fs afero.Fs, filename string) bool

FileExists checks if destination file exists

func GetDirectorySize added in v0.5.0

func GetDirectorySize(fs afero.Fs, path string) (int64, error)

func IntersectionSlices added in v0.5.0

func IntersectionSlices(slice1, slice2 []interface{}) []interface{}

IntersectionStringSlices returns the intersection of two slices of strings.

func IsExecutor added in v0.5.0

func IsExecutor(v interface{}) bool

func IsExecutorType added in v0.5.0

func IsExecutorType(v interface{}) bool

func IsGPUVendor added in v0.5.0

func IsGPUVendor(v interface{}) bool

func IsJobType added in v0.5.0

func IsJobType(v interface{}) bool

func IsJobTypes added in v0.5.0

func IsJobTypes(v interface{}) bool

func IsSameShallowType added in v0.5.0

func IsSameShallowType(a, b interface{}) bool

func IsStrictlyContained added in v0.5.0

func IsStrictlyContained(leftSlice, rightSlice []interface{}) bool

IsStrictlyContained checks if all elements of rightSlice are contained in leftSlice

func IsStrictlyContainedInt added in v0.5.0

func IsStrictlyContainedInt(leftSlice, rightSlice []int) bool

IsStrictlyContainedInt checks if all elements of rightSlice are contained in leftSlice

func NoIntersectionSlices added in v0.5.0

func NoIntersectionSlices(slice1, slice2 []interface{}) bool

func RandomBool added in v0.4.159

func RandomBool() (bool, error)

func RandomString

func RandomString(n int) (string, error)

RandomString generates a random string of length n

func ReadHTTPString added in v0.5.0

func ReadHTTPString(url string) (string, error)

ReadHTTPString GET request to http endpoint and return response as string

func SanitizeArchivePath added in v0.5.0

func SanitizeArchivePath(d, t string) (v string, err error)

SanitizeArchivePath Sanitize archive file pathing from "G305: Zip Slip vulnerability"

func SliceContains added in v0.4.159

func SliceContains(s []string, str string) bool

SliceContains checks if a string exists in a slice

func ValidateAddress added in v0.4.159

func ValidateAddress(addr string) error

ValidateAddress checks if the wallet address is a valid cardano address

func WriteToFile added in v0.5.0

func WriteToFile(fs afero.Fs, data []byte, filePath string) (string, error)

WriteToFile writes data to a file.

Types

type HTTPClient added in v0.5.0

type HTTPClient struct {
	BaseURL    string
	APIVersion string
	Client     *http.Client
}

func NewHTTPClient added in v0.5.0

func NewHTTPClient(baseURL, version string) *HTTPClient

func (*HTTPClient) MakeRequest added in v0.5.0

func (c *HTTPClient) MakeRequest(method, relativePath string, body []byte) ([]byte, int, error)

MakeRequest performs an HTTP request with the given method, path, and body It returns the response body, status code, and an error if any

type IOProgress added in v0.4.159

type IOProgress struct {
	// contains filtered or unexported fields
}

func (IOProgress) Complete added in v0.4.159

func (p IOProgress) Complete() bool

func (IOProgress) Estimated added in v0.4.159

func (p IOProgress) Estimated() time.Time

func (IOProgress) N added in v0.4.159

func (p IOProgress) N() float64

func (IOProgress) Percent added in v0.4.159

func (p IOProgress) Percent() float64

Percent calculates the percentage complete.

func (IOProgress) Remaining added in v0.4.159

func (p IOProgress) Remaining() time.Duration

func (IOProgress) Size added in v0.4.159

func (p IOProgress) Size() float64

type Reader added in v0.4.159

type Reader struct {
	Progress IOProgress
	// contains filtered or unexported fields
}

func ReaderWithProgress added in v0.4.159

func ReaderWithProgress(r io.Reader, size int64) *Reader

func (*Reader) Read added in v0.4.159

func (r *Reader) Read(p []byte) (n int, err error)

type SyncMap added in v0.5.0

type SyncMap[K comparable, V any] struct {
	sync.Map
}

A SyncMap is a concurrency-safe sync.Map that uses strongly-typed method signatures to ensure the types of its stored data are known.

func SyncMapFromMap added in v0.5.0

func SyncMapFromMap[K comparable, V any](m map[K]V) *SyncMap[K, V]

SyncMapFromMap converts a standard Go map to a concurrency-safe SyncMap.

func (*SyncMap[K, V]) Get added in v0.5.0

func (m *SyncMap[K, V]) Get(key K) (V, bool)

Get retrieves the value associated with the given key from the map. It returns the value and a boolean indicating whether the key was found.

func (*SyncMap[K, V]) Iter added in v0.5.0

func (m *SyncMap[K, V]) Iter(ranger func(key K, value V) bool)

Iter iterates over each key-value pair in the map, executing the provided function on each pair. The iteration stops if the provided function returns false.

func (*SyncMap[K, V]) Keys added in v0.5.0

func (m *SyncMap[K, V]) Keys() []K

Keys returns a slice containing all the keys present in the map.

func (*SyncMap[K, V]) Put added in v0.5.0

func (m *SyncMap[K, V]) Put(key K, value V)

Put inserts or updates a key-value pair in the map.

func (*SyncMap[K, V]) String added in v0.5.0

func (m *SyncMap[K, V]) String() string

String provides a string representation of the map, listing all key-value pairs.

type Writer added in v0.4.159

type Writer struct {
	Progress IOProgress
	// contains filtered or unexported fields
}

func WriterWithProgress added in v0.4.159

func WriterWithProgress(w io.Writer, size int64) *Writer

func (*Writer) Write added in v0.4.159

func (w *Writer) Write(p []byte) (n int, err error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL