src/chartmogul/chartmogul-feeder.js
/**
   * The middleware in charge of fetching the Biggest Plans.
   *
   * @param {Object} config - The config require for the fetch
   * @return {Promise} The promisified geckoFormatted result of the fetch
   *
   */
The middleware in charge of fetching the Biggest Plans.
@param {Object} config - The config require for the fetch
@return {Promise} The promisified geckoFormatted result of the fetch
The middleware in charge of fetching the Biggest Plans . @param { Object } config - The config require for the fetch @return { Promise } The promisified geckoFormatted result of the fetch
async fetchMostPlansPurchased(config) {
    let keyForCache = KEY_BIGGEST_PLANS;
    let listPlans = await this.requestChartMogulFor('/plans');
    if (config.filter) {
      let filter = config.filter.toLowerCase();
      const onExclude = filter.startsWith('!');
      filter = (onExclude) ? filter.substr(1) : filter;
      keyForCache += filter;
      listPlans = listPlans.filter((p) => {
        const contains = p.name.toLowerCase().includes(filter);
        return (onExclude) ? !contains : contains;
      });
    }
    const [...customersCountByPlan] = await Promise.all(
      listPlans.map((plan) => {
        return this.requestChartMogulFor('/metrics/customer-count', {
          'start-date': config['start-date'],
          'end-date': config['end-date'],
          'plans': plan.name,
        });
      })
    );
    let [lastBiggest, ...biggestCustByPlans] = await Promise.all([
      this.getCached(keyForCache),
      ...customersCountByPlan.map(({
        entries,
      }, i) => {
        return {
          'plan': listPlans[i],
          'total': entries.reduce((tot, {
            customers: c,
          }) => tot += c, 0),
        };
      }).filter(({
        total,
      }) => total > 1) 
      .sort((p1, p2) => p2.total - p1.total)
      .slice(0, 5),
    ]);
    if (lastBiggest === null) { 
      lastBiggest = [];
    }
    const items = biggestCustByPlans.map(({
      total,
      plan,
    }) => {
      const item = {
        'label': plan.name,
        'value': total,
      };
      const prevRank = lastBiggest.findIndex(
        ({
          plan: old,
        }) => old.uuid === plan.uuid
      );
      if (prevRank !== -1) {
        const diff = plan.total - lastBiggest[prevRank].total;
        if (diff > 0) {
          item['previous_rank'] = 10; 
        } else {
          if (diff < 0) {
            item['previous_rank'] = 1; 
          }
        }
      }
      return [item.label, item.value, item['previous_rank']];
    });
    this.setInCache(keyForCache, biggestCustByPlans);
    return GeckoFormatter.toLeaderboard(items);
  }
async fetchMostPlansPurchased ( config ) { let keyForCache = KEY_BIGGEST_PLANS ; let listPlans = await this . requestChartMogulFor ( '/plans' ) ; if ( config . filter ) { let filter = config . filter . toLowerCase ( ) ; const onExclude = filter . startsWith ( '!' ) ; filter = ( onExclude ) ? filter . substr ( 1 ) : filter ; keyForCache += filter ; listPlans = listPlans . filter ( ( p ) => { const contains = p . name . toLowerCase ( ) . includes ( filter ) ; return ( onExclude ) ? ! contains : contains ; } ) ; } const [ ... customersCountByPlan ] = await Promise . all ( listPlans . map ( ( plan ) => { return this . requestChartMogulFor ( '/metrics/customer-count' , { 'start-date' : config [ 'start-date' ] , 'end-date' : config [ 'end-date' ] , 'plans' : plan . name , } ) ; } ) ) ; let [ lastBiggest , ... biggestCustByPlans ] = await Promise . all ( [ this . getCached ( keyForCache ) , ... customersCountByPlan . map ( ( { entries , } , i ) => { return { 'plan' : listPlans [ i ] , 'total' : entries . reduce ( ( tot , { customers : c , } ) => tot += c , 0 ) , } ; } ) . filter ( ( { total , } ) => total > 1 ) . sort ( ( p1 , p2 ) => p2 . total - p1 . total ) . slice ( 0 , 5 ) , ] ) ; if ( lastBiggest === null ) { lastBiggest = [ ] ; } const items = biggestCustByPlans . map ( ( { total , plan , } ) => { const item = { 'label' : plan . name , 'value' : total , } ; const prevRank = lastBiggest . findIndex ( ( { plan : old , } ) => old . uuid === plan . uuid ) ; if ( prevRank !== - 1 ) { const diff = plan . total - lastBiggest [ prevRank ] . total ; if ( diff > 0 ) { item [ 'previous_rank' ] = 10 ; } else { if ( diff < 0 ) { item [ 'previous_rank' ] = 1 ; } } } return [ item . label , item . value , item [ 'previous_rank' ] ] ; } ) ; this . setInCache ( keyForCache , biggestCustByPlans ) ; return GeckoFormatter . toLeaderboard ( items ) ; }
The middleware in charge of fetching the Biggest Plans.
The middleware in charge of fetching the Biggest Plans .
// Recup all plans // For each plan, GET the customers' count with this plan // Sum, filter & sort plansCustomerCount // Keep out the customised plans // First fresh fetch // Format the result for the LeaderBoard Widget // Check the previous rank // ? More Subscribers to this plan // Move UP // 10 : Only 5 plans thus, the last one is Rank 10 (last position) // Set to 10 randomly, could have been [6 : Wiser choice :-)] // Drop DOWN