wsHelpers

package
v4.24.4 Latest Latest
Warning

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

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

Documentation

Overview

Example (OrderCacheItems)
items := map[string]fileCacheItem{
	"one": {
		localPath:        "path/one.bin",
		fileSize:         10 * 1024 * 1024,
		timestampUnixSec: 1234567891,
	},
	"three": {
		localPath:        "path/three.bin",
		fileSize:         30 * 1024 * 1024,
		timestampUnixSec: 1234567893,
	},
	"two": {
		localPath:        "path/two.bin",
		fileSize:         20 * 1024 * 1024,
		timestampUnixSec: 1234567892,
	},
	"four": {
		localPath:        "path/four.bin",
		fileSize:         40 * 1024 * 1024,
		timestampUnixSec: 1234567894,
	},
}

byAge, totalSize := orderCacheItems(items)
fmt.Printf("Total size: %v\n", totalSize)

for _, i := range byAge {
	fmt.Printf("path: %v, ts: %v, size: %v\n", i.localPath, i.timestampUnixSec, i.fileSize)
}
Output:

Total size: 104857600
path: path/four.bin, ts: 1234567894, size: 41943040
path: path/three.bin, ts: 1234567893, size: 31457280
path: path/two.bin, ts: 1234567892, size: 20971520
path: path/one.bin, ts: 1234567891, size: 10485760
Example (UpdateScanImageDataTypes)
package main

import (
	"context"
	"fmt"

	"github.com/pixlise/core/v4/api/dbCollections"
	"github.com/pixlise/core/v4/core/logger"
	"github.com/pixlise/core/v4/core/scan"
	"github.com/pixlise/core/v4/core/wstestlib"
	protos "github.com/pixlise/core/v4/generated-protos"
	"go.mongodb.org/mongo-driver/mongo/options"
)

func main() {
	db := wstestlib.GetDB()
	ctx := context.TODO()

	// Prep data
	db.Collection(dbCollections.ScansName).Drop(ctx)
	db.Collection(dbCollections.ImagesName).Drop(ctx)

	l := &logger.StdOutLoggerForTest{}
	fmt.Printf("Non-existant: %v\n", UpdateScanImageDataTypes("non-existant", db, l))

	// Add scan
	scanItem := &protos.ScanItem{
		Id:          "052822532",
		Title:       "Beaujeu",
		Description: "",
		DataTypes: []*protos.ScanItem_ScanTypeCount{
			{
				DataType: protos.ScanDataType_SD_XRF,
				Count:    234,
			},
		},
		Instrument:       1,
		InstrumentConfig: "PIXL",
		TimestampUnixSec: 1707463677,
		Meta: map[string]string{
			"Site":     "",
			"RTT":      "052822532",
			"SCLK":     "679215716",
			"Sol":      "0138",
			"DriveId":  "1812",
			"TargetId": "?",
			"Target":   "",
			"SiteId":   "5",
		},
		ContentCounts: map[string]int32{
			"MaxSpectra":        2,
			"PseudoIntensities": 225,
			"NormalSpectra":     450,
			"DwellSpectra":      0,
			"BulkSpectra":       2,
		},
	}

	scanInsert, err := db.Collection(dbCollections.ScansName).InsertOne(ctx, scanItem, options.InsertOne())
	fmt.Printf("Scan insert err: %v, scanInsert: %+v\n", err, scanInsert)

	fmt.Printf("Zero values: %v\n", UpdateScanImageDataTypes(scanItem.Id, db, l))
	scanRead, err := scan.ReadScanItem(scanItem.Id, db)
	fmt.Printf("SavedScanCounts: %v Err: %v\n", scanTypeToString(scanRead.DataTypes), err)

	// Insert image
	img := &protos.ScanImage{
		ImagePath:         "052822532/PCW_0138_0679216324_000RCM_N00518120528225320077075J03.png",
		Source:            1,
		Width:             752,
		Height:            580,
		FileSize:          230396,
		Purpose:           protos.ScanImagePurpose_SIP_VIEWING,
		AssociatedScanIds: []string{"052822532"},
		OriginScanId:      "052822532",
	}

	imgInsert, err := db.Collection(dbCollections.ImagesName).InsertOne(ctx, img, options.InsertOne())
	fmt.Printf("Image insert err: %v, imgInsert: %+v\n", err, imgInsert)

	fmt.Printf("One image: %v\n", UpdateScanImageDataTypes(scanItem.Id, db, l))
	scanRead, err = scan.ReadScanItem(scanItem.Id, db)
	fmt.Printf("SavedScanCounts: %v Err: %v\n", scanTypeToString(scanRead.DataTypes), err)

	// Insert RGBU images
	rgbu := &protos.ScanImage{
		ImagePath:         "052822532/PCCR0138_0679289188_000VIS_N005000005282253200770LUD01.tif",
		Source:            2,
		Width:             752,
		Height:            580,
		FileSize:          18173394,
		Purpose:           protos.ScanImagePurpose_SIP_MULTICHANNEL,
		AssociatedScanIds: []string{"052822532"},
		OriginScanId:      "052822532",
	}

	rgbuInsert, err := db.Collection(dbCollections.ImagesName).InsertOne(ctx, rgbu, options.InsertOne())
	fmt.Printf("RGBU Image insert err: %v, rgbuInsert: %+v\n", err, rgbuInsert)

	fmt.Printf("Two image: %v\n", UpdateScanImageDataTypes(scanItem.Id, db, l))
	scanRead, err = scan.ReadScanItem(scanItem.Id, db)
	fmt.Printf("SavedScanCounts: %v Err: %v\n", scanTypeToString(scanRead.DataTypes), err)

}

