Skip to content

Commit

Permalink
Merge pull request #223 from pixlise/feature/beams-sans-img
Browse files Browse the repository at this point in the history
Feature/beams sans img
  • Loading branch information
pnemere committed May 8, 2024
2 parents 736b9d3 + 0ada9b5 commit 405b5b1
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 53 deletions.
111 changes: 76 additions & 35 deletions api/ws/handlers/image-beam-location.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package wsHandler

import (
"context"
"errors"
"fmt"

"github.com/pixlise/core/v4/api/dbCollections"
Expand All @@ -14,55 +15,93 @@ import (
)

func HandleImageBeamLocationsReq(req *protos.ImageBeamLocationsReq, hctx wsHelpers.HandlerContext) (*protos.ImageBeamLocationsResp, error) {
if err := wsHelpers.CheckStringField(&req.ImageName, "ImageName", 1, 255); err != nil {
return nil, err
}

ctx := context.TODO()
coll := hctx.Svcs.MongoDB.Collection(dbCollections.ImageBeamLocationsName)
var locs *protos.ImageLocations

// Read the image and check that the user has access to all scans associated with it
result := coll.FindOne(ctx, bson.M{"_id": req.ImageName})
if result.Err() != nil {
if result.Err() == mongo.ErrNoDocuments {
return nil, errorwithstatus.MakeNotFoundError(req.ImageName)
// If we have generateForScanId set, we don't want to have an image name!
if len(req.GenerateForScanId) > 0 {
if len(req.ImageName) > 0 {
return nil, errorwithstatus.MakeBadRequestError(errors.New("Expected empty image name for request with GenerateForScanId set"))
}
return nil, result.Err()
}

locs := protos.ImageLocations{}
err := result.Decode(&locs)
if err != nil {
return nil, err
}
// Check user has access to this scan
_, err := wsHelpers.CheckObjectAccess(false, req.GenerateForScanId, protos.ObjectType_OT_SCAN, hctx)
if err != nil {
return nil, err
}

if len(locs.LocationPerScan) <= 0 {
return nil, fmt.Errorf("No beams defined for image: %v", req.ImageName)
}
// Read the scan item so we get the right instrument
coll := hctx.Svcs.MongoDB.Collection(dbCollections.ScansName)
scanResult := coll.FindOne(ctx, bson.M{"_id": req.GenerateForScanId}, options.FindOne())
if scanResult.Err() != nil {
return nil, errorwithstatus.MakeNotFoundError(req.GenerateForScanId)
}

scan := &protos.ScanItem{}
err = scanResult.Decode(scan)
if err != nil {
return nil, fmt.Errorf("Failed to decode scan: %v. Error: %v", req.GenerateForScanId, err)
}

// Generate away! NOTE: empty image name implies this won't write to DB
locs, err = generateIJs("", req.GenerateForScanId, scan.Instrument, hctx)
if err != nil {
return nil, err
}
} else {
// We MUST have an image name in this case
if err := wsHelpers.CheckStringField(&req.ImageName, "ImageName", 1, 255); err != nil {
return nil, err
}

coll := hctx.Svcs.MongoDB.Collection(dbCollections.ImageBeamLocationsName)

// Read the image and check that the user has access to all scans associated with it
result := coll.FindOne(ctx, bson.M{"_id": req.ImageName})
if result.Err() != nil {
if result.Err() == mongo.ErrNoDocuments {
return nil, errorwithstatus.MakeNotFoundError(req.ImageName)
}
return nil, result.Err()
}

for _, scanLocs := range locs.LocationPerScan {
_, err := wsHelpers.CheckObjectAccess(false, scanLocs.ScanId, protos.ObjectType_OT_SCAN, hctx)
err := result.Decode(&locs)
if err != nil {
return nil, err
}

if len(locs.LocationPerScan) <= 0 {
return nil, fmt.Errorf("No beams defined for image: %v", req.ImageName)
}

for _, scanLocs := range locs.LocationPerScan {
_, err := wsHelpers.CheckObjectAccess(false, scanLocs.ScanId, protos.ObjectType_OT_SCAN, hctx)
if err != nil {
return nil, err
}
}
}

// Return the coordinates from DB record
return &protos.ImageBeamLocationsResp{
Locations: &locs,
Locations: locs,
}, nil
}

func generateIJs(imageName string, scanId string, instrument protos.ScanInstrument, hctx wsHelpers.HandlerContext) error {
func generateIJs(imageName string, scanId string, instrument protos.ScanInstrument, hctx wsHelpers.HandlerContext) (*protos.ImageLocations, error) {
hctx.Svcs.Log.Infof("Generating IJ's for image: %v, scan: %v...", imageName, scanId)
// Read the dataset file
exprPB, err := wsHelpers.ReadDatasetFile(scanId, hctx.Svcs)
if err != nil {
return err
return nil, err
}

// Generate coordinates
scale := float32(100.0) // We scale XY up by this much to make them not be bunched up so much, so the image doesn't have to scale down too much (it's a bit arbitrary)
scale := float32(1)

if len(imageName) > 0 {
scale = 100 // We scale XY up by this much to make them not be bunched up so much, so the image doesn't have to scale down too much (it's a bit arbitrary)
}

coords := []*protos.Coordinate2D{}
for _, loc := range exprPB.Locations {
Expand All @@ -83,17 +122,19 @@ func generateIJs(imageName string, scanId string, instrument protos.ScanInstrume
}},
}

ctx := context.TODO()
coll := hctx.Svcs.MongoDB.Collection(dbCollections.ImageBeamLocationsName)
if len(imageName) > 0 {
ctx := context.TODO()
coll := hctx.Svcs.MongoDB.Collection(dbCollections.ImageBeamLocationsName)

result, err := coll.InsertOne(ctx, &locs, options.InsertOne())
if err != nil {
return err
}
result, err := coll.InsertOne(ctx, &locs, options.InsertOne())
if err != nil {
return nil, err
}

if result.InsertedID != imageName {
return fmt.Errorf("Inserting generated beam IJs, expected id: %v, got: %v", imageName, result.InsertedID)
if result.InsertedID != imageName {
return nil, fmt.Errorf("Inserting generated beam IJs, expected id: %v, got: %v", imageName, result.InsertedID)
}
}

return nil
return &locs, nil
}
2 changes: 1 addition & 1 deletion api/ws/handlers/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ func HandleImageUploadReq(req *protos.ImageUploadReq, hctx wsHelpers.HandlerCont
}

if generateCoords {
err = generateIJs(scanImage.ImagePath, req.OriginScanId, scan.Instrument, hctx)
_, err = generateIJs(scanImage.ImagePath, req.OriginScanId, scan.Instrument, hctx)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion data-formats
40 changes: 25 additions & 15 deletions generated-protos/image-beam-location-msgs.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion internal/cmd-line-tools/api-integration-test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func main() {

flag.Parse()

if strings.Contains(apiHost, "pixlise.org") && !strings.Contains(apiHost, "review.pixlise.org") {
if strings.Contains(apiHost, "pixlise.org") && !strings.Contains(apiHost, "review.pixlise.org") && !strings.Contains(apiHost, "dev-api.pixlise.org") {
fmt.Printf("Can't connect to an env for now: %v\n", apiHost)
return
}
Expand Down Expand Up @@ -162,6 +162,7 @@ func runLocalTests(apiHost string, isCI bool) {

// Test log query (for data import)
// Run integration test against review env?
//return

// *** NOT RUNNING THIS LOCALLY, NO LAMBDA IS STARTED *** testScanImport(apiHost)
u1Id, u2Id := testNotification(apiHost)
Expand Down

0 comments on commit 405b5b1

Please sign in to comment.