SKINNY JEANS FAST LOG PARSING WITH RUBY & SQLITE FOR HIPSTERS

EXAMPLE

  • your log file has lines that look like

0.0.0.0 - - [01/Oct/2010:00:00:00 -0700] "GET /posts/my-first-post HTTP/1.1" 200 1337 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" "-"
0.0.0.0 - - [01/Oct/2010:00:00:01 -0700] "GET /posts/my-first-post HTTP/1.1" 200 1337 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" "-"
0.0.0.0 - - [01/Oct/2010:00:00:03 -0700] "GET /posts/my-first-post HTTP/1.1" 200 1337 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" "-"
0.0.0.0 - - [02/Oct/2010:00:00:03 -0700] "GET /posts/my-first-post HTTP/1.1" 200 1337 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" "-"
0.0.0.0 - - [02/Oct/2010:00:00:04 -0700] "GET /posts/my-first-post HTTP/1.1" 200 1337 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/?q=some+stuff)" "-"
0.0.0.0 - - [02/Oct/2010:00:00:05 -0700] "GET /posts/my-first-post HTTP/1.1" 200 1337 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://search.yahoo.com/?p=some+stuff)" "-"
  • then you get 2 SQL rows in the pageviews table that look like:

2010-10-01, my-first-post, 3
2010-10-02, my-first-post, 1
  • and 1 SQL row in the pageview_keywords table that look like: (search referrals ONLY, but… WITH keyword tracking)

2010-10-02, "my-first-post", 2, "some stuff"
  • as long as you have the time zone in the log file, the timestamp will be converted and persisted as utc

WHY?

  • so you can query a database by date and path and get pageviews and have that data stored CHEAP

  • so you can query a database by date and path and SEARCH KEYWORD

  • because i couldn’t find anything simpler and Google Analytics is limited to 10,000 API requests per day

USAGE

sj = SkinnyJeans::LogParser::execute(logfile_path = "access.log", sqlite_skinny_jeans = "sqlite_skinny_jeans.db", path_regexp = /\s\/posts\/(.*)\sHTTP/, date_regexp = /\[(\d.*\d)\]/)
sj.pageview.where("date = '2010-10-01' and path = 'my-first-post'")
=> #<SkinnyJeans::Pageview id: 1, date: "2010-10-01", path: "my-first-post", pageview_count: 3>
  1. NOTE: for now **you may have to monkey patch the SkinnyJeans#parse_string_as_date**

  2. Parse oldest logs first, then run regularly against your main log, let logrotate handle the rest (skinny_jeans remembers where it left off)

  3. ASSUMES reading log files in ascending order, keeps track of last line read so you could put it on a scheduler or cron job

  4. access the 2 activerecord classes, sj.pageview (returns Pageview class), and sj.update happily within your own app

  5. enjoy the skinny jeans

PERFORMANCE

  • it parses 300,000 lines in < 9 seconds and

  • persists 6,000 requests (in 2 different tables, one of the tables is for search engine referrals) with 2 compound indexes in 18 seconds

NOTES

  • supports gzipped files

  • creates a temp copy of the log file before parsing

  • currently requires each line to be unique

  • line uniqueness is potential problem if a single client manages to hit the same page more than once in less than 1 second AND it’s the last line of a log file

LICENSE

(The MIT License)

Copyright © 2010 Jonathan Otto

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.