I've got a query that collects a bunch of statistics from the database and I want to display several graphs generated from that same data on one page. (I'm using Gruff for the graphs.) I've only come up with two basic ways to handle this: 1) Do the queries and the graph generation all in one controller action, save the graphs to files and use plain old IMG tags to get them all on the page. I'd rather not do this because I don't want a mess of temp files to deal with. 2) Use separate controller actions for each graph, each of them using send_data to output the graphs, and the view calls the actions in IMG tags: <img src="<%= url_for(:action => 'some_graph') %>" />. With this method the query has to be repeated for each graph, which is a big waste. Any ideas on how to accomplish this without running the query several times and without saving graphs to files? The best thing I can think of right now is kind of a combination and variation on both these ideas: in the initial action the data is collected, the graphs are generated and stored temporarily as blobs in the database, the ids of the blobs are passed to the view where they can be used to call a graph retrieval action like <img src="<%= url_for(:action => 'get_graph', :id => 45) %>" />. The get_graph action can delete the records as soon as send_data is called so the images aren't hanging around after it's done. Maybe an in-memory SQLite db would be well suited for this task. How does that idea sound? Any suggestions? Thanks.
on 2007-01-23 00:13
on 2007-01-23 00:24
> I've got a query that collects a bunch of statistics from the database > and I want to display several graphs generated from that same data on > one page. (I'm using Gruff for the graphs.) I've only come up with two > basic ways to handle this: > > 1) Do the queries and the graph generation all in one controller action, > save the graphs to files and use plain old IMG tags to get them all on > the page. I'd rather not do this because I don't want a mess of temp > files to deal with. Why would it be a mess? As long as there is something to key the graph report on (date range, id, username, etc.) you should be able to write all the files into a single directory which would make it easy to cleanup the next time that graph was called with those parameters... or if it was called soon enough (and depending on your business logic) just use the same files again... This way also has the advantage that if the reports took awhile to generate you could easily tweak it so users submit requests, get a "thanks we're working on it" and then have a background process do it, and email when they are ready or some such... -philip