Skip to content
This repository was archived by the owner on Jul 28, 2025. It is now read-only.

Commit a4323cf

Browse files
committed
QOL logs
1 parent 1af9421 commit a4323cf

File tree

7 files changed

+70
-56
lines changed

7 files changed

+70
-56
lines changed

src/DatabaseView.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import {
3737
Menu,
3838
} from "obsidian";
3939
import { createRoot, Root } from "react-dom/client";
40-
import { DbAutomationService } from "services/AutomationService";
40+
import { Db } from "services/CoreService";
4141
import DatabaseInfo from "services/DatabaseInfo";
4242
import { LOGGER } from "services/Logger";
4343
import { SettingsModal } from "Settings";
@@ -172,9 +172,7 @@ export class DatabaseView extends TextFileView implements HoverParent {
172172
);
173173
this.initial = obtainInitialType(this.columns);
174174

175-
this.formulas = await DbAutomationService.buildFns(
176-
this.diskConfig.yaml.config
177-
);
175+
this.formulas = await Db.buildFns(this.diskConfig.yaml.config);
178176
// Define table properties
179177
this.shadowColumns = this.columns.filter((col) => col.skipPersist);
180178
const tableProps: TableDataType = {

src/automations/Footer.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { DEFAULT_SETTINGS, FooterType } from "helpers/Constants";
22
import { Literal } from "obsidian-dataview";
33
import { DataviewService } from "services/DataviewService";
4-
import { DbAutomationService } from "services/AutomationService";
4+
import { Db } from "services/CoreService";
55

66
export default class Footer {
77
constructor(public readonly colValues: Literal[]) { }
@@ -119,7 +119,7 @@ export default class Footer {
119119
* @returns
120120
*/
121121
public sum(): string {
122-
const total = DbAutomationService.coreFns.numbers.sum(this.colValues);
122+
const total = Db.coreFns.numbers.sum(this.colValues);
123123
return `Total: ${total}`;
124124
}
125125

@@ -128,7 +128,7 @@ export default class Footer {
128128
* @returns
129129
*/
130130
public min(): string {
131-
const min = DbAutomationService.coreFns.numbers.min(this.colValues);
131+
const min = Db.coreFns.numbers.min(this.colValues);
132132
return `Min: ${min}`;
133133
}
134134

@@ -137,7 +137,7 @@ export default class Footer {
137137
* @returns
138138
*/
139139
public max(): string {
140-
const max = DbAutomationService.coreFns.numbers.max(this.colValues);
140+
const max = Db.coreFns.numbers.max(this.colValues);
141141
return `Max: ${max}`;
142142
}
143143

@@ -149,7 +149,7 @@ export default class Footer {
149149
* @returns
150150
*/
151151
public earliestDate(): string {
152-
const earliest = DbAutomationService.coreFns.luxon.earliest(this.colValues);
152+
const earliest = Db.coreFns.luxon.earliest(this.colValues);
153153
return earliest.isValid ?
154154
`Earliest: ${earliest.toFormat(DEFAULT_SETTINGS.local_settings.datetime_format)}` :
155155
null;
@@ -160,7 +160,7 @@ export default class Footer {
160160
* @returns
161161
*/
162162
public latestDate(): string {
163-
const latest = DbAutomationService.coreFns.luxon.latest(this.colValues);
163+
const latest = Db.coreFns.luxon.latest(this.colValues);
164164
return latest.isValid ?
165165
`Latest: ${latest.toFormat(DEFAULT_SETTINGS.local_settings.datetime_format)}` :
166166
null;
@@ -171,7 +171,7 @@ export default class Footer {
171171
* @returns
172172
*/
173173
public rangeDate(): string {
174-
const range = DbAutomationService.coreFns.luxon.range(this.colValues);
174+
const range = Db.coreFns.luxon.range(this.colValues);
175175
return `Range: ${range} days`
176176
}
177177

src/automations/Rollup.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { LocalSettings } from "cdm/SettingsModel";
22
import { DEFAULT_SETTINGS, InputType, ROLLUP_ACTIONS } from "helpers/Constants";
33
import { Link, Literal, SMarkdownPage } from "obsidian-dataview";
4-
import { DbAutomationService } from "services/AutomationService";
4+
import { Db } from "services/CoreService";
55
import { DataviewService } from "services/DataviewService";
66
import { LOGGER } from "services/Logger";
77
import { ParseService } from "services/ParseService";
@@ -97,7 +97,7 @@ class Rollup {
9797
return NaN;
9898
}
9999
const rawValues = this.rawValues(key);
100-
return DbAutomationService.coreFns.numbers.sum(rawValues)
100+
return Db.coreFns.numbers.sum(rawValues)
101101
}
102102

103103
/**

src/main.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { DatabaseHelperCreationModal } from 'commands/addDatabaseHelper/database
3636
import { generateDbConfiguration, generateNewDatabase } from 'helpers/CommandsHelper';
3737
import { registerDateFnLocale, t } from 'lang/helpers';
3838
import ProjectAPI from 'api/obsidian-projects-api';
39+
import { Db } from "services/CoreService";
3940

4041
interface WindowRegistry {
4142
viewMap: Map<string, DatabaseView>;
@@ -72,6 +73,7 @@ export default class DBFolderPlugin extends Plugin {
7273

7374
async onload(): Promise<void> {
7475
await this.load_settings();
76+
await this.loadServices();
7577
addIcon(DB_ICONS.NAME, DB_ICONS.ICON);
7678
this.registerEvent(
7779
app.workspace.on('window-open', (_: any, win: Window) => {
@@ -156,6 +158,10 @@ export default class DBFolderPlugin extends Plugin {
156158
loadServicesThatRequireSettings(this.settings);
157159
}
158160

161+
async loadServices() {
162+
await Db.init();
163+
}
164+
159165
public registerPriorityCodeblockPostProcessor(
160166
language: string,
161167
priority: number,
Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@ import { LocalSettings } from "cdm/SettingsModel";
66
import { Link } from "obsidian-dataview/lib/data-model/value";
77
import { DataviewService } from "./DataviewService";
88

9-
class AutomationService {
10-
private static instance: AutomationService;
9+
class CoreService {
10+
private static instance: CoreService;
1111
public coreFns: DatabaseFnType;
1212

13+
async init() {
14+
const modulesGenerator = await new ModulesGenerator();
15+
await modulesGenerator.init();
16+
this.coreFns = await modulesGenerator.generate_object();
17+
}
18+
1319
async buildFns(config: LocalSettings) {
1420
const final_object: Record<string, unknown> = {};
1521
const jsObject = await new FormulaGenerator(config).generate_object();
@@ -20,31 +26,21 @@ class AutomationService {
2026
dataview: DataviewService.getDataviewAPI(),
2127
rollup: (relation: Link[]) => new Rollup(relation)
2228
},
23-
await this.getCoreFns()
29+
this.coreFns
2430
);
2531
return final_object;
2632
}
2733

28-
async getCoreFns(): Promise<DatabaseFnType> {
29-
if (!this.coreFns) {
30-
const modulesGenerator = await new ModulesGenerator();
31-
await modulesGenerator.init();
32-
this.coreFns = await modulesGenerator.generate_object();
33-
}
34-
35-
return this.coreFns;
36-
}
37-
3834
/**
3935
* Singleton instance
4036
* @returns {VaultManager}
4137
*/
42-
public static getInstance(): AutomationService {
38+
public static getInstance(): CoreService {
4339
if (!this.instance) {
44-
this.instance = new AutomationService();
40+
this.instance = new CoreService();
4541
}
4642
return this.instance;
4743
}
4844
}
4945

50-
export const DbAutomationService = AutomationService.getInstance();
46+
export const Db = CoreService.getInstance();

src/services/Logger.ts

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
export interface LogInterface {
2-
debug(primaryMessage: string, ...supportingData: unknown[]): void;
3-
info(primaryMessage: string, ...supportingData: unknown[]): void;
4-
warn(primaryMessage: string, ...supportingData: unknown[]): void;
5-
error(primaryMessage: string, ...supportingData: unknown[]): void;
2+
debug(...args: unknown[]): void;
3+
info(...args: unknown[]): void;
4+
warn(...args: unknown[]): void;
5+
error(...args: unknown[]): void;
66
setDebugMode(isDebugModeEnabled: boolean): void;
77
setLevelInfo(value: string): void;
88
}
@@ -13,42 +13,56 @@ const LevelInfoRecord: Record<string, number> = {
1313
warn: 1,
1414
error: 0
1515
};
16+
1617
class Log implements LogInterface {
1718
private static instance: LogInterface;
1819
private isDebugModeEnabled: boolean;
1920
private levelInfo: number;
21+
public debug: (...args: unknown[]) => void;
22+
public info: (...args: unknown[]) => void;
23+
public warn: (...args: unknown[]) => void;
24+
public error: (...args: unknown[]) => void;
25+
2026
private constructor() {
2127
this.isDebugModeEnabled = false;
2228
this.levelInfo = 0;
2329
}
2430

25-
public debug(primaryMessage: string, ...supportingData: unknown[]) {
26-
if (this.levelInfo >= LevelInfoRecord.debug) {
27-
this.emitLogMessage("debug", primaryMessage, ...supportingData);
28-
}
29-
}
30-
public info(primaryMessage: string, ...supportingData: unknown[]) {
31-
if (this.levelInfo >= LevelInfoRecord.info) {
32-
this.emitLogMessage("info", primaryMessage, ...supportingData);
33-
}
34-
}
35-
public warn(primaryMessage: string, ...supportingData: unknown[]) {
36-
if (this.levelInfo >= LevelInfoRecord.warn) {
37-
this.emitLogMessage("warn", primaryMessage, ...supportingData);
38-
}
39-
}
40-
public error(primaryMessage: string, ...supportingData: unknown[]) {
41-
if (this.levelInfo >= LevelInfoRecord.error) {
42-
this.emitLogMessage("error", primaryMessage, ...supportingData);
43-
}
44-
}
45-
4631
public setDebugMode(isDebugModeEnabled: boolean) {
4732
this.isDebugModeEnabled = isDebugModeEnabled;
33+
this.configureLogger();
4834
}
4935

5036
public setLevelInfo(value: string) {
5137
this.levelInfo = LevelInfoRecord[value];
38+
this.configureLogger();
39+
40+
}
41+
42+
private configureLogger() {
43+
if (this.levelInfo >= LevelInfoRecord.debug && this.isDebugModeEnabled) {
44+
this.debug = console.log.bind(window.console, `[debug] ${new Date().toISOString()}`);
45+
} else {
46+
this.debug = () => { };
47+
}
48+
49+
if (this.levelInfo >= LevelInfoRecord.info && this.isDebugModeEnabled) {
50+
this.info = console.log.bind(window.console, `[info] ${new Date().toISOString()}`);
51+
} else {
52+
this.info = () => { };
53+
}
54+
55+
if (this.levelInfo >= LevelInfoRecord.warn && this.isDebugModeEnabled) {
56+
this.warn = console.log.bind(window.console, `[warn] ${new Date().toISOString()}`);
57+
} else {
58+
this.warn = () => { };
59+
}
60+
61+
if (this.levelInfo >= LevelInfoRecord.error && this.isDebugModeEnabled) {
62+
this.error = console.log.bind(window.console, `[error] ${new Date().toISOString()}`);
63+
} else {
64+
this.error = () => { };
65+
}
5266
}
5367

5468
public static getInstance(): LogInterface {

src/stateManagement/automations/handlers/LoadFormulasHandlerAction.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { AutomationState, TableActionResponse } from "cdm/TableStateInterface";
2-
import { DbAutomationService } from "services/AutomationService";
2+
import { Db } from "services/CoreService";
33
import { AbstractTableAction } from "stateManagement/AbstractTableAction";
44

55
export default class LoadFormulasHandlerAction extends AbstractTableAction<AutomationState> {
66
handle(tableActionResponse: TableActionResponse<AutomationState>): TableActionResponse<AutomationState> {
77
const { set, implementation } = tableActionResponse;
88
implementation.actions.loadFormulas = async (ddbbConfig) => {
9-
const formulas = await DbAutomationService.buildFns(ddbbConfig);
9+
const formulas = await Db.buildFns(ddbbConfig);
1010
set({ formula: formulas });
1111
};
1212

0 commit comments

Comments
 (0)