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.
21 # Sort beers by their availability
25 # 1st sorting priority
26 var map1
: HashMap[COMPARED, Comparable]
28 # 2nd sorting priority
29 var map2
: HashMap[COMPARED, Comparable]
32 redef fun compare
(a
, b
) do return if map1
[a
] == map1
[b
] then
34 else map1
[a
] <=> map1
[b
]
39 # Get the background for the date `self` of format `yyyy-mm-dd`
40 private fun date_to_back
: String
44 var m
= substring
(5, 2)
46 if [4..9].has
(month
) then return " "
51 var opts
= new OptionContext
52 var opt_columns
= new OptionInt("Number of columns for the graph", 70, "-c")
53 opts
.add_option
(opt_columns
)
59 var db_path
= "benitlux_sherbrooke.db"
60 if rest
.not_empty
then db_path
= rest
.first
61 var db
= new BenitluxDB.open
(db_path
)
66 print
"{beers.length} known beers"
69 var all_days
= db
.days
70 assert all_days
!= null
71 print
"{all_days.length} days, from {all_days.first} to {all_days.last}"
73 # Beers availability by days
74 var beer2days
= new HashMap[Beer, Array[String]]
76 var days
= db
.days
(beer
)
78 default_comparator
.sort days
79 beer2days
[beer
] = days
82 # Sort beers by their availability and first date of appearance
83 var availability
= new HashMap[Beer, Int]
84 var appearances
= new HashMap[Beer, String]
86 var days
= beer2days
[beer
]
87 if days
.not_empty
then
88 appearances
[beer
] = days
.first
89 availability
[beer
] = -days
.length
# Opposite for inverse sort
91 appearances
[beer
] = "err"
92 availability
[beer
] = 1
96 # Sort by availability then appearance
97 var sorter
: Comparator = new BeerComparator(availability
, appearances
)
103 var days
= beer2days
[beer
]
105 # Skip never-available beers, usually name errors
106 if days
.is_empty
then continue
108 var from
= days
.first
109 if from
== all_days
.first
then from
= " ... "
112 if to
== all_days
.last
then to
= " ... "
114 print
"- {days.length}\t{from} {to}\t{beer.name}: {beer.desc}"
117 # Sort by appearance then availability
118 sorter
= new BeerComparator(appearances
, availability
)
121 # Display the batch graph
122 print
"\nAvailability graph:"
124 # Compute `column_width` days from all the known days
125 var column_width
= opt_columns
.value
126 var days_sample
= [for i
in [1..column_width
[ do all_days
[i
*all_days
.length
/column_width
]]
127 var weeks_sample
= new Array[Array[String]]
129 # Gather columns headers for each month
130 var headers
= new Array[nullable String]
131 var iter
= all_days
.iterator
134 for day
in days_sample
do
136 var new_pre
= day
.substring
(0, 7)
138 if not day
.has_prefix
(pre
) then
140 else headers
.add
null
145 var week
= new Array[String]
146 weeks_sample
.add week
149 if item
== day
then break
155 # Draw the headers from top to bottom so they look like:
167 for header
in headers
do
168 if header
!= null then
176 var days
= beer2days
[beer
]
178 # Skip never-available beers, usually name errors
179 if days
.is_empty
then continue
181 # Print a line looking like: " ############ ###### -----########- Beer"
183 #var iter = days.iterator
184 for week
in weeks_sample
do
185 printn
if days
.has_all
(week
) then
187 else if days
.has_any
(week
) then
189 else week
.first
.date_to_back