From b2a1e214e97fe0eac3b6351fa9fe0d0a98cf288e Mon Sep 17 00:00:00 2001 From: James Ketrenos Date: Wed, 17 Oct 2018 20:37:57 -0700 Subject: [PATCH] randomDay will now only switch to days that have photos Signed-off-by: James Ketrenos --- frontend/src/ketr-photos/ketr-photos.html | 97 ++++++++++++++++++++++- server/routes/days.js | 23 +++--- 2 files changed, 105 insertions(+), 15 deletions(-) diff --git a/frontend/src/ketr-photos/ketr-photos.html b/frontend/src/ketr-photos/ketr-photos.html index 58e10aa..6c74975 100755 --- a/frontend/src/ketr-photos/ketr-photos.html +++ b/frontend/src/ketr-photos/ketr-photos.html @@ -472,7 +472,7 @@ year: { type: String, value: window.moment().format("YYYY") - }, + }, actions: { type: Array, value: [] @@ -531,6 +531,14 @@ type: String, value: "loading" }, + days: { + type: Array, + value: [] + }, + daysGrouped: { + type: Array, + value: [] + }, date: { type: String, value: window.moment().format("YYYY-MM-DD") @@ -541,7 +549,8 @@ "widthChanged(calcWidth)", "modeChanged(mode)", "dateChanged(date)", - "userChanged(user)" + "userChanged(user)", + "daysChanged(days)" ], disableLogin: function(username, password) { @@ -598,7 +607,17 @@ }, gotoRandomDay: function() { - this.date = window.moment().format("YYYY-") + window.moment(Math.ceil(Math.random() * 365), "DDD").format("MM-DD"); + var day; + if (this.daysGrouped.length == 0) { + day = window.moment(Math.ceil(Math.random() * 365), "DDD").format("MM-DD"); + this.photosToday = 0; + } else { + var groupedDay = this.daysGrouped[Math.floor(this.daysGrouped.length * Math.random())]; + this.photosToday = groupedDay.count; + day = groupedDay.date; + } + this.date = window.moment().format("YYYY-") + day; + console.log(this.date, this.photosToday); }, login: function(event) { @@ -728,6 +747,7 @@ this.setActions(); this.resetPhotos(); this._loadAlbums(); + this._loadDays(); this._loadPhotos(); }, @@ -782,6 +802,7 @@ this.path = path; this.resetPhotos(); this._loadAlbums(); + this._loadDays(); this._loadPhotos(); }, @@ -1447,6 +1468,7 @@ Polymer.dom(this.$.thumbnails).innerHTML = ""; this.next = false; this._loadAlbums(); + this._loadDays(); this._loadPhotos(); }, @@ -1585,6 +1607,75 @@ }.bind(this, path)); }, + daysChanged: function(days) { + var grouped = {}, date; + /* Build an Object with properties for each MM-DD that contains photos */ + days.forEach(function(day) { + date = day.date.replace(/[0-9]*-/, ""); + if (!grouped.date) { + grouped[date] = day.count; + } else { + grouped[date] += day.count; + } + }); + + /* Build a dense array of the days identified in previous step */ + var daysGrouped = []; + for (var key in grouped) { + daysGrouped.push({ + date: key, + count: grouped[key] + }); + } + this.daysGrouped = daysGrouped; + }, + + _loadDays: function() { + if (this.mode == "login" || this.mode == "loading") { + return; + } + + if (this.loadingDays == true) { + return; + } + this.loadingDays = true; + + var path = this.path || ""; + window.fetch("api/v1/days/" + path, function(path, error, xhr) { + this.loadingDays = false; + + if (!this.user) { + return; + } + + if (path != (this.path || "")) { + console.log("Skipping results for old query. Triggering re-fetch of days for new path."); + this._loadDays(); + return; + } + + if (error) { + console.log("Error loading days: " + (this.path || "")); + console.error(JSON.stringify(error, null, 2)); + return; + } + + var results; + try { + results = JSON.parse(xhr.responseText); + } catch (___) { + this.$.toast.text = "Unable to load/parse days list."; + this.$.toast.setAttribute("error", true); + this.$.toast.updateStyles(); + this.$.toast.show(); + console.error("Unable to parse photos"); + return; + } + + this.days = results; + }.bind(this, path)); + }, + onResize: function(event) { this.debounce("resize", function() { this.triggerVisibilityChecks(); diff --git a/server/routes/days.js b/server/routes/days.js index 47b94b2..b8ffcdc 100644 --- a/server/routes/days.js +++ b/server/routes/days.js @@ -24,22 +24,21 @@ const router = express.Router(); */ -router.get("/", function(req, res/*, next*/) { - let start = moment(req.query.start || null) || null, - end = moment(req.query.end || null) || null; - - let query = "SELECT DATE(taken) AS date,COUNT(*) AS count FROM photos WHERE path LIKE :path GROUP BY DATE(taken) ORDER BY date DESC"; +router.get("/*", function(req, res/*, next*/) { + const path = decodeURI(req.url).replace(/\?.*$/, "").replace(/^\//, ""), + query = "SELECT DATE(photos.taken) AS date,COUNT(photos.id) AS count FROM albums " + + "JOIN photos ON photos.albumId=albums.id " + + "WHERE albums.path LIKE :path GROUP BY DATE(photos.taken) ORDER BY date DESC"; + + console.log("Looking for daily photo counts in " + path); return photoDB.sequelize.query(query, { replacements: { - path: req.url.replace(/\?.*$/, "") + "%" + path: path + "%" }, - type: photoDB.Sequelize.QueryTypes.SELECT + type: photoDB.Sequelize.QueryTypes.SELECT, + raw: true }).then(function(days) { - - let results = { - items: days - }; - return res.status(200).json(results); + return res.status(200).json(days); }).catch(function(error) { console.error("Query failed: " + query); return Promise.reject(error);