You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
2.6 KiB

const cloud = require('wx-server-sdk');
//const got = require("got");
//const rp = require("request-promise")
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV
})
const config = {
submitPagesGapDay: 1,
clearPageHistoryGapDay: 30
};
const milliSecondsPerDay = 24*60*60*1000;
const collectionNameMap = {
pageHistory: "pageHistory"
}
const PagePathMap = {
shixun:"markdown/shixun/shixun/shixun",
path:"markdown/path/path/path",
mooc_case:"markdown/mooc_case/mooc_case/mooc_case",
competition:"markdown/competition/competition/competition"
}
exports.main = async (event, context) => {
const wxContext = cloud.getWXContext();
console.log(event);
let {TriggerName, Time} = event;
switch(TriggerName){
case "submitPages":{
return submitPages({Time});
}
case "clearPageHistory":{
return clearPageHistory({Time});
}
case "attandance":{
return attandance({Time});
}
}
}
async function submitPage({route, timeStart, timeEnd}){
const db = cloud.database();
const _ = db.command;
const pageHistoryCollection = db.collection(collectionNameMap.pageHistory);
let data = await pageHistoryCollection.aggregate()
.match({
page: route,
status: 200,
isCrawl: false,
time: _.lt(timeEnd).and(_.gte(timeStart))
})
.group({
_id:"$options"
})
.end();
if(data.list.length==0)
return "empty history: " + route;
let pages = data.list.map(i=>{
let query = Object.keys(i._id).map(k=>`${k}=${i._id[k]}`).join("&");
return {
path: route,
query
}
})
let res = await cloud.openapi.search.submitPages({pages});
console.log("submit pages success", route, pages.length, res, pages);
return "submit success: " + route + " " + pages.length;
}
async function submitPages({Time}){
let timeEnd = new Date(Time);
let timeStart = new Date(timeEnd.getTime() - config.submitPagesGapDay*milliSecondsPerDay);
// pages to be submitted
let routes = [
"shixun", "path", "mooc_case", "competition"
]
let promises = routes.map(i=>{
return submitPage({route: PagePathMap[i], timeStart, timeEnd})
})
return Promise.all(promises)
}
async function clearPageHistory({Time}){
console.log("clearPageHistory", Time);
let now = new Date(Time);
let timeEnd = new Date(now.getTime() - config.clearPageHistoryGapDay*milliSecondsPerDay);
const db = cloud.database();
const _ = cloud.command;
const pageHistoryCollection = db.collection(collectionNameMap.pageHistory);
let res = await pageHistoryCollection.where({
time: _.lt(timeEnd)
}).remove();
console.log("remove page History result: ", res);
return {code: 0, message:"success", data:res};
}