-
Notifications
You must be signed in to change notification settings - Fork 1
/
realtime.go
163 lines (128 loc) · 3.63 KB
/
realtime.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
package main
import (
"encoding/json"
"fmt"
// "reflect"
"github.com/boltdb/bolt"
// "log"
// "strconv"
"sync"
"time"
)
func schedule(what func(), delay time.Duration) chan bool {
stop := make(chan bool)
go func() {
for {
what()
select {
case <-time.After(delay):
case <-stop:
return
}
}
}()
return stop
}
type RealTimeWatcher struct {
StartTime time.Time
Pluse time.Duration
IsWatching chan bool
}
func StartRealTimeWatcher() {
rt := RealTimeWatcher{
time.Now(),
(1 * 500),
make(chan bool),
}
fmt.Println(client.boltDB)
ping := func() {
beg := time.Now()
didProcess := false
students := GetStudentKeys()
// loop over all students
// fmt.Println("Num Students: ", students)
// slice := []string{"a", "b", "c", "d", "e"}
// sliceLength := len(slice)
// for i := 0; i < len(students); i++ {
var wg sync.WaitGroup
wg.Add(len(students))
// fmt.Println("Running for loop…")
for i := 0; i < len(students); i++ {
go func(i int) {
defer wg.Done()
// msg := fmt.Sprintf("%4d", time.Now().UTC().Unix()) + ": Heartbeat"
// globalSocketHandler.Broadcast([]byte(msg))
// val := students[i]
// fmt.Printf("i: %v, val: %v\n", i, val)
student := GetStudent(students[i])
// if student has update flag
if student.ShouldUpdateAuditFlag {
// fmt.Println("Should Update!")
// fmt.Println(student.Name)
fmt.Println("Got:", students[i])
studentkey := students[i]
// check all thier enrolled tasks
for i := 0; i < len(student.TaskMapList); i++ {
tmap := student.TaskMapList[i]
taskMap := GetTaskMap(tmap)
// run against each activity
for j := 0; j < len(student.ActivityLogList); j++ {
alog := student.ActivityLogList[j]
activityLog := GetActivityLog(alog)
audit, wl := RunAudit(taskMap, activityLog)
fmt.Println("In Activity", studentkey)
var summary ProgressSummary
wkey := AddWhatsLeftAudit(*wl)
key := AddTaskAudit(*audit)
student.TaskAuditList = append(student.TaskAuditList, string(key))
student.WhatsLeftList = append(student.WhatsLeftList, string(wkey))
// fmt.Println("Taysk Audit Key: ", key)
summary.Major = taskMap.Name
summary.StudentKey = studentkey
summary.Comp = len(audit.ReqCommits) - len(wl.ReqCommits)
summary.Total = len(taskMap.Requirements)
summary.AuditID = string(key)
summary.TaskID = tmap
summary.ActivityID = alog
summary.LastUpdate = fmt.Sprintf("%4d", time.Now().UTC().Unix())
// fmt.Println(len(audit.Commits))
UpsertNewStudentSummary(studentkey, summary)
}
}
student.ShouldUpdateAuditFlag = false
client.boltDB.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte("enrolled_students"))
if err != nil {
return err
}
// b := tx.Bucket()
// if b == nil {
// return fmt.Errorf("Bucket %s not found!", "enrolled_students")
// }
// v := b.Get([]byte(students[i]))
jsonData, err := json.Marshal(student)
if err != nil {
fmt.Println(err)
}
err = b.Put([]byte(students[i]), jsonData)
return err
})
didProcess = true
globalSocketHandler.Broadcast([]byte(studentkey))
}
}(i)
}
wg.Wait()
// fmt.Println("Finished for loop")
if didProcess {
timeTrack(beg, "Daemon Total Run Time \t\t\t")
}
}
stop := schedule(ping, rt.Pluse*time.Millisecond)
rt.IsWatching = stop
// time.Sleep(2500 * time.Millisecond)
// stop <- true
// time.Sleep(2500 * time.Millisecond)
// fmt.Println(rt)
fmt.Println("Done")
}