LanguageStats { public static List<LanguageStats> buildStats(List<Project> projectList) { List<Project> projects = filterUniqueSnapshots(projectList); Map<Date, Map<String,Integer>> counts = new HashMap<>(); TreeSet<Date> dates = new TreeSet<>(); Set<String> languages = new HashSet<>(); for (Project p: projects) { String language = p.getPrimaryLanguage(); Date date = p.getSnapshotDate(); if (language == null) language = "unknown"; dates.add(date); languages.add(language); Map<String,Integer> hist = counts.get(date); if (hist == null) { hist = new HashMap<>(); counts.put(date, hist); } if (hist.containsKey(language)) { hist.put(language, hist.get(language) + 1); } else { hist.put(language, 1); } } List<LanguageStats> result = new ArrayList<>(); for (String l: languages) { List<Integer> projectCounts = new ArrayList<>(); List<Date> snapshotDates = new ArrayList<>(dates); for(Date d: snapshotDates) { Integer i = counts.get(d).get(l); if (i == null) { projectCounts.add(0); } else { projectCounts.add(i); } } result.add(new LanguageStats(l, projectCounts, snapshotDates)); } return result; } LanguageStats(String languageName, List<Integer> projectCounts, List<Date> snapshotDates); static List<LanguageStats> buildStats(List<Project> projectList); static List<Project> filterUniqueSnapshots(List<Project> projects); @JsonProperty(value="name") String getLanguageName(); @JsonProperty(value="project_counts") List<Integer> getProjectCounts(); @JsonProperty(value="snapshot_dates") @JsonSerialize(using = JsonDateListSerializer.class) List<Date> getSnapshotDates(); static String UNKNOWN; }
@Test public void thatStatsAreBuiltForEmptyProjects() { assertThat(LanguageStats.buildStats(Lists.newArrayList()), empty()); }
@Test public void thatStatsAreBuildForTwoProjectsOfDifferentLanguageAndSameSnapshotDate() { Date snapshotDate = new Date(); Project javaProject = new Project(); javaProject.setName("Project 1"); javaProject.setPrimaryLanguage(JAVA); javaProject.setSnapshotDate(snapshotDate); Project pythonProject = new Project(); pythonProject.setName("Project 2"); pythonProject.setPrimaryLanguage(PYTHON); pythonProject.setSnapshotDate(snapshotDate); List<LanguageStats> listOfLanguageStats = LanguageStats.buildStats(Lists.newArrayList(javaProject, pythonProject)); assertThat(listOfLanguageStats.size(), is(2)); assertThat(listOfLanguageStats, hasItem(new LanguageStatsMatcher(JAVA, Lists.newArrayList(1), Lists.newArrayList(snapshotDate)))); assertThat(listOfLanguageStats, hasItem(new LanguageStatsMatcher(PYTHON, Lists.newArrayList(1), Lists.newArrayList(snapshotDate)))); }
@Test public void thatDuplicateProjectsAreFiltered() { Date snapshotDate = new Date(); Project javaProject = new Project(); javaProject.setName("Project 1"); javaProject.setPrimaryLanguage(JAVA); javaProject.setSnapshotDate(snapshotDate); Project duplicateProject = new Project(); duplicateProject.setName("Project 1"); duplicateProject.setPrimaryLanguage(JAVA); duplicateProject.setSnapshotDate(snapshotDate); List<LanguageStats> listOfLanguageStats = LanguageStats.buildStats(Lists.newArrayList(javaProject, duplicateProject)); assertThat(listOfLanguageStats.size(), is(1)); }