-
Notifications
You must be signed in to change notification settings - Fork 1
/
quantification-retrieval.go
188 lines (156 loc) · 6.35 KB
/
quantification-retrieval.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
package wsHandler
import (
"fmt"
"path"
"github.com/pixlise/core/v4/api/dbCollections"
"github.com/pixlise/core/v4/api/filepaths"
"github.com/pixlise/core/v4/api/quantification"
"github.com/pixlise/core/v4/api/ws/wsHelpers"
"github.com/pixlise/core/v4/core/errorwithstatus"
protos "github.com/pixlise/core/v4/generated-protos"
)
func HandleQuantListReq(req *protos.QuantListReq, hctx wsHelpers.HandlerContext) (*protos.QuantListResp, error) {
items, idToOwner, err := quantification.ListUserQuants(req.SearchParams, hctx)
if err != nil {
return nil, err
}
quants := []*protos.QuantificationSummary{}
for _, item := range items {
if owner, ok := idToOwner[item.Id]; ok {
item.Owner = wsHelpers.MakeOwnerSummary(owner, hctx.SessUser, hctx.Svcs.MongoDB, hctx.Svcs.TimeStamper)
}
quants = append(quants, item)
}
return &protos.QuantListResp{
Quants: quants,
}, nil
}
func HandleQuantGetReq(req *protos.QuantGetReq, hctx wsHelpers.HandlerContext) (*protos.QuantGetResp, error) {
if err := wsHelpers.CheckStringField(&req.QuantId, "QuantId", 1, wsHelpers.IdFieldMaxLength); err != nil {
return nil, err
}
dbItem, ownerItem, err := wsHelpers.GetUserObjectById[protos.QuantificationSummary](false, req.QuantId, protos.ObjectType_OT_QUANTIFICATION, dbCollections.QuantificationsName, hctx)
if err != nil {
return nil, err
}
// TODO: something with owner? Should we add it to the outgoing item?
// If they want data too, retrieve it
var quant *protos.Quantification
if !req.SummaryOnly {
//quantPath := filepaths.GetQuantPath(hctx.SessUser.User.Id, dbItem.Params.Params.DatasetID, req.QuantId+".bin")
quantPath := path.Join(dbItem.Status.OutputFilePath, req.QuantId+".bin")
quant, err = wsHelpers.ReadQuantificationFile(req.QuantId, quantPath, hctx.Svcs)
if err != nil {
return nil, err
}
}
// We seem to have some old quants where the status struct says start time was 0, but there is another start time in quant params, so
// substitute a non-zero value in this case
if dbItem.Status.StartUnixTimeSec == 0 && dbItem.Params.StartUnixTimeSec > 0 {
dbItem.Status.StartUnixTimeSec = dbItem.Params.StartUnixTimeSec
}
dbItem.Owner = wsHelpers.MakeOwnerSummary(ownerItem, hctx.SessUser, hctx.Svcs.MongoDB, hctx.Svcs.TimeStamper)
return &protos.QuantGetResp{
Summary: dbItem,
Data: quant,
}, nil
}
func HandleQuantLastOutputGetReq(req *protos.QuantLastOutputGetReq, hctx wsHelpers.HandlerContext) (*protos.QuantLastOutputGetResp, error) {
if err := wsHelpers.CheckStringField(&req.ScanId, "ScanId", 1, wsHelpers.IdFieldMaxLength); err != nil {
return nil, err
}
if req.PiquantCommand != "quant" {
return nil, fmt.Errorf("PiquantCommand must be quant") // for now!
}
if req.OutputType != protos.QuantOutputType_QO_DATA && req.OutputType != protos.QuantOutputType_QO_LOG {
return nil, fmt.Errorf("Invalid OutputType")
}
// Get the file name
fileName := ""
if req.OutputType == protos.QuantOutputType_QO_DATA {
fileName = filepaths.QuantLastOutputFileName + ".csv" // quant only supplies this
} else {
fileName = filepaths.QuantLastOutputLogName
}
// Get the path to stream
s3Path := filepaths.GetUserLastPiquantOutputPath(hctx.SessUser.User.Id, req.ScanId, req.PiquantCommand, fileName)
result, err := hctx.Svcs.FS.ReadObject(hctx.Svcs.Config.UsersBucket, s3Path)
if err != nil {
if hctx.Svcs.FS.IsNotFoundError(err) {
// Just return empty
result = []byte{}
err = nil
} else {
return nil, err
}
}
return &protos.QuantLastOutputGetResp{Output: string(result)}, nil
}
func HandleQuantLogListReq(req *protos.QuantLogListReq, hctx wsHelpers.HandlerContext) (*protos.QuantLogListResp, error) {
if err := wsHelpers.CheckStringField(&req.QuantId, "QuantId", 1, wsHelpers.IdFieldMaxLength); err != nil {
return nil, err
}
// Check that user has access to this quant
dbItem, _, err := wsHelpers.GetUserObjectById[protos.QuantificationSummary](false, req.QuantId, protos.ObjectType_OT_QUANTIFICATION, dbCollections.QuantificationsName, hctx)
if err != nil {
return nil, err
}
logFilePaths, err := hctx.Svcs.FS.ListObjects(hctx.Svcs.Config.UsersBucket, path.Join(dbItem.Status.OutputFilePath, req.QuantId+"-logs")+"/")
if err != nil {
return nil, err
}
logFileNames := []string{}
for _, logpath := range logFilePaths {
logFileNames = append(logFileNames, path.Base(logpath))
}
return &protos.QuantLogListResp{
FileNames: logFileNames,
}, nil
}
func HandleQuantLogGetReq(req *protos.QuantLogGetReq, hctx wsHelpers.HandlerContext) (*protos.QuantLogGetResp, error) {
if err := wsHelpers.CheckStringField(&req.QuantId, "QuantId", 1, wsHelpers.IdFieldMaxLength); err != nil {
return nil, err
}
if err := wsHelpers.CheckStringField(&req.LogName, "LogName", 1, wsHelpers.IdFieldMaxLength); err != nil {
return nil, err
}
// Check that user has access to this quant
dbItem, _, err := wsHelpers.GetUserObjectById[protos.QuantificationSummary](false, req.QuantId, protos.ObjectType_OT_QUANTIFICATION, dbCollections.QuantificationsName, hctx)
if err != nil {
return nil, err
}
logPath := path.Join(dbItem.Status.OutputFilePath, req.QuantId+"-logs", req.LogName)
logData, err := hctx.Svcs.FS.ReadObject(hctx.Svcs.Config.UsersBucket, logPath)
if err != nil {
if hctx.Svcs.FS.IsNotFoundError(err) {
return nil, errorwithstatus.MakeNotFoundError(req.LogName)
}
return nil, err
}
return &protos.QuantLogGetResp{
LogData: string(logData),
}, nil
}
func HandleQuantRawDataGetReq(req *protos.QuantRawDataGetReq, hctx wsHelpers.HandlerContext) (*protos.QuantRawDataGetResp, error) {
if err := wsHelpers.CheckStringField(&req.QuantId, "QuantId", 1, wsHelpers.IdFieldMaxLength); err != nil {
return nil, err
}
// Check that user has access to this quant
dbItem, _, err := wsHelpers.GetUserObjectById[protos.QuantificationSummary](false, req.QuantId, protos.ObjectType_OT_QUANTIFICATION, dbCollections.QuantificationsName, hctx)
if err != nil {
return nil, err
}
//UserContent/5df311ed8a0b5d0ebf5fb476/089063943/Quantifications/
// Read the CSV file from S3
csvPath := path.Join(dbItem.Status.OutputFilePath, req.QuantId+".csv")
csvData, err := hctx.Svcs.FS.ReadObject(hctx.Svcs.Config.UsersBucket, csvPath)
if err != nil {
if hctx.Svcs.FS.IsNotFoundError(err) {
return nil, errorwithstatus.MakeNotFoundError(req.QuantId + ".csv")
}
return nil, err
}
return &protos.QuantRawDataGetResp{
Data: string(csvData),
}, nil
}