Add character counts to session log

This commit is contained in:
Veronica Berglyd Olsen
2025-04-29 23:31:25 +02:00
parent 67d0b486e9
commit 53e60c7b59
3 changed files with 43 additions and 21 deletions
+29 -13
View File
@@ -79,29 +79,36 @@ class NWSessionLog:
return False return False
now = time() now = time()
iNovel, iNotes, _, _ = self._project.data.initCounts iWNovel, iWNotes, iCNovel, iCNotes = self._project.data.initCounts
cNovel, cNotes, _, _ = self._project.data.currCounts cWNovel, cWNotes, cCNovel, cCNotes = self._project.data.currCounts
iTotal = iNovel + iNotes iWTotal = iWNovel + iWNotes
wDiff = cNovel + cNotes - iTotal iCTotal = iCNovel + iCNotes
wDiff = cWNovel + cWNotes - iWTotal
cDiff = cCNovel + cCNotes - iCTotal
sTime = now - self._start sTime = now - self._start
logger.info("The session lasted %d sec and added %d words", int(sTime), wDiff) logger.info(
if sTime < 300 and wDiff == 0: "The session lasted %d sec and added %d words abd %d characters",
int(sTime), wDiff, cDiff
)
if sTime < 300 and (wDiff == 0 or cDiff == 0):
logger.info("Session too short, skipping log entry") logger.info("Session too short, skipping log entry")
return False return False
try: try:
if not sessFile.exists(): if not sessFile.exists():
with open(sessFile, mode="w", encoding="utf-8") as fObj: with open(sessFile, mode="w", encoding="utf-8") as fObj:
fObj.write(self.createInitial(iTotal)) fObj.write(self.createInitial(iWTotal))
with open(sessFile, mode="a+", encoding="utf-8") as fObj: with open(sessFile, mode="a+", encoding="utf-8") as fObj:
fObj.write(self.createRecord( fObj.write(self.createRecord(
start=formatTimeStamp(self._start), start=formatTimeStamp(self._start),
end=formatTimeStamp(now), end=formatTimeStamp(now),
novel=cNovel, novel=cWNovel,
notes=cNotes, notes=cWNotes,
idle=round(idleTime) idle=round(idleTime),
cnovel=cCNovel,
cnotes=cCNotes,
)) ))
except Exception: except Exception:
@@ -129,10 +136,19 @@ class NWSessionLog:
data = json.dumps({"type": "initial", "offset": total}) data = json.dumps({"type": "initial", "offset": total})
return f"{data}\n" return f"{data}\n"
def createRecord(self, start: str, end: str, novel: int, notes: int, idle: int) -> str: def createRecord(
self, start: str, end: str, novel: int, notes: int, idle: int,
cnovel: int = 0, cnotes: int = 0,
) -> str:
"""Low level function to create a log record.""" """Low level function to create a log record."""
data = json.dumps({ data = json.dumps({
"type": "record", "start": start, "end": end, "type": "record",
"novel": novel, "notes": notes, "idle": idle, "start": start,
"end": end,
"novel": novel,
"notes": notes,
"cnovel": cnovel,
"cnotes": cnotes,
"idle": idle,
}) })
return f"{data}\n" return f"{data}\n"
+8 -6
View File
@@ -43,8 +43,8 @@ def testCoreSessions_Main(monkeypatch, mockGUI, fncPath):
assert isinstance(logFile, Path) assert isinstance(logFile, Path)
# Set some mock word counts # Set some mock word counts
project.data.setInitCounts(50, 60) project.data.setInitCounts(50, 60, 500, 600)
project.data.setCurrCounts(160, 150) project.data.setCurrCounts(160, 150, 1600, 1500)
# The project init should already have created the session # The project init should already have created the session
sessLog = project.session sessLog = project.session
@@ -71,17 +71,19 @@ def testCoreSessions_Main(monkeypatch, mockGUI, fncPath):
assert records[1]["type"] == "record" assert records[1]["type"] == "record"
assert records[1]["novel"] == 160 assert records[1]["novel"] == 160
assert records[1]["notes"] == 150 assert records[1]["notes"] == 150
assert records[1]["cnovel"] == 1600
assert records[1]["cnotes"] == 1500
assert records[1]["idle"] == 1 # Should be rounded to full seconds assert records[1]["idle"] == 1 # Should be rounded to full seconds
# Adding another record without changing word count should do nothing # Adding another record without changing word count should do nothing
project.data.setInitCounts(160, 150) project.data.setInitCounts(160, 150, 1600, 1500)
project.data.setCurrCounts(160, 150) project.data.setCurrCounts(160, 150, 1600, 1500)
assert sessLog.appendSession(1.6) is False assert sessLog.appendSession(1.6) is False
assert len(list(sessLog.iterRecords())) == 2 assert len(list(sessLog.iterRecords())) == 2
# But adding when count has changed should # But adding when count has changed should
project.data.setInitCounts(160, 150) project.data.setInitCounts(160, 150, 1600, 1500)
project.data.setCurrCounts(270, 240) project.data.setCurrCounts(270, 240, 2700, 2400)
sessLog._start -= 350.0 # Backdate the session start to allow logging sessLog._start -= 350.0 # Backdate the session start to allow logging
assert sessLog.appendSession(1.6) is True assert sessLog.appendSession(1.6) is True
records = list(sessLog.iterRecords()) records = list(sessLog.iterRecords())
+6 -2
View File
@@ -435,8 +435,8 @@ def testCoreStorage_OldFormatConvert(monkeypatch, mockGUI, fncPath):
sessLogOld.write_text(( sessLogOld.write_text((
"# Offset 150\n" "# Offset 150\n"
"# Start Time End Time Novel Notes Idle\n" "# Start Time End Time Novel Notes Idle\n"
"2021-02-02 02:02:02 2021-02-02 03:03:03 200 200 10\n" "2021-02-02 02:02:02 2021-02-02 03:03:03 200 200 10\n"
"2021-03-03 03:03:03 2021-03-03 04:04:04 300 300 20\n" "2021-03-03 03:03:03 2021-03-03 04:04:04 300 300 20\n"
), encoding="utf-8") ), encoding="utf-8")
assert sessLogOld.exists() is True assert sessLogOld.exists() is True
@@ -496,6 +496,8 @@ def testCoreStorage_OldFormatConvert(monkeypatch, mockGUI, fncPath):
"end": "2021-02-02 03:03:03", "end": "2021-02-02 03:03:03",
"novel": 200, "novel": 200,
"notes": 200, "notes": 200,
"cnovel": 0,
"cnotes": 0,
"idle": 10, "idle": 10,
} }
assert data[2] == { assert data[2] == {
@@ -504,6 +506,8 @@ def testCoreStorage_OldFormatConvert(monkeypatch, mockGUI, fncPath):
"end": "2021-03-03 04:04:04", "end": "2021-03-03 04:04:04",
"novel": 300, "novel": 300,
"notes": 300, "notes": 300,
"cnovel": 0,
"cnotes": 0,
"idle": 20, "idle": 20,
} }