@@ -23,13 +23,15 @@ const StatPeriod = new Enum(
2323 * @property {string|null } [outDir] - path of the directory where
2424 * the gathered data will be saved into csv files
2525 * @property {StatPeriod } [datePeriod=year] - grouping of the statistics
26+ * @property {boolean } [writePackageName=false] - flag used to write the name of the package into a csv column
2627 * @property {boolean } [mergeStoredData=true] - flag used to merge actual npm statistics with previously stored
2728 */
2829class WriteNpmStat {
2930 #packageName;
3031 outDir ;
3132
3233 #datePeriod;
34+ #writePackageName;
3335 #mergeStoredData;
3436
3537 /**
@@ -47,6 +49,7 @@ class WriteNpmStat {
4749 this . outDir = outDir ;
4850
4951 this . #datePeriod = StatPeriod . year ;
52+ this . #writePackageName = false ;
5053 this . #mergeStoredData = true ;
5154 }
5255
@@ -62,6 +65,14 @@ class WriteNpmStat {
6265 this . #datePeriod = StatPeriod . get ( datePeriod ) ;
6366 }
6467
68+ get writePackageName ( ) {
69+ return this . #writePackageName;
70+ }
71+
72+ set writePackageName ( writePackageName ) {
73+ this . #writePackageName = Boolean ( writePackageName ) ;
74+ }
75+
6576 get mergeStoredData ( ) {
6677 return this . #mergeStoredData;
6778 }
@@ -128,7 +139,14 @@ class WriteNpmStat {
128139 }
129140 throw new Error ( "retryLimit reached" ) ;
130141 }
131- return resolve ( [ res . start , res . downloads ] ) ;
142+ const statKey = res . start ;
143+ const statValues = [ ] ;
144+ statValues . push ( res . start ) ;
145+ if ( this . writePackageName ) {
146+ statValues . push ( this . #packageName) ;
147+ }
148+ statValues . push ( res . downloads ) ;
149+ return resolve ( [ statKey , statValues ] ) ;
132150 } ) ;
133151 } ) ;
134152 }
@@ -155,7 +173,7 @@ class WriteNpmStat {
155173 * <br> - "%Y-%m-%d", for example "2022-12-31", which means to be collected until "2022-12-31"
156174 *
157175 * <br> - undefined, which means to be collected until the actual day
158- * @param {string|null } [endDate=npmstat] - csv file's postfix
176+ * @param {string|null } [endDate=npmstat] - postfix of the csv file
159177 * @returns {Promise } Promise object represents the npm statistics for a package
160178 */
161179 writeNpmStat ( startDate , endDate , postfix = "npmstat" ) {
@@ -246,6 +264,7 @@ class WriteNpmStat {
246264 return resolve ( csvData ) ;
247265 }
248266 const csvFilePath = this . outDir + "/" + csvFile ;
267+ const writePackageName = this . writePackageName ;
249268 fs . stat ( csvFilePath , function ( err ) {
250269 if ( err != null ) {
251270 return resolve ( csvData ) ;
@@ -255,10 +274,14 @@ class WriteNpmStat {
255274 . on ( "data" , ( row ) => {
256275 if ( firstNewLine ) {
257276 if ( row . date < firstNewLine [ 0 ] ) {
258- csvData [ csvFile ] . push ( [
259- row . date ,
260- row . downloads ,
261- ] ) ;
277+ const statKey = row . date ;
278+ const statValues = [ ] ;
279+ statValues . push ( row . date ) ;
280+ if ( writePackageName ) {
281+ statValues . push ( row . package ) ;
282+ }
283+ statValues . push ( row . downloads ) ;
284+ csvData [ csvFile ] . push ( [ statKey , statValues ] ) ;
262285 }
263286 }
264287 } )
@@ -270,7 +293,6 @@ class WriteNpmStat {
270293 }
271294
272295 #writeStats( stats ) {
273- console . log ( stats ) ;
274296 if ( this . outDir ) {
275297 fs . mkdir ( this . outDir , { recursive : true } , ( err ) => {
276298 if ( err ) {
@@ -280,9 +302,15 @@ class WriteNpmStat {
280302 const csvFilePath = this . outDir + "/" + key ;
281303 const csvWriter = createCsvWriter ( {
282304 path : csvFilePath ,
283- header : [ "date" , "downloads" ] ,
305+ header : this . writePackageName
306+ ? [ "date" , "package" , "downloads" ]
307+ : [ "date" , "downloads" ] ,
308+ } ) ;
309+ const postProcessedStats = [ ] ;
310+ value . forEach ( ( stat ) => {
311+ postProcessedStats . push ( stat [ 1 ] ) ;
284312 } ) ;
285- csvWriter . writeRecords ( value ) . catch ( ( err ) => {
313+ csvWriter . writeRecords ( postProcessedStats ) . catch ( ( err ) => {
286314 throw err ;
287315 } ) ;
288316 }
0 commit comments