/
domainDAC.go
88 lines (73 loc) · 2.2 KB
/
domainDAC.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
package reports
import (
"fmt"
"github.com/nsip/dev-nrt/helper"
"github.com/nsip/dev-nrt/records"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)
type DomainDAC struct {
baseReport // embed common setup capability
cfh helper.CodeframeHelper
dacs []string
}
//
// Provides a matrix of DAC values in the form
// DAC Code : true/false
// codes are flagged as true if they were in force
// for the given domain for the student
//
func DomainDACReport(cfh helper.CodeframeHelper) *DomainDAC {
r := DomainDAC{cfh: cfh, dacs: cfh.GetDACs()}
r.initialise("./config/internal/DomainDAC.toml")
r.printStatus()
return &r
}
//
// implement the ...Pipe interface, core work of the
// report engine.
//
func (r *DomainDAC) ProcessStudentRecords(in chan *records.StudentOrientedRecord) chan *records.StudentOrientedRecord {
out := make(chan *records.StudentOrientedRecord)
go func() {
defer close(out)
for sor := range in {
if r.config.activated { // only process if active
sor.CalculatedFields = r.calculateFields(sor)
}
out <- sor
}
}()
return out
}
//
// generates a block of json that can be added to the
// record containing values that are not in the original data
//
//
func (r *DomainDAC) calculateFields(sor *records.StudentOrientedRecord) []byte {
json := sor.CalculatedFields // maintain exsting calc fields
// iterate the events of this student, are keyed by camel-case rendering of test domain
for domain, event := range sor.GetEventsByDomain() {
//
// get any active DACs for this student
//
activeCodes := map[string]struct{}{}
gjson.GetBytes(event, "NAPEventStudentLink.Adjustment.PNPCodeList.PNPCode").
ForEach(func(key, value gjson.Result) bool {
//pnpcode := value.Get("PNPCode").String() // get pnp code
pnpcode := value.String() // get pnp code
activeCodes[pnpcode] = struct{}{} // log as active
return true // keep iterating
})
//
// then create the overall DAC matrix
//
for _, dac := range r.dacs {
_, active := activeCodes[dac]
path := fmt.Sprintf("CalculatedFields.%s.NAPEventStudentLink.Adjustment.PNPCode.%s", domain, dac)
json, _ = sjson.SetBytes(json, path, active)
}
}
return json
}