Skip to content

Commit

Permalink
cleaning up some old unused code sections.
Browse files Browse the repository at this point in the history
  • Loading branch information
toddbruner committed Sep 28, 2017
1 parent 4b16c23 commit 34d2a47
Show file tree
Hide file tree
Showing 17 changed files with 29 additions and 419 deletions.
3 changes: 0 additions & 3 deletions lib/Scot/App/Mail.pm
Original file line number Diff line number Diff line change
Expand Up @@ -533,9 +533,6 @@ sub post_alertgroup {
$log->debug("Posting via direct mongo access");
my $mongo = $self->env->mongo;
my $agcol = $mongo->collection('Alertgroup');
#my $agobj = $agcol->create_from_api({
# request => { json => $data }
#});
my @agobjs = $agcol->api_create({
request => { json => $data }
});
Expand Down
148 changes: 0 additions & 148 deletions lib/Scot/Collection/Alertgroup.pm
Original file line number Diff line number Diff line change
Expand Up @@ -136,154 +136,6 @@ override api_create => sub {
return wantarray ? @alertgroups : \@alertgroups;
};

=item B<create_from_api(request_href)>
Create an alertgroup and sub alerts from a POST to the handler
=cut

sub create_from_api {
my $self = shift;
my $href = shift;
my $env = $self->env;
my $mongo = $env->mongo;
my $log = $env->log;
my $mq = $env->mq;

$log->trace("Create Alertgroup");

# alertgroup creation will receive the following in the
# json portion of the request
# request => {
# message_id => '213123',
# subject => 'subject',
# data => [ { ... href structure ... }, { ... } ... ],
# tags => [],
# sources => [],
# }

my $request = $href->{request}->{json};

my $data = $request->{data};
delete $request->{data};

if ( ref($data) ne "ARRAY" ) {
push @$data, $data;
}

my $row_limit = 100;
#if ( defined $env->alertgroup_rowlimit ) {
# $row_limit = $env->alertgroup_rowlimit;
# $log->debug("Altername rowlimit specified as ".$row_limit);
#}

if ( scalar(@$data) > $row_limit ) {
$log->warn("Large number of rows in Alertgroup, splitting...");
my @created_alertgroup;

my $x = 1;
my $subject = $href->{request}->{json}->{subject};
while ( my @subalerts = splice(@$data, 0, $row_limit) ) {
push @{$href->{request}->{json}->{data}}, @subalerts;
$href->{request}->{json}->{subject} = $subject . " part $x";
$log->debug("splitting alertgroup : ",
{filter=>\&Dumper, value => $href});
push @created_alertgroup, $self->create_from_api($href);
$x++;
}
return \@created_alertgroup;
}

my $tags = $request->{tags};
# delete $request->{tags}; # store a copy here and there

my $sources = $request->{sources};
# delete $request->{sources}; # store a copy in obj and in sources.pm

my $alertgroup = $self->create($request);

unless ( defined $alertgroup ) {
$log->error("Failed to create Alertgroup with data ",
{ filter => \&Dumper, value => $request});
return undef;
}

my $id = $alertgroup->id;

if ( defined $tags && scalar(@$tags) > 0 ) {
my $col = $mongo->collection('Tag');
foreach my $tag (@$tags) {
my $t = $col->add_tag_to("alertgroup",$id, $tag);
}
}

if ( defined $sources && scalar(@$sources) > 0 ) {
my $col = $mongo->collection('Source');
foreach my $src (@$sources) {
my $s = $col->add_source_to("alertgroup", $id, $src);
}
}

$log->trace("Creating alerts belonging to Alertgroup ". $id);

my $alert_count = 0;
my $open_count = 0;
my $closed_count = 0;
my $promoted_count = 0;

foreach my $alert_href (@$data) {

my $chref = {
data => $alert_href,
alertgroup => $id,
status => 'open',
columns => $alertgroup->columns,
};



$log->debug("Creating alert ", {filter=>\&Dumper, value => $chref});

my $alert = $mongo->collection("Alert")->create($chref);

unless ( defined $alert ) {
$log->error("Failed to create Alert from ",
{ filter => \&Dumper, value => $chref });
next;
}

# amq stuff should originate out of Api.pm
#$mq->send("scot", {
# action => "created",
# data => {
# type => "alert",
# id => $alert->id,
# who => $request->{user},
# }
#});

# not sure we need a notification for every alert, maybe just alertgroup
# alert triage may want this at some point though
# $env->amq->send_amq_notification("creation", $alert);

$alert_count++;
$open_count++ if ( $alert->status eq "open" );
$closed_count++ if ( $alert->status eq "closed" );
$promoted_count++ if ( $alert->status eq "promoted");
}

$log->debug("updating alertgroup ", $alertgroup->id);

$alertgroup->update({
'$set' => {
open_count => $open_count,
closed_count => $closed_count,
promoted_count => $promoted_count,
alert_count => $alert_count,
}
});
return $alertgroup;
}

sub refresh_data {
my $self = shift;
Expand Down
17 changes: 10 additions & 7 deletions lib/Scot/Collection/Apikey.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,27 @@ package Scot::Collection::Apikey;
use lib '../../../lib';
use v5.18;
use Moose 2;
use Data::Dumper;

extends 'Scot::Collection';

sub create_from_api {
override api_create => sub {
my $self = shift;
my $href = shift;
my $req = shift;
my $env = $self->env;
my $mongo = $env->mongo;
my $log = $env->log;

my $apikey = $self->create($href);
my $json = $req->{request}->{json};

my $apikey = $self->create($json);

unless (defined $apikey) {
$log->error("Failed to create apikey");
unless ( $apikey) {
$log->error("Error creating apikey from ",
{ filter=>\&Dumper, value=>$req});
return undef;
}
return $apikey;
}
};

sub get_users_apikeys {
my $self = shift;
Expand Down
12 changes: 0 additions & 12 deletions lib/Scot/Collection/Atmetric.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,6 @@ with qw(
Scot::Role::GetTagged
);

sub create_from_api {
my $self = shift;
my $request = shift;
my $env = $self->env;
my $log = $env->log;

$log->trace("Create AtMetric from API");

my $stat = $self->create($request);
return $stat;
}

sub upsert_metric {
my $self = shift;
my $doc = shift;
Expand Down
41 changes: 0 additions & 41 deletions lib/Scot/Collection/Checklist.pm
Original file line number Diff line number Diff line change
Expand Up @@ -64,47 +64,6 @@ sub api_subthing {
die "Unsupported subthing $subthing";
}


sub create_from_api {
my $self = shift;
my $request = shift;
my $env = $self->env;
my $json = $request->{request}->{json};
my $log = $env->log;

my @entries = @{$json->{entry}};
delete $json->{entry};

my $checklist = $self->create($json);

$log->debug("created checklist ".$checklist->id);

if ( scalar(@entries) > 0 ) {
# entries were posted in
my $mongo = $self->env->mongo;
my $ecoll = $mongo->collection('Entry');

foreach my $entry (@entries) {
$entry->{owner} = $entry->{owner} // $request->{user};
$entry->{task} = {
when => $env->now(),
who => $request->{user},
status => 'open',
};
$entry->{body} = $entry->{body};
$entry->{is_task} = 1;
$entry->{target} = {
type => "checklist",
id => $checklist->id,
};

my $obj = $ecoll->create($entry);
}
}

return $checklist;
}

sub autocomplete {
my $self = shift;
my $frag = shift;
Expand Down
36 changes: 0 additions & 36 deletions lib/Scot/Collection/Entity.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,6 @@ with qw(
Scot::Role::GetTargeted
);

sub create_from_api {
my $self = shift;
my $href = shift;
my $env = $self->env;
my $log = $env->log;
my $mq = $env->mq;

$log->trace("Creating Entity via API");

my $request = $href->{request}->{json};
my $value = $request->{value};
my $type = $request->{type};

if ( $self->entity_exists($value, $type) ) {
$log->error("Error! Entity already exists");
return undef;
}

my $entity = $self->create($request);

unless ( defined $entity ) {
$log->error("Error! Failed to create Entity with data ",
{ filter => \&Dumper, value => $request } );
return undef;
}
# Api.pm should do this
#$env->mq->send("scot", {
# action => "created",
# data => {
# type => "entity",
# id => $entity->id,
# }
#});
return $entity;
}

sub entity_exists {
my $self = shift;
my $value = shift;
Expand Down
68 changes: 4 additions & 64 deletions lib/Scot/Collection/Entry.pm
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ sub create_from_promoted_alert {
id => $event->id,
};
my $id = $alert->id;
my $agcol = $mongo->collection('Alertgroup');
my $agobj = $agcol->find_iid($alert->alertgroup+0);
my $subject = $agobj->subject;
$json->{body} =
qq|<h3>From Alert <a href="/#/alert/$id">$id</h3><br>|.
qq|<h4>|.$alert->subject.qq|</h4>|.
qq|<h4>|.$subject.qq|</h4>|.
$self->build_table($alert);
$log->debug("Using : ",{filter=>\&Dumper, value => $json});

Expand Down Expand Up @@ -269,69 +272,6 @@ EOF

}

sub create_from_api {
my $self = shift;
my $request = shift;

my $env = $self->env;
my $log = $env->log;
my $mongo = $env->mongo;
my $mq = $env->mq;

$log->trace("Custom create in Scot::Collection::Entry");

my $user = $request->{user};

my $json = $request->{request}->{json};

my $target_type = delete $json->{target_type};
my $target_id = delete $json->{target_id};

unless ( defined $target_type ) {
$log->error("Error: Must provide a target type");
return {
error_msg => "Entries must have target_type defined",
};
}

unless ( defined $target_id ) {
$log->error("Error: Must provide a target id");
return {
error_msg => "Entries must have target_id defined",
};
}

my $task = $self->validate_task($request);
if ( $task ) {
$json->{class} = "task";
$json->{metadata} = $task;
}

my $default_permitted_groups = $self->get_default_permissions(
$target_type, $target_id
);

unless ( $request->{readgroups} ) {
$json->{groups}->{read} = $default_permitted_groups->{read};
}
unless ( $request->{modifygroups} ) {
$json->{groups}->{modify} = $default_permitted_groups->{modify};
}

$json->{target} = {
type => $target_type,
id => $target_id,
};

$json->{owner} = $user;

$log->debug("Creating entry with: ", { filter=>\&Dumper, value => $json});

my $entry_obj = $self->create($json);

return $entry_obj;
}

override api_create => sub {
my $self = shift;
my $req = shift;
Expand Down
Loading

0 comments on commit 34d2a47

Please sign in to comment.