This repository has been archived by the owner on Feb 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
209 lines (176 loc) · 5.45 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"flag"
"github.com/streadway/amqp"
"github.com/tkanos/gonfig"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/logger"
"log"
"ttnmapper-gw-bbox/types"
"ttnmapper-gw-bbox/utils"
)
type Configuration struct {
AmqpHost string `env:"AMQP_HOST"`
AmqpPort string `env:"AMQP_PORT"`
AmqpUser string `env:"AMQP_USER"`
AmqpPassword string `env:"AMQP_PASSWORD"`
AmqpExchange string `env:"AMQP_EXCHANGE_INSERTED"`
AmqpQueue string `env:"AMQP_QUEUE"`
PostgresHost string `env:"POSTGRES_HOST"`
PostgresPort string `env:"POSTGRES_PORT"`
PostgresUser string `env:"POSTGRES_USER"`
PostgresPassword string `env:"POSTGRES_PASSWORD"`
PostgresDatabase string `env:"POSTGRES_DATABASE"`
PostgresDebugLog bool `env:"POSTGRES_DEBUG_LOG"`
PrometheusPort string `env:"PROMETHEUS_PORT"`
}
var myConfiguration = Configuration{
AmqpHost: "localhost",
AmqpPort: "5672",
AmqpUser: "user",
AmqpPassword: "password",
AmqpExchange: "inserted_data",
AmqpQueue: "inserted_data_gateway_bbox",
PostgresHost: "localhost",
PostgresPort: "5432",
PostgresUser: "username",
PostgresPassword: "password",
PostgresDatabase: "database",
PostgresDebugLog: false,
PrometheusPort: "9100",
}
var db *gorm.DB
var rawPacketsChannel = make(chan amqp.Delivery)
func main() {
reprocess := flag.Bool("reprocess", false, "a bool")
flag.Parse()
reprocess_gateways := flag.Args()
err := gonfig.GetConf("conf.json", &myConfiguration)
if err != nil {
log.Println(err)
}
log.Printf("[Configuration]\n%s\n", utils.PrettyPrint(myConfiguration)) // output: [UserA, UserB]
var gormLogLevel = logger.Silent
if myConfiguration.PostgresDebugLog {
log.Println("Database debug logging enabled")
gormLogLevel = logger.Info
}
dsn := "host=" + myConfiguration.PostgresHost + " port=" + myConfiguration.PostgresPort + " user=" + myConfiguration.PostgresUser + " dbname=" + myConfiguration.PostgresDatabase + " password=" + myConfiguration.PostgresPassword + " sslmode=disable"
db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{
Logger: logger.Default.LogMode(gormLogLevel),
})
if err != nil {
panic(err.Error())
}
// Create tables if they do not exist
log.Println("Performing auto migrate")
if err := db.AutoMigrate(
&types.GatewayBoundingBox{},
); err != nil {
log.Println("Unable autoMigrateDB - " + err.Error())
}
// Should we reprocess or listen for live data?
if *reprocess {
log.Println("Reprocessing")
if len(reprocess_gateways) > 0 {
ReprocessGateways(reprocess_gateways)
} else {
ReprocessAll()
}
} else {
// Start amqp listener on this thread - blocking function
log.Println("Starting AMQP thread")
// todo go subscribeToRabbitGatewayMoved()
go subscribeToRabbitRaw()
go processMessages()
log.Printf("Init Complete")
forever := make(chan bool)
<-forever
}
}
func ReprocessAll() {
log.Println("All gateways")
// Get all records
var gateways []types.Gateway
db.Find(&gateways)
for i, gateway := range gateways {
log.Println(i, "/", len(gateways), " ", gateway.NetworkId, " - ", gateway.GatewayId)
ReprocessSingleGateway(gateway)
}
}
func ReprocessGateways(gateways []string) {
for _, gatewayId := range gateways {
// The same gateway_id can exist in multiple networks, so iterate them all
var gateways []types.Gateway
db.Where("gateway_id = ?", gatewayId).Find(&gateways)
for i, gateway := range gateways {
log.Println(i, "/", len(gateways), " ", gateway.NetworkId, " - ", gateway.GatewayId)
ReprocessSingleGateway(gateway)
}
}
}
func ReprocessSingleGateway(gateway types.Gateway) {
/*
1. Find all antennas with same network and gateway id
2. All packets for antennas find min and max lat and lon
3. Gateway location
*/
var antennas []types.Antenna
db.Where("network_id = ? and gateway_id = ?", gateway.NetworkId, gateway.GatewayId).Find(&antennas)
var antennaIds []uint
for _, antenna := range antennas {
antennaIds = append(antennaIds, antenna.ID)
}
log.Println("Antenna IDs: ", antennaIds)
var result types.GatewayBoundingBox
if len(antennaIds) > 0 {
db.Raw(`
SELECT max(latitude) as north, min(latitude) as south FROM
(
SELECT *
FROM packets
WHERE antenna_id IN ?
) t
WHERE latitude != 0 AND experiment_id IS NULL
`, antennaIds).Scan(&result)
db.Raw(`
SELECT max(longitude) as east, min(longitude) as west FROM
(
SELECT *
FROM packets
WHERE antenna_id IN ?
) t
WHERE longitude != 0 AND experiment_id IS NULL
`, antennaIds).Scan(&result)
}
// Take gateway location also into account
if gateway.Latitude == 0 && gateway.Longitude == 0 {
// Gateway location not set
} else {
log.Println("Gateway location:", gateway.Latitude, gateway.Longitude)
if result.North == 0 || gateway.Latitude > result.North {
result.North = gateway.Latitude
}
if result.South == 0 || gateway.Latitude < result.South {
result.South = gateway.Latitude
}
if result.East == 0 || gateway.Longitude > result.East {
result.East = gateway.Longitude
}
if result.West == 0 || gateway.Longitude < result.West {
result.West = gateway.Longitude
}
log.Println(utils.PrettyPrint(result))
}
result.NetworkId = gateway.NetworkId
result.GatewayId = gateway.GatewayId
if result.North == 0 && result.South == 0 && result.East == 0 && result.West == 0 {
log.Println("Bounds zero, not updating")
} else {
db.Clauses(clause.OnConflict{
UpdateAll: true,
}).Create(&result)
}
}