<commit_before>import Email from '../models/email.model';
import mongoose from 'mongoose';
const ObjectId = mongoose.Types.ObjectId;

function getMonthlyPunchCard(userId){
  return Email.aggregate([
    {$match: { user: ObjectId(userId)}},
    {$project: {
        month: { $month: "$date" },
        day: { $dayOfMonth: "$date" }
      }
    },
    {$group: {_id: {month: '$month', day: '$day'}, count: {$sum:1}}},
    {$sort:{'_id.month':1, '_id.day':1}}
  ]);
}

function getDailyPunchCard(userId){
  return Email.aggregate([
    {$match: { user: ObjectId(userId)}},
    {
      $project: {
        day: { $dayOfMonth: "$date" },
        hour: { $hour: "$date" },
      }
    },
    {$group: {_id: {day: '$day', hour: '$hour'}, count: {$sum:1}}},
    {$sort:{'_id.day':1, '_id.hour':1}}
  ]);
}

function getTopSender(userId){
  return Email.aggregate([
    {$match: { user: ObjectId(userId)}},
    {$unwind: '$from'},
    {$group: {_id: {address: '$from.address', name: '$from.name'}, count: {$sum:1}}},
    {$sort: {'count':-1}}
  ]);
}

function getTopReceiver(userId){
  return Email.aggregate([
    {$match: { user: ObjectId(userId)}},
    {$unwind: '$to'},
    {$group: {_id: {address: '$to.address', name: '$to.name'}, count: {$sum:1}}},
    {$sort: {'count':-1}}
  ]);
}

function getSummary(req, res) {

  //TODO may consider only the last 12 month not all data ...
  //TODO differentiate between send, received and all mails
  //TODO use id of current user
  const userId = "5836fe8a14272b01c404257c";

  const s = {
    punchcard:{},
    network:{}
  };

  getMonthlyPunchCard(userId)
    .then((monthlySummary)=>{
      s.punchcard.monthly = monthlySummary;
      return getDailyPunchCard(userId);
    })
    .then((dailySummary)=>{
      s.punchcard.daily = dailySummary;
      return getTopSender(userId)
    })
    .then((topSender)=>{
      s.network.topsender = topSender;
      return getTopReceiver(userId)
    })
    .then((topReceiver)=>{
      s.network.topreceiver = topReceiver;
      res.status(200).send(s);
  });

}


export default {
  getSummary
};
<commit_msg>dashboard json project to simpler json<commit_after>import Email from '../models/email.model';
import mongoose from 'mongoose';
const ObjectId = mongoose.Types.ObjectId;

function getMonthlyPunchCard(userId){
  return Email.aggregate([
    {$match: { user: ObjectId(userId)}},
    {$project: {
        month: { $month: "$date" },
        day: { $dayOfMonth: "$date" }
      }
    },
    {$group: {_id: {month: '$month', day: '$day'}, count: {$sum:1}}},
    {$sort:{'_id.month':1, '_id.day':1}},
    {$project: {_id: 0, month: '$_id.month', day: '$_id.day', count:1}}
  ]);
}

function getDailyPunchCard(userId){
  return Email.aggregate([
    {$match: { user: ObjectId(userId)}},
    {
      $project: {
        day: { $dayOfMonth: "$date" },
        hour: { $hour: "$date" },
      }
    },
    {$group: {_id: {day: '$day', hour: '$hour'}, count: {$sum:1}}},
    {$sort:{'_id.day':1, '_id.hour':1}},
    {$project: {_id: 0, day: '$_id.day', hour: '$_id.hour', count:1}}
  ]);
}

function getTopSender(userId){
  return Email.aggregate([
    {$match: { user: ObjectId("5836fe8a14272b01c404257c")}},
    {$unwind: '$from'},
    {$group: {_id: {address: '$from.address', name: '$from.name'}, count: {$sum:1}}},
    {$sort: {count:-1}},
    {$project: {_id: 0, address: '$_id.address', name: '$_id.name', count:1}}
  ]);
}


function getTopReceiver(userId){
  return Email.aggregate([
    {$match: { user: ObjectId(userId)}},
    {$unwind: '$to'},
    {$group: {_id: {address: '$to.address', name: '$to.name'}, count: {$sum:1}}},
    {$sort: {'count':-1}},
    {$project: {_id: 0, address: '$_id.address', name: '$_id.name', count:1}}
  ]);
}

function getSummary(req, res) {

  //TODO may consider only the last 12 month not all data ...
  //TODO differentiate between send, received and all mails
  //TODO use id of current user
  const userId = "5836fe8a14272b01c404257c";

  const s = {
    punchcard:{},
    network:{}
  };

  getMonthlyPunchCard(userId)
    .then((monthlySummary)=>{
      s.punchcard.monthly = monthlySummary;
      return getDailyPunchCard(userId);
    })
    .then((dailySummary)=>{
      s.punchcard.daily = dailySummary;
      return getTopSender(userId)
    })
    .then((topSender)=>{
      s.network.topsender = topSender;
      return getTopReceiver(userId)
    })
    .then((topReceiver)=>{
      s.network.topreceiver = topReceiver;
      res.status(200).send(s);
  });

}


export default {
  getSummary
};

{'fraction_non_alphanumeric': 0.20432480141218004, 'fraction_numerical': 0.02030008826125331, 'mean_word_length': 3.304843304843305, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '662658', 'n_tokens_mistral': 1670, 'n_tokens_neox': 1618, 'n_words': 347}