-
Notifications
You must be signed in to change notification settings - Fork 1
/
roi-shares.go
260 lines (223 loc) · 6.95 KB
/
roi-shares.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/pixlise/core/v4/api/dbCollections"
"github.com/pixlise/core/v4/core/fileaccess"
"github.com/pixlise/core/v4/core/utils"
"go.mongodb.org/mongo-driver/mongo"
)
type SrcROISavedItemWithDataset struct {
DatasetId string
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 SrcROIWithDatasetLookup map[string]SrcROISavedItemWithDataset
func migrateOrphanedSharedROIs(
idsToMigrate []string,
userContentBucket string,
userContentFiles []string,
limitToDatasetIds []string,
fs fileaccess.FileAccess,
dest *mongo.Database,
viewerGroupId string) error {
coll := dest.Collection(dbCollections.RegionsOfInterestName)
err := coll.Drop(context.TODO())
if err != nil {
return err
}
for _, p := range userContentFiles {
if strings.HasSuffix(p, "ROI.json") && strings.HasPrefix(p, "UserContent/shared/") {
scanId := filepath.Base(filepath.Dir(p))
// 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 {
if utils.ItemInSlice(id, idsToMigrate) {
// Yes migrate this one
migrateROI(id, scanId, item, coll, dest, viewerGroupId)
}
}
}
}
return err
}
func migrateROIShares(
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 := SrcROIWithDatasetLookup{}
sharedItemIds := map[string]bool{}
roiFiles := []string{}
for _, p := range userContentFiles {
if strings.HasSuffix(p, "ROI.json") && !strings.Contains(p, "/BACKUP/") {
roiFiles = append(roiFiles, p)
}
}
for _, p := range roiFiles {
if 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 {
if !isMistSet(item) { // ignore MIST
if strings.HasPrefix(item.Name, "mist__roi.") {
fmt.Println(item.Name)
}
sharedItems[ /*scanId+"_"+*/ id] = SrcROISavedItemWithDataset{
DatasetId: scanId,
Name: item.Name,
LocationIndexes: item.LocationIndexes,
Description: item.Description,
ImageName: item.ImageName,
PixelIndexes: item.PixelIndexes,
MistROIItem: item.MistROIItem,
Tags: item.Tags,
}
sharedItemIds[id] = true
}
}
}
}
shared, err := json.MarshalIndent(sharedItems, "", " ")
if err != nil {
return err
}
err = os.WriteFile("roi-shared.json", shared, 0777)
if err != nil {
return err
}
allItems := SrcROIWithDatasetLookup{}
sharedItemToNonSharedLookup := map[string]string{}
nonSharedItemToSharedLookup := map[string]string{}
for _, p := range roiFiles {
if !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 isMistSet(item) {
continue // ignore MIST
}
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)
}
itemToSave := SrcROISavedItemWithDataset{
DatasetId: scanId,
Name: item.Name,
LocationIndexes: item.LocationIndexes,
Description: item.Description,
ImageName: item.ImageName,
PixelIndexes: item.PixelIndexes,
MistROIItem: item.MistROIItem,
Tags: item.Tags,
}
allItems[id] = itemToSave
}
}
}
user, err := json.MarshalIndent(allItems, "", " ")
if err != nil {
return err
}
err = os.WriteFile("roi-user.json", user, 0777)
if err != nil {
return err
}
for id, item := range allItems {
sharedId := findSharedVersion(id, item, sharedItems)
if len(sharedId) > 0 {
sharedItemToNonSharedLookup[sharedId] = id
nonSharedItemToSharedLookup[id] = sharedId
delete(sharedItemIds, sharedId)
}
}
// Print them out
fmt.Println("Id->Shared Id:")
for id, sharedId := range nonSharedItemToSharedLookup {
fmt.Printf("%v -> %v\n", id, sharedId)
}
fmt.Println("Orphaned shared ids:")
for id := range sharedItemIds {
fmt.Printf("%v scanId: %v, name: %v\n", id, sharedItems[id].DatasetId, sharedItems[id].Name)
}
return err
}
func isMistSet(roi SrcROISavedItem) bool {
return len(roi.MistROIItem.Species) > 0 || len(roi.MistROIItem.MineralGroupID) > 0 || len(roi.MistROIItem.Formula) > 0 || len(roi.MistROIItem.ClassificationTrail) > 0
}
func findSharedVersion(roiId string, roi SrcROISavedItemWithDataset, sharedROIs SrcROIWithDatasetLookup) string {
for sharedId, sharedItem := range sharedROIs {
// Check that they're for the same dataset id
if roi.DatasetId == sharedItem.DatasetId {
idxs := utils.SlicesEqual(roi.LocationIndexes, sharedItem.LocationIndexes)
pixels := utils.SlicesEqual(roi.PixelIndexes, sharedItem.PixelIndexes)
name := strings.Contains(roi.Name, sharedItem.Name) || strings.Contains(sharedItem.Name, roi.Name)
compares := 0
if idxs {
compares++
}
if pixels {
compares++
}
if name {
compares++
}
if compares > 1 {
fmt.Printf("user %v, shared %v, name: %v. Matches on idxs: %v, pixels: %v, name: %v\n", roiId, sharedId, roi.Name, idxs, pixels, name)
}
if name && idxs && pixels {
return sharedId
}
}
}
return ""
}