Skip to content

Commit

Permalink
feat: 紀錄當天登入的人數
Browse files Browse the repository at this point in the history
定時23:58分執行並log到mongodb

refactor(passport): 刪除無用的code
  • Loading branch information
Chinlinlee committed Jun 28, 2021
1 parent 65ccd18 commit 69e2417
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 30 deletions.
83 changes: 58 additions & 25 deletions getTrafic.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,67 @@ const path = require('path');
const moment = require('moment-timezone');
const _ = require('lodash');
const schedule = require('node-schedule');
const { MongoExe } = require('./models/common/data');
const config = require('./config/config');

let traficLog = fs.readFileSync(path.join(__dirname , "default.log") , {encoding : 'utf-8'});
traficLog = traficLog.replace(/ UTC -> IsLoggedIn:true /gim , "");
let lines = traficLog.split("\r\n");

let items = [];
for (line of lines) {
let [date , time , account] = line.split(' ');
date = date.replace("," , "");
let obj = {
datetime : `${date} ${time}` ,
account : account
function getTrafic () {
let traficLog = fs.readFileSync(path.join(__dirname , "default.log") , {encoding : 'utf-8'});
traficLog = traficLog.replace(/ UTC -> IsLoggedIn:true /gim , "");
let lines = traficLog.split("\r\n");

let items = [];
for (line of lines) {
let [date , time , account] = line.split(' ');
date = date.replace("," , "");
let obj = {
datetime : `${date} ${time}` ,
account : account
}
items.push(obj);
}
items.push(obj);
let hitItem = items.filter(v => {
if (v.account == "UTC") return false;
try {
let taipeiTime = moment.utc(v.datetime , "YYYY.MM.DD HH:mm:ss.SSSS").tz("Asia/Taipei");
let isoFormatTime = moment(taipeiTime , "YYYY.MM.DD").format('YYYY-MM-DD');
let dateNow = moment(new Date()).format('YYYY-MM-DD');
return moment(isoFormatTime , 'YYYY-MM-DD').isSameOrAfter(dateNow);
} catch (e) {
console.log(v.datetime);
return false;
}
})

hitItem = _.uniqBy(hitItem , 'account');
(async ()=> {
try {
const mongoConn = await MongoExe();
await mongoConn.db(config.MONGODB.db).collection('loginLog').findOneAndUpdate(
{ id : moment(new Date()).format('YYYY-MM-DD')} ,
{
$set : {
num : hitItem.length,
refreshTime: new Date()
}
} ,
{
upsert : true
}
);
console.log("today trafic log finished");
process.exit(0);
} catch (e) {
console.error(e);
process.exit(1);
}
})();
console.log("login stu: " , hitItem);
console.log(moment(new Date()).format('YYYY-MM-DD') , ":" , hitItem.length);
}
let hitItem = items.filter(v => {
if (v.account == "UTC") return false;
try {
let taipeiTime = moment.utc(v.datetime , "YYYY.MM.DD HH:mm:ss.SSSS").tz("Asia/Taipei");
let isoFormatTime = moment(taipeiTime , "YYYY.MM.DD").format('YYYY-MM-DD');
let dateNow = moment(new Date()).format('YYYY-MM-DD');
return moment(isoFormatTime , 'YYYY-MM-DD').isSameOrAfter(dateNow);
} catch (e) {
console.log(v.datetime);
return false;
}
})

hitItem = _.uniqBy(hitItem , 'account');
console.log(hitItem.length);

schedule.scheduleJob("0 58 23 * * *" , function () {
getTrafic();
});
getTrafic();
6 changes: 1 addition & 5 deletions models/users/passport.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
var LocalStrategy = require('passport-local').Strategy;
const fs = require('fs');
const DBdata = require('../common/data.js')
const School_Auth = require('./School_Auth.js');
const request = require('request');
const myFunc = require('../../routes/My_Func');
const cheerio = require('cheerio');
const nodeFetch = require('node-fetch');
Expand Down Expand Up @@ -90,7 +87,6 @@ module.exports = async function(passport)
//req.session.myJar = myReqObj.jar;
let logingResultCode = loginresult.split('_');
let j = myFunc.getJar(req);
let signOffJar = myFunc.getSignOffJar(req);
if (!logingResultCode.includes("true"))
{
console.log("error pwd");
Expand All @@ -110,7 +106,6 @@ module.exports = async function(passport)
let $ = cheerio.load(homeBody);
let Profile = $("#ctl00_tableProfile tr");


let stuInfo = [];
for (let i = 0 ; i < Profile.length ; i++)
{
Expand Down Expand Up @@ -173,6 +168,7 @@ module.exports = async function(passport)
if (!STNO) {
return done(null , false , req.flash('error',"無法取得資訊,請重新登入,若還是無法登入請聯繫作者。"));
}

await School_Auth.ilmsAuth(username , password , req);
return done(null ,username);
}));
Expand Down

0 comments on commit 69e2417

Please sign in to comment.