-
Notifications
You must be signed in to change notification settings - Fork 0
/
habitat.go
144 lines (118 loc) · 3.18 KB
/
habitat.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
package main
import (
"database/sql"
"encoding/json"
"log"
"math"
"time"
"github.com/willf/bloom"
)
const (
bloomFilterSize = 1000
bloomFilterHashFunctions = 5
maxExpectedDevices = 20
)
type Ecosystem struct {
M float64
K float64
B string
}
type Habitat struct {
ID int64
Key string
Ecosystem Ecosystem
TrustScore float64
db *sql.DB
}
func createEcosystem(btDevices []string, wifiNetworks []string) Ecosystem {
filter := bloom.New(bloomFilterSize, bloomFilterHashFunctions)
for _, device := range btDevices {
filter.AddString(device)
}
for _, network := range wifiNetworks {
filter.AddString(network)
}
filterData, _ := filter.MarshalJSON()
filterJSON := make(map[string]interface{})
json.Unmarshal(filterData, &filterJSON)
return Ecosystem{
M: filterJSON["m"].(float64),
K: filterJSON["k"].(float64),
B: filterJSON["b"].(string),
}
}
func (h *Habitat) determineHabitatTrust(db *sql.DB, btDevices []string, wifiNetworks []string) {
h.db = db
h.Ecosystem = createEcosystem(btDevices, wifiNetworks)
h.Key = h.Ecosystem.B
h.getHabitatID()
if h.ID == -1 {
h.insertNewHabitat()
}
h.insertVisitTime()
h.calculateTrustScore(btDevices, wifiNetworks)
h.updateTrustScore()
}
func (h *Habitat) getHabitatID() {
err := h.db.QueryRow("SELECT id FROM habitats WHERE name = ?", h.Key).Scan(&h.ID)
if err == sql.ErrNoRows {
h.ID = -1
} else if err != nil {
log.Fatal(err)
}
}
func (h *Habitat) insertNewHabitat() {
res, err := h.db.Exec("INSERT INTO habitats (name, ecosystem_m, ecosystem_k, ecosystem_b, trust_score) VALUES (?, ?, ?, ?, ?)",
h.Key, h.Ecosystem.M, h.Ecosystem.K, h.Ecosystem.B, 1.0)
if err != nil {
log.Fatal(err)
}
h.ID, err = res.LastInsertId()
if err != nil {
log.Fatal(err)
}
}
func (h *Habitat) insertVisitTime() {
_, err := h.db.Exec("INSERT INTO visit_times (habitat_id, visit_time) VALUES (?, ?)", h.ID, time.Now())
if err != nil {
log.Fatal(err)
}
}
func (h *Habitat) calculateTrustScore(btDevices []string, wifiNetworks []string) {
rows, err := h.db.Query("SELECT visit_time FROM visit_times WHERE habitat_id = ?", h.ID)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
// Calculate the trust score using a time decay approach
now := time.Now()
decayFactor := 0.5
visitTrust := 0.0
visitCount := 0
for rows.Next() {
var visitTime time.Time
err = rows.Scan(&visitTime)
if err != nil {
log.Fatal(err)
}
timeDiff := now.Sub(visitTime)
visitTrust += math.Pow(decayFactor, timeDiff.Hours()/24.0)
visitCount++
}
// Calculate the maximum possible trust score for the current number of visits
maxVisitTrust := 0.0
for i := 0; i < visitCount; i++ {
maxVisitTrust += math.Pow(decayFactor, float64(i))
}
maxVisitTrust *= 10.0 / (1.0 - decayFactor)
// Normalize the trust score to the 1.0-10.0 range
weightDevices := 0.7
deviceTrust := float64(len(btDevices)+len(wifiNetworks)) / float64(maxExpectedDevices)
h.TrustScore = ((weightDevices*deviceTrust)+((1-weightDevices)*visitTrust))/maxVisitTrust*9 + 1
}
func (h *Habitat) updateTrustScore() {
_, err := h.db.Exec("UPDATE habitats SET trust_score = ? WHERE id = ?", h.TrustScore, h.ID)
if err != nil {
log.Fatal(err)
}
}