blib/lib/Morale.pm

filter: Statementstmt 27.8% 22 / 79 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 50.0% 6 / 12 Podpod 0.0% 0 / 6 totaltotal 22.5% 28 / 124 CC 16 SCAR 27.0
CC
16
Cov
21%
validate_morale
30.0
morale_file
20.0
get_all_morales
20.0
CRAP 14.8
↑ Summary
1 #
2 # Morale.pm
3 #
4 # A Perl module for dealing with morale files and calculating
5 # company morale.
6 #
7 # TODO: Change the calculation of company morale to a timed
8 # event, instead of doing it real-time, since it could take
9 # a while for large user sets and/or remote home directories.
10 #
11 # TODO: Or, at least cache the current results for N seconds,
12 # not going back to the source until that expires.
13 #
14 # Copyright (C) 1999-2001 Gregor N. Purdy. All rights reserved.
15 #
16  
17 package Morale;
18  
19 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶ use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 1 $VERSION = 0.002;
26 1 @ISA = qw(Exporter);
27 1 @EXPORT = qw(&get_morale &set_morale &calc_morale);
28 1 %EXPORT_TAGS = ( );
29 1 @EXPORT_OK = qw(&morale_file);
30 }
31 1 ▶use vars qw($top $my_scale $co_scale $bar $my_morale $co_morale);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34  
35 1 my %morales;
36  
37  
38 #
39 # morale_file()
40 #
41  
42 sub morale_file
43 0 ▶{
Subroutine morale_file: not called CC=4 SCAR=30.0
Pod undocumented
44 0 my ($user) = @_;
45 0 my $dir;
46 my @check;
47  
48 0 ▶ if (!defined($user)) { $user = (getpwuid($>))[0]; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $user) 0 0 0
49  
50 0 $dir = (getpwnam($user))[7];
51
52 0 push @check, "/var/morale/$user";
53 0 push @check, "$dir/.morale";
54  
55 0 ▶ foreach (@check) { if (-r $_) { return $_; } }
Branch: 1 branch
BranchTrueFalseTotal
if (-r $_) 0 0 0
56  
57 0 return "$dir/.morale";
58 }
59  
60  
61 #
62 # validate_morale()
63 #
64  
65 sub validate_morale
66 0 ▶{
Subroutine validate_morale: not called CC=5 SCAR=34.0
Pod undocumented
67 0 my ($morale) = @_;
68  
69 0 ▶ if (defined($morale)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $morale) 0 0 0
70 0 $morale =~ s/^\s*(.*)\s*$/$1/;
71  
72 0 ▶ if (!($morale =~ m/^[0-9]+$/)) { undef $morale; }
Branch: 2 branches
BranchTrueFalseTotal
if (not $morale =~ /^[0-9]+$/) { } 0 0 0
elsif ($morale < 0 or $morale > 100) { } 0 0 0
73 0 ▶ elsif (($morale < 0) or ($morale > 100)) { undef $morale; }
Condition0%0/3
$morale < 0 or $morale > 100
l!l&&r!l&&!r
000
MC/DC0%0/2
$morale < 0 or $morale > 100
$morale < 0 $morale > 100
Truth table0%0/3
A or B
A= $morale < 0B= $morale > 100
ABresult
1X1
011
000
74 }
75  
76 0 return $morale;
77 }
78  
79  
80 #
81 # set_morale()
82 #
83  
84 sub set_morale
85 0 ▶{
Subroutine set_morale: not called CC=3 SCAR=24.8
Pod undocumented
86 0 my ($morale, $user) = @_;
87 0 my $file = morale_file($user);
88  
89 0 $morale = validate_morale($morale);
90  
91 0 ▶ if (!defined($morale)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $morale) 0 0 0
92 0 system "rm -f $file";
93 0 return;
94 }
95  
96 0 ▶ if (!open(MORALE, ">$file")) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open MORALE, ">$file") 0 0 0
97 0 carp "Couldn't open file `$file' for writing.";
98 0 return;
99 }
100  
101 0 print MORALE "$morale\n";
102 0 close MORALE;
103 }
104  
105  
106 #
107 # get_morale()
108 #
109 # Returns the morale for the given user, or the current user if none given.
110 #
111  
112 sub get_morale
113 0 ▶{
Subroutine get_morale: not called CC=2 SCAR=17.9
Pod undocumented
114 0 my ($user) = @_;
115  
116 0 my $file = morale_file($user);
117 0 my $morale;
118  
119 0 ▶ open MORALE, "<$file"
Branch: 1 branch
BranchTrueFalseTotal
open MORALE, "<$file" or return undef 0 0 0
120 or return undef;
121 0 $morale = <MORALE>;
122 0 close MORALE;
123  
124 0 chomp $morale;
125  
126 0 return validate_morale($morale);
127 }
128  
129  
130 #
131 # get_all_morales()
132 #
133 # Returns the intializer for a hash of user-morale associations.
134 #
135  
136 sub get_all_morales
137 0 ▶{
Subroutine get_all_morales: not called CC=4 SCAR=30.0
Pod undocumented
138 0 my $user;
139 my @users;
140  
141 0 %morales = ( );
142  
143 0 setpwent();
144 0 ▶ while ($user = getpwent) { push @users, $user; }
Branch: 1 branch
BranchTrueFalseTotal
if ($user = getpwent) 0 0 0
145 0 endpwent();
146
147 0 foreach $user (@users) {
148 # print STDERR "$user: ";
149 0 my $user_morale = get_morale($user);
150  
151 0 ▶ if (defined($user_morale)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $user_morale) { } 0 0 0
152 # print STDERR "$user_morale\n";
153 0 $morales{$user} = $user_morale;
154 } else {
155 # print STDERR "<undef>\n";
156 }
157 }
158  
159 0 return %morales;
160 }
161  
162  
163 #
164 # calc_morale()
165 #
166  
167 sub calc_morale
168 0 ▶{
Subroutine calc_morale: not called CC=3 SCAR=24.8
Pod undocumented
169 0 my $total_morale = 0;
170 0 my $count_morale = 0;
171  
172 0 get_all_morales();
173  
174 0 foreach (sort keys %morales) {
175 # print STDERR "$_: $morales{$_}\n";
176 0 $total_morale += $morales{$_};
177 0 $count_morale ++;
178 }
179  
180 0 ▶ if ($count_morale < 1) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
if ($count_morale < 1) { } 0 0 0
181 0 else { return $total_morale / $count_morale; }
182 }
183  
184  
185 #
186 # Return a true value:
187 #
188  
189 1 1;
190  
191  
192 #
193 # End of file.
194 #
195  
196 =pod
197  
198  
199 =head1 NAME
200  
201 Morale - Perl module for managing individual and calculating group morale.
202  
203  
204 =head1 SYNOPSIS
205  
206 use Morale;
207 set_morale($morale, $user); # Named user
208 set_morale($morale); # Current user
209 get_morale($user); # Named user
210 get_morale(); # Current user
211 calc_morale();
212 morale_file($user); # Named user
213 morale_file(); # Current user
214  
215  
216 =head1 DESCRIPTION
217  
218 This module exists in its current form primarily to support the tkmorale
219 program. See that program for example usage.
220  
221  
222 =head1 HISTORICAL NOTE
223  
224 This section contributed by E. Denning ``Denny'' Dahl.
225  
226 1999-06-24
227  
228 I'll never forget my interview at Thinking Machines. It took place
229 December of 1987, and I remember the trek from the Royal Sonesta
230 through the construction zone that was the east end of Cambridge,
231 in the snow and mud past the Athenaeum Building to the Carter Ink
232 building which housed TMC. Not that we were ever supposed to
233 abbreviate Thinking Machines Corporation to TMC, but I digress.
234  
235 The salient point about the interview is that I'd never come across
236 such a bright, enthusiastic and motivated bunch of people before.
237 I had worked in high energy physics and then neural networks, and
238 had come across plenty of people who were off the charts in terms
239 of intelligence. But these people at TMC were also having fun!
240 Folks had toys in their offices, they had hot-wired matrix boards,
241 they were doing object technology, they knew what Ramsey theory
242 was, and they had the coolest looking box on the block.
243  
244 After I joined, I was one of the people who had to answer the
245 question "What are all the blinking lights for?". Every time
246 we heard the question, we would give a different answer. Sometimes
247 it was a diagnostics hack, sometimes a performance feedback device,
248 sometimes purely a marketing ploy. But the blinking red lights on
249 the sides on the CM-2 captured the spirit of the place. It was like
250 a Christmas tree!
251  
252 We even have a picture of my two-year old daughter in an angelic
253 little dress reaching out to touch the side of the big black cube.
254 Almost a comic riff on Kubrick's ape and monolith image. But the
255 point you can see I'm trying to make is that I and practically
256 everyone else who worked at Thinking Machines had a deep love for
257 what we were doing there. We were trying to invent a new paradigm
258 for computation. That meant new hardware, new operating systems,
259 new languages, new everything. What could be more fun?
260  
261 This build-up made the let-down that much more painful. When I
262 joined, the company had around 150 people. It grew to more than
263 500 before the structural problems really became evident. And
264 evident is a relative term. I am sure that everyone who worked
265 there had a different impression of the beginning of the end, and
266 we all had different threshholds for holding on to our shared
267 belief that we could change the world. But the cracks started,
268 and began to widen, and eventually became too big to ignore.
269  
270 So morale started going south. I was concerned, and of course
271 always up for learning a little new technology and doing some
272 inspired hacking, so I decide that Tcl & Tk would be a good
273 vehicle for my latest greatest idea: Xmorale.
274  
275 The notion was to build a wicked simple GUI that had a single
276 slider to allow the user to input his or her morale, along with
277 a submit button, and a second slider to show the company average
278 morale. After I got conversant with Tcl & Tk (not too long) I
279 hacked together Xmorale. Perhaps I could find the source code
280 if I did enough detective work, but the essential spirit of the
281 code was quite simple.
282  
283 I used a .morale file in everyone's home directory to store each
284 individual's contribution to the global company morale. Each
285 .morale file contained a randomly generated but unique key, and
286 the global company morale table just contained entries that mapped
287 key values to morale values. The security was not ultra-tight.
288 Each person's .morale file was permission 600, but of course
289 anyone with root privileges could figure out what your personal
290 morale was.
291  
292 At any rate, the idea took off like wildfire. Within a few days
293 of release, a significant fraction of TMC'ers had submitted their
294 morales. Other hackers took up the idea and developed tools to
295 record the company morale automatically and produce time series
296 graphs. There were refinements and discussions and FAQ's and it
297 was a great time.
298  
299 Of course, the most gratifying aspect of Xmorale is that it made
300 the company management very uncomfortable. There is nothing like
301 having a staff meeting, and seeing the global company morale drop
302 precipitously in the hour following the staff meeting to let you
303 know how you are doing. So needless to say, there was vague pressure
304 to discontinue the service. But once an idea like this is born,
305 you cannot suppress it. This was social engineering at its finest.
306  
307 I wish I could say that Xmorale had a real impact on what happened
308 to TMC. Who knows? Maybe things would have been slightly different
309 without it. But after entering Chapter 11, TMC finally emerged a
310 far different and smaller beast. Recently it has been sold to
311 Oracle. And I rather doubt Larry E. would take kindly to the notion
312 of a database storing a time history of Xmorale, hour by hour, at
313 Oracle. But you never know...
314  
315 Here I thought this amusing hack would rest, until I had lunch one day
316 with one Gregor Purdy, erstwhile consultant and man about town. I told
317 him about Xmorale, and within 24 hours he had implemented a version
318 completely from scratch using late 1990's technology, by which I mean
319 Perl. Since I view this hack to be a truly subversive piece of
320 software, I can only say that I hope it spreads as far and wide as possible.
321 Deploy this at your own place of work, and then stand back to admire
322 the consequences!
323  
324  
325 =head1 AUTHOR
326  
327 Gregor N. Purdy E<lt>gregor@focusresearch.comE<gt>
328  
329  
330 =head1 COPYRIGHT
331  
332 Copyright (C) 1999-2001 Gregor N. Purdy. All rights reserved.
333  
334 This program is free software; you can redistribute it and/or
335 modify it under the same terms as Perl itself.
336  
337  
338 =cut
339  
340 #
341 # End of file.
342 #
343  
↑ Summary