import { isEmpty , isUndefined } from 'lodash' ; import moment from 'moment' ; import { PackageViewsStats } from '../types' ; interface ViewsPerDate { date : string ; total : number ; } const getLast30Days = ( ) : string [ ] => Array . from ( Array ( 30 ) . keys ( ) ) . map ( ( x ) => moment ( ) . subtract ( x , 'days' ) . format ( 'YYYY-MM-DD' ) ) ; const sumViewsPerVersions = ( stats , excludedVersions ? ) : number [ ] [ ] => { const last30Days = getLast30Days ( ) ; const statsVersions = Object . keys ( stats ) ; let versions = [ ... statsVersions ] ; if ( ! isUndefined ( excludedVersions ) ) { versions = statsVersions . filter ( ( version ) => ! excludedVersions . includes ( version ) ) ; } if ( versions . length === 0 ) return [ ] ; const versionsPerDates = last30Days . map ( ( date ) => { let views = 0 ; versions . forEach ( ( version ) => { views = views + ( stats [ version ] [ date ] | 0 ) ; } ) ; return { date : date , total : views } ; } ) ; return versionsPerDates . map ( ( vpd ) => { return [ moment ( vpd . date ) . unix ( ) * 1000 , vpd . total ] ; } ) ; } ; const getSeriesDataPerPkgVersionViews = ( stats , version ) : number [ ] [ ] => { const last30Days = getLast30Days ( ) ; if ( isEmpty ( stats ) || ! Object . keys ( stats ) . includes ( version ) ) return [ ] ; return last30Days . map ( ( date ) => { return [ moment ( date ) . unix ( ) * 1000 , stats [ version ] [ date ] || 0 ] ; } ) ; } ; export { getLast30Days , getSeriesDataPerPkgVersionViews , sumViewsPerVersions } ;