/
room_occupancy.py
46 lines (35 loc) · 1.58 KB
/
room_occupancy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# This file is part of Indico.
# Copyright (C) 2002 - 2023 CERN
#
# Indico is free software; you can redistribute it and/or
# modify it under the terms of the MIT License; see the
# LICENSE file for more details.
from datetime import date
import click
from dateutil.relativedelta import relativedelta
from indico.modules.rb.models.locations import Location
from indico.modules.rb.models.rooms import Room
from indico.modules.rb.statistics import calculate_rooms_occupancy
from indico.util.string import natural_sort_key
from indico.web.flask.app import make_app
def _main(location):
yesterday = date.today() - relativedelta(days=1)
past_month = yesterday - relativedelta(days=29)
past_year = yesterday - relativedelta(years=1)
query = Room.query
if location:
query = query.join(Location).filter(Location.name.in_(location))
rooms = sorted(query, key=lambda r: natural_sort_key(r.location_name + r.full_name))
print('Month\tYear\tPublic?\tRoom')
for room in rooms:
print('{2:.2f}%\t{3:.2f}%\t{1}\t{0}'.format(room.full_name,
'Y' if room.is_public else 'N',
calculate_rooms_occupancy([room], past_month, yesterday) * 100,
calculate_rooms_occupancy([room], past_year, yesterday) * 100))
@click.command()
@click.option('--location', '-l', multiple=True, help='Filter by given location')
def main(location):
with make_app().app_context():
_main(location)
if __name__ == '__main__':
main()