Saturday, September 8, 2012

Installing Webmin on Ubuntu

Webmin is a perfect way to administer an Unix or Linux server. Some say it is too easy, and we, IT guys, should never stoop to it!

Hope nobody ever reads these words: I use Webmin a lot. If you are also lazy and of low moral standards, you may install it on Ubuntu using this simple instructions:


1. Download the latest webmin from http:www.webmin.com .
wget http://www.webmin.com/download/deb/webmin-current.deb

2. Execute webmin package. It will fail but it’s easier for us, since we’ll just force installing dependencies, rather than installing them one by one.

sudo dpkg -i webmin-current.deb

3. Install missing dependencies and it will automatically recompile webmin

sudo apt-get -f install

Enjoy webmin on https://[serverIP]:10000 .

Friday, August 31, 2012

LIKE on PostgreSQL - how to use an index


Using Indexes on LIKE queries on PostgreSQL might be tricky.

First, starting your search with '%' will always lead to full table scan.
OK, this is easy to understand and there is a recommended way to fix it - fulltextsearch.

Second, context-independent search ILIKE will also use table scan instead of index.

But if database is initialized with utf8 you will also get painfully slow table scan, until create an index, with an option, designed for this particular purpose: text_pattern_ops

Here is an example:
CREATE INDEX  ON MyTable (myField text_pattern_ops ASC NULLS LAST);

In non-C locales (which you're almost certainly using if you're using
UTF8) the ordering which the normal text operations use can be quite complex.
Just as an example most locales have spaces being entirely insignificant. 
So no range can reliably match a prefix LIKE pattern. The text_pattern_ops use
simple character-by-character ordering which are useful for LIKE but not for
regular < and > comparisons. They're just two different orderings.

Some sources also recommend indexes with  lower option:
CREATE INDEX  ON MyTable (lower(myField) text_pattern_ops ASC NULLS LAST);

For me, it did not work with postgresql 9.0. Just stay with regular text_pattern_ops.

Sunday, August 12, 2012

Customizing JSON services in Rails

In my previous sample I got this result from a JSON service:
[
{"created_at":"2012-08-12T13:56:42Z","id":1,"time":"2012-08-12T13:30:00Z","title":"Test Event \u21161","updated_at":"2012-08-12T13:57:40Z"},
{"created_at":"2012-08-12T13:57:07Z","id":2,"time":"2012-08-17T16:00:00Z","title":"Test Event \u21162","updated_at":"2012-08-12T13:57:07Z"},
{"created_at":"2012-08-12T13:57:29Z","id":3,"time":"2012-08-20T10:00:00Z","title":"Test Event \u21161","updated_at":"2012-08-12T13:57:29Z"}]

But what if I want to customize the result? Here is when function overriding comes handy.
Override as_json function in data model Event, like this:

class Event < ActiveRecord::Base
  attr_accessible :time, :title
 def as_json(options = {})
  result = {id: self.id, EventName: self.title, WillTakePlace: self.time }
 end
end

Viola! This is customised JSON data (as you see, I chose only three attributes and renamed two of them):

[
{"id":1,"EventName":"Test Event \u21161","WillTakePlace":"2012-08-12T13:30:00Z"},
{"id":2,"EventName":"Test Event \u21162","WillTakePlace":"2012-08-17T16:00:00Z"},
{"id":3,"EventName":"Test Event \u21161","WillTakePlace":"2012-08-20T10:00:00Z"}]

and what if I need something more complicated? Probably, I would call to RABL

ROR web services: XML and JSON

Ruby on Rails makes web services unfairly easy to build. It gives ROR programmers such an advantage - probably, Rails should be banned.

Here how we make a web service: (based on Simple web service API with Ruby on Rails)

Create a simple application:


rails new eventsservices
cd eventsservices
rails g scaffold event title:string time:datetime
rake db:migrate
rails s

Open http://localhost:3000/events/, click "New Event" and type in some data.



Interesting part is here: by default, ROR knows how to show data in html JSON:

  1.   # GET /events/1
  2.   # GET /events/1.json
  3.   def show
  4.     @event = Event.find(params[:id])
  5.     respond_to do |format|
  6.       format.html # show.html.erb
  7.       format.json { render json: @event }
  8.     end
  9.   end

Adding xml is easy:

Blogspot syntax highlighting issue

Blogspot as a service is missing features in so many ways, obviously bigG is not determined to give it a good and long life (Viva G+ instead!).

For me, I found this very serious issue: blogspot syntax highlighting.
Why on earth should I use a third-party service to publish a snippet of code? Certainly, I should have an opportunity to keep my code in the blog, highlight it as pleases me and do it without hassle!
Seems like wordpress.com can do it (but it has other disadvantages).

So far I found this service: http://quickhighlighter.com
Is it perfect? Not in any way! I.e, it has a problem with long lines (I would prefer scrolling).
Anyway, found nothing better so far. At least, I have HTML code that I can easily insert:

  1. def as_json(options = {})
  2.   # default
  3.   result = self.as_json
  4.   # minimal
  5.   result = {id: self.id, name: self.name } if options[:minimal]
  6.   result
  7. end
Seems like here a small market niche still exists - despite all those GitHubs and Pastebins, which are definitely overkill for such a simple task :0)

Friday, August 10, 2012

Just for practice

Just started new small project: data visualisation from http://www.geonames.org/ using Google Map API. Here it will be: http://www.raschet-rasstoyanie.ru/ (just bought a domain) Using new for me, unexplored technologies stack - RoR and postgreSQL. It makes the whole adventure exciting and worthful.

First post

Q: What's the similarity between a Crocodile and Windows? A: Neither of them has enough bytes!