-
Notifications
You must be signed in to change notification settings - Fork 14
/
io.go
199 lines (165 loc) · 4.19 KB
/
io.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
package ipwl
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
)
type FileInput struct {
Class string `json:"class"`
FilePath string `json:"filepath"`
IPFS string `json:"ipfs"`
}
type FileOutput struct {
Class string `json:"class"`
FilePath string `json:"filepath"`
IPFS string `json:"ipfs"`
}
func (fo FileOutput) OutputType() string {
return fo.Class
}
type Output interface {
OutputType() string
}
type ArrayFileOutput struct {
Class string `json:"class"`
Files []FileOutput `json:"files"`
}
func (afo ArrayFileOutput) OutputType() string {
return afo.Class
}
type ToolInfo struct {
Name string `json:"name"`
IPFS string `json:"ipfs"`
}
type IO struct {
Tool ToolInfo `json:"tool"`
Inputs map[string]FileInput `json:"inputs"`
Outputs map[string]Output `json:"outputs"`
State string `json:"state"`
ErrMsg string `json:"errMsg"`
UserID string `json:"userId"`
BacalhauJobId string `json:"bacalhauJobId"`
}
func (io *IO) UnmarshalJSON(data []byte) error {
type Alias IO
aux := struct {
Outputs map[string]json.RawMessage `json:"outputs"`
*Alias
}{
Alias: (*Alias)(io),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
io.Outputs = make(map[string]Output)
for k, v := range aux.Outputs {
var fileOutput FileOutput
if err := json.Unmarshal(v, &fileOutput); err != nil {
return err
}
if fileOutput.Class == "Array" {
var arrayFileOutput ArrayFileOutput
if err := json.Unmarshal(v, &arrayFileOutput); err != nil {
return err
}
io.Outputs[k] = arrayFileOutput
} else {
io.Outputs[k] = fileOutput
}
}
return nil
}
func (io IO) MarshalJSON() ([]byte, error) {
type Alias IO
aux := struct {
Outputs map[string]interface{} `json:"outputs"`
*Alias
}{
Outputs: make(map[string]interface{}),
Alias: (*Alias)(&io),
}
for k, v := range io.Outputs {
aux.Outputs[k] = v
}
return json.Marshal(aux)
}
func ReadIOList(filePath string) ([]IO, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, fmt.Errorf("failed to open file: %w", err)
}
defer file.Close()
data, err := ioutil.ReadAll(file)
if err != nil {
return nil, fmt.Errorf("failed to read file: %w", err)
}
var ioLibrary []IO
err = json.Unmarshal(data, &ioLibrary)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal JSON: %w", err)
}
return ioLibrary, nil
}
func WriteIOList(ioJsonPath string, ioList []IO) error {
file, err := os.OpenFile(ioJsonPath, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
}
defer file.Close()
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ")
err = encoder.Encode(ioList)
if err != nil {
return fmt.Errorf("failed to encode IO list: %w", err)
}
return nil
}
func ContainsUserIdAnnotation(slice []string) bool {
for _, a := range slice {
if strings.HasPrefix(a, "userId=") {
return true
}
}
return false
}
func ExtractUserIDFromIOJson(ioJsonPath string) (string, error) {
file, err := os.Open(ioJsonPath)
if err != nil {
return "", fmt.Errorf("failed to open file: %w", err)
}
defer file.Close()
data, err := ioutil.ReadAll(file)
if err != nil {
return "", fmt.Errorf("failed to read file: %w", err)
}
// TODO: this is where we were previously failing on the gateway side
var ioEntries []IO
err = json.Unmarshal(data, &ioEntries)
if err != nil {
return "", fmt.Errorf("failed to unmarshal JSON: %w", err)
}
if len(ioEntries) == 0 {
return "", fmt.Errorf("no IO entries found")
}
return ioEntries[0].UserID, nil
}
func PrintIOGraphStatus(ioList []IO) {
stateCount := make(map[string]int)
// Iterate through the ioList and count the occurrences of each state
for _, io := range ioList {
stateCount[io.State]++
}
// Print the total number of IOs
fmt.Printf("Total IOs: %d\n", len(ioList))
// Print the number of IOs in each state
for state, count := range stateCount {
fmt.Printf("IOs in %s state: %d\n", state, count)
}
}
type OutputValues struct {
FilePaths []string `json:"filePaths"`
CIDs []string `json:"cids"`
CidPaths []string `json:"cidPaths"`
}