<commit_before>'use strict';

/**
 * Controller for the BreakOut-Member-Page.
 */

const Promise = require('bluebird');
const GoogleSpreadsheet = require('google-spreadsheet');
const co = require('co');
const _ = require('lodash');
const fs = require('co-fs-extra');

const config = {
  doc_id: process.env.FRONTEND_GDRIVE_DOCUMENT_ID,
  client_email: process.env.FRONTEND_GDRIVE_CLIENT_EMAIL,
  private_key: process.env.FRONTEND_GDRIVE_PRIVATE_KEY
};

/**
 * GET route for the BreakOut-Member-Page.
 * @param language
 * @param res
 */
module.exports.teamPage = (language, res) => co(function* () {
  const cachePath = ROOT + '/rendered/cache/teams.json.cache';

  const fetchMemberList = () => co(function* () {
    const doc = new GoogleSpreadsheet(config.doc_id);
    var credsJson = {
      client_email: config.client_email,
      private_key: config.private_key.replace(/\\n/g, '\n')
    };

    logger.info('Trying to authenticate with Google Docs');
    yield Promise.promisify(doc.useServiceAccountAuth)(credsJson);
    logger.info('Authenticated with Google Docs');

    logger.info('Trying to get info from Google Docs');
    const info = yield Promise.promisify(doc.getInfo)();
    logger.info('Got info from Google Docs');
    const sheet = info.worksheets[0];

    logger.info('Trying to get all rows from sheet');
    const rows = yield Promise.promisify(sheet.getRows)({
      offset: 3
    });
    logger.info('Got all rows from sheet');

    const allMember = _.sortBy(rows.reduce((init, curr) => _.concat(init, {
      name: curr.name,
      surname: curr.surname,
      url: curr.link,
      role: curr.role,
      active: curr.active
    }), []), m => m.surname);

    return [
      _.filter(allMember, m => m.active === 'ja'),
      _.filter(allMember, m => m.active === 'nein')
    ];
  }).catch(ex => {
    throw ex;
  });

  let finalMembers;

  const cacheExists = yield fs.exists(cachePath);

  // Cache file does exist
  if (cacheExists && process.env.NODE_ENV === 'production') {
    const fileStats = yield fs.stat(cachePath);

    // Cache file is outdated
    if ((new Date(fileStats.mtime).getDay()) != (new Date).getDay()) {
      logger.info('File', cachePath, 'is out of date, renewing');

      const member = yield fetchMemberList();

      yield fs.writeJson(cachePath, member);
      finalMembers = member;
    } else { // Cache file is up to date
      finalMembers = yield fs.readJson(cachePath);
    }
  } else { // Cache file does not exist
    const member = yield fetchMemberList();
    yield fs.writeJson(cachePath, member);
    finalMembers = member;
  }

  res.render('static/team/content', {
    layout: 'master',
    member: finalMembers,
    language: language<commit_msg>adds changes from member page<commit_after>'use strict';

/**
 * Controller for the BreakOut-Member-Page.
 */

const Promise = require('bluebird');
const GoogleSpreadsheet = require('google-spreadsheet');
const co = require('co');
const _ = require('lodash');
const fs = require('co-fs-extra');

const config = requireLocal('config/config.js');

/**
 * GET route for the BreakOut-Member-Page.
 * @param language
 * @param res
 */
module.exports.teamPage = (language, res) => co(function*() {
  const cachePath = ROOT + '/rendered/cache/teams.json.cache';

  const fetchMemberList = () => co(function* () {
    const doc = new GoogleSpreadsheet(config.gdrive.document_id);
    var credsJson = {
      client_email: config.gdrive.client_email,
      private_key: config.gdrive.private_key.replace(/\\n/g, '\n')
    };

    logger.info('Trying to authenticate with Google Docs');
    yield Promise.promisify(doc.useServiceAccountAuth)(credsJson);
    logger.info('Authenticated with Google Docs');

    logger.info('Trying to get info from Google Docs');
    const info = yield Promise.promisify(doc.getInfo)();
    logger.info('Got info from Google Docs');
    const sheet = info.worksheets[0];

    logger.info('Trying to get all rows from sheet');
    const rows = yield Promise.promisify(sheet.getRows)({
      offset: 3
    });
    logger.info('Got all rows from sheet');

    const allMember = _.sortBy(rows.reduce((init, curr) => _.concat(init, {
      name: curr.name,
      surname: curr.surname,
      url: curr.link,
      role: curr.role,
      active: curr.active
    }), []), m => m.surname);

    return [
      _.filter(allMember, m => m.active === 'ja'),
      _.filter(allMember, m => m.active === 'nein')
    ];
  }).catch(ex => {
    throw ex;
  });

  let finalMembers;

  const cacheExists = yield fs.exists(cachePath);

  // Cache file does exist
  if (cacheExists && process.env.NODE_ENVIRONMENT === 'prod') {
    const fileStats = yield fs.stat(cachePath);

    // Cache file is outdated
    if ((new Date(fileStats.mtime).getDay()) != (new Date).getDay()) {
      logger.info('File', cachePath, 'is out of date, renewing');

      const member = yield fetchMemberList();

      yield fs.writeJson(cachePath, member);
      finalMembers = member;
    } else { // Cache file is up to date
      finalMembers = yield fs.readJson(cachePath);
    }
  } else { // Cache file does not exist
    const member = yield fetchMemberList();
    yield fs.writeJson(cachePath, member);
    finalMembers = member;
  }

  res.render('static/team/content', {
    layout: 'master',
    member: finalMembers,
    language: language
{'fraction_non_alphanumeric': 0.14402985074626865, 'fraction_numerical': 0.0007462686567164179, 'mean_word_length': 3.2888, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7132682', 'n_tokens_mistral': 1701, 'n_tokens_neox': 1664, 'n_words': 422}