Skip to content
This repository was archived by the owner on Jan 19, 2024. It is now read-only.

Commit 46e5ab2

Browse files
committed
Switching to alternative calculation method for city avg
1 parent 67a562f commit 46e5ab2

1 file changed

Lines changed: 99 additions & 95 deletions

File tree

tools/defaultDevice/default.go

Lines changed: 99 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -51,30 +51,30 @@ func GetFrom(from int64, city string) (result vm.DeviceGetData) {
5151
result.City = city
5252

5353
// res, err := dal.ListNoProjection("live", dal.Name("timestamp").GreaterThanEqual(dal.Value(from)), true)
54-
res, err := dal.GetFromIndexWithLimit("live", "city-timestamp-index",
55-
dal.Condition{
56-
"city": {
57-
ComparisonOperator: aws.String("EQ"),
58-
AttributeValueList: []*dal.AttributeValue{
59-
{
60-
S: aws.String(city),
61-
},
62-
},
63-
},
64-
"timestamp": {
65-
ComparisonOperator: aws.String("GT"),
66-
AttributeValueList: []*dal.AttributeValue{
67-
{
68-
N: aws.String(fmt.Sprintf("%v", from)),
69-
},
70-
},
71-
},
72-
})
73-
74-
if err != nil {
75-
fmt.Println("could not retirieve data")
76-
return
77-
}
54+
// res, err := dal.GetFromIndexWithLimit("live", "city-timestamp-index",
55+
// dal.Condition{
56+
// "city": {
57+
// ComparisonOperator: aws.String("EQ"),
58+
// AttributeValueList: []*dal.AttributeValue{
59+
// {
60+
// S: aws.String(city),
61+
// },
62+
// },
63+
// },
64+
// "timestamp": {
65+
// ComparisonOperator: aws.String("GT"),
66+
// AttributeValueList: []*dal.AttributeValue{
67+
// {
68+
// N: aws.String(fmt.Sprintf("%v", from)),
69+
// },
70+
// },
71+
// },
72+
// })
73+
74+
// if err != nil {
75+
// fmt.Println("could not retirieve data")
76+
// return
77+
// }
7878

7979
dbDevicesData := make([]m.Device, 0)
8080
dRes, err := dal.ListNoProjection("devices", dal.Name("active").Equal(dal.Value(true)), true)
@@ -95,73 +95,10 @@ func GetFrom(from int64, city string) (result vm.DeviceGetData) {
9595
continue
9696
}
9797

98-
toIgnore := map[string]bool{
99-
"timestamp": true,
100-
"WATER_LEVEL_SWITCH": true,
101-
"SOIL_MOISTURE": true,
102-
"LIGHT_INTENSITY": true,
103-
"token": true,
104-
"BATTERY_VOLTAGE": true,
105-
"BATTERY_PERCENTAGE": true,
106-
"MOTION": true,
107-
"DEVICE_TEMPERATURE": true,
108-
"timestamp_sort": true,
109-
"ttl": true,
110-
"city": true,
111-
"cognito_id": true,
112-
"indoor": true,
113-
"zone_id": true,
114-
"SOIL_TEMPERATURE": true,
115-
}
116-
117-
for ki, vi := range v.Measurements {
118-
_, ok := toIgnore[ki]
119-
if !ok {
120-
measurementsData[ki] = append(measurementsData[ki], vi.(float64))
121-
}
122-
}
123-
}
124-
125-
fmt.Println("MEASUREMENTS DATA", measurementsData)
126-
127-
var dbData []map[string]interface{}
128-
err = res.Unmarshal(&dbData)
129-
if err != nil {
130-
fmt.Println("could not unmarshal data from the DB")
131-
return
132-
}
133-
134-
fmt.Println("RESULT COUNT :::: ", len(dbData))
135-
136-
// sort data by timestamp
137-
dbData = Qsort(dbData)
138-
139-
// make data distinc
140-
var distinctData []map[string]interface{}
141-
var keyList = make(map[string]bool)
142-
for _, v := range dbData {
143-
if _, ok := keyList[v["token"].(string)]; ok {
144-
continue
145-
} else {
146-
keyList[v["token"].(string)] = true
147-
distinctData = append(distinctData, v)
148-
}
149-
}
150-
151-
data := make(map[string][]float64, 0)
152-
for _, v := range distinctData {
153-
if v["indoor"] == true || v["indoor"] == "true" || v["city"] != city {
154-
continue
155-
}
156-
157-
// double check indoor for selected devices
158-
valid := validateDevice(v["token"].(string), city)
159-
if !valid {
160-
continue
161-
}
162-
98+
// count of devices included in the city avg.
16399
result.ActiveCount++
164100

101+
// sensors to be ignored for city avg.
165102
toIgnore := map[string]bool{
166103
"timestamp": true,
167104
"WATER_LEVEL_SWITCH": true,
@@ -181,18 +118,85 @@ func GetFrom(from int64, city string) (result vm.DeviceGetData) {
181118
"SOIL_TEMPERATURE": true,
182119
}
183120

184-
for ki, vi := range v {
121+
for ki, vi := range v.Measurements {
185122
_, ok := toIgnore[ki]
186123
if !ok {
187-
if ki == "AIR_CO2" {
188-
fmt.Println("CO2 READING", v["token"])
189-
}
190-
data[ki] = append(data[ki], vi.(float64))
124+
measurementsData[ki] = append(measurementsData[ki], vi.(float64))
191125
}
192126
}
193127
}
194128

195-
for k, v := range data {
129+
// fmt.Println("MEASUREMENTS DATA", measurementsData)
130+
131+
// var dbData []map[string]interface{}
132+
// err = res.Unmarshal(&dbData)
133+
// if err != nil {
134+
// fmt.Println("could not unmarshal data from the DB")
135+
// return
136+
// }
137+
138+
// fmt.Println("RESULT COUNT :::: ", len(dbData))
139+
140+
// // sort data by timestamp
141+
// dbData = Qsort(dbData)
142+
143+
// // make data distinc
144+
// var distinctData []map[string]interface{}
145+
// var keyList = make(map[string]bool)
146+
// for _, v := range dbData {
147+
// if _, ok := keyList[v["token"].(string)]; ok {
148+
// continue
149+
// } else {
150+
// keyList[v["token"].(string)] = true
151+
// distinctData = append(distinctData, v)
152+
// }
153+
// }
154+
155+
// data := make(map[string][]float64, 0)
156+
// for _, v := range distinctData {
157+
// if v["indoor"] == true || v["indoor"] == "true" || v["city"] != city {
158+
// continue
159+
// }
160+
161+
// // double check indoor for selected devices
162+
// valid := validateDevice(v["token"].(string), city)
163+
// if !valid {
164+
// continue
165+
// }
166+
167+
// result.ActiveCount++
168+
169+
// toIgnore := map[string]bool{
170+
// "timestamp": true,
171+
// "WATER_LEVEL_SWITCH": true,
172+
// "SOIL_MOISTURE": true,
173+
// "LIGHT_INTENSITY": true,
174+
// "token": true,
175+
// "BATTERY_VOLTAGE": true,
176+
// "BATTERY_PERCENTAGE": true,
177+
// "MOTION": true,
178+
// "DEVICE_TEMPERATURE": true,
179+
// "timestamp_sort": true,
180+
// "ttl": true,
181+
// "city": true,
182+
// "cognito_id": true,
183+
// "indoor": true,
184+
// "zone_id": true,
185+
// "SOIL_TEMPERATURE": true,
186+
// }
187+
188+
// for ki, vi := range v {
189+
// _, ok := toIgnore[ki]
190+
// if !ok {
191+
// if ki == "AIR_CO2" {
192+
// fmt.Println("CO2 READING", v["token"])
193+
// }
194+
// data[ki] = append(data[ki], vi.(float64))
195+
// }
196+
// }
197+
// }
198+
199+
for k, v := range measurementsData {
196200
if len(v) > 0 {
197201
var av float64
198202
for _, sv := range v {

0 commit comments

Comments
 (0)