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

Commit 7f4fc7e

Browse files
committed
rollups working correctly
1 parent eac4553 commit 7f4fc7e

File tree

6 files changed

+99
-38
lines changed

6 files changed

+99
-38
lines changed

src/automations/FormulaGenerator.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { FormulaFunctions } from "automations/formula_functions/FormulaFunctions";
22
import { IGenerateObject } from "automations/IGenerateObject";
33
import { LocalSettings } from "cdm/SettingsModel";
4+
import { Link } from "obsidian-dataview";
45
import { DataviewService } from "services/DataviewService";
6+
import Rollup from "automations/Rollup";
57
export class FormulaGenerator implements IGenerateObject {
68
public js_functions: FormulaFunctions;
79

@@ -17,7 +19,8 @@ export class FormulaGenerator implements IGenerateObject {
1719
const final_object: Record<string, any> = {};
1820
Object.assign(final_object, {
1921
js: await this.generate_js_functions(),
20-
dataview: DataviewService.getDataviewAPI()
22+
dataview: DataviewService.getDataviewAPI(),
23+
rollup: (relation: Link[], action?: string, key?: string) => new Rollup(relation, action, key)
2124
}
2225
);
2326
return final_object;

src/automations/Rollup.ts

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,54 @@
1-
import { ROLLUP_ACTIONS } from "helpers/Constants";
1+
import { DEFAULT_SETTINGS, InputType, ROLLUP_ACTIONS } from "helpers/Constants";
22
import { Link, Literal } from "obsidian-dataview";
33
import { DataviewService } from "services/DataviewService";
44
import { LOGGER } from "services/Logger";
5+
import { ParseService } from "services/ParseService";
56

67
class Rollup {
78
private pages: Record<string, Literal>[];
89
private action: string;
910
private key: string;
1011

11-
constructor(relation: Link[], action: string, key: string) {
12-
const consultedLinks = relation.map((link) =>
12+
static generatePages(relation: Link[]): Record<string, Literal>[] {
13+
return relation.map((link) =>
1314
DataviewService.getDataviewAPI().page(link.path)
1415
);
16+
}
17+
18+
constructor(relation: Link[], action?: string, key?: string) {
19+
const consultedLinks = Rollup.generatePages(relation);
1520
this.pages = consultedLinks;
1621
this.action = action;
1722
this.key = key;
1823
}
24+
1925
public dispatch(): string {
26+
let result = "";
2027
switch (this.action) {
2128
case ROLLUP_ACTIONS.SUM:
22-
return this.sum();
29+
result = this.sum();
30+
break;
31+
case ROLLUP_ACTIONS.COUNT_ALL:
32+
result = this.countAll();
33+
break;
34+
case ROLLUP_ACTIONS.ORIGINAL_VALUE:
35+
result = this.originalValue();
36+
break;
2337
default:
2438
// No valid action found
2539
LOGGER.warn(`No valid action found for rollup: ${this.action}`);
26-
return "";
2740
}
41+
return result;
42+
}
43+
44+
public getPages(): Record<string, Literal>[] {
45+
return this.pages;
2846
}
2947

48+
/**
49+
* Iters over the pages and sums the values of the key
50+
* @returns
51+
*/
3052
private sum(): string {
3153
// Check if key is not truthy, return empty string
3254
if (!this.key) {
@@ -41,6 +63,25 @@ class Rollup {
4163
return sum.toString();
4264
}
4365

66+
/**
67+
* Obtains the number of pages of the relation with the key informed
68+
* @returns
69+
*/
70+
private countAll(): string {
71+
return this.pages.filter((page) => page[this.key]).length.toString();
72+
}
73+
74+
/**
75+
* Returns the original value map of the pages with the key informed
76+
* @returns
77+
*/
78+
private originalValue(): string {
79+
// TODO use local settings of the table instead of default settings
80+
return this.pages
81+
.filter((page) => page[this.key])
82+
.map((page) => ParseService.parseLiteral(page[this.key], InputType.MARKDOWN, DEFAULT_SETTINGS.local_settings)).join(", ");
83+
}
84+
4485
}
4586

4687
export default Rollup;

src/components/modals/columnSettings/handlers/rollups/RollupActionHandler.ts

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,41 @@ export class RollupActionHandler extends AbstractHandlerClass<ColumnSettingsHand
88
handle(columnHandlerResponse: ColumnSettingsHandlerResponse): ColumnSettingsHandlerResponse {
99
const { column, containerEl, columnSettingsManager } = columnHandlerResponse;
1010
const { view } = columnSettingsManager.modal;
11-
// select rollup action
12-
const rollup_action_options: Record<string, string> = {};
13-
Object.values(ROLLUP_ACTIONS).forEach((action) => {
14-
rollup_action_options[action] = action;
15-
});
16-
17-
const rollup_action_promise = async (value: string): Promise<void> => {
18-
// Persist on disk
19-
await view.diskConfig.updateColumnConfig(column.id, {
20-
rollup_action: value
11+
const { config } = column
12+
// Check if there is a relation associated
13+
if (config.asociated_relation_id) {
14+
// select rollup action
15+
const rollup_action_options: Record<string, string> = {};
16+
Object.values(ROLLUP_ACTIONS).forEach((action) => {
17+
rollup_action_options[action] = action;
2118
});
22-
columnSettingsManager.modal.enableReset = true;
23-
};
2419

25-
new Setting(containerEl)
26-
.setName(this.settingTitle)
27-
.setDesc('Select the action to perform on the rollup')
28-
.addSearch((cb) => {
29-
new StringSuggest(
30-
cb.inputEl,
31-
rollup_action_options
32-
);
33-
cb.setPlaceholder("Select action...")
34-
.setValue(column.config.rollup_action)
35-
.onChange(rollup_action_promise);
36-
});
20+
const rollup_action_promise = async (value: string): Promise<void> => {
21+
if (config.rollup_action !== value) {
22+
config.rollup_action = value;
23+
// Persist on disk
24+
await view.diskConfig.updateColumnConfig(column.id, {
25+
rollup_action: value
26+
});
27+
columnSettingsManager.modal.enableReset = true;
28+
// re-render column settings
29+
columnSettingsManager.reset(columnHandlerResponse);
30+
}
31+
};
32+
33+
new Setting(containerEl)
34+
.setName(this.settingTitle)
35+
.setDesc('Select the action to perform on the rollup')
36+
.addSearch((cb) => {
37+
new StringSuggest(
38+
cb.inputEl,
39+
rollup_action_options
40+
);
41+
cb.setPlaceholder("Select action...")
42+
.setValue(config.rollup_action)
43+
.onChange(rollup_action_promise);
44+
});
45+
}
3746
return this.goNext(columnHandlerResponse);
3847
}
3948
}

src/components/modals/columnSettings/handlers/rollups/RollupAsociatedRelationHandler.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,18 @@ export class RollupAsociatedRelationHandler extends AbstractHandlerClass<ColumnS
1818
avaliableRelations[col.id] = col.label;
1919
});
2020
const relation_selector_promise = async (value: string): Promise<void> => {
21-
config.asociated_relation_id = value;
22-
// Persist value
23-
await view.diskConfig.updateColumnConfig(column.id, {
24-
rollup_relation: value
25-
});
26-
columnSettingsManager.modal.enableReset = true;
21+
if (config.asociated_relation_id !== value) {
22+
config.asociated_relation_id = value;
23+
// Persist on disk
24+
await view.diskConfig.updateColumnConfig(column.id, {
25+
rollup_relation: value
26+
});
27+
columnSettingsManager.modal.enableReset = true;
28+
// re-render column settings
29+
columnSettingsManager.reset(columnHandlerResponse);
30+
}
2731
};
32+
2833
new Setting(containerEl)
2934
.setName(this.settingTitle)
3035
.setDesc('Select from the existing columns to rollup')
@@ -34,7 +39,7 @@ export class RollupAsociatedRelationHandler extends AbstractHandlerClass<ColumnS
3439
avaliableRelations
3540
);
3641
cb.setPlaceholder("Search Relation...")
37-
.setValue(column.config.asociated_relation_id)
42+
.setValue(config.asociated_relation_id)
3843
.onChange(relation_selector_promise);
3944
});
4045
return this.goNext(columnHandlerResponse);

src/components/modals/columnSettings/handlers/rollups/RollupKeyHandler.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export class RollupKeyHandler extends AbstractHandlerClass<ColumnSettingsHandler
1010
const { view, columnsState, configState } = columnSettingsManager.modal;
1111
const { config } = column
1212
const allColumns = columnsState.info.getAllColumns();
13+
1314
// TODO select key column (if action allows it)
1415
const relationColumn = allColumns.find((col) => col.id === config.asociated_relation_id);
1516
const rollup_key_promise = async (value: string): Promise<void> => {

src/helpers/Constants.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,5 +442,7 @@ export const DB_ICONS = Object.freeze({
442442
});
443443

444444
export const ROLLUP_ACTIONS = Object.freeze({
445-
SUM: 'sum',
445+
SUM: 'Summatory',
446+
COUNT_ALL: 'Count All',
447+
ORIGINAL_VALUE: 'Original Value',
446448
});

0 commit comments

Comments
 (0)