-
Notifications
You must be signed in to change notification settings - Fork 0
/
pomo_view.js
124 lines (112 loc) · 4 KB
/
pomo_view.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// This is for the dashborad view chart for the pomodoro journal
const {utils} = customJS;
function pushDatasetsToChart(dataset, chart) {
// Check if data structure is as expected
if (Array.isArray(chart?.data?.datasets)) {
chart.data.datasets.push(dataset);
} else {
// Throw an error if the structure is not as expected
throw new Error("data is not structured as expected");
}
}
function pushLabelsToChart(newLabel, data) {
// Check if data structure is as expected
if (Array.isArray(data?.data?.labels)) {
data.data.labels.push(newLabel);
} else {
// Throw an error if the structure is not as expected
throw new Error("data is not structured as expected");
}
}
function initBarChartData() {
const ChartData = {
type: "bar",
data: {
labels: [],
datasets: []
}
};
return ChartData;
}
function getChartLabels(chartData) {
return chartData.data.labels;
}
function restructureDateTags(dateTag) {
// restructure the data to be used in chart
// input data: {date: {tag: count}}
// output data: {tag: [count_date1, count_date2, ...]}
const restructedData = {};
// get all tags
const allTags = utils.unique(Object.values(dateTag).map(obj => Object.keys(obj)).flat());
console.log("allTags: ");
console.log(allTags);
const dates = Object.keys(dateTag);
allTags.forEach(tag => {
restructedData[tag] = dates.map(date => dateTag[date][tag] || 0);
});
return restructedData;
}
async function getBarChartData() {
const pomoEmoji = "🍅";
let pomoData = {}
let tagCounts = {};
let chartData = initBarChartData();
console.log("starting...");
try {
const pages = await dv.pages('"Journal"');
// filter pages by file being named by "year-month-day weekday"
const daily_pages = pages.filter((page) => {
return page.file.name.match(/\d{4}-\d{2}-\d{2} \w+/)
});
console.log("daily_pages: ");
console.log(daily_pages);
for (const page of daily_pages) {
console.log("loading page " + page.file.name + "...");
const content = await dv.io.load(page.file.path);
console.log("page loaded");
console.log("pushing labels to chart...");
pushLabelsToChart(page.file.name, chartData);
console.log("content:");
console.log(content);
let pomoLines = utils.getEmojiLines(content, pomoEmoji);
console.log("pomoLines.length: " + pomoLines.length);
pomoData[page.file.name] = {
total: pomoLines.length,
}
const tags = utils.extractTags(content);
const tagFrequency = utils.getFrequency(tags);
utils.unique(tags).forEach(tag => {
pomoData[page.file.name][tag] = tagFrequency[tag];
});
}
const colorPalette = utils.setColorPalette();
let colorIndex = 0;
console.log("start to push data to chart data...");
console.log(tagCounts);
// restructure the data to be used in chart
console.log("pomoData: ");
console.log(pomoData);
pomoData = restructureDateTags(pomoData);
console.log("restructured pomoData: ");
console.log(pomoData);
Object.keys(pomoData).forEach((tag) => {
console.log("tag: " + tag)
const dataset = {
label: tag,
data: pomoData[tag],
backgroundColor: colorPalette[colorIndex % colorPalette.length]
};
console.log(dataset)
pushDatasetsToChart(dataset, chartData);
colorIndex++;
});
return chartData;
} catch (error) {
console.error("An error occurred: ", error);
throw error; // Re-throw the error to be handled by the caller
}
}
await getBarChartData().then((chartData) => {
input.win.renderChart(chartData, input.el);
}
);