Skip to content

Commit

Permalink
fix: fix lint by upgrading eslint
Browse files Browse the repository at this point in the history
- Original eslint does not support ESM
  • Loading branch information
MrOrz committed Sep 30, 2023
1 parent 5f11fc0 commit b815b59
Show file tree
Hide file tree
Showing 5 changed files with 1,421 additions and 774 deletions.
24 changes: 24 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module.exports = {
extends: [
'eslint:recommended',
'plugin:import/errors',
'plugin:import/warnings',
'prettier',
],
env: { node: true, es6: true },
plugins: ['prettier'],
parserOptions: {
sourceType: 'module',
ecmaVersion: 'latest',
},
rules: {
'prettier/prettier': [
'error',
{
trailingComma: 'es5',
singleQuote: true,
},
],
'no-console': 'off', // It's seed script! We use no-console a hell lot.
},
};
19 changes: 0 additions & 19 deletions .eslintrc.js

This file was deleted.

114 changes: 61 additions & 53 deletions dumpOpenData.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { pipeline } from 'stream/promises';
import { Readable } from 'stream';
import crypto from 'crypto';
import elasticsearch from '@elastic/elasticsearch';

// eslint-import-resolve does not support `exports` in package.json.
// eslint-disable-next-line import/no-unresolved
import { stringify as csvStringify } from 'csv-stringify/sync';
import JSZip from 'jszip';

Expand All @@ -19,10 +22,7 @@ const client = new elasticsearch.Client({
*/
function sha256(input) {
return input
? crypto
.createHash('sha256')
.update(input, 'utf8')
.digest('hex')
? crypto.createHash('sha256').update(input, 'utf8').digest('hex')
: '';
}

Expand Down Expand Up @@ -50,7 +50,7 @@ async function* scanIndex(index) {
for (const hit of scrollResult.hits.hits) {
processedCount += 1;
yield hit;
if(processedCount % 100000 === 0) {
if (processedCount % 100000 === 0) {
console.info(`${index}:\t${processedCount}/${totalCount}`);
}
}
Expand All @@ -73,43 +73,36 @@ async function* dumpArticles(articles) {
'createdAt',
'updatedAt',
'lastRequestedAt',
]
],
]);
for await (const { _source, _id } of articles) {
yield csvStringify([
[
_id,
_source.references.map(ref => ref.type).join(','),
_source.references.map((ref) => ref.type).join(','),
sha256(_source.userId),
_source.normalArticleReplyCount,
_source.appId,
_source.text,
_source.createdAt,
_source.updatedAt,
_source.lastRequestedAt,
]
],
]);
};
}
}

/**
* @param {AsyncIterable} articles
* @returns {Promise<string>} Generated CSV string
*/
async function* dumpArticleHyperlinks(articles) {
yield csvStringify([
['articleId', 'url', 'normalizedUrl', 'title'],
]);
yield csvStringify([['articleId', 'url', 'normalizedUrl', 'title']]);

for await (const { _source, _id } of articles) {
for(const hyperlink of _source.hyperlinks || []) {
for (const hyperlink of _source.hyperlinks || []) {
yield csvStringify([
[
_id,
hyperlink.url,
hyperlink.normalizedUrl,
hyperlink.title,
]
[_id, hyperlink.url, hyperlink.normalizedUrl, hyperlink.title],
]);
}
}
Expand All @@ -133,11 +126,11 @@ async function* dumpArticleCategories(articles) {
'status',
'createdAt',
'updatedAt',
]
],
]);

for await (const { _source, _id } of articles) {
for(const ac of _source.articleCategories || []) {
for (const ac of _source.articleCategories || []) {
yield csvStringify([
[
_id,
Expand All @@ -151,7 +144,7 @@ async function* dumpArticleCategories(articles) {
ac.status,
ac.createdAt,
ac.updatedAt,
]
],
]);
}
}
Expand All @@ -174,7 +167,7 @@ async function* dumpArticleReplies(articles) {
'status',
'createdAt',
'updatedAt',
]
],
]);

