record_reader.py 765 B

123456789101112131415161718192021222324252627282930
  1. import json
  2. import logging
  3. from typing import Generator
  4. from oteldatareplay.resource_span_record import ResourceSpanRecord
  5. logger = logging.getLogger(__name__)
  6. class RecordReader(object):
  7. def __init__(self, file_path):
  8. self.file_path = file_path
  9. self.fp = open(file_path, 'r')
  10. self.running = True
  11. def reset(self):
  12. self.fp.seek(0)
  13. def records_generator(self) -> Generator[ResourceSpanRecord, None, None]:
  14. line = self.fp.readline()
  15. while line and self.running:
  16. ob = json.loads(line)
  17. yield ResourceSpanRecord(ob)
  18. line = self.fp.readline()
  19. return
  20. def close(self):
  21. self.running = False
  22. self.fp.close()
  23. logger.info("Player closed")