[Groonga-commit] groonga/groonga-log at c649ebf [master] Introduce Statistic class instead of raw Hash

Back to archive index

Kouhei Sutou null+****@clear*****
Wed Sep 27 16:45:13 JST 2017


Kouhei Sutou	2017-09-27 16:45:13 +0900 (Wed, 27 Sep 2017)

  New Revision: c649ebf735915b3aa8693eb6fc0e21a6f5b78a58
  https://github.com/groonga/groonga-log/commit/c649ebf735915b3aa8693eb6fc0e21a6f5b78a58

  Message:
    Introduce Statistic class instead of raw Hash

  Added files:
    lib/groonga-log/statistic.rb
  Modified files:
    lib/groonga-log.rb
    lib/groonga-log/parser.rb
    test/test-parser.rb

  Modified: lib/groonga-log.rb (+18 -1)
===================================================================
--- lib/groonga-log.rb    2017-09-27 16:33:43 +0900 (d06ec10)
+++ lib/groonga-log.rb    2017-09-27 16:45:13 +0900 (95d3d2a)
@@ -1,2 +1,19 @@
-require "groonga-log/version"
+# Copyright (C) 2017 Kouhei Sutou <kou �� clear-code.com>
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+
 require "groonga-log/parser"
+require "groonga-log/statistic"
+require "groonga-log/version"

  Modified: lib/groonga-log/parser.rb (+14 -27)
===================================================================
--- lib/groonga-log/parser.rb    2017-09-27 16:33:43 +0900 (4b44fd9)
+++ lib/groonga-log/parser.rb    2017-09-27 16:45:13 +0900 (f4d4e1c)
@@ -15,6 +15,8 @@
 # License along with this library; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
+require "groonga-log/statistic"
+
 module GroongaLog
   class Parser
     PATTERN =
@@ -31,33 +33,18 @@ module GroongaLog
         next unless line.valid_encoding?
         m = PATTERN.match(line)
 
-        year = Integer(m['year'])
-        month = Integer(m['month'])
-        day = Integer(m['day'])
-        hour = Integer(m['hour'])
-        minute = Integer(m['minute'])
-        second = Integer(m['second'])
-        micro_second = Integer(m['micro_second'])
-        log_level = log_level_to_symbol(m['log_level'])
-        context_id = m['context_id']
-        message = m['message']
-        timestamp = Time.local(year, month, day,
-                               hour, minute, second, micro_second)
-
-        record = {
-          :timestamp => timestamp,
-          :year => year,
-          :month => month,
-          :day => day,
-          :hour => hour,
-          :minute => minute,
-          :second => second,
-          :micro_second => micro_second,
-          :log_level => log_level,
-          :context_id => context_id,
-          :message => message,
-        }
-        yield record
+        statistic = Statistic.new
+        statistic.year = Integer(m['year'])
+        statistic.month = Integer(m['month'])
+        statistic.day = Integer(m['day'])
+        statistic.hour = Integer(m['hour'])
+        statistic.minute = Integer(m['minute'])
+        statistic.second = Integer(m['second'])
+        statistic.micro_second = Integer(m['micro_second'])
+        statistic.log_level = log_level_to_symbol(m['log_level'])
+        statistic.context_id = m['context_id']
+        statistic.message = m['message']
+        yield statistic
       end
     end
 

  Added: lib/groonga-log/statistic.rb (+33 -0) 100644
===================================================================
--- /dev/null
+++ lib/groonga-log/statistic.rb    2017-09-27 16:45:13 +0900 (36c9d33)
@@ -0,0 +1,33 @@
+# Copyright (C) 2017 Kouhei Sutou <kou �� clear-code.com>
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+
+module GroongaLog
+  class Statistic < Struct.new(:timestamp,
+                               :year,
+                               :month,
+                               :day,
+                               :hour,
+                               :minute,
+                               :second,
+                               :micro_second,
+                               :log_level,
+                               :context_id,
+                               :message)
+    def timestamp
+      super || Time.local(year, month, day, hour, minute, second, micro_second)
+    end
+  end
+end

  Modified: test/test-parser.rb (+21 -7)
===================================================================
--- test/test-parser.rb    2017-09-27 16:33:43 +0900 (8677951)
+++ test/test-parser.rb    2017-09-27 16:45:13 +0900 (eb69754)
@@ -18,9 +18,16 @@
 require "helper"
 
 class ParserTest < Test::Unit::TestCase
+  def create_statistic(args)
+    statistic = GroongaLog::Statistic.new
+    args.each do |key, value|
+      statistic[key] = value
+    end
+    statistic
+  end
+
   def test_extract_field
-    expected = [{
-      :timestamp => Time.local(2017, 7, 19, 14, 41, 5, 663978),
+    raw_statistic = {
       :year => 2017,
       :month => 7,
       :day => 19,
@@ -31,13 +38,22 @@ class ParserTest < Test::Unit::TestCase
       :log_level => :notice,
       :context_id => "18c61700",
       :message => "spec:2:update:Object:32(type):8",
-    }]
+    }
+    expected = [create_statistic(raw_statistic)]
     statistics = parse(<<-LOG)
 2017-07-19 14:41:05.663978|n|18c61700|spec:2:update:Object:32(type):8
     LOG
     assert_equal(expected, statistics)
   end
 
+  def test_timestamp
+    statistics = parse(<<-LOG)
+2017-07-19 14:41:05.663978|n|18c61700|spec:2:update:Object:32(type):8
+    LOG
+    assert_equal([Time.local(2017, 7, 19, 14, 41, 5, 663978)],
+                 statistics.collect(&:timestamp))
+  end
+
   def test_log_level
     expected = [
       :emergency,
@@ -61,10 +77,8 @@ class ParserTest < Test::Unit::TestCase
 2017-07-19 14:41:06.663978|d|18c61700|debug
 2017-07-19 14:41:06.663978|-|18c61700|dump
     LOG
-    log_levels = statistics.collect do |statistic|
-      statistic[:log_level]
-    end
-    assert_equal(expected, log_levels)
+    assert_equal(expected,
+                 statistics.collect(&:log_level))
   end
 
   private
-------------- next part --------------
HTML����������������������������...
URL: https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20170927/ed5a40cd/attachment-0001.htm 



More information about the Groonga-commit mailing list
Back to archive index