-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCustomer Chart.js
More file actions
213 lines (165 loc) · 5.77 KB
/
Customer Chart.js
File metadata and controls
213 lines (165 loc) · 5.77 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: cyan; icon-glyph: users;
const { debugFeatureEnabled, getFeature } = importModule("Feature");
const { themed, ColorMode } = importModule("Config Util");
const { LinearChart } = importModule("Linear Chart");
const { Logger, getLogger } = importModule("Logger");
const { tr } = importModule("Localization");
const { presentMedium } = importModule("UI");
const parameters = getArguments();
/**
* ENTRY POINT
*/
async function main() {
const repository = new CalendarChartDataRepository();
const chart = new LinearChart({
chart: {
data: await repository.getChartData(),
xField: "month",
yField: "eventCount",
showTooltips: true,
tooltipFontSize: 30,
gridLineWidth: 1,
gridColor: themed(new Color("FEFBFA10"), new Color("707070")),
tooltipColor: new Color("999")
}
}, parameters.mode);
presentMedium(chart.getWidget());
}
/**
* Used to get arguments provided by user.
*
* period - amount of months that should be shown in chart
* calendar - name of calendar from which events should be pulled
* mode - color mode, used for color changing
* trimBlank - whether trailing months without events should be displayed
*
* @return {Object} user defined script configuration
*/
function getArguments() {
const logger = getLogger();
const arguments = JSON.parse(args.widgetParameter) ?? {};
if (!arguments.trimBlank) {
arguments.trimBlank = false;
}
if (!arguments.ColorMode) {
arguments.mode = ColorMode.Dark;
}
if (!arguments.period) {
arguments.period = 6;
}
if (debugFeatureEnabled("trimBlank")) {
arguments.trimBlank = true;
}
if (debugFeatureEnabled("period")) {
arguments.period = getFeature("period");
}
if (debugFeatureEnabled("calendar")) {
arguments.calendar = getFeature("calendar");
}
if (debugFeatureEnabled("forceLightMode")) {
arguments.mode = ColorMode.Light;
}
if (!arguments.calendar) {
throw new Error("Calendar was not provided");
}
logger.debug("Using following script arguments", arguments);
return arguments;
}
/**
* Used to retrieve events from
* calendar.
*
* @class CalendarChartDataRepository
*/
class CalendarChartDataRepository {
#logger = new Logger(CalendarChartDataRepository.name);
static #FIRST_DAY_OF_MONTH = 1;
static #MONTH_NAMES = [
tr("customerChart_january"),
tr("customerChart_february"),
tr("customerChart_march"),
tr("customerChart_april"),
tr("customerChart_may"),
tr("customerChart_june"),
tr("customerChart_july"),
tr("customerChart_august"),
tr("customerChart_september"),
tr("customerChart_october"),
tr("customerChart_november"),
tr("customerChart_december")
];
/**
* Used to get data from calendar
* and then format it into a
* month name -> amount of event map.
*
* @return {Map<String, Number>} month -> amount map
* @memberof CalendarChartDataRepository
*/
async getChartData() {
const monthNames = CalendarChartDataRepository.#MONTH_NAMES;
const dateRange = this.#getDateRange();
const eventSet = [];
const calendar = await Calendar.forEventsByTitle(parameters.calendar);
for (let rangeId = 0; rangeId + 1 < dateRange.length; rangeId++) {
const startDate = dateRange[rangeId];
const endDate = dateRange[rangeId + 1];
const events = await CalendarEvent.between(
startDate,
endDate,
[calendar]
);
const event = {
month: monthNames[startDate.getMonth()],
eventCount: events.length
};
this.#logger.debug("Adding chart segment", event);
eventSet.push(event);
}
if (parameters.skipBlank) {
const eventSetCopy = eventSet;
for (let eventId = eventSetCopy.length - 1; eventId >= 0; eventId--) {
const event = eventSetCopy[eventId];
if (event.eventCount > 0) {
break;
}
const idx = eventSet.indexOf(event);
eventSet.splice(idx, 1);
this.#logger.debug("Removing blank segment", event);
}
}
return eventSet;
}
/**
* Used to get list of dates
* which are always first day of a
* certain month.
*
* Dates are generated based on the
* provided configuration by user.
*
* @return {List<Date>} list of dates for which events should be found
* @memberof CalendarChartDataRepository
*/
#getDateRange() {
const dates = new Array();
for (let monthIdx = parameters.period - 1; monthIdx >= 0; monthIdx--) {
let monthStartDate = new Date();
monthStartDate.setDate(CalendarChartDataRepository.#FIRST_DAY_OF_MONTH);
monthStartDate.setMonth(monthStartDate.getMonth() - monthIdx);
monthStartDate = new Date(
monthStartDate.toLocaleString('en-US', {
timeZone: getFeature(".timeZone")
})
);
this.#logger.debug("Adding date to chart ranges", {monthStartDate});
dates.push(monthStartDate);
}
dates.push(new Date());
return dates;
}
}
await main();
Script.complete();