Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/backtrack user #204

Merged
merged 4 commits into from Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 38 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -47,7 +47,7 @@
"graphql": "^15.0.0",
"graphql-scalar-objectid": "^0.1.2",
"graphql-tools": "^4.0.7",
"jsrsasign": "^8.0.13",
"jsrsasign": "^8.0.20",
"koa": "^2.5.0",
"koa-passport": "^4.1.3",
"koa-router": "^7.4.0",
Expand All @@ -74,6 +74,7 @@
"@babel/plugin-syntax-dynamic-import": "^7.2.0",
"@babel/plugin-syntax-import-meta": "^7.2.0",
"@babel/preset-env": "^7.9.0",
"@cofacts/line-bot-log-parser": "0.0.1",
"@material/theme": "^3.1.0",
"@material/typography": "^3.1.0",
"@smui/button": "^1.0.0-beta.20",
Expand Down Expand Up @@ -131,4 +132,4 @@
"engines": {
"node": "12.x"
}
}
}
94 changes: 94 additions & 0 deletions src/database/backtrack.js
@@ -0,0 +1,94 @@
import UserSettings from './models/userSettings';
import UserArticleLink from './models/userArticleLink';
import { parseToJson } from '@cofacts/line-bot-log-parser';

/**
* Usage:
*
* 1. Prepare the rumors-line-bot-logs folder, and its structure should be like below.
* ```
* rumors-line-bot-logs $ tree
* .
* ├── 201705
* │ ├── 23
* │ │ ├── 00
* │ │ │ ├── 0009.70036095121560.log
* │ │ │ ├── 0021.70036095121560.log
* │ │ │ ├── 0035.70036095121560.log
* │ │ │ ├── 0058.70036095121560.log
* │ │ │ ├── 0114.70036095121560.log
* │ │ │ ├── 0125.70036095121560.log
* │ │ │ ├── 0200.70036095121560.log
* │ │ │ [...]
* │ │ ├── 01
* │ │ │ [...]
* │ │ ├── 02
* │ │ │ [...]
* │ │ ├── 03
* ```
*
* 2. run
* ```bash
* npm run build:server
* USER_ID=1 node -r dotenv/config build/database/backtrack.js
* ```
*
* @param {string} path a path of rumors-line-bot-logs folder
*
*/

async function main(logFilePath) {
const parse = async path => {
const status = {
processedLog: 0,
writtenUserSettings: 0,
writtenUserArticleLink: 0,
};

await parseToJson(path, async (data, next) => {
const timestamp = new Date(data['timestamp']);
const userId = data['userId'];
const selectedArticleId = data['context.data.selectedArticleId'];

status.processedLog++;
if (userId) {
await UserSettings.findOrInsertByUserId(userId);
status.writtenUserSettings++;

if (selectedArticleId) {
await UserArticleLink.createOrUpdateByUserIdAndArticleId(
userId,
selectedArticleId,
{
lastViewedAt: timestamp,
}
);
status.writtenUserArticleLink++;
}
}

if (status.processedLog % 1000 === 0) {
console.log(status);
}

next();
});

console.log(status);
};

for (let y = 2017; y <= 2020; y++) {
for (let m = 1; m <= 12; m++) {
const path = `${logFilePath}/${y}${Math.floor(m / 10)}${m % 10}/**/*.log`;
console.log(path);
await parse(path);
}
}
}

if (require.main === module) {
const path = '../rumors-line-bot-logs';
main(path)
.then(console.log)
.catch(console.error);
}