| 1 | package Message::Passing::Output::MongoDB; | ||
| 2 | |||
| 3 | # ABSTRACT: Module for Message::Passing to send log to mongodb | ||
| 4 | |||
| 5 | use Moose; | ||
| 6 | use MongoDB; | ||
| 7 | use AnyEvent; | ||
| 8 | use Scalar::Util qw/ weaken /; | ||
| 9 | use MooseX::Types::Moose qw/ ArrayRef HashRef Str Bool Int Num /; | ||
| 10 | use Moose::Util::TypeConstraints; | ||
| 11 | use aliased 'DateTime' => 'DT'; | ||
| 12 | use MooseX::Types::ISO8601 qw/ ISO8601DateTimeStr /; | ||
| 13 | use Data::Dumper; | ||
| 14 | use Tie::IxHash; | ||
| 15 | use namespace::autoclean; | ||
| 16 | |||
| 17 | our $VERSION = '0.052'; | ||
| 18 | $VERSION = eval $VERSION; | ||
| 19 | |||
| 20 | with qw/ | ||
| 21 | Message::Passing::Role::Output | ||
| 22 | Message::Passing::Role::HasUsernameAndPassword | ||
| 23 | /; | ||
| 24 | |||
| 25 | has connection_options => ( | ||
| 26 | is => 'ro', | ||
| 27 | isa => 'HashRef', | ||
| 28 | required => 1, | ||
| 29 | ); | ||
| 30 | |||
| 31 | has '+password' => ( | ||
| 32 | required => 0, | ||
| 33 | ); | ||
| 34 | |||
| 35 | has '+username' => ( | ||
| 36 | required => 0, | ||
| 37 | ); | ||
| 38 | |||
| 39 | has database => ( | ||
| 40 | isa => Str, | ||
| 41 | is => 'ro', | ||
| 42 | required => 1, | ||
| 43 | ); | ||
| 44 | |||
| 45 | has _client => ( | ||
| 46 | is => 'ro', | ||
| 47 | isa => 'MongoDB::MongoClient', | ||
| 48 | lazy => 1, | ||
| 49 | default => sub { | ||
| 50 | my $self = shift; | ||
| 51 | MongoDB::MongoClient->new( | ||
| 52 | $self->connection_options | ||
| 53 | ); | ||
| 54 | }, | ||
| 55 | ); | ||
| 56 | |||
| 57 | has _db => ( | ||
| 58 | is => 'ro', | ||
| 59 | isa => 'MongoDB::Database', | ||
| 60 | lazy => 1, | ||
| 61 | default => sub { | ||
| 62 | my $self = shift; | ||
| 63 | my $client = $self->_client; | ||
| 64 | |||
| 65 | my $database = $self->database; | ||
| 66 | if (defined $self->username) { | ||
| 67 | $client->authenticate($database, $self->username, $self->password) | ||
| 68 | or die "MongoDB authentication failure"; | ||
| 69 | } | ||
| 70 | |||
| 71 | return $client->get_database($self->database); | ||
| 72 | }, | ||
| 73 | ); | ||
| 74 | |||
| 75 | has collection => ( | ||
| 76 | isa => Str, | ||
| 77 | is => 'ro', | ||
| 78 | required => 1, | ||
| 79 | ); | ||
| 80 | |||
| 81 | has _collection_of_day => ( | ||
| 82 | is => 'rw', | ||
| 83 | isa => 'MongoDB::Collection', | ||
| 84 | lazy => 1, | ||
| 85 | builder => '_build_collection_of_day', | ||
| 86 | ); | ||
| 87 | |||
| 88 | sub _build_collection_of_day { | ||
| 89 | my ($self) = @_; | ||
| 90 | |||
| 91 | my $dt = DateTime->now; | ||
| 92 | my $collection_name_by_date = $self->collection .'_'. $dt->strftime('%Y%m%d'); | ||
| 93 | |||
| 94 | return $self->_db->get_collection($collection_name_by_date); | ||
| 95 | } | ||
| 96 | |||
| 97 | sub _ensure_collection_indexes { | ||
| 98 | my ($self, $collection) = @_; | ||
| 99 | $collection //= $self->_collection_of_day; | ||
| 100 | |||
| 101 | if ($self->_has_indexes) { | ||
| 102 | foreach my $index (@{$self->indexes}){ | ||
| 103 | $collection->ensure_index(@$index); | ||
| 104 | warn("ensure index " . Dumper($index)) if $self->verbose; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | return $collection; | ||
| 109 | } | ||
| 110 | |||
| 111 | has _collection_of_day_name => ( | ||
| 112 | is => 'rw', | ||
| 113 | isa => Str, | ||
| 114 | ); | ||
| 115 | |||
| 116 | sub _get_collection_by_date { | ||
| 117 | my ($self, $dt) = @_; | ||
| 118 | |||
| 119 | my $collection_name_by_date = $self->collection .'_'. $dt->strftime('%Y%m%d'); | ||
| 120 | # a new collection | ||
| 121 | if (!defined $self->_collection_of_day_name || $self->_collection_of_day_name ne $collection_name_by_date) { | ||
| 122 | $self->_flush; | ||
| 123 | my $collection_by_date = $self->_db->get_collection($collection_name_by_date); | ||
| 124 | $self->_ensure_collection_indexes($collection_by_date); | ||
| 125 | $self->_collection_of_day | ||
| 126 | and $self->_remove_expired_collection(); | ||
| 127 | $self->_collection_of_day($collection_by_date); | ||
| 128 | $self->_collection_of_day_name($collection_name_by_date) | ||
| 129 | } | ||
| 130 | |||
| 131 | return $collection_name_by_date; | ||
| 132 | } | ||
| 133 | |||
| 134 | sub _remove_expired_collection{ | ||
| 135 | my ($self) = @_; | ||
| 136 | |||
| 137 | my $retention_date = DT->now()->subtract(days => $self->retention ); | ||
| 138 | my $expired_collection_name = $self->collection .'_'. $retention_date->strftime('%Y%m%d'); | ||
| 139 | |||
| 140 | $self->_db->get_collection($expired_collection_name)->drop; | ||
| 141 | } | ||
| 142 | |||
| 143 | sub _default_port { 27017 } | ||
| 144 | |||
| 145 | has _log_counter => ( | ||
| 146 | traits => ['Counter'], | ||
| 147 | is => 'rw', | ||
| 148 | isa => Int, | ||
| 149 | default => sub {0}, | ||
| 150 | handles => { _inc_log_counter => 'inc', }, | ||
| 151 | ); | ||
| 152 | |||
| 153 | has verbose => ( | ||
| 154 | isa => 'Bool', | ||
| 155 | is => 'ro', | ||
| 156 | default => sub { | ||
| 157 | -t STDIN | ||
| 158 | }, | ||
| 159 | ); | ||
| 160 | |||
| 161 | sub consume { | ||
| 162 | my ($self, $data) = @_; | ||
| 163 | return unless $data; | ||
| 164 | |||
| 165 | my $date; | ||
| 166 | if (my $epochtime = delete($data->{epochtime})) { | ||
| 167 | $date = DT->from_epoch(epoch => $epochtime); | ||
| 168 | delete($data->{date}); | ||
| 169 | } | ||
| 170 | elsif (my $try_date = delete($data->{date})) { | ||
| 171 | if (is_ISO8601DateTimeStr($try_date)) { | ||
| 172 | $date = to_DateTime($try_date); | ||
| 173 | } | ||
| 174 | } | ||
| 175 | $date ||= DT->from_epoch(epoch => time()); | ||
| 176 | |||
| 177 | my $collection = $self->_get_collection_by_date($date); | ||
| 178 | |||
| 179 | push (@{$self->queue}, $data); | ||
| 180 | |||
| 181 | if (scalar(@{$self->queue}) > 1000) { | ||
| 182 | $self->_flush; | ||
| 183 | } | ||
| 184 | |||
| 185 | #$collection->insert($data) | ||
| 186 | #or warn "Insertion failure: " . Dumper($data) . "\n"; | ||
| 187 | if ($self->verbose) { | ||
| 188 | $self->_inc_log_counter; | ||
| 189 | warn("Total " . $self->_log_counter . " records inserted in MongoDB\n"); | ||
| 190 | } | ||
| 191 | } | ||
| 192 | |||
| 193 | sub _flush { | ||
| 194 | my $self = shift; | ||
| 195 | weaken($self); | ||
| 196 | return if $self->_am_flushing; | ||
| 197 | my $queue = $self->queue; | ||
| 198 | return unless scalar @$queue; | ||
| 199 | $self->_am_flushing(1); | ||
| 200 | |||
| 201 | eval { | ||
| 202 | $self->_collection_of_day->batch_insert($queue); | ||
| 203 | 1; | ||
| 204 | } or do { | ||
| 205 | $self->_client->connect(); | ||
| 206 | warn("Failed to do the insertion of logs. \n"); | ||
| 207 | }; | ||
| 208 | $self->_clear_queue; | ||
| 209 | $self->_am_flushing(0); | ||
| 210 | } | ||
| 211 | |||
| 212 | has queue => ( | ||
| 213 | is => 'ro', | ||
| 214 | isa => ArrayRef, | ||
| 215 | default => sub { [] }, | ||
| 216 | init_arg => undef, | ||
| 217 | lazy => 1, | ||
| 218 | clearer => '_clear_queue', | ||
| 219 | ); | ||
| 220 | |||
| 221 | has _am_flushing => ( | ||
| 222 | isa => Bool, | ||
| 223 | is => 'rw', | ||
| 224 | default => 0, | ||
| 225 | ); | ||
| 226 | |||
| 227 | has _flush_timer => ( | ||
| 228 | is => 'ro', | ||
| 229 | lazy => 1, | ||
| 230 | builder => '_build_flush_time', | ||
| 231 | ); | ||
| 232 | |||
| 233 | sub _build_flush_time { | ||
| 234 | my $self = shift; | ||
| 235 | weaken($self); | ||
| 236 | AnyEvent->timer( | ||
| 237 | after => 10, | ||
| 238 | interval => 10, | ||
| 239 | cb => sub { $self->_flush }, | ||
| 240 | ); | ||
| 241 | } | ||
| 242 | |||
| 243 | has indexes => ( | ||
| 244 | isa => ArrayRef[ArrayRef[HashRef]], | ||
| 245 | is => 'ro', | ||
| 246 | predicate => '_has_indexes', | ||
| 247 | ); | ||
| 248 | |||
| 249 | has retention => ( | ||
| 250 | is => 'ro', | ||
| 251 | isa => Num, | ||
| 252 | default => 7, # days | ||
| 253 | documentation => 'Int, days to retent log, set 0 to always keep log', | ||
| 254 | ); | ||
| 255 | |||
| 256 | has collect_fields => ( | ||
| 257 | isa => 'Bool', | ||
| 258 | is => 'ro', | ||
| 259 | default => 0, | ||
| 260 | ); | ||
| 261 | |||
| 262 | has _observer => ( | ||
| 263 | is => 'ro', | ||
| 264 | lazy => 1, | ||
| 265 | builder => '_build_observer' | ||
| 266 | ); | ||
| 267 | |||
| 268 | sub _build_observer { | ||
| 269 | my $self = shift; | ||
| 270 | weaken($self); | ||
| 271 | my $retention_date = DT->now()->subtract(days => $self->retention); | ||
| 272 | AnyEvent->timer( | ||
| 273 | after => 30, | ||
| 274 | interval => 24*3600, | ||
| 275 | cb => sub { | ||
| 276 | } | ||
| 277 | ); | ||
| 278 | } | ||
| 279 | |||
| 280 | sub BUILD { | ||
| 281 | my ($self) = @_; | ||
| 282 | $self->_flush_timer; | ||
| 283 | $self->_observer | ||
| 284 | if $self->collect_fields; | ||
| 285 | } | ||
| 286 | |||
| 287 | 1; | ||
| 288 | |||
| 289 | =head1 NAME | ||
| 290 | |||
| 291 | Message::Passing::Output::MongoDB - message-passing out put to MongoDB | ||
| 292 | |||
| 293 | =head1 SYNOPSIS | ||
| 294 | |||
| 295 | message-pass --input STDIN | ||
| 296 | --output MongoDB --output_options '{ "database":"log_database", "collection":"logs"}' | ||
| 297 | |||
| 298 | {"foo":"bar"} | ||
| 299 | |||
| 300 | =head1 DESCRIPTION | ||
| 301 | |||
| 302 | Module for L<Message::Passing>, send output to MongoDB | ||
| 303 | |||
| 304 | =head1 METHODS | ||
| 305 | |||
| 306 | =over | ||
| 307 | |||
| 308 | =item consume | ||
| 309 | |||
| 310 | Consumes a message by JSON encoding it save it in MongoDB | ||
| 311 | |||
| 312 | =back | ||
| 313 | |||
| 314 | =head1 ATTRIBUTES | ||
| 315 | |||
| 316 | =over | ||
| 317 | |||
| 318 | =item database | ||
| 319 | |||
| 320 | Required, Str, the database to use. | ||
| 321 | |||
| 322 | =item collection | ||
| 323 | |||
| 324 | Required, Str, the collection to use. | ||
| 325 | |||
| 326 | =item connection_options | ||
| 327 | |||
| 328 | HashRef, takes any options as MongoDB::MongoClient->new(\%options) do | ||
| 329 | |||
| 330 | =item username | ||
| 331 | |||
| 332 | Str, mongodb authentication user | ||
| 333 | |||
| 334 | =item password | ||
| 335 | |||
| 336 | Str, mongodb authentication password | ||
| 337 | |||
| 338 | =item indexes | ||
| 339 | |||
| 340 | ArrayRef[ArrayRef[HashRef]], mongodb indexes | ||
| 341 | |||
| 342 | ... | ||
| 343 | indexes => [ | ||
| 344 | [{"foo" => 1, "bar" => -1}, { unique => true }], | ||
| 345 | [{"foo" => 1}], | ||
| 346 | ] | ||
| 347 | ... | ||
| 348 | |||
| 349 | =item collect_fields | ||
| 350 | |||
| 351 | Bool, default to 0, set to 1 to collect the fields' key and inserted in collection | ||
| 352 | $self->collection . "_keys", execution at the starting and once per day. | ||
| 353 | |||
| 354 | =item retention | ||
| 355 | |||
| 356 | Int, time in seconds to conserver logs, set 0 to keep it permanent, default is | ||
| 357 | a week | ||
| 358 | |||
| 359 | =item verbose | ||
| 360 | |||
| 361 | Boolean, verbose | ||
| 362 | |||
| 363 | =back | ||
| 364 | |||
| 365 | =head1 SEE ALSO | ||
| 366 | |||
| 367 | L<Message::Passing> | ||
| 368 | |||
| 369 | =head1 SPONSORSHIP | ||
| 370 | |||
| 371 | This module exists due to the wonderful people at Suretec Systems Ltd. | ||
| 372 | <http://www.suretecsystems.com/> who sponsored its development for its | ||
| 373 | VoIP division called SureVoIP <http://www.surevoip.co.uk/> for use with | ||
| 374 | the SureVoIP API - | ||
| 375 | <http://www.surevoip.co.uk/support/wiki/api_documentation> | ||
| 376 | |||
| 377 | =head1 AUTHOR, COPYRIGHT AND LICENSE | ||
| 378 | |||
| 379 | See L<Message::Passing>. | ||
| 380 | |||
| 381 | =cut | ||
| 382 |