Say I have a hash with a list of delivery drivers (for the classic flower shop scenario). Each driver has a rating and an event signal URL (ESL). I want to raise an event only to the top three drivers in that list, sorted by ranking.
With a relational database, I'd run a query like this:
SELECT esl FROM driver ORDER BY ranking LIMIT 3;
Is there a way to do this in KRL? There are two requirements:
foreach
iteratesThe second could be solved like this:
rule reset_counter {
select when rfq delivery_ready
noop();
always {
clear ent:loop_counter;
raise explicit event loop_drivers;
}
}
rule loop_on_drivers {
select when explicit loop_drivers
foreach app:drivers setting (driver)
pre {
esl = driver.pick("$.esl");
}
if (ent:loop_counter < 3) then {
// Signal the driver's ESL
}
always {
ent:loop_counter += 1 from 0;
}
}
But that's kind of kludgy. Is there a more KRL-ish way to do it? And how should I solve the ordering problem?
EDIT: Here's the format of the app:drivers
array, to make the question easier to answer:
[
{
"id": "1",
"rating": "5",
"esl": "http://example.com/esl"
},
{
"id": "2",
"rating": "3",
"esl": "http://example.com/esl2"
}
]
Without knowing the form of the hash, it's impossible to give you a specific answer, but you can use the sort operator to sort and then use the pick operator or hash
Something like
driver_data.sort(function(){...}).pick("$..something[:2]")
"something" is the name from the hash of the relevant field.