Improve auto-title generation for tracks, using date from filename if available

This commit is contained in:
Paul Bienkowski 2021-05-13 20:52:50 +02:00
parent ce2a27ed51
commit 7aee81dcee
3 changed files with 45 additions and 28 deletions

View file

@ -115,20 +115,18 @@ schema.pre('validate', async function (next) {
// 0..4 Night, 4..10 Morning, 10..14 Noon, 14..18 Afternoon, 18..22 Evening, 22..00 Night // 0..4 Night, 4..10 Morning, 10..14 Noon, 14..18 Afternoon, 18..22 Evening, 22..00 Night
// Two hour intervals // Two hour intervals
const DAYTIMES = [ const DAYTIMES = [
'Night', 'Night', // 0h - 2h
'Night', 'Night', // 2h - 4h
'Morning', 'Morning', // 4h - 6h
'Morning', 'Morning', // 6h - 8h
'Morning', 'Morning', // 8h - 10h
'Noon', 'Noon', // 10h - 12h
'Noon', 'Noon', // 12h - 14h
'Afternoon', 'Afternoon', // 14h - 16h
'Afternoon', 'Afternoon', // 16h - 18h
'Afternoon', 'Evening', // 18h - 20h
'Evening', 'Evening', // 20h - 22h
'Evening', 'Night', // 22h - 24h
'Evening',
'Night',
]; ];
function getDaytime(dateTime) { function getDaytime(dateTime) {
@ -199,6 +197,7 @@ class Track extends mongoose.Model {
this.processingStatus = 'pending'; this.processingStatus = 'pending';
this.processingLog = null; this.processingLog = null;
this.processingJobId = uuid(); this.processingJobId = uuid();
this.statistics = null;
await this.save(); await this.save();
@ -226,19 +225,31 @@ class Track extends mongoose.Model {
return; return;
} }
// for (const property of ['publicTrackData', 'trackData']) { // Try to figure out when this file was recorded. Either we have it in then
// if (this[property]) { // statistics, e.g. after parsing and processing the track, or we can maybe
// await this.populate(property).execPopulate(); // derive it from the filename.
// if (this[property].recordedAt) { let recordedAt = null;
// const dateTime = DateTime.fromJSDate(this[property].recordedAt);
// const daytime = getDaytime(dateTime);
// this.title = `${daytime} ride on ${dateTime.toLocaleString(DateTime.DATE_MED)}`;
// await this.save();
// return
// }
// }
// }
if (this.statistics && this.statistics.recordedAt != null) {
recordedAt = DateTime.fromJSDate(this.statistics.recordedAt);
} else if (this.originalFileName) {
const match = this.originalFileName.match(/^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}\.[0-9]{2}\.[0-9]{2}/);
if (match) {
recordedAt = DateTime.fromFormat(match[0], "yyyy-MM-dd'T'HH.mm.ss");
if (!recordedAt.isValid) {
recordedAt = null;
}
}
}
if (recordedAt) {
const daytime = getDaytime(recordedAt);
this.title = `${daytime} ride on ${recordedAt.toLocaleString(recordedAt.DATE_MED)}`;
await this.save();
return;
}
// Detecting recording date failed, use filename
if (this.originalFileName) { if (this.originalFileName) {
this.title = _.upperFirst(_.words(this.originalFileName.replace(/(\.obsdata)?\.csv$/, '')).join(' ')); this.title = _.upperFirst(_.words(this.originalFileName.replace(/(\.obsdata)?\.csv$/, '')).join(' '));
await this.save(); await this.save();

View file

@ -200,12 +200,13 @@ router.post(
} }
await track.save() await track.save()
await track.autoGenerateTitle()
if (fileBody) { if (fileBody) {
await track.queueProcessing(); await track.queueProcessing();
} }
await track.autoGenerateTitle()
return res.json({ track: track.toJSONFor(req.user) }); return res.json({ track: track.toJSONFor(req.user) });
}), }),
); );
@ -262,12 +263,13 @@ router.put(
} }
await track.save(); await track.save();
await track.autoGenerateTitle()
if (process) { if (process) {
await track.queueProcessing() await track.queueProcessing()
} }
await track.autoGenerateTitle()
return res.json({ track: track.toJSONFor(req.user) }); return res.json({ track: track.toJSONFor(req.user) });
}), }),
); );

View file

@ -101,6 +101,10 @@ queue.process('processTrack', async (job) => {
track.processingStatus = 'complete'; track.processingStatus = 'complete';
await track.save(); await track.save();
// Maybe we have found out the recording date, regenerate the automatic
// title (if not yet manually set)
await track.autoGenerateTitle()
} catch (err) { } catch (err) {
console.error('Processing failed:', err); console.error('Processing failed:', err);
track.processingLog += String(err) + '\n' + err.stack + '\n'; track.processingLog += String(err) + '\n' + err.stack + '\n';