How do you select the most recently created object within an object set in Workshop?

I’m working on a personal project using Foundry/AIP, where a user can upload an image of their meal and then get a summary of the nutrients in that meal. In the workshop, I’m trying to figure out how I can make it so that the summary is generated for the most recent meal that was added to the object set. I use AIP Logic to generate this summary, and I pass in a “meal” object along with additional information. However, I don’t know how to specify which meal object is passed into this function. Is there a way to accomplish what I want to do?

I have included screenshots of my Workshop application for additional clarity.


Hi @jaysen_quan is there a specific reason why you are passing a multi-selected object set into your function - and not a single selection?
How would the user flow be in your imaginary app? I could imagine this in a way that users select a meal from a object list or table. If yes, then you could enfore the sorting of the table/list to be desc. by creation/updated date and enable auto-selection. This way the single selected object would always be the latest (until the user selects a different). Your meals object set would be the input to the object table/list.

Hi @Phil-M, when I added the “Meal” object type into Workshop and created a variable for that object type, I couldn’t find a good way to filter out a specific meal from the entire object set.

The user flow would be something like this:

The user uploads an image of a meal to a media uploader. That image gets processed in a data pipeline, where the food items are extracted from the image, and then a new Meal object gets created. Once this object is created, I am hoping to pass it into an AIP Logic function, where I generate a “meal summary” that presents the user with estimated nutrition facts about their meal. (This is just a Markdown block under the “Generate Description” button that displays the output string from the AIP logic function)

Here is a screenshot for clarity of what the UI in Workshop looks like right now. There’s probably an easier / cleaner way to do this, but I’m relatively new to Foundry so not quite familiar with the ins-and-outs of the platform yet.

Hey,

If you were using the standard Button group widget, there’s a configuration option at the very bottom that allows you to bind the newly created object against an Object set variable that you could then use as an input to your AIP Logic function.

Given that you’re using the Media Uploader widget instead, that is not available. Does your object type have a Timestamp property to the effect of “created on” or “uploaded on”? If so, you can write a TypeScript function that always pulls the latest object when you sort in descending order, convert the object set to an object list, and access the first element (0-index). You’d publish this function, and use it to back an Object set variable in your workshop module (and let it recompute automatically).

Hey @joshOntologize, my object type does have a Timestamp property, and I think by using it, I was able to come up with a workable way to handle my desired functionality.

The only downside of this approach is that a user would need to wait a few minutes before uploading their next image (right now I have it set at the past hour, but I might change it to a few minutes). But, I think this is a good enough workaround for my purposes. Let me know what you think.

Hey @jaysen_quan,

That’s a solid workaround! If this approach is acceptable for your use case, you should be good to go. I’ve pasted below a code snippet that you could use with a TypeScript function instead (where you create a new Object Set variable in your Workshop module, and back it by the published function with the below code).

import { Function } from "@foundry/functions-api";
import { Objects, YourObjectType } from "@foundry/ontology-api";

export class MyFunctions {

    @Function()
    public returnLatestObject(): YourObjectType {
        // This returns an Object list/array
        const latestObjectList = Objects.search().yourObjectType()
            .orderBy(obj => obj.nameOfTimestampProperty.desc())
            .take(1);

        // This array contains exactly one element/object, return that exact object
        return latestObjectList[0];
    }

}

Regarding the following:

The latency you’re observing here is a byproduct of the workflow design and the way Foundry works in general. Based on something you mentioned in a previous post in this thread (see below), I’m assuming you’re storing the user-uploaded image in a Media Set you’ve created, and you have a schedule configured to run every time it detects an update/new file appended to that media set. This would then build the dataset that ultimately backs your Meal object type (please correct me if I’m wrong!).

The latency is the sum of:

  1. time taken for the schedule to detect the update (new image uploaded),
  2. time taken for the dataset to actually build (you can optimize this by making this process incrementally),
  3. time taken for the Ontology to detect the new version of a backing dataset, and finally
  4. time taken for the changelog, indexing, and hydrating steps into your Object Type in OSv2