<commit_before>const
    errors = require('feathers-errors'),
    {locator} = require('locator.js'),
    {isClipMediaRefWithTitle} = require('constants.js'),
    {getLoggedInUserInfo} = require('middleware/auth/getLoggedInUserInfo.js'),
    {queryGoogleApiData} = require('services/googleapi/googleapi.js'),
    {isNonAnonUser} = require('util.js'),
    _ = require('lodash');

function routes () {
  const app = this,
        Models = locator.get('Models'),
        {MediaRef} = Models,
        PodcastService = locator.get('PodcastService'),
        EpisodeService = locator.get('EpisodeService'),
        ClipService = locator.get('ClipService'),
        PlaylistService = locator.get('PlaylistService'),
        UserService = locator.get('UserService');

  app.get('/', getLoggedInUserInfo, function (req, res) {

    let pageIndex = req.query.page || 1;
    let offset = (pageIndex * 10) - 10;
    let params = {};

    let queryObj = {
      metrics: 'ga:uniquePageviews',
      dimensions: 'ga:pagePath',
      startDate: '7daysAgo',
      endDate: 'today',
      sort: '-ga:uniquePageviews',
      // maxResults: only 10 will load on a page, but we don't want
      // clips without titles to appear in the list
      maxResults: 20,
      startIndex: offset || 1,
      filters: 'ga:pagePath=~/clips'
    }

    // NOTE: If no clips were returned by Google API, then in the catch statement
    // populate the home page with all the most recent clips created that have a title
    return new Promise((resolve, reject) => {
      queryGoogleApiData(resolve, reject, queryObj)
    })
    .then(data => {
      // Extract clip ids from data to pass into db query
      let clipIdArray = [];
      data.forEach((clipItem) => {
        let urlPath = clipItem[0];
        let clipId = urlPath.replace('/clips/', '');
        clipIdArray.push(clipId);
      });

      params.sequelize = {
        where: {
          id: clipIdArray,
          $and: isClipMediaRefWithTitle
        },
        limit: 10
      };

      return ClipService.find(params)
        .then(clips => {
          // TODO: handle 404 if beyond range of page object
          res.render('home/index.html', {
            clips: clips,
            pageIndex: pageIndex,
            showNextButton: clips.length < 1 ? false : true,
            currentPage: 'Home Page'
          });
        })
        .catch(e => {
          console.log(e);
        });<commit_msg>Sort the clips based on Google API results after the find clip service runs<commit_after>const
    errors = require('feathers-errors'),
    {locator} = require('locator.js'),
    {isClipMediaRefWithTitle} = require('constants.js'),
    {getLoggedInUserInfo} = require('middleware/auth/getLoggedInUserInfo.js'),
    {queryGoogleApiData} = require('services/googleapi/googleapi.js'),
    {isNonAnonUser} = require('util.js'),
    _ = require('lodash');

function routes () {
  const app = this,
        Models = locator.get('Models'),
        {MediaRef} = Models,
        PodcastService = locator.get('PodcastService'),
        EpisodeService = locator.get('EpisodeService'),
        ClipService = locator.get('ClipService'),
        PlaylistService = locator.get('PlaylistService'),
        UserService = locator.get('UserService');

  app.get('/', getLoggedInUserInfo, function (req, res) {

    let pageIndex = req.query.page || 1;
    let offset = (pageIndex * 10) - 10;
    let params = {};

    let queryObj = {
      metrics: 'ga:uniquePageviews',
      dimensions: 'ga:pagePath',
      startDate: '7daysAgo',
      endDate: 'today',
      sort: '-ga:uniquePageviews',
      // maxResults: only 10 will load on a page, but we don't want
      // clips without titles to appear in the list
      maxResults: 20,
      startIndex: offset || 1,
      filters: 'ga:pagePath=~/clips'
    }

    // NOTE: If no clips were returned by Google API, then in the catch statement
    // populate the home page with all the most recent clips created that have a title
    return new Promise((resolve, reject) => {
      queryGoogleApiData(resolve, reject, queryObj)
    })
    .then(data => {
      // Extract clip ids from data to pass into db query
      let clipIdArray = [];
      data.forEach((clipItem) => {
        let urlPath = clipItem[0];
        let clipId = urlPath.replace('/clips/', '');
        clipIdArray.push(clipId);
      });

      params.sequelize = {
        where: {
          id: clipIdArray,
          $and: isClipMediaRefWithTitle
        },
        limit: 10
      };

      return ClipService.find(params)
        .then(clips => {

          // Sort the clips in order of most visited according to Google API
          var sortedClips = _.sortBy(clips, function (clip) {
            return clipIdArray.indexOf(clip.id);
          });

          // TODO: handle 404 if beyond range of page object
          res.render('home/index.html', {
            clips: sortedClips,
            pageIndex: pageIndex,
            showNextButton: clips.length < 1 ? false : true,
            currentPage: 'Home Page'
          });
        })
        .catch(e => {
          console.log(e);
        });
{'fraction_non_alphanumeric': 0.12504861921431348, 'fraction_numerical': 0.007001166861143524, 'mean_word_length': 2.3483072916666665, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2302316', 'n_tokens_mistral': 1553, 'n_tokens_neox': 1485, 'n_words': 392}