This repository has been archived by the owner on Mar 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
/
storeDevice.go
94 lines (76 loc) · 2.75 KB
/
storeDevice.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
package stats
func (s statStore) GetAggregatedDeviceUsers(entityType, entityID, deviceType, orgID string) (total int, devices []deviceByType, err error) {
//TODO handle diffenent entities
devices = []deviceByType{}
total = 0
rows, err := s.DB.Query(`select count(*),
COALESCE(device_hygiene->'deviceOS'->>'osName','Unknown') as os_name,
COALESCE(device_hygiene->'deviceOS'->>'osVersion','') as os_version
from devices WHERE org_id=$1 AND type=$2
GROUP BY device_hygiene->'deviceOS'->>'osName',device_hygiene->'deviceOS'->>'osVersion'
`, orgID, deviceType)
if err != nil {
return -1, devices, err
}
defer rows.Close()
for rows.Next() {
var userDevice deviceByType
err = rows.Scan(&userDevice.Value, &userDevice.Name, &userDevice.Version)
if err != nil {
return -1, devices, err
}
//total += userDevice.Value
devices = append(devices, userDevice)
}
//TODO wrap error
err = s.DB.QueryRow(`SELECT count(id) from devices where type=$1 AND org_id=$2`, deviceType, orgID).Scan(&total)
return total, devices, err
}
func (s statStore) GetAggregatedMobileDevices(entityType, entityID, orgID string) (total int, devices []deviceByType, err error) {
//TODO handle diffenent entities
devices = []deviceByType{}
total = 0
rows, err := s.DB.Query(`select count(*),
COALESCE(device_hygiene->'deviceInfo'->>'brand','Unknown') as os_name,
COALESCE(device_hygiene->'deviceInfo'->>'deviceModel','') as os_version
from devices WHERE org_id=$1 AND type=$2
GROUP BY device_hygiene->'deviceInfo'->>'brand',device_hygiene->'deviceInfo'->>'deviceModel'
`, orgID, "mobile")
if err != nil {
return -1, devices, err
}
defer rows.Close()
for rows.Next() {
var userDevice deviceByType
err = rows.Scan(&userDevice.Value, &userDevice.Name, &userDevice.Version)
if err != nil {
return -1, devices, err
}
// total += userDevice.Value
devices = append(devices, userDevice)
}
err = s.DB.QueryRow(`SELECT count(id) from devices where type='mobile' AND org_id=$1`, orgID).Scan(&total)
return total, devices, err
}
func (s statStore) GetAggregatedBrowsers(entityType, entityID, orgID string) (total int, devices []deviceByType, err error) {
rows, err := s.DB.Query(`select
COALESCE(name,'') as browser_name,
COALESCE(version,'') as browser_version,
count(*)
from browsers where org_id=$1
GROUP BY name,version ;`, orgID)
if err != nil {
return -1, devices, err
}
for rows.Next() {
var userDevice deviceByType
err = rows.Scan(&userDevice.Name, &userDevice.Version, &userDevice.Value)
if err != nil {
return -1, devices, err
}
//total += userDevice.Value
devices = append(devices, userDevice)
}
err = s.DB.QueryRow(`SELECT count(id) from browsers where org_id=$1`, orgID).Scan(&total)
return total, devices, err
}