Skip to content

Commit 5059d26

Browse files
committed
writenpmstat.js: add mergeStoredData
1 parent 338054b commit 5059d26

File tree

2 files changed

+85
-12
lines changed

2 files changed

+85
-12
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"dependencies": {
3232
"npm-stat-api": "^1.0.0",
3333
"enum": "^3.0.0",
34-
"csv-writer": "^1.6.0"
34+
"csv-writer": "^1.6.0",
35+
"csv-parser": "^3.0.0"
3536
},
3637
"devDependencies": {
3738
"npm-run-all": "^4.1.5",

src/writenpmstat.js

Lines changed: 83 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const fs = require("fs");
22

33
const npm = require("npm-stat-api");
44
const Enum = require("enum");
5+
const csv = require("csv-parser");
56
const createCsvWriter = require("csv-writer").createArrayCsvWriter;
67

78
const StatDate = require("./statdate.js");
@@ -17,6 +18,7 @@ class WriteNpmStat {
1718
outDir;
1819

1920
#datePeriod;
21+
#mergeStoredData;
2022

2123
constructor(packageName, outDir) {
2224
if (!packageName) {
@@ -27,6 +29,7 @@ class WriteNpmStat {
2729
this.outDir = outDir;
2830

2931
this.#datePeriod = StatPeriod.year;
32+
this.#mergeStoredData = true;
3033
}
3134

3235
get packageName() {
@@ -41,6 +44,14 @@ class WriteNpmStat {
4144
this.#datePeriod = StatPeriod.get(datePeriod);
4245
}
4346

47+
get mergeStoredData() {
48+
return this.#mergeStoredData;
49+
}
50+
51+
set mergeStoredData(mergeStoredData) {
52+
this.#mergeStoredData = Boolean(mergeStoredData);
53+
}
54+
4455
getNpmStat(startDay, endDay) {
4556
const statDate = new StatDate(startDay, endDay);
4657
const days = WriteNpmStat.getDays(statDate.start, statDate.end);
@@ -84,13 +95,15 @@ class WriteNpmStat {
8495
return new Promise((resolve) => {
8596
const stats = this.getNpmStat(startDay, endDay);
8697
stats.then((stats) => {
87-
const processedStats = this.#groupStats(
98+
const grouped = this.#groupStats(
8899
stats,
89100
startDay,
90101
endDay,
91102
postfix
92103
);
93-
this.#writeStats(processedStats);
104+
this.#mergeStats(grouped).then((merged) => {
105+
this.#writeStats(merged);
106+
});
94107
return resolve();
95108
});
96109
});
@@ -99,7 +112,7 @@ class WriteNpmStat {
99112
#groupStats(stats, startDay, endDay, postfix) {
100113
const statDate = new StatDate(startDay, endDay);
101114
const days = WriteNpmStat.getDays(statDate.start, statDate.end);
102-
const processedStats = {};
115+
const grouped = {};
103116
if (this.datePeriod) {
104117
let substring;
105118
if (this.datePeriod === StatPeriod.year) {
@@ -114,36 +127,95 @@ class WriteNpmStat {
114127
const prefix = day.substring(0, substring);
115128
if (!initialized[prefix]) {
116129
initialized[prefix] = true;
117-
processedStats[prefix + "_" + postfix + ".csv"] = [
130+
grouped[prefix + "_" + postfix + ".csv"] = [
118131
[day, stats[day]],
119132
];
120133
} else {
121-
processedStats[prefix + "_" + postfix + ".csv"].push([
134+
grouped[prefix + "_" + postfix + ".csv"].push([
122135
day,
123136
stats[day],
124137
]);
125138
}
126139
});
127140
} else {
128-
processedStats[postfix + ".csv"] = [];
141+
grouped[postfix + ".csv"] = [];
129142
days.forEach((day) => {
130-
processedStats[postfix + ".csv"].push([day, stats[day]]);
143+
grouped[postfix + ".csv"].push([day, stats[day]]);
131144
});
132145
}
133-
console.log(processedStats);
134-
return processedStats;
146+
return grouped;
147+
}
148+
149+
#mergeStats(stats) {
150+
return new Promise((resolve) => {
151+
if (!this.mergeStoredData) {
152+
return resolve(stats);
153+
}
154+
const csvFiles = {};
155+
const csvFilesReady = [];
156+
for (const [key, value] of Object.entries(stats)) {
157+
const csvFileReady = this.#readCsv(key, value[0]);
158+
csvFilesReady.push(csvFileReady);
159+
csvFileReady.then((csvData) => {
160+
Object.assign(csvFiles, csvData);
161+
});
162+
}
163+
Promise.all(csvFilesReady).then(() => {
164+
for (const [key] of Object.entries(stats)) {
165+
console.log(csvFiles);
166+
console.log(key);
167+
if (csvFiles[key]) {
168+
stats[key] = csvFiles[key].concat(stats[key]);
169+
}
170+
}
171+
return resolve(stats);
172+
});
173+
});
174+
}
175+
176+
#readCsv(csvFile, firstNewLine) {
177+
return new Promise((resolve) => {
178+
const csvData = {};
179+
csvData[csvFile] = [];
180+
if (!this.outDir) {
181+
return resolve(csvData);
182+
}
183+
const csvFilePath = this.outDir + "/" + csvFile;
184+
fs.stat(csvFilePath, function (err) {
185+
if (err != null) {
186+
return resolve(csvData);
187+
}
188+
fs.createReadStream(csvFilePath)
189+
.pipe(csv())
190+
.on("data", (row) => {
191+
if (firstNewLine) {
192+
if (row.date < firstNewLine[0]) {
193+
csvData[csvFile].push([
194+
row.date,
195+
row.downloads,
196+
]);
197+
}
198+
}
199+
})
200+
.on("end", () => {
201+
return resolve(csvData);
202+
});
203+
});
204+
});
135205
}
136206

137207
#writeStats(stats) {
208+
console.log(stats);
138209
if (this.outDir) {
139210
fs.mkdir(this.outDir, { recursive: true }, (err) => {
140211
if (err) {
141212
throw err;
142213
}
143214
for (const [key, value] of Object.entries(stats)) {
215+
const csvFilePath = this.outDir + "/" + key;
144216
const csvWriter = createCsvWriter({
145-
path: this.outDir + "/" + key,
146-
header: ["date", "download"],
217+
path: csvFilePath,
218+
header: ["date", "downloads"],
147219
});
148220
csvWriter.writeRecords(value).catch((err) => {
149221
throw err;

0 commit comments

Comments
 (0)