insight 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/insight.rb +104 -0
  2. metadata +45 -0
data/lib/insight.rb ADDED
@@ -0,0 +1,104 @@
1
+ require 'psych'
2
+ require 'sqlite3'
3
+
4
+ module Vision
5
+ class Rule
6
+ attr_accessor :rule
7
+ def initialize(db, rule, config)
8
+ @db = db
9
+ @rule = rule
10
+ @config = config
11
+ @regexps = {}
12
+
13
+ @fileregex = Regexp.new(config["file"])
14
+
15
+ @config["regex"].each{|key, value|
16
+ @regexps[key] = Regexp.new(value)
17
+ }
18
+ createSchema()
19
+ createIndexes()
20
+ end
21
+ def createIndexes()
22
+ @config["schema"]["indexes"].each{|key, value|
23
+ id = "#{rule}#{key}"
24
+ query = "CREATE INDEX IF NOT EXISTS #{id} on #{rule} ( #{value} );"
25
+ @db.execute query
26
+ }
27
+ end
28
+ def createSchema()
29
+ query = "CREATE TABLE IF NOT EXISTS #{@rule} ( "
30
+ count = 0
31
+ @columns = ""
32
+ @columnholders = ""
33
+ @config["schema"]["columns"].each{|key, value|
34
+ query = query + ", " if count != 0
35
+ @columns = @columns + ", " if count != 0
36
+ @columnholders = @columnholders + ", " if count != 0
37
+ @columns = @columns + key
38
+ @columnholders = @columnholders + "?"
39
+ query = query + key + " " + value
40
+ count = count + 1
41
+ }
42
+ query = query + ");"
43
+ @db.execute query
44
+ end
45
+ def applies?(file)
46
+ m = @fileregex.match(file)
47
+ return !m.nil?
48
+ end
49
+ def insert(m)
50
+ query = "INSERT INTO #{@rule} (#{@columns}) VALUES ( #{@columnholders} )"
51
+
52
+ @db.execute(query, m.captures)
53
+ end
54
+ def line(line)
55
+ @regexps.each{|name, regexp|
56
+ m = regexp.match(line)
57
+ insert(m) if m
58
+ }
59
+ end
60
+ end
61
+
62
+ class Parser
63
+ @rules = {}
64
+ def initdb(db)
65
+ begin
66
+ @db = SQLite3::Database.new db
67
+ rescue SQLite3::Exception => e
68
+ puts "Exception opening #{db}"
69
+ puts e
70
+ end
71
+ end
72
+ def initialize(config, db)
73
+ @rules = []
74
+ @config = Psych.load( File.open(config) )
75
+
76
+ initdb(db)
77
+
78
+ @config.each_key{|key|
79
+ @rules.push(Rule.new(@db, key, @config[key]))
80
+ }
81
+ end
82
+ def parse(file)
83
+ @db.execute "BEGIN TRANSACTION;"
84
+
85
+ @rulesToApply = []
86
+ @rules.each{|rule|
87
+ if(rule.applies?(file))
88
+ @rulesToApply.push(rule)
89
+ end
90
+ }
91
+
92
+ File.open(file, 'r+') do |file|
93
+ file.each_line { |line|
94
+ @rulesToApply.each{|rule|
95
+ rule.line(line)
96
+ }
97
+ }
98
+ end
99
+
100
+ @db.execute("COMMIT TRANSACTION;")
101
+ end
102
+ end
103
+ end
104
+
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: insight
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ian Reay
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-24 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Insight!
15
+ email: ian.reay@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/insight.rb
21
+ homepage: https://github.com/ianreay/insight
22
+ licenses: []
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 1.8.25
42
+ signing_key:
43
+ specification_version: 3
44
+ summary: Insight, the data analysis framework
45
+ test_files: []