func scanTypeToString(dt []*protos.ScanItem_ScanTypeCount) string {
	result := ""
	for _, i := range dt {
		result += fmt.Sprintf(" %v %v=%v", result, i.DataType, i.Count)
	}
	return result
}
Output:

Non-existant: mongo: no documents in result
Scan insert err: <nil>, scanInsert: &{InsertedID:052822532}
Zero values: <nil>
SavedScanCounts:   SD_XRF=234 Err: <nil>
Image insert err: <nil>, imgInsert: &{InsertedID:052822532/PCW_0138_0679216324_000RCM_N00518120528225320077075J03.png}
One image: <nil>
SavedScanCounts:   SD_XRF=234   SD_XRF=234 SD_IMAGE=1 Err: <nil>
RGBU Image insert err: <nil>, rgbuInsert: &{InsertedID:052822532/PCCR0138_0679289188_000VIS_N005000005282253200770LUD01.tif}
Two image: <nil>
SavedScanCounts:   SD_XRF=234   SD_XRF=234 SD_IMAGE=1   SD_XRF=234   SD_XRF=234 SD_IMAGE=1 SD_RGBU=1 Err: <nil>

Index

Examples

Constants

View Source
const Auth0UserIdFieldMaxLength = 32
View Source
const DescriptionFieldMaxLength = 1024 * 5
View Source
const IdFieldMaxLength = 32
View Source
const SourceCodeMaxLength = 1024 * 1024 * 5 // Trying to be very generous here, but maybe this is not enough?
View Source
const TagListMaxLength = 100

Variables

View Source
var MaxFileCacheAgeSec = int64(60 * 5)
View Source
var MaxFileCacheSizeBytes = uint64(200 * 1024 * 1024)

Functions

func CheckFieldLength

func CheckFieldLength[T any](field []T, fieldName string, minLength int, maxLength int) error

func CheckObjectAccess

func CheckObjectAccess(requireEdit bool, objectId string, objectType protos.ObjectType, hctx HandlerContext) (*protos.OwnershipItem, error)

Checks object access - if requireEdit is true, it checks for edit access otherwise just checks for view access. Returns an error if it failed to determine or if access is not granted, returns error formed with MakeUnauthorisedError

func CheckObjectAccessForUser

func CheckObjectAccessForUser(requireEdit bool, objectId string, objectType protos.ObjectType, userId string, memberOfGroupIds []string, viewerOfGroupIds []string, db *mongo.Database) (*protos.OwnershipItem, error)

func CheckStringField

func CheckStringField(field *string, fieldName string, minLength int, maxLength int) error

func ClearCacheForScanId added in v4.20.6

func ClearCacheForScanId(scanId string, ts timestamper.ITimeStamper, l logger.ILogger)

func DeleteUserObject

func DeleteUserObject[T any](objectId string, objectType protos.ObjectType, collectionName string, hctx HandlerContext) (*T, error)

func FetchOwnershipSummary added in v4.3.12

func FetchOwnershipSummary(ownership *protos.OwnershipItem, sessionUser SessionUser, db *mongo.Database, ts timestamper.ITimeStamper, fullDetails bool) *protos.OwnershipSummary

func FindUserIdsFor

func FindUserIdsFor(objectId string, mongoDB *mongo.Database) ([]string, error)

func GenerateIJs added in v4.24.3

func GenerateIJs(imageName string, scanId string, instrument protos.ScanInstrument, svcs *services.APIServices) (*protos.ImageLocations, error)

func GetCachedUserGroupMembership

func GetCachedUserGroupMembership(userId string) ([]string, bool)

func GetCachedUserGroupViewership added in v4.8.1

func GetCachedUserGroupViewership(userId string) ([]string, bool)

func GetDBUser

func GetDBUser(userId string, db *mongo.Database) (*protos.UserDBItem, error)

func GetUserIdsForGroup

func GetUserIdsForGroup(groupIds []string, mongoDB *mongo.Database) ([]string, error)

func GetUserObjectById

func GetUserObjectById[T any](forEditing bool, objectId string, objectType protos.ObjectType, collectionName string, hctx HandlerContext) (*T, *protos.OwnershipItem, error)

func HasPermission

func HasPermission(userPermissions map[string]bool, toCheck protos.Permission) bool

func ListAccessibleIDs

func ListAccessibleIDs(requireEdit bool, objectType protos.ObjectType, svcs *services.APIServices, requestorSession SessionUser) (map[string]*protos.OwnershipItem, error)

Gets all object IDs which the user has access to - if requireEdit is true, it checks for edit access otherwise just checks for view access Returns a map of object id->creator user id

func ListGroupAccessibleIDs

func ListGroupAccessibleIDs(requireEdit bool, objectType protos.ObjectType, groupID string, mongoDB *mongo.Database) (map[string]*protos.OwnershipItem, error)

func MakeFilter

func MakeFilter(
	searchParams *protos.SearchParams,
	requireEdit bool,
	objectType protos.ObjectType,
	hctx HandlerContext) (bson.M, map[string]*protos.OwnershipItem, error)

func MakeFullOwnerSummary added in v4.3.12

func MakeFullOwnerSummary(ownership *protos.OwnershipItem, sessionUser SessionUser, db *mongo.Database, ts timestamper.ITimeStamper) *protos.OwnershipSummary

func MakeOwnerForWrite

func MakeOwnerForWrite(objectId string, objectType protos.ObjectType, creatorUserId string, createTimeUnixSec int64) *protos.OwnershipItem

func MakeOwnerSummary

func MakeOwnerSummary(ownership *protos.OwnershipItem, sessionUser SessionUser, db *mongo.Database, ts timestamper.ITimeStamper) *protos.OwnershipSummary

func NotifyUserInfoChange

func NotifyUserInfoChange(userId string)

func ReadDatasetFile

func ReadDatasetFile(scanId string, svcs *services.APIServices) (*protos.Experiment, error)

func ReadDiffractionFile

func ReadDiffractionFile(scanId string, svcs *services.APIServices) (*protos.Diffraction, error)

func ReadQuantificationFile

func ReadQuantificationFile(quantId string, quantPath string, svcs *services.APIServices) (*protos.Quantification, error)

func SendForSession

func SendForSession(s *melody.Session, wsmsg *protos.WSMessage)

func UpdateScanImageDataTypes added in v4.20.1

func UpdateScanImageDataTypes(scanId string, db *mongo.Database, logger logger.ILogger) error

Types

type HandlerContext

type HandlerContext struct {
	Session  *melody.Session
	SessUser SessionUser
	Melody   *melody.Melody
	Svcs     *services.APIServices
}

type SessionUser

type SessionUser struct {
	SessionId              string
	User                   *protos.UserInfo
	Permissions            map[string]bool
	MemberOfGroupIds       []string
	ViewerOfGroupIds       []string
	NotificationSubscribed bool
}

func CreateDBUser

func CreateDBUser(sessionId string, jwtUser jwtparser.JWTUserInfo, db *mongo.Database) (*SessionUser, error)

If we have a successful login and the user is not in our DB, we write a default record for them, so if they change their details we have a spot to save it already NOTE: This is (at time of writing) the only way to add a user to the DB

func GetSessionUser

func GetSessionUser(s *melody.Session) (SessionUser, error)

func MakeSessionUser

func MakeSessionUser(sessionId string, jwtUser jwtparser.JWTUserInfo, db *mongo.Database) (*SessionUser, error)

JWT user has the user ID and permissions that we get from Auth0. The rest is handled within PIXLISE, so lets read our DB to see if this user exists and get their user name, email, icon, etc

Jump to

Keyboard shortcuts

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