Doing an “ORDER BY … LIMIT …” style query on a hash in KRL

StackOverflow https://stackoverflow.com/questions/9875840

  •  26-05-2021
  •  | 
  •  

質問

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:

  1. A way to sort the hash
  2. A way to limit the number of times a foreach iterates

The 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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top