#!/usr/bin/python3 helpText = """del_lag.py Remove data from the local database that should have been lagged, but somehow got added anyway. A one-off script to fix a (hopefully) one-off problem. """ import sys sys.path.append("../webexec") import datetime, hydro_lib, time lagged = {} with open( 'pathnames.list', 'r' ) as txt: for line in txt: if line[0] == "#": continue try: ( pathname, units, lag ) = line.split( '\t' ) lag = int( lag ) if lag > 0: lagged[pathname] = lag except: pass print( "Got {} lagged series".format( len( lagged ) ) ) hydro_lib.connect( '../data/hydro.db' ) cur = hydro_lib.dbconn.cursor() now = datetime.datetime.now() for tsid in lagged: lag = lagged[tsid] series = hydro_lib.rec( [], table="seriescatalog", keys=hydro_lib.schemas["seriescatalog"]) hash = series.get( cur, "name", tsid )["tablename"] if hash == '': print( "Unable to delete from {} - no matching series".format( tsid ) ) continue cutoff = time.mktime( ( now - datetime.timedelta( days=lag ) ).timetuple() ) sql = "DELETE FROM {} WHERE DateTime >= {}".format( hash, cutoff ) print( "{}: {}".format( tsid, sql ) ) try: cur.execute( sql ) except Exception as e: print( "Unable to delete from {}: {}".format( tsid, e ) ) hydro_lib.dbconn.commit()