-
Notifications
You must be signed in to change notification settings - Fork 2
/
convert.go
56 lines (45 loc) · 1.2 KB
/
convert.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
package jitter
import (
"encoding/json"
"fmt"
"log"
"net/http"
"gogs.bellstone.ca/james/jitter/lib/data"
"gogs.bellstone.ca/james/jitter/lib/mef"
"gogs.bellstone.ca/james/jitter/lib/mem"
)
type memWithScores struct {
*mem.Mem `json:"participant"`
mef.OutScores `json:"outlierScores"`
}
func ConvertMemHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
mefData, err := data.AsMef()
if err != nil {
setError(w, "Error loading MEF: "+err.Error())
return
}
norm, err := getFilteredNormsFromRequest(r, mefData)
if err != nil {
setError(w, "Error getting filtered norms due to "+err.Error())
return
}
if r.Body == nil {
setError(w, "Could not load participant from empty body")
return
}
var mws memWithScores
mws.Mem, err = mem.Import(r.Body)
if err != nil {
setError(w, "Could not load Mem from body because "+err.Error())
return
}
mws.OutScores = norm.OutlierScores(mws.Mem)
jsOSArray, err := json.Marshal(&mws)
if err != nil {
setError(w, "Could not create outlier score JSON due to error: "+err.Error())
return
}
log.Println("Served converted MEM for " + mws.Mem.Header.Name)
fmt.Fprintln(w, string(jsOSArray))
}