]> gitweb.factorcode.org Git - factor.git/blob - extra/rosetta-code/text-processing/max-licenses/max-licenses.factor
4bf895ccbaae11281b71416b70156cd0cabb3cc6
[factor.git] / extra / rosetta-code / text-processing / max-licenses / max-licenses.factor
1 ! Copyright (c) 2012 Anonymous
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors http.client io io.encodings.ascii io.files
4 io.files.temp kernel math math.parser sequences
5 splitting urls ;
6 IN: rosetta-code.text-processing.max-licenses
7
8 ! http://rosettacode.org/wiki/Text_processing/Max_licenses_in_use
9
10 ! A company currently pays a fixed sum for the use of a
11 ! particular licensed software package. In determining if it has a
12 ! good deal it decides to calculate its maximum use of the
13 ! software from its license management log file.
14
15 ! Assume the software's licensing daemon faithfully records a
16 ! checkout event when a copy of the software starts and a checkin
17 ! event when the software finishes to its log file. An example of
18 ! checkout and checkin events are:
19
20 !  License OUT @ 2008/10/03_23:51:05 for job 4974
21 !  ...
22 !  License IN  @ 2008/10/04_00:18:22 for job 4974
23
24 ! Save the 10,000 line log file from here into a local file then
25 ! write a program to scan the file extracting both the maximum
26 ! licenses that were out at any time, and the time(s) at which
27 ! this occurs.
28
29 TUPLE: maxlicense max-count current-count times ;
30
31 <PRIVATE
32
33 : <maxlicense> ( -- max ) -1 0 V{ } clone \ maxlicense boa ; inline
34
35 : out? ( line -- ? ) "OUT" subseq-index? ; inline
36
37 : line-time ( line -- time ) split-words harvest fourth ; inline
38
39 : update-max-count ( max -- max' )
40     dup [ current-count>> ] [ max-count>> ] bi >
41     [ dup current-count>> >>max-count V{ } clone >>times ] when ;
42
43 : (inc-current-count) ( max ? -- max' )
44     [ [ 1 + ] change-current-count ]
45     [ [ 1 - ] change-current-count ]
46     if
47     update-max-count ; inline
48
49 : inc-current-count ( max ? time -- max' time )
50     [ (inc-current-count) ] dip ;
51
52 : current-max-equal? ( max -- max ? )
53     dup [ current-count>> ] [ max-count>> ] bi = ;
54
55 : update-time ( max time -- max' )
56     [ current-max-equal? ] dip
57     swap
58     [ [ suffix ] curry change-times ] [ drop ] if ;
59
60 : split-line ( line -- ? time ) [ out? ] [ line-time ] bi ;
61
62 : process ( max line -- max ) split-line inc-current-count update-time ;
63
64 MEMO: mlijobs ( -- lines )
65     "mlijobs.txt" temp-file dup file-exists? [
66         URL" http://rosettacode.org/resources/mlijobs.txt"
67         over download-to
68     ] unless ascii file-lines ;
69
70 PRIVATE>
71
72 : find-max-licenses ( -- max )
73     mlijobs <maxlicense> [ process ] reduce ;
74
75 : print-max-licenses ( max -- )
76     [ times>> ] [ max-count>> ] bi
77     "Maximum simultaneous license use is " write
78     number>string write
79     " at the following times: " print
80     [ print ] each ;