-
Notifications
You must be signed in to change notification settings - Fork 101
/
DailyLogAggregator.js
417 lines (329 loc) · 11.7 KB
/
DailyLogAggregator.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/**************************************
* Configurations
*************************************/
// NOTE: This script uses the Cache script (https://github.com/yaylinda/scriptable/blob/main/Cache.js)
// Make sure to add the Cache script in Scriptable as well!
// NOTE 2: When setting up the widget, make sure to put something (anything) in the "Widget Params" field.
// This is the name of the directory (under Scriptable) in iCloud, to store the daily log files.
const DAILY_LOG_CACHE_NAME = 'DailyLogs';
// Text colors to display for the various field categories (default: white)
const CATEGORY_CONFIGURATIONS = {
// TODO - Replace me with your own data!
SAMPLE_CAT_1: {
color: '#7AE7B9',
},
SAMPLE_CAT_2: {
color: '#5BD2F0',
},
SAMPLE_CAT_3: {
color: '#FF6663',
},
};
// Fields to collect data for in the Alert
// Alert shows up when you click on the Widget
const FIELDS = [
// TODO - Replace me with your own data!
{ label: 'Enough Sleep', category: 'SAMPLE_CAT_1' },
{ label: 'Shower', category: 'SAMPLE_CAT_1' },
{ label: 'Exercised', category: 'SAMPLE_CAT_1' },
{ label: 'Vitamins', category: 'SAMPLE_CAT_1' },
{ label: 'Vegetables', category: 'SAMPLE_CAT_2' },
{ label: 'Caffeine', category: 'SAMPLE_CAT_2' },
{ label: 'Happy', category: 'SAMPLE_CAT_3' },
{ label: 'Sad', category: 'SAMPLE_CAT_3' },
];
// Fields to show aggregated data for in the Widget
// NOTE: Make sure this is a proper subset of the items in FIELDS
const FIELDS_TO_AGGREGATE = [
// TODO - Replace me with your own data!
{ label: 'Enough Sleep', category: 'SAMPLE_CAT_1' },
{ label: 'Shower', category: 'SAMPLE_CAT_1' },
{ label: 'Exercised', category: 'SAMPLE_CAT_1' },
{ label: 'Vitamins', category: 'SAMPLE_CAT_1' },
{ label: 'Vegetables', category: 'SAMPLE_CAT_2' },
{ label: 'Caffeine', category: 'SAMPLE_CAT_2' },
{ label: 'Happy', category: 'SAMPLE_CAT_3' },
{ label: 'Sad', category: 'SAMPLE_CAT_3' },
];
// Date format for the Alert title. Ex: "Fri, Nov 6"
const DATE_TEXT_FORMAT = new Intl.DateTimeFormat('en-US', {
weekday: 'short',
month: 'short',
day: 'numeric',
});
// Date format for the data grid. Showing the DOW as a letter.
const SHORT_DATE_FORMAT = new Intl.DateTimeFormat('en-US', {
weekday: 'narrow',
})
// Maximum number of days to fetch historical data for
// NOTE: If you increase this, make sure to play around with the font sizes so that the data fits
const NUM_DAYS = 7;
// Padding around the widget
const PADDING = 5;
// Horizontal padding under the title text line
const TITLE_TEXT_SPACING = 10;
// Title text size in the widget
const TITLE_TEXT_SIZE = 15;
// Vertical padding between columns
const VERTICAL_TEXT_SPACING = 5;
// Vertical padding between columns of data grid
const VERTICAL_DATA_GRID_SPACING = 2;
// Text size
const TEXT_SIZE = 10;
// Whether or not to use a background image for the widget (if false, use gradient color)
const USE_BACKGROUND_IMAGE = false;
/**************************************
* Initial Setups
*************************************/
// Import Cache module
const Cache = importModule('Cache');
const DAILY_LOG_CACHE = new Cache(DAILY_LOG_CACHE_NAME);
/**
* Convenience function to add days to a Date.
*
* @param {*} days The number of days to add
*/
Date.prototype.addDays = function (days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
};
// Fetch data from DailyLogs cache
const data = await fetchData();
// Show alert with current data (if running script in app)
if (config.runsInApp) {
const alert = createAlert(data.dateText, data.today);
const response = await alert.present();
if (response === 0) {
console.log('Cancel was pressed... doing nothing');
} else if (response === 1) {
console.log('Submit was pressed');
const updatedFields = [];
for (let i = 0; i < FIELDS.length; i++) {
const value = alert.textFieldValue(i);
updatedFields.push({
label: FIELDS[i].label,
category: FIELDS[i].category,
value,
});
}
DAILY_LOG_CACHE.write(data.dateKey, updatedFields);
data.today = updatedFields;
data.fields[0] = updatedFields;
}
}
// Create widget with data
const widget = createWidget(data);
// Set background image of widget, if flag is true
if (USE_BACKGROUND_IMAGE) {
// Determine if our image exists and when it was saved.
const files = FileManager.local();
const path = files.joinPath(files.documentsDirectory(), 'daily-log-aggregated-widget-background');
const exists = files.fileExists(path);
// If it exists and we're running in the widget, use photo from cache
if (exists && config.runsInWidget) {
widget.backgroundImage = files.readImage(path);
// If it's missing when running in the widget, use a gradient black/dark-gray background.
} else if (!exists && config.runsInWidget) {
const bgColor = new LinearGradient();
bgColor.colors = [new Color("#29323c"), new Color("#1c1c1c")];
bgColor.locations = [0.0, 1.0];
widget.backgroundGradient = bgColor;
// But if we're running in app, prompt the user for the image.
} else if (config.runsInApp && !args.widgetParameter){
const img = await Photos.fromLibrary();
widget.backgroundImage = img;
files.writeImage(path, img);
}
}
// Set widget
Script.setWidget(widget);
Script.complete();
/**************************************
* Alert
*************************************/
function createAlert(dateText, fields) {
// console.log(`Creating alert, dateText=${dateText}, fields=${JSON.stringify(fields)}`);
const alert = new Alert();
alert.title = `Daily Log for ${dateText}`;
fields.forEach(field => {
alert.addTextField(field.label, `${field.value}`);
});
alert.addAction('Cancel'); // 0
alert.addAction('Submit'); // 1
return alert;
}
/**************************************
* Widget
*************************************/
function createWidget(data) {
console.log(`Creating widget data=${JSON.stringify(data)}`);
// Widget
const widget = new ListWidget();
const bgColor = new LinearGradient();
bgColor.colors = [new Color("#29323c"), new Color("#1c1c1c")];
bgColor.locations = [0.0, 1.0];
widget.backgroundGradient = bgColor;
widget.setPadding(PADDING, PADDING, PADDING, PADDING);
// Main stack
const stack = widget.addStack();
stack.layoutVertically();
// Top stack
const topStack = stack.addStack();
topStack.layoutHorizontally();
// Title stack and text of widget
const titleTextLine = topStack.addText(`Daily Logs`);
titleTextLine.textColor = Color.white();
titleTextLine.font = new Font('Menlo-Bold', TITLE_TEXT_SIZE);
topStack.addSpacer(TITLE_TEXT_SPACING);
// Main bottom stack
// Data will be added column by column
const bottomStack = stack.addStack();
bottomStack.layoutHorizontally();
// Field label column
addColumnToStack(bottomStack, [{ value: '' }]
.concat(FIELDS_TO_AGGREGATE
.map(field => ({
value: field.label,
color: CATEGORY_CONFIGURATIONS[field.category].color,
}))));
bottomStack.addSpacer(VERTICAL_TEXT_SPACING);
// Use to keep track of how many days each field was completed
const fieldCompletionCounts = {};
FIELDS_TO_AGGREGATE.forEach(field => fieldCompletionCounts[field.label] = 0);
// Collect data to format one column for each day's data
const dataByDay = data.fields.reverse();
let date = parseDateFromKey(data.dateKey);
date = date.addDays(-1 * (dataByDay.length - 1));
// One column per day
dataByDay.forEach(dayData => {
const columnData = [];
// First cell: date label
columnData.push({
value: `${SHORT_DATE_FORMAT.format(date)}`,
isBold: true,
align: 'center',
});
// One cell per field data
FIELDS_TO_AGGREGATE.forEach(field => {
const fieldFromDay = dayData.find(d => d.label === field.label);
if (fieldFromDay) {
columnData.push({ value: fieldFromDay.value ? '✅' : '❌' });
fieldCompletionCounts[fieldFromDay.label] += fieldFromDay.value ? 1 : 0;
} else {
columnData.push({ value: '?'});
}
});
addColumnToStack(bottomStack, columnData);
bottomStack.addSpacer(VERTICAL_DATA_GRID_SPACING);
date = date.addDays(1);
});
// Vertical spacing between data grid column and completion percentage
bottomStack.addSpacer(VERTICAL_TEXT_SPACING);
// Column of percentage for each field
const completionData = [{ value: ' '}];
FIELDS_TO_AGGREGATE.forEach(field => {
const completionCount = fieldCompletionCounts[field.label];
let completionRate = (completionCount * 100 / data.fields.length).toFixed(0);
// Add some left padding for numbers of 1 or 2 digits
if (completionRate.length === 1) {
completionRate = ' ' + completionRate;
} else if (completionRate.length === 2) {
completionRate = ' ' + completionRate;
}
completionData.push({
value: `[${completionRate}%]`,
align: 'right',
color: completionCount === data.fields.length ? '#66ff00' : '#ffffff',
});
});
addColumnToStack(bottomStack, completionData);
return widget;
}
function addColumnToStack(stack, columnData) {
// console.log(`columnData: ${JSON.stringify(columnData)}`);
const column = stack.addStack();
column.layoutVertically();
column.spacing = VERTICAL_TEXT_SPACING;
columnData.forEach(cd => addCellToColumn(column, cd));
}
function addCellToColumn(column, data) {
const cell = column.addStack();
cell.layoutVertically();
addTextToStack(cell, data);
}
function addTextToStack(stack, data) {
console.log(`addTextToStack, data: ${JSON.stringify(data)}`);
const { value, color, isBold, align } = data;
const textLine = stack.addText(value);
textLine.font = new Font(`Menlo${isBold ? '-Bold' : ''}`, TEXT_SIZE);
if (color) {
textLine.textColor = new Color(color);
}
if (align === 'center') {
textLine.centerAlignText();
} else if (align === 'right') {
textLine.rightAlignText();
} else {
textLine.leftAlignText();
}
}
/**************************************
* Fetch Data
*************************************/
async function fetchData() {
// Determine day
let date = new Date();
if (date.getHours() < 5) {
date = date.addDays(-1);
}
// Format today's date for display
const dateText = DATE_TEXT_FORMAT.format(date);
const dateKey = `${date.getFullYear()}_${padNumber(date.getMonth())}_${padNumber(date.getDate())}`;
const fields = [];
for (let i = 0; i < NUM_DAYS; i++) {
const dateCacheKey = `${date.getFullYear()}_${padNumber(date.getMonth())}_${padNumber(date.getDate())}`;
let dayData = await DAILY_LOG_CACHE.read(dateCacheKey);
if (!dayData) {
if (i === 0) {
// This means it's a new day, so we need to initialize field data and create a new log entry
console.log(`Cache miss for key=${dateCacheKey}, creating new log entry...`);
dayData = initializeFields();
DAILY_LOG_CACHE.write(dateCacheKey, dayData);
} else {
console.log(`Cache miss for key=${dateCacheKey}, breaking out of loop...`);
break;
}
}
fields.push(dayData);
date = date.addDays(-1);
}
console.log(`Got ${fields.length} days of data`);
return {
today: fields[0],
fields,
dateText,
dateKey,
};
}
function initializeFields() {
return FIELDS.map((field) => ({
label: field.label,
category: field.category,
value: '',
}));
}
function padNumber(num) {
return num < 10 ? `0${num}` : num;
}
/**************************************
* Aggregate Data
*************************************/
function parseDateFromKey(dateKey) {
const dateParts = dateKey.split('_');
return new Date(
parseInt(dateParts[0]), // year
parseInt(dateParts[1]), // month (0-indexed)
parseInt(dateParts[2]), // day of month
);
}