comparison weather_server/locations.py @ 31:9bc3687e1e5e

logfile: Add an index, and don't keep everything in RAM. - Adds index BSON file, updated upon writing. - Limits amount of data in RAM. - Gracefully handles writes that don't update index.
author Paul Fisher <paul@pfish.zone>
date Tue, 07 Jul 2020 19:51:30 -0400
parents 7def5611895b
children 2f3473416c11
comparison
equal deleted inserted replaced
30:c760ab7f93c2 31:9bc3687e1e5e
46 e['ingest_time'] = timestamp 46 e['ingest_time'] = timestamp
47 self.logger.write_rows(entries) 47 self.logger.write_rows(entries)
48 48
49 @property 49 @property
50 def entries(self) -> t.Iterable[t.Dict[str, object]]: 50 def entries(self) -> t.Iterable[t.Dict[str, object]]:
51 return self.logger.data 51 return self.logger.cached_data
52 52
53 def latest(self) -> t.Optional[types.Reading]: 53 def latest(self) -> t.Optional[types.Reading]:
54 most_recent = reversed(self.logger.data) 54 most_recent = reversed(self.logger.cached_data)
55 for entry in most_recent: 55 for entry in most_recent:
56 try: 56 try:
57 return types.Reading.from_dict(entry) 57 return types.Reading.from_dict(entry)
58 except KeyError: 58 except KeyError:
59 pass # go to the older one. 59 pass # go to the older one.