Hi there,
I’m currently working with a setup where:
- Each customer has 1+ “sites” (i.e. locations, facilities, etc.)
- Each site has 1+ devices
The sites are implemented as user groups that are nested underneath the the customer. I’d like to be able to display the number of devices per site for a given customer. I’ve thought of a few ways of doing this and would like to know which is the idiomatic way using Losant.
1) AJAX calls with JS
For this one I had loaded a customer and then used the ID to fetch all of the devices. I could then loop over them and build up an object that mapped a location to the number of devices (which could be determined by tags). While this is straight forward, I don’t like having to use JS to then manipulate the DOM
2) Manually associate devices with sites
What I like about this solution is that it’s pretty simple. The devices are loaded when I fetch a user, so I can then template them in use Handlebars. What I don’t like is that I have to manually associate devices with subgroups – this won’t scale, and doesn’t make use of tags (which is the recommended way in Losant)
3) Build an Object in a Workflow
The idea here is:
- Create an object
- Given a particular user:
- For each subgroup:
- Create a key with the subgroup name, and then a value which is the number of devices for said subgroup (i.e. site)
I found this to be really hard to do using the Workflow system, and gave up part way through. That may be me not knowing which nodes to reach for, though. If this is feasible, then it seems it’d be the right option as I could just use it as an Experience Page’s workflow.
Thanks in advance! I’m sure there are other possible solutions that I’ve missed.
The way I would approach this is to first ensure that your devices are associated with groups by device tags - as in, a tag on the device with the key “groupId” and the value is the ID of which experience group (site, customer, etc.) the device is associated with. This scales very well and makes these kinds of queries much easier.
This would then allow you to return device counts per site by building a map in a workflow. Something like the following …
- Endpoint Trigger to kick off the flow. The trigger’s payload tells us who the user is making the request and also provides an array of
experienceGroups
the user is directly assigned to.
- Group: Get Node that uses an advanced query to return all groups whose
parentId
is the ID of the group the user is assigned to.
- Device: Get Node that uses an advanced query to return all devices by
experienceUserId
or experienceGroupId
.
Now you have all of the user’s child groups and devices in hand, and if the devices are associated with groups (sites) by the groupId
tag, you can iterate over the devices and count how many are associated with each unique group ID.
1 Like
Hey Dylan,
Sorry to resurrect this thread as when I originally looked at it I wasn’t currently iterating on this problem. I agree with the steps you have – that’s actually exactly what I’m doing. But when you say:
Now you have all of the user’s child groups and devices in hand, and if the devices are associated with groups (sites) by the groupId
tag, you can iterate over the devices and count how many are associated with each unique group ID
Do you mean to do this in JS on the front-end? Or is there another node combination I could use (like a Loop
node combined with some other nodes)? Thanks, and sorry
UPDATE: I ended up just going with the JS solution after setting up a JSON API endpoint with the nodes you suggested. I’ll mark that as the solution again
1 Like
Thanks for the info. I found this helpful.
1 Like