Calibrating a MBP Battery

I recently noticed the battery on my 12+ month old MBP was only lasting about 2 hours.

A single charge on this model is supposed to last about 7 hours. I expected some decline after a year, but a drop off to 2 hours seemed off. In addition, unlike previous battery declines/issues the time seemed accurate. In other words, it was not reporting more time and then quickly draining.

A quick look around Google led me to the following Apple support document Calibrating your computer’s battery for best performance

Quick Steps:

  1. Completely charge your battery (and keep it charged for 2 hours).
  2. Unplug the power adapter and allow the battery to completely drain. You can use your computer during this time.
  3. Allow it to sit powerless for at least 5 hours.
  4. Recharge the battery.

I was skeptical, but after completing these steps, my full charge estimate went from about 2 hours to almost 5 hours.

Tip: Here is a great tool for MacBook users to monitor their battery’s health – Battery Health Monitor.

Update: It is now appears to be estimating the battery time at about 4 hours instead of 5. Not horrible, but not as good as I originally was seeing. I will report back of the decline continues.

Accessing a Restful Service with Ruby via NTLM

I have been looking for an easy to use library for accessing a restful service which sits behind NTLM via Ruby for sometime now and had been largely unsuccessful until now!

Last night after a bit more googling, I went back to twitter for help. Grant Carpenter put me on the right path with this:

I had the ntlm part working by patching rubyntlm (and then modifying savon). next stop: wsdl impedance http://bit.ly/982btc

The bitly link above points to a defunct project ruby-httpclient. However, it appears to have been reborn as httpclient with its source now hosted on gitub.

Getting a sample up and running could not be easier.

gem install httpclient
gem install rubyntlm

Note: If you are still doing sudo gem install and not using RVM you are really missing out.

require 'httpclient'

domain = 'http://mydomain.com'
user = 'username'
password = 'password'
client = HTTPClient.new
client.set_auth(domain,user,password)
puts client.get("#{domain}/my-path/service/something.json").content

I have only been using the httpclient library for an hour or so, but so far the it appears to be very robust with built in support for just about everything Http.

OData

Yesterday, I made the trip into NYC to added the OData Roadshow.

For those who have not looked into OData:

The Open Data Protocol (OData) is a Web protocol for querying and updating data that provides a way to unlock your data and free it from silos that exist in applications today. OData does this by applying and building upon Web technologies such as HTTP, Atom Publishing Protocol (AtomPub) and JSON to provide access to information from a variety of applications, services, and stores.

  • I personally do not feel it is truly RESTful, but I am willing to give up some of the tenants of REST in order to gain consistency.
  • While the protocol specification states it is read and write, I would be 99% of implementations will be read only. In fact, in the 5 hours of content covered by Microsoft, there were zero examples of updates.
  • If you are using a Linq to “Your Data Access” technology adding OData at a high level will be simple. If you are not using one, I expect this as quite a bit of work.

Overall, I really like the concept. Simple conventions for querying and representing data. IMO, this is the kind of stuff Microsoft should be doing more of (instead of silverlight/windows phone/etc). Couple with their data market (Dallas). I would expect to see much more data becoming available via OData.

On a side note, if the OData Roadshow is coming through your town, I would highly recommend checking it out.

Finally, for a walk through on setting up your own OData service, check out Hanselman’s OData For StackOverflow post.

Dynamics and MongoDB Revisited

A couple of months ago I wrote about wiring up C# 4.0 dynamics with MongoDB. At the time, I opened up the MongoDB-CSharp library and modified a bit of the code.

For obvious reasons this was not a good long term solution. In what appeared to be moment of clarity a couple of days ago, I decided to try to use extension methods and add dynamics on top of MongoDB-CSharp.

To the credit of the MongoDB-CSharp team, getting the basic functionality up and running was very simple. However, in the end it turns out that two compiler hacks (extension methods and dynamics) do not make a right. :)

Once in place, the code to use it looks something like this:

using (var m = new Mongo())
{
    var db = m["mongocsharp"];
    
    //ext method to get our "special" collection
    var col = db.GetDynoCollection("sample"); 
    m.Connect();

    //dynamic object to store our data
    dynamic newDoc = new DynoDocument();
    newDoc.title = "Dyno Collections";
    newDoc.slug = "dyno";

    col.Insert(newDoc);

    dynamic query = new DynoDocument(); 
    query.slug = "dyno"
    dynamic result = col.FindOne(query);
    Console.WriteLine(result.title);

    m.Disconnect();
}

While it works, it still feels too awkward to be useful IMO. Normally, this I would just delete something like this, but I am trying to let less code die on my machine, so I published it all as a GitHub gist: DynoMongo.

After working on this, I have come to the following conclusions:

  1. MongoDB-CSharp provides adequate flexibility on a schema if I need it in .NET. Mix in Automapper and a helper or two you are all set.
  2. Norm provides more structure if that is a requirementl
  3. Dynamics are really just a parlor trick. If I want the flexibility of a dynamic language, using C# is a mistake.
  4. I should probably try to do something similar in IronRuby, but with all the excellent MongoDB Ruby libraries out there, I am not sure it is even worth the thought.

Anyway, if someone feels this is interesting or useful, feel free to grab the gist, DynoMongo, and run with it.

SEO with Rack-Rewrite

I am still very much in the camp of write good (and consistent) content first and let SEO handle itself. However, that does not mean you should not keep an eye out for fundamental problems which can cause bad search engine results.

One of these problems I believe every developer of public web sites needs to be mindful of is duplicate content. Duplicate content causes quite a few problems:

  • You may appear to be a spammer in the “eyes” of a search engine
  • Links may appear to be directed at two or more urls
  • Your content may have to compete with itself

I have long been a fan of ISAPI_Rewrite for IIS to help manage and control some of these problems (which is in turn heavily influenced by mod_rewrite). However, since I have moved this site to Heroku, I needed to find another solution.

Thankfully, due to the awesomeness of Rack and middleware, I found a component called Rack-Rewrite and I was able to leverage it with just a couple of minutes effort.

A web server agnostic rack middleware for defining and applying rewrite rules. In many cases you can get away with Rack::Rewrite instead of writing Apache mod_rewrite rules.

I am already using a customized Rack application, Rack-Jekyll, to power this site, so plugging in Rack-Rewrite was just as simple as adding a couple of lines to my rackup file.

Here are the full contents of my config.ru

require "rack/jekyll"
require "rack-rewrite"

ENV['RACK_ENV'] ||= 'development'
ENV['SITE_URL'] ||= 'scottw.com'

use Rack::Rewrite do

    r301 %r{.*}, "http://#{ENV['SITE_URL']}$&", :if => Proc.new {|rack_env|
        ENV['RACK_ENV'] == 'production' && rack_env['SERVER_NAME'] != ENV['SITE_URL']
      }    

    r301 %r{^(.+)/$}, '$1'
  end

run Rack::Jekyll.new

The two rules I am running on this site ensure that only scottw.com (no www.) is used and that no links end in a “/”. The first is particularly important since Heroku issues you a custom url as well.

What is really interesting about Rack-Rewrite is the ability to execute code as part of your rewrites. This enables a lot of flexibility (such as ignoring some rewrites when running in development mode).

Want to see more? Check out the archive or browse by tags.