Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# commitStats
# commitStats (Alston and Diana)

A program written in poor style to help give practice with the VS Code debugger and refactoring.

Expand Down
76 changes: 44 additions & 32 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,20 @@
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);

// Asking the user to enter the CSV filename and store their input
System.out.print("Enter the CSV filename: ");
String f = s.nextLine();

List<Map<String, String>> dta = new ArrayList<>();
try (Scanner fs = new Scanner(new File(f))) {
fs.nextLine();

while (fs.hasNextLine()) {
String[] v = fs.nextLine().split(",");

int chg = Integer.parseInt(v[2]);

Map<String, String> mp1 = new HashMap<>();
mp1.put("id", v[0]);
mp1.put("tm", v[1]);
mp1.put("chg", String.valueOf(chg));
dta.add(mp1);
}
} catch (FileNotFoundException e) {
System.out.println("Error reading the file: " + e.getMessage());
s.close();
return;

// Call the helper method to parse the CSV file
List<Map<String, String>> dataList = parseCSV(f);
if (dataList == null) {
s.close();
return;
}


// Creating a Hashmap and iterating through the data to get the id of the fork
Map<String, List<Map<String, String>>> mp2 = new HashMap<>();
for (Map<String, String> d : dta) {
for (Map<String, String> d : dataList) {
String id = d.get("id");
List<Map<String, String>> lst = mp2.get(id);
if (lst == null) {
Expand All @@ -42,22 +29,24 @@ public static void main(String[] args) {
}
lst.add(d);
}
// Counting the total number of forks
int cnt = mp2.size();

System.out.println("There are " + cnt + " forks available (fork1 to fork" + cnt + ").");
// Statement for the user to choose through the menu to analyze the/all forks
System.out.print("Enter the fork number to analyze (or 'all' for all forks): ");
String inp = s.nextLine();

// Selects commits based on the user input
List<Map<String, String>> sel;
if (inp.equalsIgnoreCase("all")) {
sel = dta;
sel = dataList;
} else {
String id = "fork" + inp;
sel = mp2.get(id);
}

int sz = sel.size();

// Commit Time Stamps-Format
DateTimeFormatter f1 = DateTimeFormatter.ISO_DATE_TIME;
LocalDateTime lat = null;
for (Map<String, String> d : sel) {
Expand All @@ -68,16 +57,17 @@ public static void main(String[] args) {
}
DateTimeFormatter f2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
String latT = lat.format(f2);

// Calculating the total lines changed
double tot = 0.0;
int tlc = 0;
for (Map<String, String> d : sel) {
int lc = Integer.parseInt(d.get("chg"));
tot += lc;
tlc += lc;
}
// Calculating average lines per commit
double avg = tot / sz;

// Finding the max and the minimum lines updated in the commit
int mx = Integer.MIN_VALUE;
int mn = Integer.MAX_VALUE;
for (Map<String, String> d : sel) {
Expand All @@ -89,15 +79,37 @@ public static void main(String[] args) {
mn = chg;
}
}

// Statistics from the program
System.out.println("\nStatistics:");
System.out.println("Number of commits: " + sz);
System.out.println("Number of commits: " + sz);
System.out.println("Most recent commit timestamp: " + latT);
System.out.printf("Average lines changed per commit: %.2f\n", avg);
System.out.println("Total lines changed across all commits: " + tlc);
System.out.println("Max lines changed in a commit: " + mx);
System.out.println("Min lines changed in a commit: " + mn);

// Close the scanner
s.close();
}
// Helper method
public static List<Map<String, String>> parseCSV(String filename) {
List<Map<String, String>> dataList = new ArrayList<>();
try (Scanner fs = new Scanner(new File(filename))) {
fs.nextLine();

while (fs.hasNextLine()) {
String[] v = fs.nextLine().split(",");
int chg = Integer.parseInt(v[2]);

Map<String, String> commitData = new HashMap<>();
commitData.put("id", v[0]);
commitData.put("tm", v[1]);
commitData.put("chg", String.valueOf(chg));
dataList.add(commitData);
}
} catch (FileNotFoundException e) {
System.out.println("Error reading the file: " + e.getMessage());
return null; // return null to indicate failure
}
return dataList;
}
}