summary refs log tree commit diff
path: root/lib/githubexplorer/Gexf.pm
blob: f7e38cbc7ce3d4c5d0617aae57aeb2bcc012d2d4 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package githubexplorer::Gexf;

use Moose;
use XML::Simple;
use 5.010;

has schema => (is => 'ro', isa => 'Object', required => 1);
has id_edges => (is => 'rw', isa => 'Num', traits  => ['Counter'], default =>
0, handles => {inc_edges => 'inc'});

has graph => (
    is      => 'rw',
    isa     => 'HashRef',
    default => sub {
        my $graph = {
            gexf => {
                version => "1.1",
                meta    => { creator => ['linkfluence'] },
                graph   => {
                    type       => 'static',
                    attributes => {
                        class     => 'node',
                        type      => 'static',
                        attribute => [
                            {
                                id    => 0,
                                type  => 'float',
                                title => 'name'
                            },
                            {
                                id => 1,
                                type => 'string',
                                title => 'type',
                            },
                            {
                                id    => 2,
                                type  => 'float',
                                title => 'followers_count'
                            },
                            {
                                id    => 3,
                                type  => 'float',
                                title => 'following_count'
                            },
                            {
                                id => 4,
                                type => 'float',
                                title => 'forks',
                            },
                            {
                                id => 5,
                                type => 'string',
                                title => 'location',
                            },
                            {
                                id => 6,
                                type => 'float',
                                title => 'public_gist_count',
                            },
                            {
                                id => 7,
                                type => 'float',
                                title => 'public_repo_count',
                            },
                            {
                                id => 8,
                                type => 'string',
                                title => 'language',
                            },
                            {
                                id => 9,
                                type => 'string',
                                title => 'description',
                            },
                            {
                                id => 10,
                                type => 'float',
                                title => 'watchers',
                            }
                        ]
                    }
                }
            }
        };
    }
);

sub gen_gexf {
    my $self = shift;
    $self->profiles;
    #$self->repositories;
    say "total nodes : ".scalar (@{ $self->graph->{gexf}->{graph}->{nodes}->{node} });
    say "total edges : ".scalar (@{ $self->graph->{gexf}->{graph}->{edges}->{edge} });
    my $xml_out = XMLout( $self->graph, AttrIndent => 1, keepRoot => 1 );
    return $xml_out;
}

sub profiles {
    my $self     = shift;
    say "start profiles ...";
    my $profiles = $self->schema->resultset('Profiles')->search();

    while ( my $profile = $profiles->next ) {
        my $node = {
            id              => $profile->id,
            label           => $profile->login,
            attvalues => {
                attvalue => [
                    { for => 0, value => $profile->name},
                    { for => 1, value => "profile"},
                    { for => 2, value => $profile->followers_count},
                    { for => 3, value => $profile->following_count},
                    { for => 5, value => $profile->location},
                    { for => 6, value => $profile->public_gist_count},
                    { for => 7, value => $profile->public_repo_count},
                ]
            },
        };
        push @{ $self->graph->{gexf}->{graph}->{nodes}->{node} }, $node;
    }

    my $edges = $self->schema->resultset('Follow')->search();
    my $id    = 0;
    while ( my $edge = $edges->next ) {
        my $e = {
            source   => $edge->origin->id,
            target   => $edge->dest->id,
            id       => $self->inc_edges,
        };
        push @{ $self->graph->{gexf}->{graph}->{edges}->{edge} }, $e;
    }
    say " done";
}

sub repositories {
    my $self = shift;

    say "start repositories ...";
    my $repositories = $self->schema->resultset('Repositories')->search({fork => 0});
    while (my $repos = $repositories->next) {

        next if $repos->name =~ /dotfiles/i;
        # available in forks ?
        my $check_fork = $self->schema->resultset('Fork')->search({repos => $repos->id});
        next if $check_fork->count < 1;

        if (!grep {$_->{id} eq "repos_".$repos->name} @{$self->graph->{gexf}->{graph}->{nodes}->{node}}) {
            my $language = $self->schema->resultset('RepoLang')->search({repository => $repos->id}, {order_by => 'size'})->first;
            my $lang = $language ? $language->language->name : 'none';
            my $node = {
                id => "repos_".$repos->name,
                label => $repos->name,
                attvalues => {
                    attvalue => [
                        { for => 0,  value => $repos->name},
                        { for => 1,  value => "repository"},
                        { for => 4,  value => $repos->forks},
                        { for => 9,  value => $repos->description},
                        { for => 10, value => $repos->watchers},
                        { for => 8,  value => $lang},
                    ],
                },
            };
            push @{ $self->graph->{gexf}->{graph}->{nodes}->{node} }, $node;
        }
        my $e = {
            source   => $repos->id_profile->id,
            target   => "repos_".$repos->name,
            id       => $self->inc_edges,
        };
        push @{ $self->graph->{gexf}->{graph}->{edges}->{edge} }, $e;
    }

    my $forks = $self->schema->resultset('Fork')->search();

    while (my $fork = $forks->next) {
        next if $fork->repos->name =~ /dotfiles/i;
        my $e = {
            source   => $fork->profile->id,
            target   => "repos_".$fork->repos->name,
            id       => $self->inc_edges,
        };
        push @{ $self->graph->{gexf}->{graph}->{edges}->{edge} }, $e;
    }
    say " done";
}

1;