Skip to content
Snippets Groups Projects
Select Git revision
  • main
1 result

README.md

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    tmss_create_standalone_reservations 4.01 KiB
    #!/usr/bin/env python3
    
    import sys
    import argparse
    from datetime import datetime, date, timedelta, time
    from lofar.sas.tmss.client.tmss_http_rest_client import TMSSsession
    
    # by default, we start creating the reservation(s) for next tuesday and onwards.
    today = date.today()
    tuesday = today + timedelta(days=1-today.weekday()) # tuesday is weekday=1
    next_tuesday = tuesday + timedelta(days=7) if tuesday < today else tuesday
    
    parser = argparse.ArgumentParser(description="Create 1 (default) or more reservation(s) for the Standalone-mode for international LOFAR station(s).", add_help=True)
    parser.add_argument("-c", "--count",
                        type=int,
                        help="create this number of reservations (one per week, starting at --start_date), default: 1",
                        default=1)
    parser.add_argument('-s', '--start_date',
                        help="create the first upcoming standalone reservation (of the given --nr_of_units) starting at this date (format=\"YYYY-MM-DD\"), default=\"%s\""%(next_tuesday.strftime("%Y-%m-%d"),),
                        default=next_tuesday.strftime("%Y-%m-%d"))
    parser.add_argument('-t', '--time',
                        help="starttime of the reservation(s) (format=\"HH:MM\" in UTC), default=\"06:45\"\n"
                             "guideline 06:45 in summer, 07:45 in winter. 15min to switch, station is handed over at the hour.",
                        default="06:45")
    parser.add_argument('-d', '--duration',
                        help="the duration (in hours) of the reservation. Default is 32hr: 15min switching at start, +31hr standalone time, +45min switching at end",
                        type=float,
                        default=32)
    parser.add_argument('-S', '--stations',
                        help="specify stations to reserve, either by group name, or as comma-seperated-station-names. Default: \"International\"",
                        default="International")
    parser.add_argument('-R', '--rest_api_credentials', type=str,
                        help='TMSS django REST API credentials name, default: TMSSClient',
                        default='TMSSClient')
    args = parser.parse_args()
    
    
    with TMSSsession.create_from_dbcreds_for_ldap(args.rest_api_credentials) as client:
        for counter in range(args.count):
            start_timestamp = datetime.strptime(args.start_date + " " + args.time, "%Y-%m-%d %H:%M") + timedelta(days=7*counter)
            stop_timestamp = start_timestamp + timedelta(hours=args.duration)
    
            if args.stations != "International" and ',' not in args.stations:
                stations_schema = client.get_common_schema_template('stations')
                groups = stations_schema['schema']['definitions']['station_group']['anyOf']
                try:
                    selected_group = next(g for g in groups if g['title'].lower() == args.stations.lower())
                    stations = selected_group['properties']['stations']['enum'][0]
                except StopIteration:
                    print("No such station group: '%s'" % (args.stations,))
                    print("Available station groups: %s" % (', '.join([g['title'] for g in groups]),))
                    exit(1)
            elif ',' in args.stations:
                stations = [s.strip() for s in args.stations.split(',')]
            else:
                # defaults from the template: International
                stations = None
    
            reservation = client.create_reservation_for_strategy_template(start_time=start_timestamp,
                                                                          stop_time=stop_timestamp,
                                                                          reservation_name="ILT stations in local mode",
                                                                          strategy_name="ILT stations in local mode",
                                                                          stations=stations)
    
            # adapt api url to frontend view url, and print results
            view_url = reservation['url'].replace('api/reservation/', 'reservation/view/')
            print("created reservation id=%d name='%s' url: %s" % (reservation['id'], reservation['name'], view_url))