Archive for February, 2010

Electricity Tariff Increase Calculator

Posted on the February 24th, 2010 under South Africa by

The National Energy Regulator of South Africa on Wednesday granted state-owned utility Eskom a 24,8% tariff increase for the 2010/11 financial year, falling short of the power firm’s request for a 35% hike. For the following two financial years, Eskom was granted increases of 25,8% and 25,9% respectively.

How much does that amount to for you? Try the calculator below:

Read the story on Mail and Guardian

Road Closures for opening of Parliament

Posted on the February 8th, 2010 under Internet,South Africa by

Here’s a Google Map of the road closures for the opening of South African Parliament


View Opening of Parliament 2010 in a larger map

Would it not have been easier for them to stick to DeWaal Drive, Roeland Street?

MySQL – getting two items per source in a single query

Posted on the February 4th, 2010 under Software and Technology by

Assume you have the following table called stories with the following data. And you have run the following query:

SELECT * FROM stories ORDER BY storydate DESC
id source title storydate
10 cnn Story 10 2010-01-21
9 bbcnews Story 9 2010-01-20
8 bbcnews Story 8 2010-01-19
7 skynews Story 7 2010-01-18
6 cnn Story 6 2010-01-17
5 bbcnews Story 5 2010-01-16
4 cnn Story 4 2010-01-15
3 skynews Story 3 2010-01-14
2 skynews Story 2 2010-01-13
1 cnn Story 1 2010-01-12

Now for the challenge. What happens if you are required to get the top two stories from each source? One option is to first get the order of the sources, and then loop through each one of them:

SELECT DISTINCT source FROM stories ORDER BY storydate DESC

<loop “source” as “sourceid”>

      SELECT * FROM stories WHERE source = "{sourceid}" ORDER BY storydate DESC LIMIT 2

</loop>

Another option is to do the in MySQL itself! Unsure if there is a better way of doing this, but here’s mine using UNION and Sub Selects:

(SELECT * FROM stories WHERE source =
(SELECT DISTINCT source FROM stories ORDER BY storydate DESC LIMIT 0,1) ORDER BY storydate DESC LIMIT 2)
UNION
(SELECT * FROM stories WHERE source =
(SELECT DISTINCT source FROM stories ORDER BY storydate DESC LIMIT 1,1) ORDER BY storydate DESC LIMIT 2)
UNION
(SELECT * FROM stories WHERE source =
(SELECT DISTINCT source FROM stories ORDER BY storydate DESC LIMIT 2,1) ORDER BY storydate DESC LIMIT 2)

Explanation:

The bold section are subselects and form the WHERE clause of the query. Here  we want the first, second and third as per source, but only one at a time. Once we have two results per item, we use UNION to join them together.

One thing to note though. Even though it returns the latest two stories per source, the final table is not sorted by storydate! This can be done using PHP sorting techniques. Is there a way of doing this one time in MySQL?

Getting started with Wireshark, the Fiddler for Ubuntu

Posted on the February 1st, 2010 under Internet,Linux,Software and Technology by

Fiddler is a useful tool for tracking http requests and responses. It’s similar to the Net tag in Firebug, except that it can be used to track all requests, not only browser-based ones.

For Ubuntu users, the recommended alternative is Wireshark. Wireshark is noted as being more powerful than Fiddler, but the focus will be on the features most commonly used by web developers for tracking.

Installation

Wireshark is found in the Ubuntu repositories, so simply search for wireshark in Synaptic, or enter sudo apt-get install wireshark

Usage

Under Applications > Internet, you will notice there are two options. Wireshark and Wireshark (as root). Run the second one (as root) as this gives you more data to track.

wireshark-1

After wireshark has started, the process is straightforward:

  1. Choose which network interfaces to capture traffic from
  2. Start capturing
  3. Filter captured requests

1. Choosing which network interfaces to capture traffic from. From the menu, choose: Capture > Interfaces

wireshark-2

This will bring up a list of network devices.

2. Since my work involves debugging work off my laptop (http://localhost etc), I only start device lo (127.0.0.1). This is useful as it immediately ignores all other traffic.

3. The one thing you will notice is that fiddler will log both the TCP and HTTP requests. For the purpose of debugging AJAX, etc., we are only interested in HTTP requests, TCP requests are not required. These can be hidden by adding a filter.

wireshark-3

In the field next to Filter:, enter http and click on Apply.

wireshark-4

The steps will now show all traffic coming on 127.0.0.1 via HTTP. The last step is picking up the AJAX parts for debugging. Notice that for each request, there is a response. Unlike Fiddler, Wireshark does not combine the request and the response. So click on the response line, and then expand the Line-based text data row. This will show the text of the AJAX response.

wireshark-5

Conclusion

Wireshark takes more steps than Fiddler, and Fiddler is a more focussed program than Wireshark. Wireshark also captures traffic without having to install a plugin into firefox, etc., so it’s useful for debugging applications as well. The biggest improvement to make Wireshark truly replace Fiddler is to turn off truncation of the response text. Wish I knew how!