import { Count , Counts , Plugin , PluginData , PluginStats } from '../types.js' ; import { pluginStatsKeys } from './plugins-stats-keys.js' ; export function parseData ( pluginsData , stats , { handle , limit , sortComparator } ) { const filteredPluginsData = handle === null ? pluginsData : filterPluginsByHandle ( pluginsData , handle ) ; let pluginsSummary = [ ] ; for ( const pluginData of filteredPluginsData ) { const pluginSummaryCounts = { } as { [ key in keyof PluginStats ] : Count ; } ; for ( const key of pluginStatsKeys ) { const counts = [ ] ; for ( const statsItem of stats ) { const pluginStats = statsItem [ pluginData . id ] ; counts . push ( typeof pluginStats === 'undefined' ? 0 : pluginStats [ key ] ) ; } counts . push ( pluginData [ key ] ) ; pluginSummaryCounts [ key ] = { count : counts [ counts . length - 1 ] , deltas : computeDeltas ( counts ) , totalDelta : counts [ counts . length - 1 ] - counts [ 0 ] } ; } pluginsSummary . push ( { name : pluginData . name , publisher : pluginData . publisherName , ... pluginSummaryCounts } ) ; } pluginsSummary . sort ( sortComparator ) ; if ( typeof limit !== 'undefined' && limit !== - 1 ) { pluginsSummary = pluginsSummary . slice ( 0 , limit ) ; } return { plugins : pluginsSummary , totals : computeTotals ( pluginsSummary ) } ; } function filterPluginsByHandle ( pluginsData , handle ) { return pluginsData . filter ( function ( plugin ) { return plugin . publisherHandle === handle ; } ) ; } function computeDeltas ( counts ) { const result = [ ] ; counts . forEach ( function ( count , index ) { if ( index === 0 ) { return ; } result . push ( count - counts [ index - 1 ] ) ; } ) ; return result ; } function computeTotals ( pluginsSummary ) { const totals = { } as { [ key in keyof PluginStats ] : Count ; } ; for ( const key of pluginStatsKeys ) { totals [ key ] = { count : 0 , deltas : [ ] , totalDelta : 0 } ; for ( const plugin of pluginsSummary ) { totals [ key ] . count += plugin [ key ] . count ; plugin [ key ] . deltas . forEach ( function ( delta , index ) { if ( typeof totals [ key ] . deltas [ index ] === 'undefined' ) { totals [ key ] . deltas [ index ] = 0 ; } totals [ key ] . deltas [ index ] += delta ; } ) ; totals [ key ] . totalDelta += plugin [ key ] . totalDelta ; } } return totals ; }