package Lacuna::Web::ApiKey; use Moose; use utf8; no warnings qw(uninitialized); extends qw(Lacuna::Web); use UUID::Tiny ':std'; use Lacuna::Mailer; sub www_view_stats { my ($self, $request) = @_; my $pair = Lacuna->db->resultset('Lacuna::DB::Result::ApiKey')->search({ private_key => $request->param('private_key')})->first; unless (defined $pair) { confess [404, 'That private key could not be found']; } my $name = ''.$pair->public_key.''; $name = ($pair->name) ? $pair->name . ' ('.$name.')' : $name; my $row = sub { return ''.$_[0].''.$_[1].''.$_[2].''; }; my $dt_parser = Lacuna->db->storage->datetime_parser; my $thirty_days_ago = $dt_parser->format_datetime( DateTime->now->subtract(days=>30) ); my $out = "

Stats for $name

"; my $login = Lacuna->db->resultset('Lacuna::DB::Result::Log::Login')->search({api_key => $pair->public_key}); $out .= $row->('Empires Using', $login->search({date_stamp => { '>=' => $thirty_days_ago }},{group_by => ['empire_id']})->count, $login->search(undef,{group_by => ['empire_id']})->count ); $out .= $row->('Logins', $login->search({date_stamp => { '>=' => $thirty_days_ago }})->count, $login->count ); my $essentia = Lacuna->db->resultset('Lacuna::DB::Result::Log::Essentia')->search({api_key => $pair->public_key, amount => { '<' => 0 } }); $out .= $row->('Essentia Spent', $essentia->search({date_stamp => { '>=' =>$thirty_days_ago }})->get_column('amount')->sum * -1, $essentia->get_column('amount')->sum * -1 ); my $lottery = Lacuna->db->resultset('Lacuna::DB::Result::Log::Lottery')->search({api_key => $pair->public_key }); $out .= $row->('Lottery Votes', $lottery->search({date_stamp => { '>=' =>$thirty_days_ago }})->count, $lottery->count ); my $rpc = Lacuna->db->resultset('Lacuna::DB::Result::Log::RPC')->search({api_key => $pair->public_key }); $out .= $row->('RPC Calls', $rpc->search({date_stamp => { '>=' =>$thirty_days_ago }})->count, $rpc->count ); $out .= '
StatisticPast 30 DaysAll Time
'; return $self->wrapper($out, { title => 'Your Stats', logo => 1 }); } sub www_generate_key { my ($self, $request) = @_; my $pair = Lacuna->db->resultset('Lacuna::DB::Result::ApiKey')->new({ public_key => create_uuid_as_string(UUID_V4), private_key => create_uuid_as_string(UUID_V4), email => $request->param('email'), name => $request->param('name'), ip_address => $request->address, }); $pair->insert; my $out = q{

Your Key

Public Key:
}. $pair->public_key .q{

Private Key:
}. $pair->private_key .q{

}; if ($pair->email) { my $mailer = Lacuna::Mailer->new; $mailer->send( $pair->email, 'Lacuna API Key', "Here is the copy of the API Key you requested.\n\nPublic Key: ".$pair->public_key."\n\nPrivate Key: ".$pair->private_key, ); } return $self->wrapper($out, { title => 'Your API Key', logo => 1 }); } sub www_default { my ($self, $request) = @_; my $out = q{

Lacuna Expanse API Key Console

You can either request a key or view the stats of an existing key.
View Stats Private Key:


Create Key Program / Client Name:
(Optional)

Email Address:
(If you want the key emailed to you as well.)

You may want to view the API Key FAQ if you are unfamiliar with API Keys.
}; return $self->wrapper($out, { title => 'Lacuna Expanse API Key Console', logo => 1 }); } no Moose; __PACKAGE__->meta->make_immutable(inline_constructor => 0);