Class: PcaprLocal::DB

Inherits:
Object
  • Object
show all
Defined in:
lib/pcapr_local/db.rb

Class Method Summary collapse

Class Method Details

.get_db(config) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pcapr_local/db.rb', line 9

def self.get_db config
    database = config.fetch "database"
    base = config.fetch "uri"

    couch_url = File.join(base, database)
    db = CouchRest.database! couch_url
    patch_db db

    begin
        design = db.get("_design/pcaps") 
    rescue RestClient::ResourceNotFound
        design = CouchRest::Design.new
        design.name = "pcaps"
        design.database = db
        db.save_doc design
    end

    db.update_doc "_design/pcaps" do |design|
        design['views'] = self.views
        design
    end

    db
end

.patch_db(pcapr_local) ⇒ Object

Patch couch to include create and update times for each document.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/pcapr_local/db.rb', line 35

def self.patch_db pcapr_local
    def pcapr_local.update_doc id, &block
        super(id) do |doc|
            doc = block.call doc
            doc['updated_at'] = Time.now
            doc
        end
    end

    def pcapr_local.save_doc *args
        doc = args[0]
        now = Time.now
        doc["created_at"] ||= now
        doc["updated_at"] ||= now
        super
    end

    # Performs a query and yields each row to the the supplied block.
    # Uses paging to split the query into page_size chunks.
    def pcapr_local.each_in_view view, query=nil, page_size=50 #block
        query ||= {}
        limit = page_size + 1
        query[:limit] = limit

        begin
            res = self.view(view, query)
            rows = res['rows']
            next_doc = rows.size == limit ? rows.pop : nil
            if next_doc
                query[:startkey] = next_doc['key']
                query[:startkey_docid] = next_doc['id']
            end

            rows.each do |row|
                yield row
            end
        end while next_doc
    end

    nil
end

.viewsObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/pcapr_local/db.rb', line 77

def self.views
        {
        "by_created_at" => {
            "map" => %q(function(doc) {
                if (doc.type === 'pcap') {
                    emit(Date.parse(doc.created_at), null);
                }
            })
        },
        "by_inode" => {
            "map" => %q(function(doc) {
                if (doc.type === 'pcap') {
                    emit(doc.stat.inode, null);
                }
            })
        },
        "by_filename" => {
            "map" => %q(function(doc) {
                if (doc.type === 'pcap') {
                    emit(doc.filename, null);
                }
            })
        },
        "by_directory" => {
            "map" => %q(function(doc) {
                if (doc.type === 'pcap') {
                    var paths = doc.filename.split('/');
                    paths.unshift(paths.length-1);
                    paths.pop();
                    paths.push(Date.parse(doc.created_at));
                    emit(paths,1);
                }
            }),
            "reduce" => '_sum'
        },
        "by_path" => {
            "map" => %q(function(doc) {
                if (doc.type === 'pcap') {
                    var paths = doc.filename.split('/');
                    paths.pop();
                    emit(paths,1);
                }
            }),
            "reduce" => '_sum'
        },
        "by_service" => {
            "map" => %q(function(doc) {
                if (doc.type === 'pcap' && doc.index) {
                    var services = doc.index.services;
                    for (var i=0; i<services.length; ++i) {
                        emit([ services[i], Date.parse(doc.created_at) ], 1);
                    }
                }
            }),
            "reduce" => '_sum'
        },
        "indexed" => {
            "map" => %q(function(doc) {
                if (doc.type === 'pcap') {
                    if (doc.status === 'indexed') {
                        emit(doc.filename, null);
                    }
                }
            })
        },
        "queued" => {
            "map" => %q(function(doc) {
                if (doc.type === 'pcap') {
                    if (doc.status === 'queued' || doc.status === 'indexing') {
                        emit(doc.filename, null);
                    }
                }
            })
        },
        "by_status" => {
            "map" => %q(function(doc) {
                if (doc.type === 'pcap') {
                    emit([ doc.status, Date.parse(doc.created_at) ], 1);
                }
            }),
            "reduce" => '_sum'
        },
        "by_keyword" => {
            "map" => %q(function(doc) {
                if (doc.type === 'pcap') {
                    var keywords = {};
                    var paths = doc.filename.split('/');
                    for (var p in paths) {
                        var tokens = paths[p].toLowerCase().split(/[ -_\.\\\/\(\)]/);
                        for (var t in tokens) {
                            if (tokens[t].length > 2) {
                                keywords[tokens[t]] = true;
                            }
                        }
                    }
                                            
                    for (var k in keywords) {
                        emit([ k, Date.parse(doc.created_at) ], 1);
                    }
                }
            }),
            "reduce" => '_sum'
        },
        "statistics" => {
            "map" => %q(function(doc) {
                if (doc.type === 'pcap') {
                    emit('pcaps', 1);
                    emit('bytes', doc.stat.size);
                    if (doc.index) {
                        emit('packets', doc.index.about.packets);
                        emit('flows', doc.index.about.flows);
                        emit('services', doc.index.about.services);
                    }
                }
            }),
            "reduce" => '_sum'
        }
        }
end