If you had read my last post, I wrote that I was going to do some katas about WattDepot. WattDepot is web-server that collect energy data from various places. It is a ongoing project that try to reduce the waste of energy and increase awareness of proper management of the electricity. The data can be retrieved as a XML format and parsed into some form of data that can be analyze and manipulated for research purposes.
I have completed all of the katas, but I really had a hard time doing some of those. I had to do a quick scan through some APIs such as Calendar, XMLGregorianCalendar, Tstamp, Date, and the WattDepot. Below is a summary from each kata I did.
Kata 1: SourceListing
Implement a class called SourceListing, whose main() method accepts one argument (the URL of the WattDepot server) and which writes out the URL argument and a list of all sources defined on that server and their descriptions, sorted in alphabetical order by source name. Use the System.out.format method to provide a nicely formatted list.
- Simplest kata out of the 6. I just did a loop through all the sources from the server. I got from each one their name and description.
- Time taken : 15 minutes.
Kata 2: SourceLatency
Implement a class called SourceLatency, whose main() method accepts one argument (the URL of the WattDepot server) and which writes out the URL argument and a list of all sources defined on that server and the number of seconds since data was received for that source, sorted in ascending order by this latency value. If no data has every been received for that source, indicate that. Use the System.out.format method to provide a nicely formatted list.
- Things got complicated when I had to retrieve data from each source. For this kata I had to compute the latency of each server for which I need to get the properties from each source by retrieving the sensordata. After I had figure it out how to get the latency, I then had to sort it out in order by the latency. I went back to the Collection API, and I decided to use the SortedMap. SortedMap will automatically sort the keys by their "natural ordering". However SortedMap allows duplicates so I had to do some checking for that. I just create a condition to check if the latency to be stored is already set in the collection. If it is, then I just retrieve the value ( a list of the sources) and just add the new source to the list.
- Time taken: 1 hour.
Kata 3: SourceHierarchy
Implement a class called SourceHierarchy, whose main() method accepts one argument (the URL of the WattDepot server) and which writes out the URL argument and a hierarchical list of all sources defined on that server. The hierarchy represents the source and subsource relationship between sources.
- Well, for this kata I think I overestimated it. I thought I will just have to write a recursive method to check all the sources. But wait, what if the next one to be checked is a sub-source of the first one? Then I just delete that one I continue with the rest of list. But what if that sub-source has also other sub-sources?. After hitting my head many times on my desk, I decided to do it a dumb way. I checked that the sources in the WattDepot server I was testing, there is only two level of hierarchy. So what I did is to get all the parent sources first, and then check each of them for their sub-sources. It should work so far until two level of hierarchy. If a sub-source has other sub-sources, my code will not work properly.
- Time taken: > 3 hours.
Kata 4: EnergyYesterday
Implement a class called EnergyYesterday, whose main() method accepts one argument (the URL of the WattDepot server) and which writes out the URL argument and a list of all sources defined on that server and the amount of energy in watt-hours consumed by that source during the previous day, sorted in ascending order by watt-hours of consumption. If no energy has every been consumed by that source, indicate zero. Use the System.out.format method to provide a nicely formatted list.
- This kata was as bad as the last one. Not of the difficulty of coding it, but I had to look through a bunch of API to get yesterday's date. I spent hours and hours looking through the Calendar API, the Date API, and XMLGregorianCalendar API. Without success in getting the date, I got frustrated and I haven't worked on the katas for the whole last weekend. Sunday at night, I resumed my research about dates API, and came across the Tstamp API. Wow, Tstamp was all I need (and some methods from the DateFormat and Calendar API) to get what I was looking for. After I got the two times (one from yesterday's date at 00:00:00 and the other at 23:59:59) I just used the EnergyConsumed method from WattDepot and compute the energy consumed. After that I used again the TreeMap to sort the list according to the energy.
- Time taken: > 3 hours.
Kata 5: HighestRecordedPowerYesterday
Implement a class called HighestRecordedPowerYesterday, whose main() method accepts one argument (the URL of the WattDepot server) and which writes out the URL argument and a list of all sources defined on that server and the highest recorded power associated with that source during the previous day, sorted in ascending order by watts. Also indicate the time when that power value was observed. If no power data is associated with that source, indicate that. Use the System.out.format method to provide a nicely formatted list.
- If you had done the previous, then this shouldn't be that bad. The problem is with the non-virtual and virtual sources. Non-virtual sources have all the energy data, but virtual sources does not. What I did (as recomended by Prof. Philip Johnson) is to increase the interval of the time (in XMLGregorianCalendar) by 60 minutes. This will get us 24 energy data for each sources. Overall there are more than 1000 data retrieved, so it takes around 4-5 minutes for my code to query all the data. I just used the Tstamp again (Thanks!) to increase the time. Then I will just create another method to get the time at which the highest energy recorded occurred for each source. This method I had to do a little research also, because I had to convert the current XMLGregorianCalendar format to a Date format. Again I used the another SortedMap with key as the energy to sorted the list, and the value as a TreeMap that stores both the source name and the time.
- Time taken: 45 minutes.
Kata 6: MondayAverageEnergy
Implement a class called MondayAverageEnergy, whose main() method accepts one argument (the URL of the WattDepot server) and which writes out the URL argument and a list of all sources defined on that server and the average energy consumed by that source during the previous two Mondays, sorted in ascending order by watt-hours. Use the System.out.format method to provide a nicely formatted list.
- After going through all the hard work of reading a bunch of times API, this one was pretty easy. After I got the two previous Monday, which is basically almost the same as the last two previous katas, but changing some lines of code, I just computed average of both. Then I stored in the computed average (as the key), and the source name (as the value) in a SortedMap.
- Time taken: 30 minutes.
Overall, after doing this katas I think I just tasted the bitterness and joy of doing programming. I got so annoyed when I couldn't figure out the solution, but I was jumping around when it worked. I just got also some more experiences on reading APIs, which in the past I barely looked at them. But now I understand that actually looking at every API, it will not only help you a lot, but also will make you write faster coding.