All files / app/selectors authors.ts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189          8x           8x               2x     2x 2x       2x 14x 14x 14x 14x 14x 14x   2x 12x           12x           8x       1x 1x 1x 1x 1x   1x 7x 7x 7x 7x 7x 2x   7x 2x   7x         1x                     8x 14x 14x 16x 16x   14x     8x                                                                                   8x       2x 14x 14x 14x       2x     8x 2x 2x   2x 16x               16x 14x   16x 2x   16x     16x 56x   16x 16x 16x 16x 14x 14x     2x          
import { createSelector } from 'reselect';
import { AuthorsContainerSorts } from 'app/actions/ActionTypes';
import { getAuthorsContainerSort } from './stateVars';
import { getFilteredCommits, getCommitsForTimeplot } from './commits';
 
export const getTimeplotAuthorsAndCommits = createSelector(
  getCommitsForTimeplot,
  getAuthorsContainerSort,
  commitsByAuthors
);
 
export const getAuthorsAndCommits = createSelector(
  getFilteredCommits,
  getAuthorsContainerSort,
  commitsByAuthors
);
 
function commitsByAuthors(commits, authorsContainerSort) {
  // TODO: dear prettier, this \/ looks like shit
  const { commitsByAuthorName, authorsAndCommitsByEmail } = collateCommits(
    commits
  );
  const authorsAndCommits = [];
  const deduped = dedupeAuthorsAndCommits(
    commitsByAuthorName,
    authorsAndCommitsByEmail
  );
  for (const key in deduped) {
    const authorAndCommits = commitsByAuthorName[key];
    const { linesAdded, linesDeleted } = sumImpact(authorAndCommits.commits);
    authorAndCommits.linesAdded = linesAdded;
    authorAndCommits.linesDeleted = linesDeleted;
    authorAndCommits.files = Object.keys(authorAndCommits.files);
    authorsAndCommits.push(authorAndCommits);
  }
  return authorsAndCommits.sort((a, b) => {
    switch (authorsContainerSort) {
      case AuthorsContainerSorts.LINES:
        return b.linesAdded + b.linesDeleted - (a.linesAdded + a.linesDeleted);
      case AuthorsContainerSorts.COMMITS:
        return b.commits.length - a.commits.length;
      case AuthorsContainerSorts.TIME:
        return b.lastCommitOn - a.lastCommitOn;
    }
    return 0;
  });
}
 
export const getAuthorsStats = createSelector(
  getAuthorsAndCommits,
  getAuthorsContainerSort,
  authorsAndCommits => {
    let totalLinesAdded = 0;
    let totalLinesDeleted = 0;
    let totalCommits = 0;
    let maxImpact = 0;
    let maxCommits = 0;
 
    const authorsArray = authorsAndCommits.map(ac => {
      totalCommits += ac.commits.length;
      totalLinesDeleted += ac.linesDeleted;
      totalLinesAdded += ac.linesAdded;
      const impact = ac.linesAdded + ac.linesDeleted;
      if (impact > maxImpact) {
        maxImpact = impact;
      }
      if (ac.commits.length > maxCommits) {
        maxCommits = ac.commits.length;
      }
      return {
        ...ac,
      };
    });
 
    return {
      totalLinesAdded,
      totalLinesDeleted,
      totalCommits,
      maxImpact,
      maxCommits,
      authors: authorsArray,
    };
  }
);
 
const sumImpact = commits => {
  const impact = { linesAdded: 0, linesDeleted: 0 };
  for (const commit of commits) {
    impact.linesAdded += commit.linesAdded;
    impact.linesDeleted += commit.linesDeleted;
  }
  return impact;
};
 
const consolidateAuthorAndCommits = (
  commitsByAuthorName,
  authorAndCommitsArray
) => {
  let authorAndCommitsResult = null;
  for (const authorAndCommits of authorAndCommitsArray) {
    if (!authorAndCommitsResult) {
      authorAndCommitsResult = authorAndCommits;
      delete commitsByAuthorName[authorAndCommits.authorName];
      continue;
    }
    if (
      authorAndCommits.authorName.length >
      authorAndCommitsResult.authorName.length
    ) {
      authorAndCommitsResult.authorName = authorAndCommits.authorName;
    }
    for (const email of authorAndCommits.authorEmails) {
      if (authorAndCommitsResult.authorEmails.indexOf(email) === -1) {
        authorAndCommitsResult.authorEmails.push(email);
      }
    }
    if (authorAndCommits.authorDate < authorAndCommitsResult.firstCommitOn) {
      authorAndCommitsResult.firstCommitOn = authorAndCommits.authorDate;
    }
    if (authorAndCommits.authorDate > authorAndCommitsResult.lastCommitOn) {
      authorAndCommitsResult.lastCommitOn = authorAndCommits.authorDate;
    }
    for (const file in authorAndCommits.files) {
      authorAndCommitsResult.files[file] = true;
    }
    authorAndCommitsResult.commits = authorAndCommitsResult.commits.concat(
      authorAndCommits.commits
    );
    delete commitsByAuthorName[authorAndCommits.authorName];
  }
  commitsByAuthorName[
    authorAndCommitsResult.authorName
  ] = authorAndCommitsResult;
  return authorAndCommitsResult;
};
 
const dedupeAuthorsAndCommits = (
  commitsByAuthorName,
  authorsAndCommitsByEmail
) => {
  for (const email in authorsAndCommitsByEmail) {
    const byEmail = authorsAndCommitsByEmail[email];
    Eif (byEmail.length <= 1) {
      continue;
    }
    consolidateAuthorAndCommits(commitsByAuthorName, byEmail);
  }
  return commitsByAuthorName;
};
 
const collateCommits = commits => {
  const commitsByAuthorName = {};
  const authorsAndCommitsByEmail = {};
 
  commits.forEach(commit => {
    const commitsForThisAuthor = commitsByAuthorName[commit.authorName] || {
      authorName: commit.authorName,
      authorEmails: [],
      commits: [],
      files: {},
      firstCommitOn: commit.authorDate,
      lastCommitOn: commit.authorDate,
    };
    if (commitsForThisAuthor.authorEmails.indexOf(commit.authorEmail) === -1) {
      commitsForThisAuthor.authorEmails.push(commit.authorEmail);
    }
    if (commit.authorDate < commitsForThisAuthor.firstCommitOn) {
      commitsForThisAuthor.firstCommitOn = commit.authorDate;
    }
    Iif (commit.authorDate > commitsForThisAuthor.lastCommitOn) {
      commitsForThisAuthor.lastCommitOn = commit.authorDate;
    }
    for (const file of commit.files) {
      commitsForThisAuthor.files[file] = true;
    }
    commitsForThisAuthor.commits.push(commit);
    commitsByAuthorName[commit.authorName] = commitsForThisAuthor;
    const byEmail = authorsAndCommitsByEmail[commit.authorEmail] || [];
    if (!byEmail.includes(commitsForThisAuthor)) {
      byEmail.push(commitsForThisAuthor);
      authorsAndCommitsByEmail[commit.authorEmail] = byEmail;
    }
  });
  return {
    commitsByAuthorName,
    authorsAndCommitsByEmail,
  };
};