better-word-count/src/stats/StatsManager.ts

371 lines
12 KiB
TypeScript
Raw Normal View History

2022-11-15 20:39:34 +00:00
import { debounce, Debouncer, TFile, Vault, Workspace } from "obsidian";
2023-02-08 23:15:04 +00:00
import type BetterWordCount from "../main";
2022-11-10 15:23:23 +00:00
import { STATS_FILE } from "../constants";
2022-12-06 15:12:59 +00:00
import type { Day, VaultStatistics } from "./Stats";
2022-11-10 15:23:23 +00:00
import moment from "moment";
import {
getCharacterCount,
getSentenceCount,
2023-02-08 21:39:28 +00:00
getPageCount,
2022-11-10 15:23:23 +00:00
getWordCount,
getCitationCount,
getFootnoteCount,
2022-11-10 15:23:23 +00:00
} from "../utils/StatUtils";
export default class StatsManager {
private vault: Vault;
private workspace: Workspace;
2023-02-08 23:15:04 +00:00
private plugin: BetterWordCount;
2022-11-10 15:23:23 +00:00
private vaultStats: VaultStatistics;
private today: string;
2022-11-15 20:39:34 +00:00
public debounceChange;
2022-11-10 15:23:23 +00:00
2023-02-08 23:15:04 +00:00
constructor(vault: Vault, workspace: Workspace, plugin: BetterWordCount) {
2022-11-10 15:23:23 +00:00
this.vault = vault;
this.workspace = workspace;
2023-02-08 21:39:28 +00:00
this.plugin = plugin;
2022-12-06 15:12:59 +00:00
this.debounceChange = debounce(
(text: string) => this.change(text),
50,
false
);
2022-11-10 15:23:23 +00:00
this.vault.on("rename", (new_name, old_path) => {
if (this.vaultStats.modifiedFiles.hasOwnProperty(old_path)) {
const content = this.vaultStats.modifiedFiles[old_path];
delete this.vaultStats.modifiedFiles[old_path];
this.vaultStats.modifiedFiles[new_name.path] = content;
}
});
this.vault.on("delete", (deleted_file) => {
if (this.vaultStats.modifiedFiles.hasOwnProperty(deleted_file.path)) {
delete this.vaultStats.modifiedFiles[deleted_file.path];
}
});
2022-11-10 15:23:23 +00:00
this.vault.adapter.exists(STATS_FILE).then(async (exists) => {
if (!exists) {
const vaultSt: VaultStatistics = {
history: {},
modifiedFiles: {},
};
await this.vault.adapter.write(STATS_FILE, JSON.stringify(vaultSt));
this.vaultStats = JSON.parse(await this.vault.adapter.read(STATS_FILE));
} else {
this.vaultStats = JSON.parse(await this.vault.adapter.read(STATS_FILE));
2022-12-06 15:12:59 +00:00
if (!this.vaultStats.hasOwnProperty("history")) {
const vaultSt: VaultStatistics = {
history: {},
modifiedFiles: {},
};
await this.vault.adapter.write(STATS_FILE, JSON.stringify(vaultSt));
}
this.vaultStats = JSON.parse(await this.vault.adapter.read(STATS_FILE));
2022-11-10 15:23:23 +00:00
}
await this.updateToday();
});
}
async update(): Promise<void> {
this.vault.adapter.write(STATS_FILE, JSON.stringify(this.vaultStats));
}
async updateToday(): Promise<void> {
if (this.vaultStats.history.hasOwnProperty(moment().format("YYYY-MM-DD"))) {
this.today = moment().format("YYYY-MM-DD");
return;
}
this.today = moment().format("YYYY-MM-DD");
const totalWords = await this.calcTotalWords();
const totalCharacters = await this.calcTotalCharacters();
const totalSentences = await this.calcTotalSentences();
const totalFootnotes = await this.calcTotalFootnotes();
const totalCitations = await this.calcTotalCitations();
2023-02-08 21:39:28 +00:00
const totalPages = await this.calcTotalPages();
2022-11-10 15:23:23 +00:00
const newDay: Day = {
words: 0,
characters: 0,
sentences: 0,
2023-02-08 21:39:28 +00:00
pages: 0,
2022-11-10 15:23:23 +00:00
files: 0,
footnotes: 0,
citations: 0,
2022-11-10 15:23:23 +00:00
totalWords: totalWords,
totalCharacters: totalCharacters,
totalSentences: totalSentences,
totalFootnotes: totalFootnotes,
totalCitations: totalCitations,
2023-02-08 21:39:28 +00:00
totalPages: totalPages,
2022-11-10 15:23:23 +00:00
};
this.vaultStats.modifiedFiles = {};
this.vaultStats.history[this.today] = newDay;
await this.update();
}
public async change(text: string) {
const fileName = this.workspace.getActiveFile().path;
const currentWords = getWordCount(text);
const currentCharacters = getCharacterCount(text);
const currentSentences = getSentenceCount(text);
const currentCitations = getCitationCount(text);
const currentFootnotes = getFootnoteCount(text);
2023-02-08 21:39:28 +00:00
const currentPages = getPageCount(text, this.plugin.settings.pageWords);
2022-11-10 15:23:23 +00:00
if (
this.vaultStats.history.hasOwnProperty(this.today) &&
this.today === moment().format("YYYY-MM-DD")
) {
let modFiles = this.vaultStats.modifiedFiles;
if (modFiles.hasOwnProperty(fileName)) {
2022-12-06 15:12:59 +00:00
this.vaultStats.history[this.today].totalWords +=
currentWords - modFiles[fileName].words.current;
this.vaultStats.history[this.today].totalCharacters +=
currentCharacters - modFiles[fileName].characters.current;
this.vaultStats.history[this.today].totalSentences +=
currentSentences - modFiles[fileName].sentences.current;
this.vaultStats.history[this.today].totalFootnotes +=
currentSentences - modFiles[fileName].footnotes.current;
this.vaultStats.history[this.today].totalCitations +=
currentSentences - modFiles[fileName].citations.current;
2023-02-08 21:39:28 +00:00
this.vaultStats.history[this.today].totalPages +=
currentPages - modFiles[fileName].pages.current;
2023-03-29 22:38:07 +00:00
2022-11-10 15:23:23 +00:00
modFiles[fileName].words.current = currentWords;
modFiles[fileName].characters.current = currentCharacters;
modFiles[fileName].sentences.current = currentSentences;
2023-03-29 22:38:07 +00:00
modFiles[fileName].footnotes.current = currentFootnotes;
modFiles[fileName].citations.current = currentCitations;
2023-02-08 21:39:28 +00:00
modFiles[fileName].pages.current = currentPages;
2022-11-10 15:23:23 +00:00
} else {
modFiles[fileName] = {
words: {
initial: currentWords,
current: currentWords,
},
characters: {
initial: currentCharacters,
current: currentCharacters,
},
sentences: {
initial: currentSentences,
current: currentSentences,
2022-12-06 15:12:59 +00:00
},
footnotes: {
initial: currentFootnotes,
current: currentFootnotes,
},
citations: {
initial: currentCitations,
current: currentCitations,
2023-02-08 21:39:28 +00:00
pages: {
initial: currentPages,
current: currentPages,
},
2022-11-10 15:23:23 +00:00
};
}
const words = Object.values(modFiles)
.map((counts) =>
Math.max(0, counts.words.current - counts.words.initial)
)
.reduce((a, b) => a + b, 0);
const characters = Object.values(modFiles)
.map((counts) =>
Math.max(0, counts.characters.current - counts.characters.initial)
)
.reduce((a, b) => a + b, 0);
const sentences = Object.values(modFiles)
.map((counts) =>
Math.max(0, counts.sentences.current - counts.sentences.initial)
)
.reduce((a, b) => a + b, 0);
2023-03-29 22:38:07 +00:00
const footnotes = Object.values(modFiles)
.map((counts) =>
Math.max(0, counts.footnotes.current - counts.footnotes.initial)
)
.reduce((a, b) => a + b, 0);
const citations = Object.values(modFiles)
.map((counts) =>
Math.max(0, counts.citations.current - counts.citations.initial)
)
2023-02-08 21:39:28 +00:00
const pages = Object.values(modFiles)
.map((counts) =>
Math.max(0, counts.pages.current - counts.pages.initial)
)
.reduce((a, b) => a + b, 0);
2022-11-10 15:23:23 +00:00
this.vaultStats.history[this.today].words = words;
this.vaultStats.history[this.today].characters = characters;
this.vaultStats.history[this.today].sentences = sentences;
this.vaultStats.history[this.today].footnotes = footnotes;
this.vaultStats.history[this.today].citations = citations;
2023-02-08 21:39:28 +00:00
this.vaultStats.history[this.today].pages = pages;
2022-11-10 15:23:23 +00:00
this.vaultStats.history[this.today].files = this.getTotalFiles();
await this.update();
} else {
this.updateToday();
}
}
2022-11-15 20:39:34 +00:00
public async recalcTotals() {
if (!this.vaultStats) return;
if (
this.vaultStats.history.hasOwnProperty(this.today) &&
this.today === moment().format("YYYY-MM-DD")
) {
const todayHist: Day = this.vaultStats.history[this.today];
todayHist.totalWords = await this.calcTotalWords();
2022-12-06 15:12:59 +00:00
todayHist.totalCharacters = await this.calcTotalCharacters();
todayHist.totalSentences = await this.calcTotalSentences();
todayHist.totalFootnotes = await this.calcTotalFootnotes();
todayHist.totalCitations = await this.calcTotalCitations();
2023-02-08 21:39:28 +00:00
todayHist.totalPages = await this.calcTotalPages();
2022-11-15 20:39:34 +00:00
this.update();
} else {
this.updateToday();
}
}
2022-11-10 15:23:23 +00:00
private async calcTotalWords(): Promise<number> {
let words = 0;
const files = this.vault.getFiles();
for (const i in files) {
const file = files[i];
if (file.extension === "md") {
words += getWordCount(await this.vault.cachedRead(file));
}
}
return words;
}
private async calcTotalCharacters(): Promise<number> {
let characters = 0;
const files = this.vault.getFiles();
for (const i in files) {
const file = files[i];
if (file.extension === "md") {
characters += getCharacterCount(await this.vault.cachedRead(file));
}
}
return characters;
}
private async calcTotalSentences(): Promise<number> {
let sentence = 0;
const files = this.vault.getFiles();
for (const i in files) {
const file = files[i];
if (file.extension === "md") {
sentence += getSentenceCount(await this.vault.cachedRead(file));
}
}
return sentence;
}
2023-02-08 21:39:28 +00:00
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;
}
2022-11-10 15:23:23 +00:00
private async calcTotalFootnotes(): Promise<number> {
let footnotes = 0;
const files = this.vault.getFiles();
for (const i in files) {
const file = files[i];
if (file.extension === "md") {
footnotes += getFootnoteCount(await this.vault.cachedRead(file));
}
}
return footnotes;
}
private async calcTotalCitations(): Promise<number> {
let citations = 0;
const files = this.vault.getFiles();
for (const i in files) {
const file = files[i];
if (file.extension === "md") {
citations += getCitationCount(await this.vault.cachedRead(file));
}
}
return citations;
}
2022-12-06 15:12:59 +00:00
public getDailyWords(): number {
return this.vaultStats.history[this.today].words;
}
public getDailyCharacters(): number {
return this.vaultStats.history[this.today].characters;
}
public getDailySentences(): number {
return this.vaultStats.history[this.today].sentences;
}
2023-03-29 22:38:07 +00:00
public getDailyFootnotes(): number {
return this.vaultStats.history[this.today].footnotes;
}
public getDailyCitations(): number {
return this.vaultStats.history[this.today].citations;
}
2023-02-08 21:39:28 +00:00
public getDailyPages(): number {
return this.vaultStats.history[this.today].pages;
}
2022-11-10 15:23:23 +00:00
public getTotalFiles(): number {
return this.vault.getMarkdownFiles().length;
}
2022-11-15 20:39:34 +00:00
public async getTotalWords(): Promise<number> {
if (!this.vaultStats) return await this.calcTotalWords();
2022-11-10 15:23:23 +00:00
return this.vaultStats.history[this.today].totalWords;
}
2022-11-15 20:39:34 +00:00
public async getTotalCharacters(): Promise<number> {
if (!this.vaultStats) return await this.calcTotalCharacters();
2022-11-10 15:23:23 +00:00
return this.vaultStats.history[this.today].totalCharacters;
}
2022-11-15 20:39:34 +00:00
public async getTotalSentences(): Promise<number> {
if (!this.vaultStats) return await this.calcTotalSentences();
2022-11-10 15:23:23 +00:00
return this.vaultStats.history[this.today].totalSentences;
}
2023-03-29 22:38:07 +00:00
public async getTotalFootnotes(): Promise<number> {
if (!this.vaultStats) return await this.calcTotalFootnotes();
return this.vaultStats.history[this.today].totalFootnotes;
}
public async getTotalCitations(): Promise<number> {
if (!this.vaultStats) return await this.calcTotalCitations();
return this.vaultStats.history[this.today].totalCitations;
}
2023-02-08 21:39:28 +00:00
public async getTotalPages(): Promise<number> {
if (!this.vaultStats) return await this.calcTotalPages();
return this.vaultStats.history[this.today].totalPages;
}
2022-11-10 15:23:23 +00:00
}