summaryrefslogtreecommitdiff
path: root/lib/GitHub/Collector/Command/edges.pm
blob: 4ffe0c86d1bf6bb4a411f8a914c1207d354347ea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
package GitHub::Collector::Command::edges;

use Moose;
use boolean;

extends qw(MooseX::App::Cmd::Command);

with qw(
  GitHub::Collector::Role::Context
  GitHub::Collector::Role::Logger
  GitHub::Collector::Role::MongoDB
);

sub execute {
    my $self = shift;

    $self->log("start to merge contributions");

    my $profiles = $self->db_profiles->find({edges_done => false});

    while ( my $profile = $profiles->next ) {
        next if $self->_is_done($profile->{login});
        $self->log("merge contributions for ".$profile->{login});
        $self->_contributions($profile->{login});
    }

    $self->log("done merging contributions");
}

sub _is_done {
    my ($self, $login) = @_;
    $self->db_edges->find({source => $login})->count;
}

sub _contributions {
    my ( $self, $login ) = @_;

    my $contributions =
      $self->db_contributors->find( { contributor => $login } );

    my $profiles = {};

    while ( my $contrib = $contributions->next ) {
        my $project = $self->db_repositories->find_one(
            { uniq_name => $contrib->{project} } );

        next if $project->{size} == 0;
        my $total =
          int( ( $contrib->{contributions} / $project->{size} ) * 100 );
        $total ||= 1;
        $profiles->{ $contrib->{owner} } += $total;
    }

    foreach my $pr ( keys %$profiles ) {
        $self->db_edges->insert({
            source => $login,
            target => $pr,
            weight => $profiles->{$pr}
        });
    }
    $self->db_profiles->update(
        { login  => $login },
        { '$set' => { edges_done => true } },
    );
}

1;