def db_state0( starttime ): stime = DateTime(starttime) db = DBI(dbi='sybase', server='sybase', user='******', database='aca') query = """select * from cmd_states where datestart = (select max(datestart) from cmd_states where datestart < '%s')""" % stime.date state0 = db.fetchone( query ) return state0
def main(loadseg_rdb_dir, dryrun=False, test=False, dbi='sqlite', server='db_base.db3' ,database=None, user=None, verbose=False): """ Command Load Segment Table Updater Read RDB table from SKA arc iFOT events area and update load_segments table Meant to be run as a cront task with no arguments. Details: Reads most recent RDB file from arc data iFOT events load_segments directory. Checks loads in that file for overlap and prolonged separations Removes outdated table entries Inserts new and newly modified entries Note that dryrun mode does not show timelines which *would* be updated, as an update to the load_segments table must happen prior to get_timelines() """ dbh = DBI(dbi=dbi, server=server, database=database, user=user, verbose=verbose) ch = logging.StreamHandler() ch.setLevel(logging.WARN) if verbose: ch.setLevel(logging.DEBUG) log.addHandler(ch) if dryrun: log.info("LOAD_SEG INFO: Running in dryrun mode") loadseg_dir = loadseg_rdb_dir # get the loads from the arc ifot area all_rdb_files = glob.glob(os.path.join(loadseg_dir, "*")) rdb_file = max(all_rdb_files) log.debug("LOAD_SEG DEBUG: Updating from %s" % rdb_file) orig_rdb_loads = Ska.Table.read_ascii_table(rdb_file, datastart=3) ifot_loads = rdb_to_db_schema( orig_rdb_loads ) if len(ifot_loads): # make any scripted edits to the tables of parsed files to override directory # mapping import fix_tl_processing fix_tl_processing.repair(dbh) # make any scripted edits to the load segments table import fix_load_segments ifot_loads = fix_load_segments.repair(ifot_loads) max_timelines_id = dbh.fetchone( 'SELECT max(id) AS max_id FROM timelines')['max_id'] or 0 if max_timelines_id == 0 and test == False: raise ValueError("TIMELINES: no timelines in database.") update_loads_db( ifot_loads, dbh=dbh, test=test, dryrun=dryrun ) db_loads = dbh.fetchall("""select * from load_segments where datestart >= '%s' order by datestart """ % ( ifot_loads[0]['datestart'] ) ) update_timelines_db(loads=db_loads, dbh=dbh, max_id=max_timelines_id, dryrun=dryrun, test=test) log.removeHandler(ch)