Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ Let's analyze this codeblock:
const plugin = this.app.plugins.plugins["view-count"];
const cache = plugin.viewCountCache;

const DURATION = "7-days";
const TIME_PERIOD = "7-days";

dv.table(["Name", "Trending Weight"],
dv.pages().sort(p => cache.getTrendingWeight(p.file, DURATION), "desc")
.map(p => [p.file.name, cache.getTrendingWeight(p.file, DURATION)])
dv.pages().sort(p => cache.getTrendingWeight(p.file, TIME_PERIOD), "desc")
.map(p => [p.file.name, cache.getTrendingWeight(p.file, TIME_PERIOD)])
.slice(0,10)
);
```
Expand All @@ -115,7 +115,7 @@ Let's analyze this codeblock:
5. Format an array of data that includes object with just the file name and the trending weight
6. Limit the results to 10 notes

The duration can be updated with various values. See the [Duration](#duration) section below.
The time period can be updated with various values. See the [time period](#time-period) section below.

## API

Expand All @@ -135,7 +135,7 @@ Then you can use the cache to get a view count or trending weight.

```javascript
//Get the trending weight
const weight = cache.getTrendingWeight(file, duration);
const weight = cache.getTrendingWeight(file, timePeriod);
console.log(weight);
//output: 22

Expand All @@ -149,16 +149,16 @@ Here are the typescript definitions for these functions:

```javascript
getViewCount: (file: TFile) => number;
getTrendingWeight: (file: TFile, duration: DurationFilter) => number;
getTrendingWeight: (file: TFile, timePeriod: TimePeriod) => number;
```

### Duration
### Time period

The `getTrendingWeight` function accepts a duration.
The `getTrendingWeight` function accepts a time period.

Here are the options:

| Duration | Description |
| Value | Description |
| ---------- | ------------------------------------- |
| `3-days` | The last 3 days |
| `7-days` | The last 7 days |
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "view-count",
"name": "View Count",
"version": "2.4.0",
"version": "2.4.1",
"minAppVersion": "1.4.0",
"description": "Track view count for each vault file.",
"author": "DecafDev",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-view-count",
"version": "2.4.0",
"version": "2.4.1",
"description": "Track view count for each vault file",
"main": "main.js",
"scripts": {
Expand Down
11 changes: 5 additions & 6 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { LOG_LEVEL_OFF } from "./logger/constants";
import { DurationFilter } from "./storage/types";
import { TView } from "./svelte/types";
import { ViewCountPluginSettings } from "./types";
import { TimePeriod, TView, ViewCountPluginSettings } from "./types";

export const VIEW_COUNT_ITEM_VIEW = "view-count";

Expand All @@ -13,7 +11,8 @@ export const DEFAULT_SETTINGS: ViewCountPluginSettings = {
logLevel: LOG_LEVEL_OFF,
excludedPaths: [],
templaterDelay: 0,
currentView: TView.MOST_VIEWED,
durationFilter: DurationFilter.DAYS_3,
listSize: 20,
currentView: TView.VIEWS,
timePeriod: TimePeriod.DAYS_3,
itemCount: 20,
}

28 changes: 22 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Plugin, TFile } from 'obsidian';
import ViewCountSettingsTab from './obsidian/view-count-settings-tab';
import { ViewCountPluginSettings, ViewCountPluginSettings_1_2_1, ViewCountPluginSettings_1_2_2, ViewCountPluginSettings_2_3_1 } from './types';
import { TView, TimePeriod, ViewCountPluginSettings } from './types';
import ViewCountItemView from './obsidian/view-count-item-view';
import { DEFAULT_SETTINGS, VIEW_COUNT_ITEM_VIEW } from './constants';
import Logger from 'js-logger';
Expand All @@ -10,8 +10,10 @@ import { isVersionLessThan } from './utils';
import ViewCountCache from './storage/view-count-cache';
import { migrateFileStorage } from './migration/migrate-file-storage';
import { migratePropertyStorage } from './migration/migrate-property-storage';
import { DurationFilter } from './storage/types';
import { TView } from './svelte/types';
import { ViewCountPluginSettings_1_2_2 } from './types/types-1.2.2';
import { ViewCountPluginSettings_1_2_1 } from './types/types-1.2.1';
import { ViewCountPluginSettings_2_3_1 } from './types/types-2.3.1';
import { DurationFilter_2_4_0, TView_2_4_0, ViewCountPluginSettings_2_4_0 } from './types/types-2.4.0';

