-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
182 lines (153 loc) · 3.42 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
package main
import (
"database/sql"
"fmt"
"math/rand"
"time"
//"log"
_ "github.com/lib/pq"
"log"
"strconv"
"sync"
)
//devices
type DevicesStruct struct {
Id int
Name string
Userid int
}
//metrics
type DevicesMetricStruct struct {
Id int
Deviceid int
Metric [5]int
LocalTime time.Time
ServerTime time.Time
}
//alerts
type DeviceAlertStruct struct {
Id int
Deviceid int
Message string
}
var DB *sql.DB
var dbUser = "postgres"
var dbPass = "gfhjkm"
var dbName = "dm"
func init() {
var err error
dbinfo := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable", dbUser, dbPass, dbName)
DB, err = sql.Open("postgres", dbinfo)
if err != nil {
panic(err)
}
//defer DB.Close()
}
func GetAllDevicesFromDB() chan DevicesStruct {
out := make(chan DevicesStruct)
rows, err := DB.Query("SELECT * FROM devices;")
if err != nil {
//panic(err)
}
//defer rows.Close()
//fmt.Println(rows)
var numberOfRows = 10000
var m sync.Mutex
for numberOfRows>0 {
go func() {
var newDevice DevicesStruct
m.Lock()
rows.Next()
err := rows.Scan(&newDevice.Id, &newDevice.Name, &newDevice.Userid)
m.Unlock()
if err != nil {
//panic(err)
}
out <- newDevice
}()
numberOfRows--
}
return out
}
func CreateMetric(in chan DevicesStruct) chan DevicesMetricStruct {
time.Sleep(5 * time.Second)
out := make(chan DevicesMetricStruct)
for v := range in {
go func(ds DevicesStruct) {
var newMetric DevicesMetricStruct
newMetric.Id = TableIDs("device_metrics")
newMetric.Deviceid = ds.Id
for i := 0; i < len(newMetric.Metric); i++ {
newMetric.Metric[i] = rand.Intn(50)
}
newMetric.LocalTime = time.Now().AddDate(0, 0, -1)
newMetric.ServerTime = time.Now()
log.Println(newMetric)
var stringQ = "INSERT INTO device_metrics (Id, device_Id, metric_1, metric_2, metric_3, metric_4, metric_5, local_time, server_time) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)"
_, err := DB.Exec(stringQ,
newMetric.Id,
newMetric.Deviceid,
newMetric.Metric[0],
newMetric.Metric[1],
newMetric.Metric[2],
newMetric.Metric[3],
newMetric.Metric[4],
newMetric.LocalTime,
newMetric.ServerTime)
if err != nil {
fmt.Println(err.Error())
//return
}
out <- newMetric
}(v)
}
close(out)
return out
}
func checkMetrics(in chan DevicesMetricStruct) {
go func() {
var newAlert DeviceAlertStruct
for v := range in {
for i := 0; i < len(v.Metric); i++ {
if v.Metric[i] == 43 {
newAlert.Id = TableIDs("device_alerts")
newAlert.Deviceid = v.Deviceid
newAlert.Message = "Bad metric param on device " + strconv.Itoa(v.Deviceid)
_, err := DB.Exec("INSERT INTO device_alerts (id, device_id, message) VALUES ($1, $2, $3)", newAlert.Id, newAlert.Deviceid, newAlert.Message)
if err != nil {
fmt.Println(err.Error())
//return
}
}
}
}
}()
}
func TableIDs(nameT string) (lastID int) {
stringQ := "SELECT COUNT(ID) FROM " + nameT + ";"
rows, err := DB.Query(stringQ)
if err != nil {
panic(err)
}
defer rows.Close()
for rows.Next() {
err := rows.Scan(&lastID)
if err != nil {
fmt.Println(err.Error())
//return
}
}
if err = rows.Err(); err != nil {
fmt.Println(err.Error())
}
lastID++
return lastID
}
func main() {
//GetAllDevicesFromDB()
allDevices := GetAllDevicesFromDB()
allMetrics := CreateMetric(allDevices)
for r := range allMetrics {
fmt.Println(r)
}
}