Ext JS 4.0 providing "unconventional" data to charts - javascript

I have a store which always contains a single record. Lets say the record looks like this:
{'good': 5, 'bad': 2, 'neutral': 3}
How would I render that as a Pie chart? Ext.js normally uses each record in a store as one point of the series. In my case, I have only one record and want it to be used as three points in the series.

You have a couple options available to you. You can create a custom Ext.data.reader.Reader subclass that will convert your raw data into your formatted records, you can override the store's loadRawData method to convert JSON data into an Ext.data.ResultSet, or you can set a callback on the store's load method to get your record and manually convert it into your necessary record format. If you're going for reusability, I suggest making a Reader.
The Reader#readRecords method takes raw data and returns a ResultSet, which sounds exactly like what you're after. Take a look at the source from the Sencha docs website and you should be able to figure out where to go from there. Once you have your custom reader, pass it to your store's proxy during configuration and it should handle the work for you. If it doesn't automatically, you may need to call the store loadRawData, passing your JSON response.

Split it into three records. You can create your own instances of Record and fill them with data using your original record as source.

Related

How to do full-text search in a MySQL table using Fuse.js and Redis?

I have a table with a thousand records in it and I want to do a google like search full-text/fuzzy search.
I read about MySQL v8's Full-Text search and let's say we don't have that functionality yet.
There is this JavaScript library called Fuse.js that do fuzzy-search which is what I need.
I can combine it by creating a API that returns the table data in JSON format and then pass it to Fuse.js to do a fuzzy-search.
Now, I think it's not recommended to load all data from table every time someone wants to search.
I read about Redis, and the first thing that came in my mind is to save all table data in Redis using JSON.stringify and just call it every time instead of querying the database. Then whenever a data is added in the table, I will also update the contents of the data in Redis.
Is there a better way to do this?
That is a very common caching pattern.
If you need a more efficient way to store and retrieve your JSON to/from Redis you might want to consider one of the available Redis Modules.
e.g.
RedisJSON allows you to efficiently store, retrieve, project (jsonpath) and update in place.
RediSearch allows you to have full text search over Redis Hash and efficiently retrieve data according to the user's query.
Last
RedisJSON2 (aka RedisDoc) combines both modules above, meaning efficient JSON store and retrieve with Full Text support

Get fixed number of JSON objects from third-party API

I'm working with this returned API from a third party:
(note: returns LARGE dataset)
https://www.saferproducts.gov/RestWebServices/Recall?format=json
I know I can get this giant object like so:
$.getJSON('https://www.saferproducts.gov/RestWebServices/Recall?format=json', function(json, textStatus) {
console.log(json);
});
This returns ~7000 objects. There is no way in the API call to dictate how many objects you want returned. It's all or nothing.
Here's the question...can I use getJSON (or similar) to only get the first 5 objects and stop without having to load the entire JSON file first?
I did something similar a while a go. I used PHP to fetch a webpage of an api. Then I would cache it. Via PHP logic, I stored a variable inside a text file which contained all the information from the webpage. I had another file that stored the timestamp. Then, when the page was called, php would check the timestamp to see how old it was. If it was too old, it'd recache the page and return relevant information. If it was still valid, it would just return the cached information. If you only want the last 5, the PHP logic wouldn't be too hard to write that in. Then, jQuery would query the PHP page.
They don't have anything called out in their documentation for limiting the returns. I think their intent is for you to narrow down your search so you're not fetching every single item. You could always email them and ask as what Mike McCaughan said, if they don't have a 'limit' baked in, then no, it's not possible.
It also looks like they offer weekly downloads that you can just create your own API and add a limit property:
https://www.fda.gov/%20Safety/Recalls/EnforcementReports/default.htm
Reference:
https://github.com/presidential-innovation-fellows/recalls-api/wiki/data-sources
https://www.cpsc.gov/Recalls/CPSC-Recalls-Application-Program-Interface-API-Information/
https://www.cpsc.gov/Global/info/Recall/CPSC-Recalls-Retrieval-Web-Services-Programmers-Guide_3-25-2015.pdf
If there really is no option for limiting that call, then I'd suggest caching, showing some kind of processing wheel while the call takes place or narrowing your query. They have options to for filtering that may work for you such as the following:
RecallNumber
RecallDateStart
RecallDateEnd
LastPublishDateStart
LastPublishDateEnd
RecallURL
RecallTitle
ConsumerContact
RecallDescription
ProductName
ProductDescription
ProductModel
ProductType
RecallInconjunctionCountry
ImageURL
Injury
ManufacturerCountry
UPC – see caveat below
Hazard
Manufacturer
Remedy
Retailer

