#!/usr/bin/env python import time import sys import elementtree.ElementTree as ET def repair(tree, newupdate): step = int(tree.findtext("step")) newupdate = newupdate - (newupdate % step) elt = tree.find("lastupdate") lastupdate = int(elt.text) elt.text = str(newupdate) diffupdate = lastupdate - newupdate sys.stderr.write("Setting to new lastupdate: %i (going %i in the past)\n" % (newupdate, diffupdate)) sys.stderr.write("Lastupdate: %i\n" % lastupdate) sys.stderr.write("Step: %i\n" % step) assert newupdate < lastupdate, "This script is not made for going in the future" rras = tree.findall("rra") for rra in rras: pdpperrow = int(rra.findtext("pdp_per_row")) substep = pdpperrow * step db = rra.find("database") rowcount = len(db) laststamp = lastupdate - (lastupdate % substep) firststamp = laststamp - (rowcount-1) * substep sys.stderr.write("RRA:: pdp_per_row=%i (substep=%i) rowcount=%i firststamp=%i laststamp=%i\n" % (pdpperrow, substep, rowcount, firststamp, laststamp)) #Data SourceS dss = rra.findall("cdp_prep/ds") dscount = len(dss) sys.stderr.write(" dscount=%i\n" % (dscount)) unkrow = ET.Element("row") for ds in dss: unkvalue = ds.findtext("unknown_datapoints") subv = ET.SubElement(unkrow,"v") subv.text = unkvalue oldrows = list(db) newrows = [] if firststamp < newupdate: assert laststamp > newupdate, "Trying to go a in kinda future past ..." keepcount = ((newupdate-firststamp) // substep) + 1 newrows = oldrows[:keepcount] else: #Otherwise, there's nothing to keep keepcount = 0 #Now, fill-in missing lines in the past misscount = rowcount - len(newrows) sys.stderr.write(" tokeep=%i missing=%i\n" % (keepcount, misscount)) for i in range(misscount): newrows.insert(0, unkrow) #And write it del db[:] for row in newrows: db.append(row) if __name__ == "__main__": tree = ET.parse(sys.stdin) now = int(time.time()) repair(tree, now) tree.write(sys.stdout)