export default class ViewCountPlugin extends Plugin {
settings: ViewCountPluginSettings = DEFAULT_SETTINGS;
Expand Down Expand Up @@ -96,14 +98,28 @@ export default class ViewCountPlugin extends Plugin {
if (isVersionLessThan(settingsVersion, "2.4.0")) {
console.log("Migrating settings from 2.3.1 to 2.4.0");
const typedData = (data as unknown) as ViewCountPluginSettings_2_3_1;
const newData: ViewCountPluginSettings = {
const newData: ViewCountPluginSettings_2_4_0 = {
...typedData,
durationFilter: DurationFilter.DAYS_3,
currentView: TView.MOST_VIEWED,
durationFilter: DurationFilter_2_4_0.DAYS_3,
currentView: TView_2_4_0.MOST_VIEWED,
listSize: 20
}
data = newData as unknown as Record<string, unknown>;
}

if (isVersionLessThan(settingsVersion, "2.4.1")) {
console.log("Migrating settings from 2.4.0 to 2.4.1");
const typedData = (data as unknown) as ViewCountPluginSettings_2_4_0;
const newData: ViewCountPluginSettings = {
...typedData,
timePeriod: TimePeriod.DAYS_3,
currentView: TView.VIEWS,
itemCount: 20
}
delete (newData as any).durationFilter;
delete (newData as any).listSize;
data = newData as unknown as Record<string, unknown>;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/migration/migrate-file-storage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { App } from "obsidian";
import { ViewCountEntry, ViewCountEntry_1_2_2 } from "src/storage/types";
import { getFilePath, parseEntries, stringifyEntries } from "src/storage/utils";
import { ViewCountPluginSettings_1_2_2 } from "src/types";
import { ViewCountPluginSettings_1_2_2 } from "src/types/types-1.2.2";

export const migrateFileStorage = async (app: App, settings: ViewCountPluginSettings_1_2_2) => {
console.log("Migrating file storage");
Expand Down
2 changes: 1 addition & 1 deletion src/migration/migrate-property-storage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { App } from "obsidian";
import { ViewCountEntry } from "src/storage/types";
import { getFilePath, stringifyEntries } from "src/storage/utils";
import { ViewCountPluginSettings_1_2_2 } from "src/types";
import { ViewCountPluginSettings_1_2_2 } from "src/types/types-1.2.2";

export const migratePropertyStorage = async (app: App, settings: ViewCountPluginSettings_1_2_2) => {
console.log("Migrating property storage");
Expand Down
13 changes: 0 additions & 13 deletions src/storage/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,4 @@ export interface ViewCountEntry_1_2_2 {
path: string;
viewCount: number;
lastViewMillis: number;
}

export type ListSize = 10 | 15 | 20 | 25 | 50 | 100;

export enum DurationFilter {
MONTH = "month",
WEEK_ISO = "week-iso",
WEEK = "week",
TODAY = "today",
DAYS_30 = "30-days",
DAYS_14 = "14-days",
DAYS_7 = "7-days",
DAYS_3 = "3-days"
}
34 changes: 17 additions & 17 deletions src/storage/view-count-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { App, Notice, TFile } from "obsidian";
import { getFilePath, parseEntries, shouldTrackFile, stringifyEntries } from "./utils";
import Logger from "js-logger";
import _ from "lodash";
import { DurationFilter, ViewCountEntry } from "./types";
import { ViewCountEntry } from "./types";
import EventManager from "src/event/event-manager";
import { getStartOf14DaysAgoMillis, getStartOf30DaysAgoMillis, getStartOf31DaysAgoMillis, getStartOf3DaysAgoMillis, getStartOf7DaysAgoMillis, getStartOfMonthMillis, getStartOfTodayMillis, getStartOfWeekMillis } from "src/utils/time-utils";
import { ViewCountPluginSettings } from "src/types";
import { TimePeriod, ViewCountPluginSettings } from "src/types";

export default class ViewCountCache {
private app: App;
Expand Down Expand Up @@ -124,48 +124,48 @@ export default class ViewCountCache {
* Gets the trending weight (the score) for a file.
* Note: This is a public method for usage with DataviewJS
* @param file - The file to get the weight for
* @param duration - The duration used to calculate the weight
* @param timePeriod - The time period used to calculate the weight
*/
getTrendingWeight(file: TFile, duration: DurationFilter) {
getTrendingWeight(file: TFile, timePeriod: TimePeriod) {
const entry = this.entries.find((entry) => entry.path === file.path);
if (!entry) {
return 0;
}
return this.getNumTimesOpenedForEntry(entry, duration);
return this.getNumTimesOpenedForEntry(entry, timePeriod);
}

getNumTimesOpenedForEntry(entry: ViewCountEntry, duration: DurationFilter) {
getNumTimesOpenedForEntry(entry: ViewCountEntry, timePeriod: TimePeriod) {
const { openLogs } = entry;

let timeMillis = 0;

switch (duration) {
case DurationFilter.MONTH:
switch (timePeriod) {
case TimePeriod.MONTH:
timeMillis = getStartOfMonthMillis();
break;
case DurationFilter.WEEK:
case TimePeriod.WEEK:
timeMillis = getStartOfWeekMillis(false);
break;
case DurationFilter.WEEK_ISO:
case TimePeriod.WEEK_ISO:
timeMillis = getStartOfWeekMillis(true);
break;
case DurationFilter.TODAY:
case TimePeriod.TODAY:
timeMillis = getStartOfTodayMillis();
break;
case DurationFilter.DAYS_30:
case TimePeriod.DAYS_30:
timeMillis = getStartOf30DaysAgoMillis();
break;
case DurationFilter.DAYS_14:
case TimePeriod.DAYS_14:
timeMillis = getStartOf14DaysAgoMillis();
break;
case DurationFilter.DAYS_7:
case TimePeriod.DAYS_7:
timeMillis = getStartOf7DaysAgoMillis();
break;
case DurationFilter.DAYS_3:
case TimePeriod.DAYS_3:
timeMillis = getStartOf3DaysAgoMillis();
break;
default:
throw new Error(`DurationFilter ${duration} is not supported`);
throw new Error(`TimePeriod ${timePeriod} is not supported`);
}

return openLogs.filter((log) => log.timestampMillis >= timeMillis).length;
Expand Down Expand Up @@ -290,7 +290,7 @@ export default class ViewCountCache {
const updatedEntries = this.entries.map((entry) => {
if (entry.path === targetPath) {
//Keep 31 days of logs.
//This will support both 30-days and month durations
//This will support both 30-days and month time periods
const start31DaysAgoMillis = getStartOf31DaysAgoMillis();
const filteredLogs = entry.openLogs.filter((log) => log.timestampMillis >= start31DaysAgoMillis);

Expand Down
Loading