From 172ede41e0d33440947e9a8bd0025dbcbfb55b64 Mon Sep 17 00:00:00 2001 From: Mikhail Savin Date: Tue, 7 Jan 2025 19:37:22 +0300 Subject: [PATCH 1/3] feat: Add excluded directories Define new settings for exclude some directories (like templates) from statistic --- src/collect.ts | 52 ++++++++++++++++++++++++++++--------------------- src/main.ts | 19 +++++++++--------- src/metrics.ts | 2 +- src/settings.ts | 15 +++++++++++++- 4 files changed, 55 insertions(+), 33 deletions(-) diff --git a/src/collect.ts b/src/collect.ts index 626b604..e7a8dad 100644 --- a/src/collect.ts +++ b/src/collect.ts @@ -16,6 +16,7 @@ export class FullVaultMetricsCollector { private metadataCache: MetadataCache; private data: Map = new Map(); private backlog: Array = new Array(); + private excludeDirectories: string; private vaultMetrics: FullVaultMetrics = new FullVaultMetrics(); constructor(owner: Component) { @@ -27,6 +28,11 @@ export class FullVaultMetricsCollector { return this; } + public setExcludeDirectories(excludeDirectories: string) { + this.excludeDirectories = excludeDirectories; + return this; + } + public setMetadataCache(metadataCache: MetadataCache) { this.metadataCache = metadataCache; return this; @@ -55,6 +61,8 @@ export class FullVaultMetricsCollector { }); this.owner.registerInterval(+setInterval(() => { this.processBacklog() }, 2000)); + this.setExcludeDirectories(this.excludeDirectories); + return this; } @@ -72,7 +80,7 @@ export class FullVaultMetricsCollector { private async processBacklog() { while (this.backlog.length > 0) { let path = this.backlog.shift(); - if (path == null) { + if ((path == null) || (path.split("/").some((dir) => this.excludeDirectories.includes(dir)))) { break; } // console.log(`processing ${path}`); @@ -85,7 +93,7 @@ export class FullVaultMetricsCollector { this.update(path, metrics); } } catch (e) { - console.log(`error processing ${path}: ${e}`); + // console.log(`error processing ${path}: ${e}`); } } // console.log(`path = ${path}; file = ${file}`); @@ -201,26 +209,26 @@ class NoteMetricsCollector { metrics.size = file.stat?.size; metrics.links = metadata?.links?.length || 0; const words = await this.vault.cachedRead(file).then((content: string) => { - return metadata.sections?.map(section => { - const sectionType = section.type; - const startOffset = section.position?.start?.offset; - const endOffset = section.position?.end?.offset; - const tokenizer = NoteMetricsCollector.TOKENIZERS.get(sectionType); - if (!tokenizer) { - console.log(`${file.path}: no tokenizer, section.type=${section.type}`); - return 0; - } else { - const tokens = tokenizer.tokenize(content.substring(startOffset, endOffset)); - return tokens.length; - } - }).reduce((a, b) => a + b, 0); - }).catch((e) => { - console.log(`${file.path} ${e}`); - return 0; - }); - metrics.words = words ?? 0; - metrics.quality = metrics.links / metrics.notes ?? 0.0; - metrics.tags = metadata?.tags?.length ?? 0; + return metadata.sections?.map(section => { + const sectionType = section.type; + const startOffset = section.position?.start?.offset; + const endOffset = section.position?.end?.offset; + const tokenizer = NoteMetricsCollector.TOKENIZERS.get(sectionType); + if (!tokenizer) { + console.log(`${file.path}: no tokenizer, section.type=${section.type}`); + return 0; + } else { + const tokens = tokenizer.tokenize(content.substring(startOffset, endOffset)); + return tokens.length; + } + }).reduce((a, b) => a + b, 0); + }).catch((e) => { + console.log(`${file.path} ${e}`); + return 0; + }); + metrics.words = words ?? 0; + metrics.quality = metrics.notes !== 0 ? (metrics.links / metrics.notes) : 0.0; + metrics.tags = metadata?.tags?.length ?? 0; return metrics; } diff --git a/src/main.ts b/src/main.ts index 8ff639a..32673d3 100644 --- a/src/main.ts +++ b/src/main.ts @@ -36,6 +36,7 @@ export default class FullStatisticsPlugin extends Plugin { setVault(this.app.vault). setMetadataCache(this.app.metadataCache). setFullVaultMetrics(this.vaultMetrics). + setExcludeDirectories(this.settings.excludeDirectories). start(); this.statusBarItem = new FullStatisticsStatusBarItem(this, this.addStatusBarItem()). @@ -63,7 +64,7 @@ export default class FullStatisticsPlugin extends Plugin { class StatisticView { /** Root node for the {@link StatisticView}. */ - private containerEl: HTMLElement; + private containerElementsForVaultFullStatistics: HTMLElement; /** Formatter that extracts and formats a value from a {@link Statistics} instance. */ private formatter: (s: FullVaultMetrics) => string; @@ -74,7 +75,7 @@ class StatisticView { * @param containerEl The parent element for the view. */ constructor(containerEl: HTMLElement) { - this.containerEl = containerEl.createSpan({ cls: ["obsidian-vault-full-statistics--item"] }); + this.containerElementsForVaultFullStatistics = containerEl.createSpan({ cls: ["obsidian-vault-full-statistics--item"] }); this.setActive(false); } @@ -82,7 +83,7 @@ class StatisticView { * Sets the name of the statistic. */ setStatisticName(name: string): StatisticView { - this.containerEl.addClass(`obsidian-vault-full-statistics--item-${name}`); + this.containerElementsForVaultFullStatistics.addClass(`obsidian-vault-full-statistics--item-${name}`); return this; } @@ -103,13 +104,13 @@ class StatisticView { * mutually exclusive. */ setActive(isActive: boolean): StatisticView { - this.containerEl.removeClass("obsidian-vault-full-statistics--item--active"); - this.containerEl.removeClass("obsidian-vault-full-statistics--item--inactive"); + this.containerElementsForVaultFullStatistics.removeClass("obsidian-vault-full-statistics--item--active"); + this.containerElementsForVaultFullStatistics.removeClass("obsidian-vault-full-statistics--item--inactive"); if (isActive) { - this.containerEl.addClass("obsidian-vault-full-statistics--item--active"); + this.containerElementsForVaultFullStatistics.addClass("obsidian-vault-full-statistics--item--active"); } else { - this.containerEl.addClass("obsidian-vault-full-statistics--item--inactive"); + this.containerElementsForVaultFullStatistics.addClass("obsidian-vault-full-statistics--item--inactive"); } return this; @@ -120,14 +121,14 @@ class StatisticView { * Statistics}. */ refresh(s: FullVaultMetrics) { - this.containerEl.setText(this.formatter(s)); + this.containerElementsForVaultFullStatistics.setText(this.formatter(s)); } /** * Returns the text content of the view. */ getText(): string { - return this.containerEl.getText(); + return this.containerElementsForVaultFullStatistics.getText(); } } diff --git a/src/metrics.ts b/src/metrics.ts index 2ca9b2a..5a5e38e 100644 --- a/src/metrics.ts +++ b/src/metrics.ts @@ -21,7 +21,7 @@ export class FullVaultMetrics extends Events implements FullVaultMetrics { words: number = 0; quality: number = 0.00001; tags: number = 0; - + public reset() { this.files = 0; this.notes = 0; diff --git a/src/settings.ts b/src/settings.ts index afed683..ee8b56a 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -3,6 +3,7 @@ import { App, PluginSettingTab, Setting } from "obsidian"; import StatisticsPlugin from "./main"; export interface FullStatisticsPluginSettings { + excludeDirectories: string, displayIndividualItems: boolean, showNotes: boolean, showAttachments: boolean, @@ -26,7 +27,19 @@ export class FullStatisticsPluginSettingTab extends PluginSettingTab { let { containerEl } = this; containerEl.empty(); - + + new Setting(containerEl) + .setName("Exclude directories") + .setDesc("Exclude directories from statistics") + .addText((value) => { + value + .setValue(this.plugin.settings.excludeDirectories) + .onChange(async (value) => { + this.plugin.settings.excludeDirectories = value; + await this.plugin.saveSettings(); + }); + }); + new Setting(containerEl) .setName("Show individual items") .setDesc("Whether to show multiple items at once or cycle them with a click") From 39f75f579c237304981369085549045146b35d04 Mon Sep 17 00:00:00 2001 From: Mikhail Savin Date: Tue, 7 Jan 2025 19:41:48 +0300 Subject: [PATCH 2/3] fix: Add to readme new feature --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 4c52565..b8efc96 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ Status bar item with vault full statistics including: - size of vault – total size of all files in the vault - (new feature) vault quality – number of links divided by number of notes - (new feature) number of tags – count of all tags in the vault +- (new feature) excluded dirs – comma-separated list of directories excluded from statistics ## Installation From c8a1357e5d86710ff0ec03ff3185c12069f2e8b4 Mon Sep 17 00:00:00 2001 From: Mikhail Savin Date: Tue, 7 Jan 2025 19:46:28 +0300 Subject: [PATCH 3/3] fix: Uncomment console.log with error Signed-off-by: Mikhail Savin --- src/collect.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/collect.ts b/src/collect.ts index e7a8dad..78913b8 100644 --- a/src/collect.ts +++ b/src/collect.ts @@ -93,7 +93,7 @@ export class FullVaultMetricsCollector { this.update(path, metrics); } } catch (e) { - // console.log(`error processing ${path}: ${e}`); + console.log(`error processing ${path}: ${e}`); } } // console.log(`path = ${path}; file = ${file}`);