2018-10-23 23:45:42 +08:00
|
|
|
'use strict';
|
|
|
|
|
2018-03-28 19:27:01 +08:00
|
|
|
module.exports = function setLastUpdatedTimeToLayergroup () {
|
|
|
|
return function setLastUpdatedTimeToLayergroupMiddleware (req, res, next) {
|
|
|
|
const { mapConfigProvider, analysesResults } = res.locals;
|
|
|
|
const layergroup = res.body;
|
|
|
|
|
2018-04-10 21:25:36 +08:00
|
|
|
mapConfigProvider.createAffectedTables((err, affectedTables) => {
|
2018-03-28 19:27:01 +08:00
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!affectedTables) {
|
2020-06-09 18:21:47 +08:00
|
|
|
res.locals.cache_buster = 0;
|
|
|
|
layergroup.layergroupid = `${layergroup.layergroupid}:${res.locals.cache_buster}`;
|
|
|
|
layergroup.last_updated = new Date(res.locals.cache_buster).toISOString();
|
|
|
|
|
2018-03-28 19:27:01 +08:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
var lastUpdateTime = affectedTables.getLastUpdatedAt();
|
|
|
|
|
|
|
|
lastUpdateTime = getLastUpdatedTime(analysesResults, lastUpdateTime) || lastUpdateTime;
|
|
|
|
|
|
|
|
// last update for layergroup cache buster
|
|
|
|
layergroup.layergroupid = layergroup.layergroupid + ':' + lastUpdateTime;
|
|
|
|
layergroup.last_updated = new Date(lastUpdateTime).toISOString();
|
|
|
|
|
2020-04-29 20:32:08 +08:00
|
|
|
res.locals.cache_buster = lastUpdateTime;
|
|
|
|
|
2018-03-28 19:27:01 +08:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function getLastUpdatedTime (analysesResults, lastUpdateTime) {
|
2018-03-28 19:27:01 +08:00
|
|
|
if (!Array.isArray(analysesResults)) {
|
|
|
|
return lastUpdateTime;
|
|
|
|
}
|
2019-10-22 01:07:24 +08:00
|
|
|
return analysesResults.reduce(function (lastUpdateTime, analysis) {
|
|
|
|
return analysis.getNodes().reduce(function (lastNodeUpdatedAtTime, node) {
|
2018-03-28 19:27:01 +08:00
|
|
|
var nodeUpdatedAtDate = node.getUpdatedAt();
|
|
|
|
var nodeUpdatedTimeAt = (nodeUpdatedAtDate && nodeUpdatedAtDate.getTime()) || 0;
|
|
|
|
return nodeUpdatedTimeAt > lastNodeUpdatedAtTime ? nodeUpdatedTimeAt : lastNodeUpdatedAtTime;
|
|
|
|
}, lastUpdateTime);
|
|
|
|
}, lastUpdateTime);
|
|
|
|
}
|