# /// script
# requires-python = ">=3.10"
# dependencies = []
# ///
import re
import sys

parser = re.compile(r'celery_worker-(?P<workerid>\d+?)\s+\|\s+\[(?P<date>.*?) (?P<time>.*?): (?P<tag>.*?)\] Task (?P<taskname>.*?)\[(?P<taskid>.*?)\] (?P<msg>.*?)$')

def main() -> None:
    path_in = sys.argv[1] if len(sys.argv) > 1 else 'testdata.txt'
    path_out = sys.argv[2] if len(sys.argv) > 2 else 'out.txt'
    with open(path_in, 'r') as fin:
        with open(path_out, 'w') as fout:
            fout.write("workerid,datetime,tag,taskname,taskid,msg,msgtime\n")
            while True:
                line = fin.readline()
                if line == "":
                    break
                if not "Task" in line:
                    continue
                m = parser.search(line)
                if m is None:
                    continue
                d = m.groupdict()
                msgsplit = d["msg"].split(' ')
                msg = msgsplit[0]
                if msg == 'succeeded':
                    msgtime = msgsplit[2][:-1][:7]
                else:
                    msgtime= ""
                out = f'{d["workerid"]},{d["date"]}T{d["time"].replace(",",".")}Z,{d["tag"]},{d["taskname"]},{d["taskid"]},{msg},{msgtime}\n'
                fout.write(out)
            pass
    pass


if __name__ == "__main__":
    main()
