-
Notifications
You must be signed in to change notification settings - Fork 2
/
outliers.go
75 lines (61 loc) · 1.73 KB
/
outliers.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
package jitter
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"gogs.bellstone.ca/james/jitter/lib/data"
"gogs.bellstone.ca/james/jitter/lib/mef"
"gogs.bellstone.ca/james/jitter/lib/mem"
)
func OutlierScoreHandler(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
}
name, mm, err := getMemFromRequest(r, mefData)
if err != nil {
setError(w, "Could not load Mem from request because "+err.Error())
return
}
os := norm.OutlierScores(mm)
jsOSArray, err := json.Marshal(&os)
if err != nil {
setError(w, "Could not create outlier score JSON due to error: "+err.Error())
return
}
log.Println("Served outlier scores for " + name)
fmt.Fprintln(w, string(jsOSArray))
}
func getMemFromRequest(r *http.Request, mefData mef.Mef) (string, *mem.Mem, error) {
name := r.FormValue("name")
if name == "" {
if r.Body == nil {
return "", nil, errors.New("could not make outlier scores for empty participant")
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return "", nil, errors.New("could not read request body because " + err.Error())
}
var mm mem.Mem
err = json.Unmarshal(body, &mm)
if err != nil {
return "", nil, errors.New("could not unmarshal Mem because " + err.Error())
}
return "Uploaded MEM '" + mm.Header.Name + "'", &mm, nil
}
mm := mefData.MemWithKey(name)
if mm == nil {
return "", nil, errors.New("could not find participant '" + name + "'")
}
return name, mm, nil
}