|
| 1 | +const npm = require("npm-stat-api"); |
| 2 | +const Enum = require("enum"); |
| 3 | + |
| 4 | +const StatDate = require("./statdate.js"); |
| 5 | + |
| 6 | +const StatPeriod = new Enum( |
| 7 | + ["year", "month", "day", null], |
| 8 | + { ignoreCase: true }, |
| 9 | + { freeze: true } |
| 10 | +); |
| 11 | + |
| 12 | +class WriteNpmStat { |
| 13 | + #packageName; |
| 14 | + |
| 15 | + #datePeriod; |
| 16 | + |
| 17 | + constructor(packageName) { |
| 18 | + if (!packageName) { |
| 19 | + throw new Error("packageName is a required argument"); |
| 20 | + } |
| 21 | + |
| 22 | + this.#packageName = packageName; |
| 23 | + |
| 24 | + this.#datePeriod = StatPeriod.year; |
| 25 | + } |
| 26 | + |
| 27 | + get packageName() { |
| 28 | + return this.#packageName; |
| 29 | + } |
| 30 | + |
| 31 | + get datePeriod() { |
| 32 | + return this.#datePeriod; |
| 33 | + } |
| 34 | + |
| 35 | + set datePeriod(datePeriod) { |
| 36 | + this.#datePeriod = StatPeriod.get(datePeriod); |
| 37 | + } |
| 38 | + |
| 39 | + getNpmStat(startDay, endDay) { |
| 40 | + const statDate = new StatDate(startDay, endDay); |
| 41 | + const days = WriteNpmStat.getDays(statDate.start, statDate.end); |
| 42 | + return new Promise((resolve) => { |
| 43 | + const stats = []; |
| 44 | + days.forEach((day) => { |
| 45 | + stats.push(this.#getStat(day, 100)); |
| 46 | + }); |
| 47 | + Promise.all(stats).then((stats) => { |
| 48 | + resolve(Object.fromEntries(stats)); |
| 49 | + }); |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + static getDays(startDay, endDay) { |
| 54 | + const arr = []; |
| 55 | + const dt = new Date(startDay); |
| 56 | + for (dt; dt <= new Date(endDay); dt.setDate(dt.getDate() + 1)) { |
| 57 | + arr.push(StatDate.formatDate(new Date(dt))); |
| 58 | + } |
| 59 | + return arr; |
| 60 | + } |
| 61 | + |
| 62 | + #getStat(day, retryLimit, retryCount) { |
| 63 | + retryLimit = retryLimit || Number.MAX_VALUE; |
| 64 | + retryCount = Math.max(retryCount || 0, 0); |
| 65 | + return new Promise((resolve) => { |
| 66 | + npm.stat(this.packageName, day, day, (err, res) => { |
| 67 | + if (err) { |
| 68 | + if (retryCount < retryLimit) { |
| 69 | + return this.#getStat(day, retryLimit, retryCount + 1); |
| 70 | + } |
| 71 | + throw new Error("retryLimit reached"); |
| 72 | + } |
| 73 | + return resolve([res.start, res.downloads]); |
| 74 | + }); |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + writeNpmStat(startDay, endDay, postfix = "npmstat") { |
| 79 | + return new Promise((resolve) => { |
| 80 | + const stats = this.getNpmStat(startDay, endDay); |
| 81 | + stats.then((stats) => { |
| 82 | + this.#groupStats(stats, startDay, endDay, postfix); |
| 83 | + return resolve(); |
| 84 | + }); |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + #groupStats(stats, startDay, endDay, postfix) { |
| 89 | + const statDate = new StatDate(startDay, endDay); |
| 90 | + const days = WriteNpmStat.getDays(statDate.start, statDate.end); |
| 91 | + const processedStats = {}; |
| 92 | + if (this.datePeriod) { |
| 93 | + let substring; |
| 94 | + if (this.datePeriod === StatPeriod.year) { |
| 95 | + substring = 4; |
| 96 | + } else if (this.datePeriod === StatPeriod.month) { |
| 97 | + substring = 7; |
| 98 | + } else if (this.datePeriod === StatPeriod.month) { |
| 99 | + substring = 10; |
| 100 | + } |
| 101 | + const initialized = {}; |
| 102 | + days.forEach((day) => { |
| 103 | + const prefix = day.substring(0, substring); |
| 104 | + if (!initialized[prefix]) { |
| 105 | + initialized[prefix] = true; |
| 106 | + processedStats[prefix + "_" + postfix + ".csv"] = [ |
| 107 | + [day, stats[day]], |
| 108 | + ]; |
| 109 | + } else { |
| 110 | + processedStats[prefix + "_" + postfix + ".csv"].push([ |
| 111 | + day, |
| 112 | + stats[day], |
| 113 | + ]); |
| 114 | + } |
| 115 | + }); |
| 116 | + } else { |
| 117 | + processedStats[postfix + ".csv"] = []; |
| 118 | + days.forEach((day) => { |
| 119 | + processedStats[postfix + ".csv"].push([day, stats[day]]); |
| 120 | + }); |
| 121 | + } |
| 122 | + console.log(processedStats); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +module.exports = WriteNpmStat; |
0 commit comments