randomDay will now only switch to days that have photos

Signed-off-by: James Ketrenos <james_git@ketrenos.com>
This commit is contained in:
James Ketr 2018-10-17 20:37:57 -07:00
parent 2e98b93078
commit b2a1e214e9
2 changed files with 105 additions and 15 deletions

View File

@ -531,6 +531,14 @@
type: String, type: String,
value: "loading" value: "loading"
}, },
days: {
type: Array,
value: []
},
daysGrouped: {
type: Array,
value: []
},
date: { date: {
type: String, type: String,
value: window.moment().format("YYYY-MM-DD") value: window.moment().format("YYYY-MM-DD")
@ -541,7 +549,8 @@
"widthChanged(calcWidth)", "widthChanged(calcWidth)",
"modeChanged(mode)", "modeChanged(mode)",
"dateChanged(date)", "dateChanged(date)",
"userChanged(user)" "userChanged(user)",
"daysChanged(days)"
], ],
disableLogin: function(username, password) { disableLogin: function(username, password) {
@ -598,7 +607,17 @@
}, },
gotoRandomDay: function() { 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) { login: function(event) {
@ -728,6 +747,7 @@
this.setActions(); this.setActions();
this.resetPhotos(); this.resetPhotos();
this._loadAlbums(); this._loadAlbums();
this._loadDays();
this._loadPhotos(); this._loadPhotos();
}, },
@ -782,6 +802,7 @@
this.path = path; this.path = path;
this.resetPhotos(); this.resetPhotos();
this._loadAlbums(); this._loadAlbums();
this._loadDays();
this._loadPhotos(); this._loadPhotos();
}, },
@ -1447,6 +1468,7 @@
Polymer.dom(this.$.thumbnails).innerHTML = ""; Polymer.dom(this.$.thumbnails).innerHTML = "";
this.next = false; this.next = false;
this._loadAlbums(); this._loadAlbums();
this._loadDays();
this._loadPhotos(); this._loadPhotos();
}, },
@ -1585,6 +1607,75 @@
}.bind(this, path)); }.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) { onResize: function(event) {
this.debounce("resize", function() { this.debounce("resize", function() {
this.triggerVisibilityChecks(); this.triggerVisibilityChecks();

View File

@ -24,22 +24,21 @@ const router = express.Router();
*/ */
router.get("/", function(req, res/*, next*/) { router.get("/*", function(req, res/*, next*/) {
let start = moment(req.query.start || null) || null, const path = decodeURI(req.url).replace(/\?.*$/, "").replace(/^\//, ""),
end = moment(req.query.end || null) || null; 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";
let query = "SELECT DATE(taken) AS date,COUNT(*) AS count FROM photos WHERE path LIKE :path GROUP BY DATE(taken) ORDER BY date DESC"; console.log("Looking for daily photo counts in " + path);
return photoDB.sequelize.query(query, { return photoDB.sequelize.query(query, {
replacements: { replacements: {
path: req.url.replace(/\?.*$/, "") + "%" path: path + "%"
}, },
type: photoDB.Sequelize.QueryTypes.SELECT type: photoDB.Sequelize.QueryTypes.SELECT,
raw: true
}).then(function(days) { }).then(function(days) {
return res.status(200).json(days);
let results = {
items: days
};
return res.status(200).json(results);
}).catch(function(error) { }).catch(function(error) {
console.error("Query failed: " + query); console.error("Query failed: " + query);
return Promise.reject(error); return Promise.reject(error);