DBI.pm 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package Trog::Log::DBI;
  2. use strict;
  3. use warnings;
  4. use parent qw{Log::Dispatch::DBI};
  5. use Ref::Util qw{is_arrayref};
  6. use Capture::Tiny qw{capture_merged};
  7. use POSIX qw{mktime};
  8. use POSIX::strptime qw{strptime};
  9. our ( $referer, $ua, $urchin );
  10. sub create_statement {
  11. my $self = shift;
  12. # This is a writable view. Consult schema for its behavior.
  13. my $sql = "INSERT INTO all_requests (uuid, date, ip_address, user, method, route, referer, ua, code) VALUES (?,?,?,?,?,?,?,?,?)";
  14. my $sql2 = "INSERT INTO messages (uuid, message) VALUES (?,?)";
  15. $self->{sth2} = $self->{dbh}->prepare($sql2);
  16. my $sql3 = "INSERT INTO urchin_requests (request_uuid, utm_source, utm_medium, utm_campaign, utm_term, utm_content) VALUES (?,?,?,?,?,?)";
  17. $self->{sth3} = $self->{dbh}->prepare($sql3);
  18. return $self->{dbh}->prepare($sql);
  19. }
  20. my %buffer;
  21. sub log_message {
  22. my ( $self, %params ) = @_;
  23. # Rip apart the message. If it's got any extended info, lets grab that too.
  24. my $msg = $params{message};
  25. my $message;
  26. my ( $date, $uuid, $ip, $user, $method, $code, $route ) = $msg =~ m!^([\w|\-|:]+) \[INFO\]: RequestId ([\w|\-]+) From ([\w|\.|:]+) \|(\w+)\| (\w+) (\d+) (.+)!;
  27. # Otherwise, let's mark it down in the "messages" table. This will be deferred until the final write.
  28. if ( !$date ) {
  29. ( $date, $uuid, $ip, $user, $message ) = $msg =~ m!^([\w|\-|:]+) \[\w+\]: RequestId ([\w|\-]+) From ([\w|\.|:]+) \|(\w+)\| (.+)!;
  30. $buffer{$uuid} //= [];
  31. push( @{ $buffer{$uuid} }, $message );
  32. return 1;
  33. }
  34. # If this is a mangled log, forget it.
  35. return unless $date && $uuid;
  36. # 2024-01-20T22:37:41Z
  37. # Transform the date into an epoch so we can do math on it
  38. my $fmt = "%Y-%m-%dT%H:%M:%SZ";
  39. my @cracked = strptime( $date, $fmt );
  40. #XXX get a dumb warning otherwise
  41. pop @cracked;
  42. my $epoch = mktime(@cracked);
  43. # Allow callers to set quasi-tracking parameters.
  44. # We only care about this in DB context, as it's only for metrics, which are irrelevant in text logs/debugging.
  45. $referer //= 'none';
  46. $ua //= 'none';
  47. $urchin //= {};
  48. my $res = $self->{sth}->execute( $uuid, $epoch, $ip, $user, $method, $route, $referer, $ua, $code );
  49. # Dump in the accumulated messages
  50. if ( is_arrayref( $buffer{$uuid} ) && @{ $buffer{$uuid} } ) {
  51. $self->{sth2}->bind_param_array( 1, $uuid );
  52. $self->{sth2}->bind_param_array( 2, $buffer{$uuid} );
  53. $self->{sth2}->execute_array( {} );
  54. delete $buffer{$uuid};
  55. }
  56. # Record urchin data if there is any.
  57. if ( %$urchin && $urchin->{utm_source} ) {
  58. $self->{sth3}->execute( $uuid, $urchin->{utm_source}, $urchin->{utm_medium}, $urchin->{utm_campaign}, $urchin->{utm_term}, $urchin->{utm_content} );
  59. }
  60. return $res;
  61. }
  62. 1;