This Ruby script reads a log file with website requests data and displays statistics about visits and unique visits to the different endpoints.
The scripts expects each line in the log file to be of the format:
route ip
where route
is a endpoint path and ip
is the endpoint's requester ip address.
A generic error message is displayed in the case of issues finding, accessing or parsing the file.
The implementation is based in 3 different components:
-
A simple Ruby script located in /bin folder that check for basic error conditions and delegate job to the next 2 Ruby classes:
-
The
Analizer
class that locates the log filename and store visit statistics in 2 data structures during the parsing process. -
The
TextFormatter
presenter class that decorates an analizer instance and provides methods to print listings of endpoint visits in plain text.
git clone [email protected]:ltello/visits.git
cd visits
If Ruby runtime is not installed in your machine, please install it first. Some popular ruby version managers might be helpful: rbenv, rvm
You will need Bundler gem to install Ruby dependencies. In the root directory of the cloned script check it is installed and accessible typing:
bundle -v
Install it if you need to:
gem install bundler
Once installed, the next command will install of Ruby dependencies for the script to run properly
bundle install
./bin/visits.rb path_to_log_filename
Depending on the contents of your log filename it will display something similar to:
Page Views:
/about/2 90 visits
/contact 89 visits
/index 82 visits
/about 81 visits
/help_page/1 80 visits
/home 78 visits
-------------------------
Unique Page Views:
/contact 23 visits
/help_page/1 23 visits
/home 23 visits
/index 23 visits
/about/2 22 visits
/about 21 visits```
In the root folder where you cloned the application, type:
rspec