Alfresco custom UI controls - Associations

I'm trying to build a custom UI control in alfresco to display the associations of an object type that I have.
Basically I have two object types; Code, which is a key value pair, and CodeScheme which contains multiple child associations to codes, it's essentially a mirror of a map structure I have in a different system.
The problem I have is that the codes are automatically generated, so they get the UID names, whereas really I'd like to present them as 'key=value', 'key=value', etc (ideally I'd like to present it as a table).
I've already created a custom control and added it to share-config-custom, and confirmed that the configuration is working correctly. What I'm not really clear on now is:
a) How to attach a javascript to the control so that I can process the association data.
b) How to get hold of the codes in javascript, and read their properties.
I'm just looking for a push in the right direction.
Thanks :)
One idea would be to use a form filter. Your form filter could iterate over the child references, fetch each child node, grab the data you want to display and then add one or more new properties with that data.
Then, your form control is hooked to the fields your form filter dynamically added to the form data. It can then read and display the data as needed.
Without a form filter I think you'd have to use JavaScript to parse the child association refs and use AJAX calls to fetch each child's node data, then format that as needed. The form filter idea would be less traffic from the browser.

Data Structure for holding parsed csv Bus route data

I have a csv file of about 10k rows x 25 columns. The csv contains information of bus routes, stops, etc. I have a select box with all the routes and the user will be able to pick a single route to show on the map, and then they will be able to click on individual stops (another select box) to get a closer look on the map. I am wanting to know what will be the best way to parse and structure to store this information and be able to perform fast queries (database?), and how should I store the result of the query (array, json object, dictionary, data Table?). I won't need all columns every time, so I will pick the useful columns to make the query a little faster.
Each time a user will select a different route, I will make a query to get all the stops and other relevant information and loop through the data to display it on the map (maybe store results of last 5 queries?). What will be the best way to store this result? Showing the specific stop information won't be too big of a deal since it will be a smaller subset of the already queried results.
Let me know if you need any additional information that will assist with the answers.
Google releasd a public scheme called gtfs which is a transport data structure. You would ideally use a graph data structure. I have found neo4j a good option.
See this github project for an example of how to use a graph database for this purpose:
https://github.com/tguless/neo4j-gtfs

Transforming api json responses for NodeJS

I'm ending up having to do hacks to convert 'true' to just true and it's creating code smell.
Is there a library like https://github.com/thephpleague/fractal that allows me to transform my response into the types I need?
In cases like this it's almost always better to fix the API to return data in a usable format rather than trying to post-process the result on the client.
In your case there are several routes you could take:
Store the list as a JSON string directly in the database.
This means you don't have to do any processing on the server and can just return it 'as is'. However you lose the ability to do queries on the data directly and need to resort to things like LIKE and string operations.
Store the data relationally, and process it on the server to turn it into JSON
Here you retain the ability to do queries on your data, but you may need to do several queries to get all the data you need and then connect it on the server. (eg. you would do one SELECT on the user table to get a user, and then you would need to do another SELECT on the friends table where the userid matches your first user. You would then need to merge these results to create your JSON.) This is usually the best way to do it.
You can also turn the result into JSON directly inside the database engine using a user defined function. For example using https://github.com/mysqludf/lib_mysqludf_json#readme
This is somewhat similar to 2, but it ties your stored procs to the JSON format.

Categories