Created
January 9, 2016 13:30
-
-
Save 844196/17be7cc7ba2fe17ebf58 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'yaml' | |
| require 'csv' | |
| require 'date' | |
| require 'time' | |
| require 'singleton' | |
| module LogParser | |
| class Configuration | |
| # 設定メソッド | |
| # LogParser#configure, LogParser#yaml_load | |
| end | |
| module Record | |
| # パーサー | |
| # LogParser#parse | |
| end | |
| module Record::Content | |
| # レコードオブジェクト | |
| # Base | |
| # |- User | |
| # |- PC | |
| # `- UseDateTime | |
| end | |
| end | |
| module LogParser | |
| class Configuration | |
| include Singleton | |
| # FIXME: gemにする時は../config/default.yamlをロードする様に修正 | |
| @@defaults = YAML.load(DATA) | |
| def self.defaults | |
| @@defaults | |
| end | |
| def initialize | |
| @@defaults.each {|k,v| self.send("#{k}=", v) } | |
| end | |
| attr_accessor *@@defaults.keys | |
| end | |
| class << self | |
| def config | |
| Configuration.instance | |
| end | |
| def configure | |
| yield(config) | |
| end | |
| def yaml_load(path: '') | |
| YAML.load_file(path).each do |k, v| | |
| config.instance_variable_set("@#{k}", v) | |
| end | |
| end | |
| end | |
| end | |
| module LogParser::Record::Content | |
| class Base | |
| def to_h | |
| end | |
| end | |
| end | |
| module LogParser::Record::Content | |
| class User < Base | |
| attr_reader :user_id | |
| def initialize(user_id) | |
| @user_id = user_id | |
| end | |
| def student? | |
| LogParser.config.student_id[:legal_pattern] === @user_id | |
| end | |
| def join_year | |
| @user_id.slice(LogParser.config.student_id[:legal_pattern], :join_year) if student? | |
| end | |
| def department | |
| get_section(:department) | |
| end | |
| def faculty | |
| get_section(:faculty) | |
| end | |
| private | |
| def get_section(query) | |
| if student? | |
| LogParser.config.instance_variable_get("@#{query}").each do |section, code| | |
| match = @user_id.slice(LogParser.config.student_id[:legal_pattern], query) | |
| return section if code.map(&:to_s).include?(match) | |
| end | |
| '???' | |
| end | |
| end | |
| end | |
| end | |
| module LogParser::Record::Content | |
| class PC < Base | |
| attr_reader :client_id | |
| def initialize(client_id) | |
| @client_id = client_id | |
| end | |
| def place | |
| LogParser.config.place.each do |place, regexp| | |
| return place if @client_id.match(regexp) | |
| end | |
| 'unknown' | |
| end | |
| end | |
| end | |
| module LogParser::Record::Content | |
| class UseDateTime < Base | |
| attr_reader :use_date, :startup_time, :shutdown_time | |
| def initialize(use_date, startup_time, shutdown_time) | |
| @use_date = use_date | |
| @startup_time = startup_time | |
| @shutdown_time = shutdown_time | |
| end | |
| def uptime | |
| (@shutdown_time - @startup_time).to_i # seconds | |
| end | |
| end | |
| end | |
| module LogParser::Record | |
| @record = Struct.new('Record', :pc, :user, :use_date_time) | |
| def self.parse(row) | |
| @record.new( | |
| Content::PC.new(row[:agent]), | |
| Content::User.new(row[:user_id]), | |
| Content::UseDateTime.new(row[:date], row[:startup], row[:shutdown]) | |
| ) | |
| end | |
| end | |
| __END__ | |
| :faculty: | |
| :department: | |
| :place: | |
| :student_id: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment