Add Page Calculations

This commit is contained in:
minermaniac447 2023-02-08 16:39:28 -05:00 committed by GitHub
parent 9885035ac8
commit 759b4d949c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -5,19 +5,22 @@ import moment from "moment";
import { import {
getCharacterCount, getCharacterCount,
getSentenceCount, getSentenceCount,
getPageCount,
getWordCount, getWordCount,
} from "../utils/StatUtils"; } from "../utils/StatUtils";
export default class StatsManager { export default class StatsManager {
private vault: Vault; private vault: Vault;
private workspace: Workspace; private workspace: Workspace;
private plugin: Plugin;
private vaultStats: VaultStatistics; private vaultStats: VaultStatistics;
private today: string; private today: string;
public debounceChange; public debounceChange;
constructor(vault: Vault, workspace: Workspace) { constructor(vault: Vault, workspace: Workspace, plugin: Plugin) {
this.vault = vault; this.vault = vault;
this.workspace = workspace; this.workspace = workspace;
this.plugin = plugin;
this.debounceChange = debounce( this.debounceChange = debounce(
(text: string) => this.change(text), (text: string) => this.change(text),
50, 50,
@ -76,15 +79,18 @@ export default class StatsManager {
const totalWords = await this.calcTotalWords(); const totalWords = await this.calcTotalWords();
const totalCharacters = await this.calcTotalCharacters(); const totalCharacters = await this.calcTotalCharacters();
const totalSentences = await this.calcTotalSentences(); const totalSentences = await this.calcTotalSentences();
const totalPages = await this.calcTotalPages();
const newDay: Day = { const newDay: Day = {
words: 0, words: 0,
characters: 0, characters: 0,
sentences: 0, sentences: 0,
pages: 0,
files: 0, files: 0,
totalWords: totalWords, totalWords: totalWords,
totalCharacters: totalCharacters, totalCharacters: totalCharacters,
totalSentences: totalSentences, totalSentences: totalSentences,
totalPages: totalPages,
}; };
this.vaultStats.modifiedFiles = {}; this.vaultStats.modifiedFiles = {};
@ -97,6 +103,8 @@ export default class StatsManager {
const currentWords = getWordCount(text); const currentWords = getWordCount(text);
const currentCharacters = getCharacterCount(text); const currentCharacters = getCharacterCount(text);
const currentSentences = getSentenceCount(text); const currentSentences = getSentenceCount(text);
const currentPages = getPageCount(text, this.plugin.settings.pageWords);
if ( if (
this.vaultStats.history.hasOwnProperty(this.today) && this.vaultStats.history.hasOwnProperty(this.today) &&
this.today === moment().format("YYYY-MM-DD") this.today === moment().format("YYYY-MM-DD")
@ -110,9 +118,12 @@ export default class StatsManager {
currentCharacters - modFiles[fileName].characters.current; currentCharacters - modFiles[fileName].characters.current;
this.vaultStats.history[this.today].totalSentences += this.vaultStats.history[this.today].totalSentences +=
currentSentences - modFiles[fileName].sentences.current; currentSentences - modFiles[fileName].sentences.current;
this.vaultStats.history[this.today].totalPages +=
currentSentences - modFiles[fileName].pages.current;
modFiles[fileName].words.current = currentWords; modFiles[fileName].words.current = currentWords;
modFiles[fileName].characters.current = currentCharacters; modFiles[fileName].characters.current = currentCharacters;
modFiles[fileName].sentences.current = currentSentences; modFiles[fileName].sentences.current = currentSentences;
modFiles[fileName].pages.current = currentPages;
} else { } else {
modFiles[fileName] = { modFiles[fileName] = {
words: { words: {
@ -127,6 +138,10 @@ export default class StatsManager {
initial: currentSentences, initial: currentSentences,
current: currentSentences, current: currentSentences,
}, },
pages: {
initial: currentPages,
current: currentPages,
},
}; };
} }
@ -145,10 +160,16 @@ export default class StatsManager {
Math.max(0, counts.sentences.current - counts.sentences.initial) Math.max(0, counts.sentences.current - counts.sentences.initial)
) )
.reduce((a, b) => a + b, 0); .reduce((a, b) => a + b, 0);
const pages = Object.values(modFiles)
.map((counts) =>
Math.max(0, counts.pages.current - counts.pages.initial)
)
.reduce((a, b) => a + b, 0);
this.vaultStats.history[this.today].words = words; this.vaultStats.history[this.today].words = words;
this.vaultStats.history[this.today].characters = characters; this.vaultStats.history[this.today].characters = characters;
this.vaultStats.history[this.today].sentences = sentences; this.vaultStats.history[this.today].sentences = sentences;
this.vaultStats.history[this.today].pages = pages;
this.vaultStats.history[this.today].files = this.getTotalFiles(); this.vaultStats.history[this.today].files = this.getTotalFiles();
await this.update(); await this.update();
@ -167,6 +188,7 @@ export default class StatsManager {
todayHist.totalWords = await this.calcTotalWords(); todayHist.totalWords = await this.calcTotalWords();
todayHist.totalCharacters = await this.calcTotalCharacters(); todayHist.totalCharacters = await this.calcTotalCharacters();
todayHist.totalSentences = await this.calcTotalSentences(); todayHist.totalSentences = await this.calcTotalSentences();
todayHist.totalPages = await this.calcTotalPages();
this.update(); this.update();
} else { } else {
this.updateToday(); this.updateToday();
@ -211,6 +233,20 @@ export default class StatsManager {
return sentence; return sentence;
} }
private async calcTotalPages(): Promise<number> {
let pages = 0;
const files = this.vault.getFiles();
for (const i in files) {
const file = files[i];
if (file.extension === "md") {
pages += getPageCount(await this.vault.cachedRead(file), this.plugin.settings.pageWords);
}
}
return pages;
}
public getDailyWords(): number { public getDailyWords(): number {
return this.vaultStats.history[this.today].words; return this.vaultStats.history[this.today].words;
@ -224,6 +260,10 @@ export default class StatsManager {
return this.vaultStats.history[this.today].sentences; return this.vaultStats.history[this.today].sentences;
} }
public getDailyPages(): number {
return this.vaultStats.history[this.today].pages;
}
public getTotalFiles(): number { public getTotalFiles(): number {
return this.vault.getMarkdownFiles().length; return this.vault.getMarkdownFiles().length;
} }
@ -242,4 +282,9 @@ export default class StatsManager {
if (!this.vaultStats) return await this.calcTotalSentences(); if (!this.vaultStats) return await this.calcTotalSentences();
return this.vaultStats.history[this.today].totalSentences; return this.vaultStats.history[this.today].totalSentences;
} }
public async getTotalPages(): Promise<number> {
if (!this.vaultStats) return await this.calcTotalPages();
return this.vaultStats.history[this.today].totalPages;
}
} }