-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4040 from airqo-platform/hf-list-cohorts
addressing a runtime error for listing cohorts
- Loading branch information
Showing
2 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
const axios = require("axios"); | ||
const isEmpty = require("is-empty"); | ||
|
||
const url = "http://localhost:3000/api/v2/devices/sites"; | ||
const config = { | ||
headers: { | ||
Authorization: "", | ||
}, | ||
}; | ||
|
||
const NETWORK_MAPPINGS = { | ||
// iqair: "permian-health", | ||
// usembassy: "us-embassy", | ||
// urbanbetter: "urban-better", | ||
// kcca: "kcca", | ||
// airqo: "airqo", | ||
// Add more mappings as needed | ||
}; | ||
|
||
const DEFAULT_GROUP = "unknown"; | ||
|
||
axios | ||
.get(url, config) | ||
.then(async (response) => { | ||
const groups = response.data.sites | ||
.map((site) => { | ||
// Look up the group based on network, with a fallback to a default | ||
const group = NETWORK_MAPPINGS[site.network] || DEFAULT_GROUP; | ||
|
||
// Optionally log devices with unknown networks | ||
if (group === DEFAULT_GROUP) { | ||
console.log( | ||
`Unrecognized network for device ${site.name}: ${site.network}` | ||
); | ||
} | ||
|
||
return group; | ||
}) | ||
// Remove any 'unknown' groups if you want only mapped networks | ||
.filter((group) => group !== DEFAULT_GROUP); | ||
|
||
console.log("the data:"); | ||
console.dir({ groups }); | ||
|
||
// Process devices in batches | ||
for (let i = 0; i < response.data.sites.length; i += 10) { | ||
const batch = response.data.sites.slice(i, i + 10); | ||
|
||
for (const site of batch) { | ||
const group = NETWORK_MAPPINGS[site.network] || DEFAULT_GROUP; | ||
|
||
if (group !== DEFAULT_GROUP) { | ||
const url = `http://localhost:3000/api/v2/devices/sites?id=${site._id}`; | ||
const data = { groups: [group] }; | ||
// console.log("the data:"); | ||
// console.dir(data); | ||
|
||
try { | ||
// Uncomment if you want to make the PUT request | ||
const putResponse = await axios.put(url, data, config); | ||
console.log("PUT response:", putResponse.data); | ||
} catch (error) { | ||
console.error("PUT error:", error.message); | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
.catch((error) => { | ||
console.error("GET error:", error); | ||
}); |