Saturday, October 31, 2009

Bypass the ON UPDATE trigger in mysql

Recently at work, I came across a problem where I had to bypass the ON UPDATE trigger for a particular table. I dint want this to happen as I was making manual updates on the backend and dint want the change to get reflected on the UI. To prevent this, you could add the column in the UPDATE clause (even though it doesnt have to change). Something like
   CREATE TABLE  test (
         id integer not null auto_increment,
         name varchar(20) not null,
         upd_date datetime default current_timestamp on update current_timestamp
   );

   ................
If I just do a
      UPDATE test set name = 'raja' where id = 2;
that would update the upd_date column as well. IF I want to avoid the upd_date column from getting updated, you could do a
      UPDATE test set name = 'raja', upd_date = upd_date where id = 2;

Sunday, October 18, 2009

Adding code snippets to Blogger

As mentioned earlier, I was using Wordpress for my blogging attempts, but since I never went past 2 or 3 posts in total, its not something worth mentioning. But what wordpress had was easy support for code snippets.

Since this is going to be a technical blog, I needed ways to display code properly and in Wordpress, it was pretty simple using the "sourcecode" custom tag.

The equivalent of this is SyntaxHighlighter. View this post for a description as to how to add code snippets for blogger.

Add Search Engine - Firefox and IE7

Artifacts (Issues, comments, discussions) drive most of what we do at my day job. During an iteration meeting today, I found that most of us know the artifact id for getting to a page but the route to get to that page is atleast 2 clicks away (2 clicks before you get to the page to enter artifact id and one more to get to the actual page). I was wondering if it would be possible to use the "search engine" helpers that are available in Firefox 2 and IE 7. That would just make us enter the artifact id in the "Search engine" box and it takes us directly to that page (ofcourse after you login once to the application).

Luckily the OpenSearch Specification helps us here. Just Create an xml that adheres to the Opensearch specification and loading that page in both Firefox(2 and above) and IE(Version 7 and above) adds those to the list of search engines managed by your browser. There is a good tutorial at mozilla developer center. Ill cover the basics here. If you are interested, I can even make an application that lets you fill in the details and will post the xml file that you can use for adding your own search engine. Please add a comment if you are interested.

Without further ado, here is the minimal template for the xml file
  
  
      Some Short Name that appears in the Search Engines box
      Long Description
      
  
Call this file, say, plugin.xml in your web root directory. For a simple Apache server install, place this in /var/www (or whatever your DocumentRoot is). An example of the template modified for Google Search Engine is shown below

      My Google
      Google search implemented as firefox addon
      

Save that as google-plugin.xml. The thing to note above is the {searchTerms} in the URL Tag above. This will be replaced with whatever is entered in the searchbox. So if you entered "test", the url will be http://www.google.co.in/search?hl=en&q=test and will take you to that page.Using this as a template, you could modify any REST-ful GET URLs that will retrieve your page. Heck, it would be nice if all webapps have this behaviour as we are all used to entering things in the Search URLs rather than a few clicks.

Deploying this in Browsers:


Firefox (Only 2 and above):
  • Create a HTML File with this tag in the HEAD section of the file
        <link rel="search" type="application/opensearchdescription+xml" title="Some identifiable text" href="/plugin.xml" />
  • In the above example, replace the title with what you want to see in the Search Engine box and href with path to your Opensearch XML File. In my case, its in the DocumentRoot, so the /plugin.xml.
  • Load the html file in your system.
  • Once the page is loaded, you should see a "Add <title>" in the Search engines list.
  • Click on the Button and you should have that plugin added right into your browser.
  • Make that your default section by going to "Manage Search Engines" or just select it to enter text in there which will take you to google
  • If you need to delete this,go to "Manage search engine" and Remove it

Internet Explorer 7:
  • Load the plugin.html directly in your browser
  • In the Search List, click on the button that says "Add Search Providers" and click on the one that you just added
  • Once its done, the plugin should be added in your search list
  • Make that your default or select it to enter search terms
  • As in Firefox, to delete it, go to "Manage engines" and delete it

Note: I wasnt able to get this working on a Firefox 1.0 browser with the above steps. But it looks like the file is located in your system at <Home Directory>/.mozilla/firefox/<encrypted profile dir>/searchplugins directory. I need to check if just copying the plugin.xml into this directory and a subsequent restart will have the same effect. I will confirm this.There are other interesting things you can do with this. You can add an image to the searchbar by adding a "Image" tag with the contents of the image base64 encoded. Also for Mozilla based browsers, there is a Suggestion URL that can be used to retrieve the list of possible suggestions as you start typing. This should be a cool one to do and I will post a tutorial for that.

nxclient setup tricks

