-
Notifications
You must be signed in to change notification settings - Fork 1
/
roi.go
217 lines (184 loc) · 6.12 KB
/
roi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package main
import (
"context"
"fmt"
"path/filepath"
"strings"
"github.com/pixlise/core/v4/api/dbCollections"
"github.com/pixlise/core/v4/core/fileaccess"
"github.com/pixlise/core/v4/core/indexcompression"
"github.com/pixlise/core/v4/core/utils"
protos "github.com/pixlise/core/v4/generated-protos"
"go.mongodb.org/mongo-driver/mongo"
)
type SrcMistROIItem struct {
Species string `json:"species"`
MineralGroupID string `json:"mineralGroupID"`
ID_Depth int32 `json:"ID_Depth"`
ClassificationTrail string `json:"ClassificationTrail"`
Formula string `json:"formula"`
}
type SrcROIItem struct {
Name string `json:"name"`
LocationIndexes []int32 `json:"locationIndexes"`
Description string `json:"description"`
ImageName string `json:"imageName,omitempty"`
PixelIndexes []int32 `json:"pixelIndexes,omitempty"`
MistROIItem SrcMistROIItem `json:"mistROIItem"`
Tags []string `json:"tags"`
}
type SrcROISavedItem struct {
*SrcROIItem
*SrcAPIObjectItem
}
type SrcROILookup map[string]SrcROISavedItem
func migrateROIs(
userContentBucket string,
userContentFiles []string,
limitToDatasetIds []string,
fs fileaccess.FileAccess,
dest *mongo.Database,
userGroups map[string]string) error {
coll := dest.Collection(dbCollections.RegionsOfInterestName)
err := coll.Drop(context.TODO())
if err != nil {
return err
}
sharedItems := SrcROILookup{}
sharedItemScanIds := map[string]string{}
for _, p := range userContentFiles {
if strings.HasSuffix(p, "ROI.json") && strings.HasPrefix(p, "UserContent/shared/") {
scanId := filepath.Base(filepath.Dir(p))
if len(limitToDatasetIds) > 0 && !utils.ItemInSlice(scanId, limitToDatasetIds) {
fmt.Printf(" SKIPPING shared roi for dataset id: %v...\n", scanId)
continue
}
// Read this file
items := SrcROILookup{}
err = fs.ReadJSON(userContentBucket, p, &items, false)
if err != nil {
return err
}
// Store these till we're finished here
for id, item := range items {
sharedItems[ /*scanId+"_"+*/ id] = item
sharedItemScanIds[id] = scanId
}
sharedItems = items
}
}
allItems := SrcROILookup{}
for _, p := range userContentFiles {
if strings.HasSuffix(p, "ROI.json") && !strings.HasPrefix(p, "UserContent/shared/") {
scanId := filepath.Base(filepath.Dir(p))
userIdFromPath := filepath.Base(filepath.Dir(filepath.Dir(p)))
if shouldIgnoreUser(userIdFromPath) {
fmt.Printf(" SKIPPING import of ROI from user: %v aka %v\n", userIdFromPath, usersIdsToIgnore[userIdFromPath])
continue
}
if len(limitToDatasetIds) > 0 && !utils.ItemInSlice(scanId, limitToDatasetIds) {
fmt.Printf(" SKIPPING roi for dataset id: %v...\n", scanId)
continue
}
// Read this file
items := SrcROILookup{}
err = fs.ReadJSON(userContentBucket, p, &items, false)
if err != nil {
return err
}
// Write these to DB and also remember them for later...
for id, item := range items {
if ex, ok := allItems[id]; ok {
fmt.Printf("Duplicate: %v - %v vs %v\n", id, item.Name, ex.Name)
continue
}
if item.SrcAPIObjectItem.Creator.UserID != userIdFromPath {
fmt.Printf("Unexpected ROI user: %v, path had id: %v. ROI was likely copied to another user, skipping...\n", item.SrcAPIObjectItem.Creator.UserID, userIdFromPath)
continue
}
allItems[id] = item
viewerGroupId := ""
if removeIfSharedROI(item, sharedItems) {
viewerGroupId = userGroups["PIXL-FM"]
}
migrateROI(id, scanId, item, coll, dest, viewerGroupId)
}
}
}
fmt.Printf("ROIs inserted: %v\n", len(allItems))
fmt.Println("Adding the following orphaned ROIs (shared but original not found):")
for id, shared := range sharedItems {
fmt.Printf("%v\n", id)
migrateROI(id, sharedItemScanIds[id], shared, coll, dest, userGroups["PIXL-FM"])
}
return err
}
func removeIfSharedROI(roi SrcROISavedItem, sharedROIs SrcROILookup) bool {
for c, sharedItem := range sharedROIs {
if roi.Name == sharedItem.Name &&
roi.Creator.UserID == sharedItem.Creator.UserID {
// Remove this from the shared list
delete(sharedROIs, c)
return true
}
}
return false
}
func migrateROI(roiId string, scanId string, item SrcROISavedItem, coll *mongo.Collection, dest *mongo.Database, viewerGroupId string) error {
tags := item.Tags
if tags == nil {
tags = []string{}
}
scanIdxs, err := indexcompression.EncodeIndexList(item.LocationIndexes)
if err != nil {
return fmt.Errorf("ROI %v: location list error: %v", roiId, err)
}
pixIdxs, err := indexcompression.EncodeIndexList(item.PixelIndexes)
if err != nil {
return fmt.Errorf("ROI %v: pixel list error: %v", roiId, err)
}
destROI := protos.ROIItem{
Id: roiId,
ScanId: scanId,
Name: item.Name,
Description: item.Description,
Tags: tags,
ScanEntryIndexesEncoded: scanIdxs,
ImageName: item.ImageName,
PixelIndexesEncoded: pixIdxs,
ModifiedUnixSec: uint32(item.CreatedUnixTimeSec),
IsMIST: item.MistROIItem.ClassificationTrail != "",
}
_, err = coll.InsertOne(context.TODO(), &destROI)
if err != nil {
return err
}
err = saveOwnershipItem(destROI.Id, protos.ObjectType_OT_ROI, item.Creator.UserID, "", viewerGroupId, uint32(item.CreatedUnixTimeSec), dest)
if err != nil {
return err
}
if item.MistROIItem.ClassificationTrail != "" {
err = migrateMistROI(roiId, scanId, item.MistROIItem, dest)
if err != nil {
return err
}
}
return nil
}
func migrateMistROI(roiId string, scanId string, mistROI SrcMistROIItem, dest *mongo.Database) error {
coll := dest.Collection(dbCollections.MistROIsName)
destMistROI := protos.MistROIItem{
Id: roiId,
ScanId: scanId,
Species: mistROI.Species,
MineralGroupID: mistROI.MineralGroupID,
IdDepth: mistROI.ID_Depth,
ClassificationTrail: mistROI.ClassificationTrail,
Formula: mistROI.Formula,
}
_, err := coll.InsertOne(context.TODO(), &destMistROI)
if err != nil {
return err
}
return nil
}