Ember data retrieve multiple instances of the same model in one route - ember.js

I currently have an ember app that's trying to retrieve data from a 3rd party API using Ember Data. The urls are in the format, /user_id/date/food, which would retrieve the user's consumed food on the given day. I want to retrieve the list of foods the user consumed given a date range (2015-06-07, 2015-08-10).
I tried to use Ember.query and filter out the unnecessary data, but the API doesn't have an endpoint which would return all of the consumed foods.
Currently I'm supporting the single day query using queryRecord and passing the day in.

If /user_id/date/food is the only endpoint the 3rd party API is providing, the only option you would have is to make a request for every day in the range you want to get. You might want to use Ember.RSVP.all to wait till all requests are retrieved and finally concatenate the result for example.
But this is not really recommended, because a large range would be very inefficient.

Related

I have a boto3 lambda function which uses table.scan to scan the dynamoDB. I want to send limited data to frontend to show 10 entries, cant use query

I want someone to help me with how to implement pagination in boto3 without query, NextToken. I want the dynamoDB data to be sent to the frontend but 10 entries at a time and every time send the next 10 entries to the frontend.
First I thought I could use the query but the primary key would filter most of the data and I want all the results, just 10 entries at a time.
Also on the frontend, it has to be like page1(10 entries), page2(next 10 ) , like that .
There's no direct approach from what I'm getting your question.
Above mentioned approach by Mark B is good but not for your case you won't be able to move to any page other than next or go to previous page directly with this approach.
You can implement a not so cost efficient approach by adding a field to db of page number and using a lambda function to maintain pagination in db
To implement pagination with a DynamoDB scan operation you have to save the LastEvaluatedKey value that was returned from the first scan, and pass that as the ExclusiveStartKey on the next scan.
On the first scan operation (page == 0) you would simply pass Limit=10 in the query parameters.
On the second scan operation (page == 1) you would pass Limit=10, ExclusiveStartKey=<lastScanResult.LastEvaluatedKey>.
Note that you will have to pass the LastEvaluatedKey value to the frontend as part of your result data, the frontend will have to store that in memory, and send it as part of the request data when it requests the next page.

Configurable methods in Postman request

This request is working fine
https://localhost:3000/api/catalog/1435
Being catalog the method and 1435 the catalogId to get.
I cannot transform the server (ASP controller) to work with
https://localhost:3000/api/catalog/?1435
that is, 1435 become a Query param.
How can I make the last method configurable in a Postman request?
The original seems perfectly fine to be honest:
https://localhost:3000/api/catalog/1435
Now, if you want to query the catalog, and thus use a query string, you would likely want to do it as:
https://localhost:3000/api/catalog?id=1435
This is what a client could do to GET a catalog that matches id 1435. In my mind, this makes more sense if you can query a catalog by multiple parameters. Because from a data model point of view if the catalog resource key is the id, then I would use what you had originally.

Real Time Google Analytics API - Identify user session

I'm retreiving event data using Real Time Google Analytics API, so as to trigger responses each time conditions are met - while the user navigates.
This is my actual query on Google Analytics Real Time API (which works perfectly!)
return service.data().realtime().get(
ids='ga:' + profile_id,
metrics='rt:totalEvents',
dimensions='rt:eventAction,rt:eventLabel,rt:eventCategory',
max_results='25').execute()
I'd like to show results grouped by each particular session or user. So as to trigger a message to this particular user if some conditions are met.
Is that possible? And if so, how do apply this criteria to this query?
"Trigger a message to a particular user" would imply that you either have personally identifiable data stored in GA, which would violate Googles TOS, or that you map an anonymous ID (clientid or UserID or similar) to a key stored in an external database (which might be legally murky, depending on your legislation). Since I don't want to throw away the answer I have written before reading your question to the end :-) I am going to assume the latter.
So, is that possible? No, not really. By default GA does not identify neither an identifier for the user (client id or user id) nor for the session (a session identifier is present only in the BigQuery export schema).
The realtime API has a very limited set of dimensions (mostly I think because data aggregation does not happen in realtime), so you can't even use custom dimensions. Your only chance would be to overwrite one of the standard fields, i.e. campaign information.
Of course this destroys the original data in the field. So you should use an extra view for the API query, send a custom dimension with the user identifier along, and then use an advanced filter to copy the custom dimension value to a standard field (while you original data is safe in your other data views). This is a bit hackish, though.
Also the realtime API only displays the current hit per user, so you cannot group by user in the query in any case - you'd need to download and store the data to an external database and do your aggregation there.

Preventing Ember Data Caching & loading model data on demand

We are considering moving from Backbone to Ember. There are a few issues through I can't get answers to from the docs.
1) Ember-Data caches it's data. Our application is multi-user so other users need to be able to see new records created by everyone. Is there a way around this? I read on another post that when a query string is passed, ember data does not cache data, is this true? If it is, can I then just always send query string and nothing will be cached?
2) Ember data has a single model in the router that appears to be instantiated at route load time. I can see that your can request data from multiple sources by returning an object with many this.store.find calls. Say I have a select element and when you select an option, another select gets populated with items based on the first select (which requires a call back to the server). How would that work, how can I get model data on demand (not at route load time)?
I'm not sure if it answers your question but you can always call
model.reload()
to refetch data from server so you can work with up to date data.
You may want to consider Faye (http://faye.jcoglan.com/), which would let you have a pub/sub setup that could update your store by listening to topics of interest. This uses WebSocket for the streaming interface. You could then put new objects into the store, remove or update existing objects which the server could publish to the client.

Best Practices to update multiple records with a single server request

I have a User model which hasMany phones. The UI for the user allows to add/delete/update phones on the single form.
When user submits the form all changes to the phone list are sent to the server with a single request.
I have extended the App.UserSerializer with custom serializeHasMany to include all the phone details in the single request.
The real problem is to sync the store state after the request is complete.
Basically I need to solve these two problems:
Remove deleted records from the store. I could not find any methods which just removes a record from a store.
Update new records with the ids generated by server. (Or just remove the new records from the store and hasMany array since response creates the dups for the added records)
Is there any best practices or work arounds for this kind of scenarios?
Thank you.
I think the best practice for now is just sticking to regular REST. In your case this will mean a few extra requests (really though, how many phones can a user have?), but it will spare you a lot of effort in handling things manually.
Ember may support bulk updates in the future (https://github.com/emberjs/data/blob/master/TRANSITION.md, "We plan to support batch saving with a single HTTP request through a dedicated API in the future.")