-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck-claim.js
275 lines (210 loc) · 7.08 KB
/
check-claim.js
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
function generateSlidingWindowArray(till, size) {
const arr = []
for (let i = 0; i <= till - size + 1; i++) {
const arrX = []
for (let j = i; j < i + size; j++) {
arrX.push(j)
}
arr.push(arrX)
}
return arr
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms))
}
function generateDatesBetween(startTimestamp, endTimestamp) {
const dates = []
const startDate = new Date(startTimestamp) // Convert to milliseconds
const endDate = new Date(endTimestamp) // Convert to milliseconds
// Loop through each day
for (let date = startDate; date <= endDate; date.setDate(date.getDate() + 1)) {
const year = date.getFullYear()
const month = (date.getMonth() + 1).toString().padStart(2, "0")
const day = date.getDate().toString().padStart(2, "0")
const formattedDate = `${year}-${month}-${day}`
dates.push(formattedDate)
}
return dates
}
function filterPastDates(dates) {
const currentDate = new Date() // Get the current date
// Filter out past dates
const filteredDates = dates.filter((date) => {
const [year, month, day] = date.split("-").map(Number)
const dateToCompare = new Date(year, month - 1, day) // Months are 0-based in JavaScript
return dateToCompare < currentDate
})
return filteredDates
}
function calculateAverageNumberProperties(objects) {
const propertySums = {}
const propertyCounts = {}
objects.forEach((obj) => {
Object.entries(obj).forEach(([key, value]) => {
if (typeof value === "number") {
if (!propertySums[key]) {
propertySums[key] = 0
propertyCounts[key] = 0
}
propertySums[key] += value
propertyCounts[key]++
}
})
})
const averages = {}
for (const key in propertySums) {
if (propertySums.hasOwnProperty(key)) {
averages[key] = propertySums[key] / propertyCounts[key]
}
}
return averages
}
function findMaxValues(objects) {
const maxValues = {}
objects.forEach((obj) => {
Object.entries(obj).forEach(([key, value]) => {
if (typeof value === "number") {
if (!maxValues[key] || value > maxValues[key]) {
maxValues[key] = value
}
}
})
})
return maxValues
}
function linearMap(value, mapFrom, mapTo, clamp = true) {
const slope = (mapTo.to - mapTo.from) / (mapFrom.to - mapFrom.from)
const ans = slope * (value - mapFrom.from) + mapTo.from
return clamp ? clampValue(ans, { min: mapTo.from, max: mapTo.from }) : ans
}
async function isClaimValid(land, _options) {
const options = { ..._options }
options.checkIntervals = options.checkIntervals || 5
const geolocationResponse = await Functions.makeHttpRequest({
url: `http://api.positionstack.com/v1/reverse?access_key=${secrets.POSITIONSTACK_API_KEY}&query=${land.location.latitude},${land.location.longitude}`,
})
if (geolocationResponse.error) {
console.log("Geo Location Error")
return -1
}
const geolocation = geolocationResponse.data
const location = geolocation.data[0].locality || geolocation.data[0].region
const dates = filterPastDates(generateDatesBetween(land.insuredFrom, land.insuredTo))
const weatherData = []
for (const date of dates.slice(0, 3)) {
await sleep(100)
const weatherDataResponse = await Functions.makeHttpRequest({
url: `http://api.weatherapi.com/v1/history.json?key=${secrets.WEATHER_API_KEY}&q=${location}&dt=${date}`,
})
if (weatherDataResponse.error) {
console.log("Weather API Error")
return -1
}
const data = weatherDataResponse.data
data.forecast.forecastday.forEach((element) => {
weatherData.push(element.hour)
})
}
const idealCropResponse = await Functions.makeHttpRequest({
url: "https://api.npoint.io/8fb36c3096dbc24926a7",
})
if (idealCropResponse.error) {
console.log("Ideal Crop API Error")
return
}
const ideal = idealCropResponse.data
const cropBest = ideal.filter(
(item) => item.crop.toLowerCase().replace(/ /g, "") === land.cropName.toLowerCase().replace(/ /g, "")
)[0]
const windowSlides = generateSlidingWindowArray(
weatherData.length - 1,
Math.min(options.checkIntervals, Math.min(1, weatherData.length - 3))
)
let worstCondition = {}
for (const window of windowSlides) {
const avgToArr = []
for (const i of window) {
avgToArr.push(weatherData[i])
}
let arrY = []
for (const i of avgToArr) {
arrY.push(calculateAverageNumberProperties(i))
}
const diffArr = []
for (const d of arrY) {
diffArr.push(prefixKeysWithAvg(d), cropBest) //(calculateAbsoluteDifferenceObject(d, cropBest) as Condition);
}
worstCondition = findMaxValues(diffArr)
}
let prob = 0
prob += clampValue(linearMap(worstCondition.avghumidity, { from: 0, to: 100 }, { from: 0, to: 15 }), {})
prob += clampValue(linearMap(worstCondition.avgtemp_c, { from: 0, to: 50 }, { from: 0, to: 10 }), { max: 10 })
prob += clampValue(linearMap(worstCondition.maxtemp_c, { from: 0, to: 50 }, { from: 0, to: 15 }), { max: 15 })
prob += clampValue(linearMap(worstCondition.daily_chance_of_rain, { from: 30, to: 100 }, { from: 0, to: 10 }), {
max: 10,
})
prob += clampValue(linearMap(worstCondition.maxwind_kph, { from: 0, to: 30 }, { from: 0, to: 15 }), { max: 15 })
prob += clampValue(linearMap(worstCondition.totalsnow_cm, { from: 0, to: 50 }, { from: 0, to: 15 }), { max: 15 })
prob += clampValue(linearMap(worstCondition.totalprecip_in, { from: 0, to: 30 }, { from: 0, to: 15 }), { max: 15 })
return clampValue(linearMap(prob, { from: 0, to: 90 }, { from: 0, to: 100 }), { min: 0, max: 99 })
}
function prefixKeysWithAvg(obj) {
const newObj = {}
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
const newKey = "avg" + key
newObj[newKey] = obj[key]
}
}
return newObj
}
function calculateAverageObject(objects) {
if (objects.length === 0) {
return null
}
const keys = Object.keys(objects[0])
const result = {}
for (const key of keys) {
const values = objects.map((obj) => obj[key]).filter((value) => typeof value === "number")
if (values.length > 0) {
const sum = values.reduce((acc, value) => acc + value, 0)
result[key] = sum / values.length
}
}
return result
}
function calculateAbsoluteDifferenceObject(obj1, obj2) {
const result = {}
for (let key in obj1) {
if (typeof obj1[key] === "number" && typeof obj2[key] === "number") {
result[key] = Math.abs(obj1[key] - obj2[key])
}
}
return result
}
function clampValue(value, options) {
let ans = value
if (options.min && ans < options.min) {
ans = options.min
}
if (options.max && ans > options.max) {
ans = options.max
}
return ans
}
const latitude = parseInt(args[0]) / 10 ** 6
const longitude = parseInt(args[1]) / 10 ** 6
const cropName = args[2]
const insuredFrom = parseInt(args[3]) * 1000
const insuredTo = parseInt(args[4]) * 1000
const land = {
location: {
latitude,
longitude,
},
cropName,
insuredFrom,
insuredTo,
}
const prob = await isClaimValid(land, { checkIntervals: 5 })
return Functions.encodeUint256(Number(prob > 70))