summaryrefslogtreecommitdiff
path: root/lib/GitHub/Collector/Command/edges.pm
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--lib/GitHub/Collector/Command/edges.pm67
1 files changed, 67 insertions, 0 deletions
diff --git a/lib/GitHub/Collector/Command/edges.pm b/lib/GitHub/Collector/Command/edges.pm
new file mode 100644
index 0000000..4ffe0c8
--- /dev/null
+++ b/lib/GitHub/Collector/Command/edges.pm
@@ -0,0 +1,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;