NoMachine is a uber cool alternative to VNC or any of the remote access solutions. Whats more, its very secure as it runs on top of ssh and doesnt need any special ports to be managed. You can use your regular ssh access to get into your box and its pretty fast too.
However, there are specific issues when installing the nxserver and nxclient. Ive attempted to mention a few of the problems and possible steps for getting an error free install and happy computing :)
1. Download the nxserver, nxclient and nxnode from here (Assuming the server is on Linux). Install the rpms or deb packages on the Server.
2. Download the nxclient and install it on your client machine.
3. Generate a public-private key combination on the client machine.
raja@binarytech:~$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/raja/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/raja/.ssh/id_rsa.
Your public key has been saved in /home/raja/.ssh/id_rsa.pub.
The key fingerprint is:
ce:ed:01:c9:f5:60:a9:4d:03:1e:19:a2:b7:8e:32:c0 raja@binarytech.in
After this, you would have id_rsa.pub (containing the public key) and id_rsa (containing the private key).
4. Now we need to check how we can use NXClient to connect to the server. Copy the public key (the id_rsa.pub from earlier step) into ~nx/.ssh/authorized_keys2 on the server machine. The typical directory for ~nx would be /usr/NX/home/nx.
5. Try and connect to the server from the client. This should succeed without asking for passwords
6. Login into the server as the user you want to connect. In this case, I want to login as "raja"
7. Enter your password.
8. You can run any command once you are in the shell. Play with it before you quit out of the shell.
9. Now we are good with connecting to the server as "nx" from the shell. Try creating a client configuration using the "NX Client for Linux".
10. Mostly this should work and connect you to the server. In cases it doesnt and throw messages like "Authentication User: nx
Using auth method: public key
Authentication failed"
Chances are that the key generated as part of the server may be stale. Go to the server machine and run a keygen
-bash-3.1$ sudo /usr/NX/bin/nxserver --keygen
NX> 704 Starting: server-keygen operation at: Tue Oct 07 12:36:25 2008.
NX> 704 Generating new ssh-keys. Please wait.
NX> 704 Keys generated correctly. Backing up files.
NX> 704 Back up of keys made. Updating files.
NX> 704 Keys updated. NX clients should now use key:
NX> 704 /usr/NX/share/keys/default.id_dsa.key
NX> 704 to get connected to this NX server.
11. Copy the key from /usr/NX/share/keys/default.id_dsa.key into your NX Client wizard and try connecting. This should work perfectly.
Happy NX Machine!!
If you have issues with connecting, drop me a post and I can try to fix it.

Saturday, October 17, 2009

Time Tracking System in Ruby on Unfuddle

Last year, when I wanted to create a timetracking system for my team, I thought of picking one from the Opensource world but couldnt find one that will fit my needs. I could have customized one of the existing ones, but they were either commercial or too primitive. I took that as an excuse to do an app in Rails and called it YATSA (Yet Another TimeSheet Application). It has the following concepts
  • People
  • Department
  • Activities
People belong to a Department and there could be many activities in a department. When someone logs in to enter time, they are presented with the departments to which they have access and on selecting a department, the activities associated with that department are chosen. The person can then enter start/end times and a description. You could then run reports for yourself or your team (if you are an Department Admin) and export the reports to Excel as well.

The Features document is available here. The project itself is uploaded to Unfuddle. Unfuddle does not have public access yet, but if you need the code, let me know, I can help create an account on the system for you to access it (The free version only supports 2 users, and Im one of them, Im planning to use the other version as the "public" user). There have been requests from a commercial firm asking to add Projects to this list so we can associate People to Projects.  I will be adding those into this system. I will also add instructions on how to set this up, so if anyone is interested, they could use this. There is no license for it right now, but Ill set it up under Apache License 2.0.

Sunday, October 11, 2009

HMAC SHA Signatures using Python for Amazon webservices

Ive recently been working on the Amazon Product Advertising APIs to get some information from Amazon and display it to my users in a different format.

Since Django is such a wonderful framework, I decided to do this using app using Django. The Amazon Product Advertising documentation has examples for Java, C# and PHP, but not much help for Python. There isnt anything unique to Python as the calls are all either REST based or SOAP based, but there was one part that was tricky -- The HMAC SHA Signatures required for REST requests.

For starters, HMAC is abbreviation for keyed-Hash Message Authentication Code. For more information about HMAC, look up the RFC or the wikipedia entry. There is enough information in the documentation about how to make a REST request but none whatsoever about creating the HMAC based signature.

So, after some debugging and browsing through the forums, here is a snippet that will show how to create a HMAC-SHA based signature that you can use with AWS.

