-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
242 lines (196 loc) · 5.65 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
)
var (
/*
labsDB is the wrapper around the lab user info database.
It keeps track of all the users and the current jobs that
have been handed out to them.
*/
labsDB *LabsDB
/*
workDB is the wrapper around the database that keeps track of
all the jobs that have been assigned and yet to be assigned.
*/
workDB *WorkDB
/*
labelsDB is the wrapper around the database that stores all the
classifications that the users send back to the server. Keys are
Block ID's, values are Block JSONs
*/
labelsDB *LabelsDB
/*
manifestFile is the path to the path_manifests.csv file.
This contains the name of the clan files and the paths to
all the blocks that are a part of them.
format:
[clan_file, block_index, path_to_block]
*/
manifestFile string
/*
configFile is the path to the main config file,
which has the server admin keys and other metadata
*/
configFile string
/*
mainConfig is the Config struct produced from reading
the configFile
*/
mainConfig Config
/*
dataMap is the global map of CLAN files to block paths
*/
dataMap DataMap
/*
workItemMap is a map of WorkItem to bool.
The boolean value represents whether or not
the particular work item is active or not.
Active means it's been sent out for coding
and has not been submitted back yet.
*/
workItemMap WorkItemMap
/*
workItemMapEncoded is a json encoded version of the workItemMap
*/
workItemMapEncoded []byte
/*
activeWorkItems is a map of WorkItem ID's. All the ID's
represent blocks which have been sent out to be worked on.
(i.e. active blocks)
format:
map["30_13_coderJS_final-2:::6" : true , "32_13_coderJS_final-5:::16" : true, ......]
The ID's are a concatenation of the name of the CLAN file of origin
and the block index, separated by ":::".
*/
activeWorkItems ActiveDataQueue
)
const (
/*
dataPath is the path to where all the
CLAN files and audio blocks are going
to be stored
*/
dataPath = "data"
/*
numBlocksToSend is the number of blocks that will be sent
from any given CLAN file to the end user upon request
*/
numBlocksToSend = 5
)
/*
Config is a struct representing metadata about
the configuration state of the server upon startup.
It's loaded from the config.json file read in as argument
from the command line upon starting the server.
*/
type Config struct {
AdminKey string `json:"admin_key"`
WorkMapLoaded bool `json:"work_map_loaded"`
Labs []string `json:"labs"`
LabsDBPath string `json:"labs_db_path"`
WorkDBPath string `json:"work_db_path"`
LabelsDBPath string `json:"labels_db_path"`
}
func (conf *Config) encode() ([]byte, error) {
enc, err := json.MarshalIndent(conf, "", " ")
if err != nil {
return nil, err
}
return enc, nil
}
func (conf *Config) writeFile() {
fmt.Println("tried to update the config file")
encodedConf, err := conf.encode()
if err != nil {
log.Fatal(err)
}
writeErr := ioutil.WriteFile(configFile, encodedConf, 0644)
if err != nil {
log.Fatal(writeErr)
}
}
func (conf *Config) labIsRegistered(labKey string) bool {
for _, lab := range conf.Labs {
if lab == labKey {
return true
}
}
return false
}
func (conf *Config) labIsAdmin(labKey string) bool {
if labKey == conf.AdminKey {
return true
}
return false
}
func readConfigFile(path string) Config {
file, err := ioutil.ReadFile(path)
if err != nil {
log.Fatal(err)
}
var config Config
json.Unmarshal(file, &config)
return config
}
func shutDown() {
}
func setDBPaths() {
workDBPath = mainConfig.WorkDBPath
labsDBPath = mainConfig.LabsDBPath
labelsDBPath = mainConfig.LabelsDBPath
}
func main() {
configFile = os.Args[1]
manifestFile = os.Args[2]
mainConfig = readConfigFile(configFile)
setDBPaths()
fmt.Println("mainConfig: ")
fmt.Println(mainConfig)
// Open the LabsDB
labsDB = LoadLabsDB()
defer labsDB.Close()
workDB = LoadWorkDB()
defer workDB.Close()
labelsDB = LoadLabelsDB()
defer labelsDB.Close()
dataMap := fillDataMap()
// get the WorkItemMap, either from the dataMap,
// or from the workDB on disk.
if !mainConfig.WorkMapLoaded {
workItemMap = dataMap.partitionIntoWorkItemsMap()
workItemMapEncoded, _ = json.Marshal(workItemMap)
workDB.persistWorkItemMap(workItemMap)
mainConfig.WorkMapLoaded = true
mainConfig.writeFile()
} else {
workItemMap = workDB.loadItemMap()
workItemMapEncoded, _ = json.Marshal(workItemMap)
}
fmt.Println("# of work items map: ", len(workItemMap))
http.HandleFunc("/", mainHandler)
http.HandleFunc("/v1/get-block/", getBlockHandler)
http.HandleFunc("/v1/get-specific-block/", getSpecificBlockHandler)
http.HandleFunc("/v1/get-block-list/", getWorkItemMapHandler)
http.HandleFunc("/v1/delete-block/", deleteBlockHandler)
http.HandleFunc("/v1/delete-user/", deleteUserHandler)
http.HandleFunc("/v1/lab-info/", labInfoHandler)
http.HandleFunc("/v1/all-lab-info/", allLabInfoHandler)
http.HandleFunc("/v1/add-user/", addUserHandler)
http.HandleFunc("/v1/submit-labels/", submitLabelsHandler)
http.HandleFunc("/v1/submit-wo-labels/", submitWOLabelsHandler)
http.HandleFunc("/v1/get-labels/", getLabelsHandler)
http.HandleFunc("/v1/get-lab-labels/", getLabLabelsHandler)
http.HandleFunc("/v1/get-all-labels/", getAllLabelsHandler)
http.HandleFunc("/v1/get-train-labels/", getTrainingLabelsHandler)
http.HandleFunc("/v1/get-relia-labels/", getReliabilityHandler)
http.HandleFunc("/v1/migrate-add-block-labels/", migrateAddLabeledBlockHandler)
http.HandleFunc("/v1/migrate-add-user/", migrateAddUserHandler)
http.HandleFunc("/v1/migrate-set-active-work-item/", migrateSetActiveWorkItemHandler)
http.ListenAndServe(":8080", nil)
}