for await (const { _source, _id } of articles) {
Expand All @@ -191,7 +184,7 @@ async function* dumpArticleReplies(articles) {
ar.status,
ar.createdAt,
ar.updatedAt,
]
],
]);
}
}
Expand All @@ -216,7 +209,7 @@ async function* dumpReplies(replies) {
_source.appId,
_source.text,
_source.createdAt,
]
],
]);
}
}
Expand All @@ -226,19 +219,12 @@ async function* dumpReplies(replies) {
* @returns {Promise<string>} Generated CSV string
*/
async function* dumpReplyHyperlinks(replies) {
yield csvStringify([
['replyId', 'url', 'normalizedUrl', 'title']
]);
yield csvStringify([['replyId', 'url', 'normalizedUrl', 'title']]);

for await (const { _source, _id } of replies) {
for (const hyperlink of _source.hyperlinks || []) {
yield csvStringify([
[
_id,
hyperlink.url,
hyperlink.normalizedUrl,
hyperlink.title,
]
[_id, hyperlink.url, hyperlink.normalizedUrl, hyperlink.title],
]);
}
}
Expand Down Expand Up @@ -280,7 +266,7 @@ async function* dumpReplyRequests(replyRequests) {
'userIdsha256',
'appId',
'createdAt',
]
],
]);

for await (const { _source } of replyRequests) {
Expand All @@ -299,7 +285,7 @@ async function* dumpReplyRequests(replyRequests) {
sha256(_source.userId),
_source.appId,
_source.createdAt,
]
],
]);
}
}
Expand Down Expand Up @@ -331,7 +317,7 @@ async function* dumpArticleReplyFeedbacks(articleReplyFeedbacks) {
sha256(_source.userId),
_source.appId,
_source.createdAt,
]
],
]);
}
}
Expand All @@ -342,7 +328,17 @@ async function* dumpArticleReplyFeedbacks(articleReplyFeedbacks) {
*/
async function* dumpAnalytics(analytics) {
yield csvStringify([
['type', 'docId', 'date', 'lineUser', 'lineVisit', 'webUser', 'webVisit', 'liffUser', 'liffVisit'],
[
'type',
'docId',
'date',
'lineUser',
'lineVisit',
'webUser',
'webVisit',
'liffUser',
'liffVisit',
],
]);

for await (const { _source } of analytics) {
Expand All @@ -355,8 +351,8 @@ async function* dumpAnalytics(analytics) {
_source.stats.lineVisit,
_source.stats.webUser,
_source.stats.webVisit,
(_source.stats.liff || []).reduce((sum, {user}) => sum + user, 0),
(_source.stats.liff || []).reduce((sum, {visit}) => sum + visit, 0),
(_source.stats.liff || []).reduce((sum, { user }) => sum + user, 0),
(_source.stats.liff || []).reduce((sum, { visit }) => sum + visit, 0),
],
]);
}
Expand All @@ -367,11 +363,11 @@ async function* dumpAnalytics(analytics) {
* @returns {(source: AsyncIterable) => void}
*/
function writeFile(fileName) {
return source => {
return (source) => {
const zip = new JSZip();
zip.file(fileName, Readable.from(source), {binary: false});
zip.file(fileName, Readable.from(source), { binary: false });

return new Promise(resolve => {
return new Promise((resolve) => {
// Ref: https://stuk.github.io/jszip/documentation/howto/write_zip.html#in-nodejs
//
zip
Expand All @@ -394,18 +390,34 @@ function writeFile(fileName) {
* Main process
*/
pipeline(scanIndex('articles'), dumpArticles, writeFile('articles.csv'));
pipeline(scanIndex('articles'), dumpArticleReplies, writeFile('article_replies.csv'));
pipeline(scanIndex('articles'), dumpArticleHyperlinks, writeFile('article_hyperlinks.csv'));
pipeline(scanIndex('articles'), dumpArticleCategories, writeFile('article_categories.csv'));
pipeline(
scanIndex('articles'),
dumpArticleReplies,
writeFile('article_replies.csv')
);
pipeline(
scanIndex('articles'),
dumpArticleHyperlinks,
writeFile('article_hyperlinks.csv')
);
pipeline(
scanIndex('articles'),
dumpArticleCategories,
writeFile('article_categories.csv')
);

pipeline(scanIndex('replies'), dumpReplies, writeFile('replies.csv'));
pipeline(scanIndex('replies'), dumpReplyHyperlinks, writeFile('reply_hyperlinks.csv'));
pipeline(
scanIndex('replies'),
dumpReplyHyperlinks,
writeFile('reply_hyperlinks.csv')
);

pipeline(
scanIndex('replyrequests'),
dumpReplyRequests,
writeFile('reply_requests.csv')
)
);

pipeline(scanIndex('categories'), dumpCategories, writeFile('categories.csv'));

Expand All @@ -415,8 +427,4 @@ pipeline(
writeFile('article_reply_feedbacks.csv')
);

pipeline(
scanIndex('analytics'),
dumpAnalytics,
writeFile('analytics.csv')
);
pipeline(scanIndex('analytics'), dumpAnalytics, writeFile('analytics.csv'));
Loading

0 comments on commit b815b59

Please sign in to comment.