1 | #!/bin/env perl |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use LATMOS::Accounts; |
---|
6 | use Getopt::Long; |
---|
7 | use Pod::Usage; |
---|
8 | use LATMOS::Accounts::Utils; |
---|
9 | use LATMOS::Accounts::Log; |
---|
10 | use GraphViz; |
---|
11 | |
---|
12 | =head1 NAME |
---|
13 | |
---|
14 | la-group -- Graph user/cells-department |
---|
15 | |
---|
16 | =head1 SYNOPSIS |
---|
17 | |
---|
18 | la-group [options] |
---|
19 | |
---|
20 | =cut |
---|
21 | |
---|
22 | GetOptions( |
---|
23 | 'c|config=s' => \my $config, |
---|
24 | 'b|base=s' => \my $base, |
---|
25 | 'help' => sub { pod2usage(0) }, |
---|
26 | ) or pod2usage(); |
---|
27 | |
---|
28 | =head1 OPTIONS |
---|
29 | |
---|
30 | =over 4 |
---|
31 | |
---|
32 | =item -c|--config configdir |
---|
33 | |
---|
34 | Use this configuration directory instead the default one |
---|
35 | |
---|
36 | =item -b|--base basename |
---|
37 | |
---|
38 | Query this specific base instead default. |
---|
39 | |
---|
40 | =back |
---|
41 | |
---|
42 | =cut |
---|
43 | |
---|
44 | my $LA = LATMOS::Accounts->new($config, noacl => 1); |
---|
45 | my $labase = $LA->base($base); |
---|
46 | $labase && $labase->load or die "Cannot load base"; |
---|
47 | |
---|
48 | my $g = GraphViz->new( |
---|
49 | layout => 'dot', |
---|
50 | overlap => 'false', |
---|
51 | rankdir => 1, |
---|
52 | width => 10, height => 8, |
---|
53 | ); |
---|
54 | |
---|
55 | open(my $h, '>', 'graph.png') or die 'cannot open graph'; |
---|
56 | |
---|
57 | #foreach ($labase->list_objects('user')) { |
---|
58 | # $g->add_node($_); |
---|
59 | #} |
---|
60 | |
---|
61 | my %users; |
---|
62 | my %cluster; |
---|
63 | |
---|
64 | sub add_user { |
---|
65 | my ($user) = @_; |
---|
66 | $users{$user} and return; |
---|
67 | my $ou = $labase->get_object('user', $user) or return; |
---|
68 | my $dpmt = $ou->get_attributes('department'); |
---|
69 | if ($dpmt) { |
---|
70 | $cluster{$dpmt} ||= { |
---|
71 | name => $dpmt, |
---|
72 | }; |
---|
73 | } |
---|
74 | $g->add_node($user, ($dpmt ? (cluster => $cluster{$dpmt}) : ())); |
---|
75 | 1; |
---|
76 | } |
---|
77 | |
---|
78 | $g->add_node('latmos'); |
---|
79 | foreach my $gr ($labase->list_objects('group')) { |
---|
80 | my $o = $labase->get_object('group', $gr); |
---|
81 | my $sutype = $o->get_c_field('sutype'); |
---|
82 | ($sutype || '') =~ /^(dpmt|cell)$/ or next; |
---|
83 | $g->add_node($gr,fillcolor => ($sutype eq 'dpmt' ? 'red' : 'green'), style => 'filled'); |
---|
84 | my $manager = $o->get_attributes('managedBy') || ''; |
---|
85 | warn "MANAGER $manager"; |
---|
86 | if ($manager) { |
---|
87 | add_user($manager); |
---|
88 | $g->add_edge('latmos', $gr); |
---|
89 | $g->add_edge($manager => $gr, weight => 3); |
---|
90 | } |
---|
91 | foreach ($o->get_attributes('member')) { |
---|
92 | $_ eq $manager and next; |
---|
93 | add_user($_); |
---|
94 | warn "$_ => $gr"; |
---|
95 | $g->add_edge($gr => $_, weight => 1); |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | print $h $g->as_png; |
---|
100 | close($h); |
---|