1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
19 # Sort beers by their availability
23 # 1st sorting priority
24 var map1
: HashMap[COMPARED, Comparable]
26 # 2nd sorting priority
27 var map2
: HashMap[COMPARED, Comparable]
30 redef fun compare
(a
, b
) do return if map1
[a
] == map1
[b
] then
32 else map1
[a
] <=> map1
[b
]
37 # Get the background for the date `self` of format `yyyy-mm-dd`
38 private fun date_to_back
: String
42 var m
= substring
(5, 2)
44 if [4..9].has
(month
) then return " "
50 var db_path
= "benitlux_sherbrooke.db"
51 var db
= new DB.open
(db_path
)
56 print
"{beers.length} known beers"
59 var all_days
= db
.days
60 assert all_days
!= null
61 print
"{all_days.length} days, from {all_days.first} to {all_days.last}"
63 # Beers availability by days
64 var beer2days
= new HashMap[Beer, Array[String]]
66 var days
= db
.days
(beer
)
68 default_comparator
.sort days
69 beer2days
[beer
] = days
72 # Sort beers by their availability and first date of appearance
73 var availability
= new HashMap[Beer, Int]
74 var appearances
= new HashMap[Beer, String]
76 var days
= beer2days
[beer
]
77 if days
.not_empty
then
78 appearances
[beer
] = days
.first
79 availability
[beer
] = -days
.length
# Opposite for inverse sort
81 appearances
[beer
] = "err"
82 availability
[beer
] = 1
86 # Sort by availability then appearance
87 var sorter
: Comparator = new BeerComparator(availability
, appearances
)
93 var days
= beer2days
[beer
]
95 # Skip never-available beers, usually name errors
96 if days
.is_empty
then continue
99 if from
== all_days
.first
then from
= " ... "
102 if to
== all_days
.last
then to
= " ... "
104 print
"- {days.length}\t{from} {to}\t{beer.name}: {beer.desc}"
107 # Sort by appearance then availability
108 sorter
= new BeerComparator(appearances
, availability
)
111 # Display the batch graph
112 print
"\nAvailability graph:"
114 # Compute `column_width` days from all the known days
115 var column_width
= 70
116 var days_sample
= [for i
in column_width
.times
do all_days
[i
*all_days
.length
/column_width
]]
118 # Gather columns headers for each month
119 var headers
= new Array[nullable String]
121 for day
in days_sample
do
122 var new_pre
= day
.substring
(0, 7)
124 if not day
.has_prefix
(pre
) then
126 else headers
.add
null
131 # Draw the headers from top to bottom so they look like:
143 for header
in headers
do
144 if header
!= null then
152 var days
= beer2days
[beer
]
154 # Skip never-available beers, usually name errors
155 if days
.is_empty
then continue
157 # Print a line looking like: " ############ ###### -----########-: Beer"
158 for s
in days_sample
do printn
if days
.has
(s
) then "#" else s
.date_to_back
159 print
": {beer.name}"