sp_log_parser 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b997ede7f1b2e48a507221cd26b4e40e2d97e795dcf88f506c50120cc54e7f01
4
+ data.tar.gz: 261287d351523587ffe68f07d5487322c8fa9e999412679a3ec9c464a68a6bcc
5
+ SHA512:
6
+ metadata.gz: 0e6577baa1328d2ccb9a083c768d617ef2c0b40c8353b42b900c4254de1e0eb238ca8990dc848cf3c893a9b8931cf4f6b7b0025bd435b0b6fef21104182a983f
7
+ data.tar.gz: fe02182186e706b561a84208b3c0b6d98ec3984b1701dc65a3e5592413b5475d9316442d76529520dfbb888c14818f703ea362be7705da2fd444b6512fd2b3ac
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.1
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem "rake", "~> 12.0"
6
+ gem "minitest", "~> 5.0"
@@ -0,0 +1,21 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ sp_log_parser (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ minitest (5.14.2)
10
+ rake (12.3.3)
11
+
12
+ PLATFORMS
13
+ ruby
14
+
15
+ DEPENDENCIES
16
+ minitest (~> 5.0)
17
+ rake (~> 12.0)
18
+ sp_log_parser!
19
+
20
+ BUNDLED WITH
21
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Mario Zugaj
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,60 @@
1
+ # LogParser
2
+
3
+ Simple log parser for you webserver logs.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'sp_log_parser'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install sp_log_parser
20
+
21
+ ## Usage
22
+
23
+ Execute the following to parse your log file and display visit statistics:
24
+
25
+ ```shell
26
+ $ bundle exec exe/parser $PATH_TO_YOUR_LOG_FILE
27
+ ```
28
+
29
+ You will get output similar to this:
30
+ ```
31
+ --------Views--------------
32
+ /help_page/1 => 5 views
33
+ /home => 2 views
34
+ /contact => 2 views
35
+ /about => 1 view
36
+ /index => 1 view
37
+ /about/2 => 1 view
38
+ -----Unique Views----------
39
+ /help_page/1 => 5 unique views
40
+ /home => 2 unique views
41
+ /about => 1 unique view
42
+ /index => 1 unique view
43
+ /about/2 => 1 unique view
44
+ /contact => 1 unique view
45
+ ```
46
+
47
+ ## Development
48
+
49
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
50
+
51
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
52
+
53
+ ## Contributing
54
+
55
+ Bug reports and pull requests are welcome on GitLab at https://gitlab.com/mariozugaj/sp-log-parser.
56
+
57
+
58
+ ## License
59
+
60
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "sp_log_parser"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'sp_log_parser'
4
+ raw_logs = File.readlines(ARGV[0])
5
+ visits = Parser.parse(raw_logs)
6
+ stats = Stats.new(visits)
7
+ puts "--------Views--------------"
8
+ StatsDisplay.display(stats.views)
9
+ puts "-----Unique Views----------"
10
+ StatsDisplay.display(stats.unique_views, unique: true)
@@ -0,0 +1,10 @@
1
+ require "sp_log_parser/version"
2
+ require "sp_log_parser/visits"
3
+ require "sp_log_parser/visit"
4
+ require "sp_log_parser/stats"
5
+ require "sp_log_parser/stats_display"
6
+ require "sp_log_parser/parser"
7
+
8
+ module SPLogParser
9
+
10
+ end
@@ -0,0 +1,11 @@
1
+ class Parser
2
+ def self.parse(logs)
3
+ return if logs.empty?
4
+
5
+ visits = Visits.new
6
+ logs.inject(visits) do |visits_bucket, log_entry|
7
+ visits_bucket << Visit.new(log_entry.strip)
8
+ end
9
+ visits
10
+ end
11
+ end
@@ -0,0 +1,31 @@
1
+ class Stats
2
+ Stat = Struct.new(:webpage, :views)
3
+
4
+ attr_reader :visits
5
+
6
+ def initialize(visits)
7
+ @visits = visits
8
+ end
9
+
10
+ def views
11
+ visits.unique_webpages.map do |webpage|
12
+ Stat.new(webpage, views_for(webpage).size)
13
+ end
14
+ end
15
+
16
+ def unique_views
17
+ visits.unique_webpages.map do |webpage|
18
+ Stat.new(webpage, unique_views_for(webpage).size)
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def views_for(webpage)
25
+ visits.for(webpage)
26
+ end
27
+
28
+ def unique_views_for(webpage)
29
+ views_for(webpage).uniq { |visit| visit.ip_address }
30
+ end
31
+ end
@@ -0,0 +1,21 @@
1
+ class StatsDisplay
2
+ class << self
3
+ def display(stats, unique: false)
4
+ sorted_stats(stats).each do |stat|
5
+ puts "#{stat.webpage} => #{pluralize(stat.views, unique)}"
6
+ end
7
+ end
8
+
9
+ private
10
+
11
+ def sorted_stats(stats)
12
+ stats.sort_by(&:views).reverse
13
+ end
14
+
15
+ def pluralize(count, unique)
16
+ word = count == 1 ? "view" : "views"
17
+
18
+ [(count || 0), ("unique" if unique), word].compact.join(" ")
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module SPLogParser
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,25 @@
1
+ class Visit
2
+ attr_reader :log_entry
3
+
4
+ def initialize(log_entry)
5
+ @log_entry = log_entry
6
+ end
7
+
8
+ def ==(other)
9
+ log_entry == other.log_entry
10
+ end
11
+
12
+ def webpage
13
+ parsed_log[0]
14
+ end
15
+
16
+ def ip_address
17
+ parsed_log[1]
18
+ end
19
+
20
+ private
21
+
22
+ def parsed_log
23
+ log_entry.split("\s")
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ class Visits
2
+ attr_reader :bucket
3
+
4
+ def initialize
5
+ @bucket = []
6
+ end
7
+
8
+ def <<(visit)
9
+ @bucket << visit
10
+ end
11
+
12
+ def unique_webpages
13
+ @unique_webpages ||= webpages.uniq
14
+ end
15
+
16
+ def for(webpage)
17
+ @bucket.select { |visit| visit.webpage == webpage }
18
+ end
19
+
20
+ private
21
+
22
+ def webpages
23
+ @bucket.map(&:webpage)
24
+ end
25
+ end
@@ -0,0 +1,26 @@
1
+ require_relative 'lib/sp_log_parser/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "sp_log_parser"
5
+ spec.version = SPLogParser::VERSION
6
+ spec.authors = ["Mario Zugaj"]
7
+ spec.email = ["mario@mariozugaj.com"]
8
+
9
+ spec.summary = %q{Simple parser for webserver logs.}
10
+ spec.homepage = "https://gitlab.com/mariozugaj/sp-log-parser"
11
+ spec.license = "MIT"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
+
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = "https://gitlab.com/mariozugaj/sp-log-parser"
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sp_log_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mario Zugaj
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-09-16 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - mario@mariozugaj.com
16
+ executables:
17
+ - parser
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".gitignore"
22
+ - ".travis.yml"
23
+ - Gemfile
24
+ - Gemfile.lock
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - bin/console
29
+ - bin/setup
30
+ - exe/parser
31
+ - lib/sp_log_parser.rb
32
+ - lib/sp_log_parser/parser.rb
33
+ - lib/sp_log_parser/stats.rb
34
+ - lib/sp_log_parser/stats_display.rb
35
+ - lib/sp_log_parser/version.rb
36
+ - lib/sp_log_parser/visit.rb
37
+ - lib/sp_log_parser/visits.rb
38
+ - sp_log_parser.gemspec
39
+ homepage: https://gitlab.com/mariozugaj/sp-log-parser
40
+ licenses:
41
+ - MIT
42
+ metadata:
43
+ homepage_uri: https://gitlab.com/mariozugaj/sp-log-parser
44
+ source_code_uri: https://gitlab.com/mariozugaj/sp-log-parser
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 2.3.0
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubygems_version: 3.1.2
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: Simple parser for webserver logs.
64
+ test_files: []