In the below code, its assumed that you followed this doc upto Step 7 since there is nothing thats specific to any programming language.

import hmac
import haslib
import base64
import urllib
.....
AWS_SECRET_KEY=
.....
request = """
Build request as shown in the above link till Step 7
"""
h = hmac.new(AWS_SECRET_KEY, request, hashlib.sha256)
signature = base64.b64encode(h.digest())
request = "%s&Signature=%s" %(request,urllib.quote(sig))
...
rest_output = urllib.urlopen('https://ecs.amazonaws.com/onca/xml?%s' %(request)).read()
...



The line for urllib.quote is required because Amazon wants us to quote the output that is gotten from HMAC based signature (Step 9 in the above document).

Hope it helps in doing a Python based HMAC-SHA signature generation for AWS.

Monday, October 5, 2009

Pound Sign with Accented A in PHP -- Solved

I had this weird problem in one of the PDFs that was getting generated through ezPDF. The problem was that the price in that page was appearing as a pound character with an accented A before it. My code looked like
$pound_sign = '£';
.....
$pdf->ezText($pound_sign.$somevalue);

On most of the pages, this appeared properly as a pound sign followed by value but in some pages, it wasnt. After lot of debugging and googling around, the problem seems to be one of encoding. If there is a pound sign and its interpreted as a UTF-8 character, it is represented by a 2-byte sequence
   0xC2 0xA3

0xC2 is the accented A character and 0xA3 is the one for the pound sign, but if its interpreted as a ISO-8859-1 (Latin) encoding, it is represented as
   0xA3

which is what we want.

So, to solve the problem, something like

   $pound_sign = html_entity_decode("&#xA3;", ENT_COMPAT, 'ISO-8859-1');

worked and the accented A disappeared. This is because Im forcing the encoding to be ISO-8859-1 and the character passed to it in 0xA3.

Hope this helps someone

Sunday, October 4, 2009

Widget For Tracking Blogspot Visitors

Ive been googling for some Blogger Visitor widgets and most of them seem to redirect you to specific sites for getting information after you add a bit of code in the templates. Services like comstat seem to do that.

Having used Wordpress before, I like their idea of seeing each post and how many users visited those pages from the admin pages. That's more intuitive and easy for the user. Infact, the earlier post I made about Monitoring Traffic for Blogger Hosted sites requires that you login to Google Analytics to view that.

Im thinking of writing a plugin that would gather information from Google Analytics (And they have an api) and show it as a widget in the Admin Panel. Im doing this just to learn a bit of how to write widgets for Blogger as well as make it easier for bloggers to view traffic info.

Is there something like this available already ?

Saturday, October 3, 2009

Monitoring Traffic for Blogger Hosted Sites

I started using Blogger yesterday and from earlier experiences, I wanted to know how to monitor traffic for these types of hosted sites. It gives an indication as to what content is being looked upon more often (and its also a booster to post more if you have more people reading ur blog :-))

Blogger doesnt have a way to monitor traffic on its own unlike Wordpress, where the Dashboard will tell you on a post-by-post basis as to the number of people who visited that particular page. Instead we could do this through Google Analytics. Here are the steps

  • Login using your Gmail account to Google Analytics 
  • In the Dashboard section, create a new profile. This screen allows you to enter the domain name of the site where you want traffic monitored. Enter your blog address (xxx.blogspot.com) and give a suitable name for it
  • The following page will give you a piece of javascript that you will have to copy-paste and add it to your Blogger HTML template. Within Blogger, go to your Layout page and onto the "Edit HTML" link. In the "Edit Template" textbox, go to the bottom of the page and just before the ending body tag, add this piece of javascript obtained from Google Analytics
  • Thats it. Finish creating the setup on Google Analytics.
  • When you visit the Dashboard for Google Analytics immediately, the "status" column will give you an indication as to whether you entered the javascript correctly or not. This doesnt happen immediately and might take upto 24 hours according to Google. In my case, it happened in about an hour or so. If the "Status" column indicates a warning symbol, then there could be something wrong and this page should say what went wrong. There is also a video available from that page that shows how to add the tracking code
  • Wait for 24 hours and you should see data coming into Google Analytics.

Friday, October 2, 2009

Bug in GoogleStore page

I just created this new blog so I could try and post regularly, Lots of failed attempts because of my laziness, but anyway, the first post (as it usually happens) isnt a dummy post. Its a bug report, yay!!

When accessing the "gear" link from Blogger profile pages, I get a error page from Google Store, nothing to worry there, it happens here and there, but whats surprising is this block of code thats spit on the page.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="err.aspx.cs" Inherits="Ecometry.Web.Impl.err" %>




Would anyone know where to file a bug report for this?