In my previous post I discussed making graphs, or rather redrawing existing graphs using a new library. Since I was only redrawing the graphs, I only discussed the final step for making a graph: feeding the data to the graph library. I have also been making new graphs from scratch, however, which is a bit more complicated. I’ll first describe how a graphing tool works in crash-stats:
1. First some parameters are chosen (either by the user, or by whoever set the default parameters), such as dates of crashes, products that crashed, versions of those products, etc.
What the user sees during step 1.
2. These parameters are encoded in a URL and sent off to the server. Since we are using Django, the URL also specifies which Django view to send our parameters to.
What the user sees during steps 2 – 4.
3. The view function specified by the URL is called. The view does some processing of the parameters and sends them to the middleware API. The view then returns the data it gets back from the middleware as a JSON object.
5. Now we are ready to do everything in my previous post: hand the data over to Metrics Graphics and receive back a beautifully drawn graph.
What the user sees after step 5.
So, code-wise, making a new graphing tool involves writing: a form so the user can choose parameters, an AJAX request to be performed when the form is submitted, a new Django view (which will (a) process the parameters, (b) talk to the middleware and (c) return the data), and some functionality to format the data for the graph, draw the graph, and add it to the DOM.
A final note about testing Django views, in particular mocking. The idea of the test is that you check that your view does everything it is supposed to: that it processes the parameters and sends them in the correct form to the middleware, and that it processes the data that it gets back from the middleware correctly. What you don’t want to do here is test the middleware, so you define a function that mocks the call to the middleware. Inside this function you can check that your parameters have been passed through correctly, and you can also ask it to return some data in place of what the middleware would have returned, and then check that the view processes it correctly.