<commit_before>const { getAllUsers } = require('../../storage/users');

const MS_PER_DAY = 1000 * 60 * 60 * 24;
const daysAgo = date => Math.floor((Date.now() - new Date(date).getTime()) / MS_PER_DAY);

const getDistribution = list => list.reduce((obj, stat) => {
  obj[stat] = (obj[stat] || 0) + 1;
  return obj;
}, {});

const makeAppAnalysis = (models, info) => {
  const getResources = Promise.all([
    getAllUsers(),
    models.Session.findAll({ state: { $not: 'deleted' } })
  ]);
  getResources.then(([users, sessions]) => {
    const userSessions = users.map(user => ({ user, sessions: sessions.filter(session => session.owner === user.user_id) }));
    const viewStats = sessions.map(session => (session.analytics ? (session.analytics.views || 0) : 0));
    const totalViews = viewStats.reduce((a, b) => a + b);
    const analysis = {
      timestamp: new Date(),
      version: process.env.npm_package_version,
      info,
      stats: {
        user: {
          total: users.length,
          active: users.filter(user => daysAgo(user.last_login) > 28).length,
          activityDistribution: getDistribution(users.map(user => daysAgo(user.last_login))),
          published: userSessions.filter(data => data.sessions.length).length
        },
        session: {
          total: sessions.length,
          published: sessions.filter(session => session.state === 'published').length,
          publishedYesterday: sessions.filter(session => session.state === 'published' && daysAgo(session.updatedAt) === 1).length,
          views: {
            total: totalViews,
            distribution: getDistribution(viewStats)
          }
        }
      }
    };
    models.Analysis.create({ analysis });
  });
};
<commit_msg>track git hash on deployment<commit_after>const { getAllUsers } = require('../../storage/users');

const MS_PER_DAY = 1000 * 60 * 60 * 24;
const daysAgo = date => Math.floor((Date.now() - new Date(date).getTime()) / MS_PER_DAY);

const getDistribution = list => list.reduce((obj, stat) => {
  obj[stat] = (obj[stat] || 0) + 1;
  return obj;
}, {});

const makeAppAnalysis = (models, info) => {
  const getResources = Promise.all([
    getAllUsers(),
    models.Session.findAll({ state: { $not: 'deleted' } })
  ]);
  getResources.then(([users, sessions]) => {
    const userSessions = users.map(user => ({ user, sessions: sessions.filter(session => session.owner === user.user_id) }));
    const viewStats = sessions.map(session => (session.analytics ? (session.analytics.views || 0) : 0));
    const totalViews = viewStats.reduce((a, b) => a + b);
    const analysis = {
      timestamp: new Date(),
      version: process.env.npm_package_version,
      gitHead: process.env.npm_package_gitHead,
      info,
      stats: {
        user: {
          total: users.length,
          active: users.filter(user => daysAgo(user.last_login) > 28).length,
          activityDistribution: getDistribution(users.map(user => daysAgo(user.last_login))),
          published: userSessions.filter(data => data.sessions.length).length
        },
        session: {
          total: sessions.length,
          published: sessions.filter(session => session.state === 'published').length,
          publishedYesterday: sessions.filter(session => session.state === 'published' && daysAgo(session.updatedAt) === 1).length,
          views: {
            total: totalViews,
            distribution: getDistribution(viewStats)
          }
        }
      }
    };
    models.Analysis.create({ analysis });
  });
};

{'fraction_non_alphanumeric': 0.16047632548908422, 'fraction_numerical': 0.009639920612418485, 'mean_word_length': 2.915649278579356, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, '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': '3285558', 'n_tokens_mistral': 1080, 'n_tokens_neox': 1036, 'n_words': 214}