-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP3A1_Yellajosyula_ryellajo.java
598 lines (403 loc) · 15.7 KB
/
P3A1_Yellajosyula_ryellajo.java
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
import java.util.*;
import java.io.*;
import java.text.*;
/**
Author's Name: Rajya Laxmi Yellajosyula
Creation day: July 29th 2017
Last modification day: August 4th 2017
This program processes the log file emacs.log,
fetches the total number of files,
File with the most number of revisions,
File with the most number of users committs,
Earliest commit to a file,
User(s) with most commits in the log.
*/
public class P3A1_Yellajosyula_ryellajo
{
//fields that hold the log file Name and total files count in the log
private int filecount;
private String fileName;
//Constructor that accepts name of the log file to be processed
public P3A1_Yellajosyula_ryellajo(String f) throws FileNotFoundException
{
filecount = 0;
fileName = f;
}
//Accessor method for file name
public String getFileNames()
{
return fileName;
}
//Mutator method for file Name
public void setFileName(String s)
{
fileName = s;
}
//Method that gets the total number of files in the log -Accessor for fileCount
public int getFileCount() throws FileNotFoundException,IOException
{
BufferedReader br = new BufferedReader(new FileReader(fileName));
String line;
while ((line = br.readLine()) != null)
{
if(line.contains("RCS file:"))
filecount++;
}
return filecount;
}
//Mutator method for file count
public void setFileCount(int n)
{
filecount = n;
}
//Method that fetches the maximum revisions of each file
public Map<String,Integer> getMaxRevisions() throws FileNotFoundException,IOException
{
Map<String,Integer> fileContents = new HashMap<>();
BufferedReader br = new BufferedReader(new FileReader(fileName));
String line,file = new String();
int revisions = 0;
while ((line = br.readLine()) != null)
{
if(line.contains("RCS file:"))
{
String lines[] = line.split(":");
file = lines[1];
}
if(line.contains("total revisions:"))
{
//Splits the string in to parts to fetch the revisions number
String parts[] = line.split(";");
//Replaces spaces before number with zero to make it easy to convert into integer
String temp1 = parts[0].replace(" ", "0");
//Fetches the integer value of revisions string
String temp = temp1.substring(temp1.indexOf("total revisions:")+17 , temp1.length());
//Assigns it to revisions value
if(revisions<Integer.parseInt(temp))
{
revisions = Integer.parseInt(temp);
}
}
}
fileContents.put(file,revisions);
return fileContents;
}
//Method that return the file which has maximum commits made to it
public Map<String,Integer> getMaxUserCommits() throws FileNotFoundException,IOException
{
Map<String,Integer> fileMax = new HashMap<>();
Map<String,List<String>> fileContents = new HashMap<>();
BufferedReader br = new BufferedReader(new FileReader(fileName));
String line,user,file = new String();
int userCount = 0,userMax =0;
List<String> userValues = null;
while ((line = br.readLine()) != null)
{
if(line.contains("RCS file:"))
{
String lines[] = line.split(":");
file = lines[1];
userCount = 0;
userValues = new ArrayList<String>();
}
if(line.contains("author:"))
{
String users[] = line.split(";");
user = users[1].substring(9);
if(!userValues.contains(user))
{userValues.add(user);
}
}
fileContents.put(file,userValues);
}
int max = 0;
String fileFinal = new String();
for(Map.Entry<String,List<String>> m:fileContents.entrySet())
{
if(m.getValue() != null)
{
if(max<m.getValue().size())
{
max = m.getValue().size();
fileFinal = m.getKey();
}
}
}
fileMax.put(fileFinal,max);
return fileMax;
}
//Method that fetches the user who made maximum commits and the count
public Map<String,Integer> MaxUserCommits() throws FileNotFoundException,IOException
{
Map<String,Integer> fileContents = new HashMap<>();
BufferedReader br = new BufferedReader(new FileReader(fileName));
String line,userName = "",file = new String();
int commits = 0;
String maxFile = new String();
ArrayList<String> users = new ArrayList<String>();
while ((line = br.readLine()) != null)
{
if(line.contains("author:"))
{
String lines[] = line.split(";");
userName = lines[1];
fileContents.put(userName,commits);
}
}
for(Map.Entry<String,Integer> m:fileContents.entrySet())
{
BufferedReader br1 = new BufferedReader(new FileReader(fileName));
while ((line = br1.readLine()) != null)
{
if(line.contains(m.getKey()))
fileContents.put(m.getKey(),m.getValue()+1);
}
}
int max = 0;
for(Map.Entry<String,Integer> m:fileContents.entrySet())
{
if(m.getValue()>max)
{ max = m.getValue();
file = m.getKey();
}
}
Map<String,Integer> maxUser = new HashMap<>();
maxUser.put(file,max);
return maxUser;
}
//Method that returns user who committed maximum number of times to a specific file
public Map<String,Integer> userMostCommits(String file) throws IOException,FileNotFoundException,ParseException
{
Map<String,Integer> fileMax = new HashMap<>();
Map<String,List<String>> fileContents = new HashMap<>();
BufferedReader br = new BufferedReader(new FileReader(fileName));
String line,user;
int userMax =0,lineNumber =1;
List<String> userValues = new ArrayList<String>();
while ((line = br.readLine()) != null)
{
lineNumber++;
if(line.contains(file))
{
userValues = new ArrayList<String>();
break;
}
}
while ((line = br.readLine()) != null)
{
if(line.contains("author:"))
{
String users[] = line.split(";");
user = users[1].substring(9);
if(!userValues.contains(user))
userValues.add(user);
}
if(line.contains("===="))
break;
}
fileContents.put(file,userValues);
int userCount = 0;
Map<String,Integer> contents = new HashMap<>();
for(String s1:userValues)
contents.put(s1,userCount);
BufferedReader br1 = new BufferedReader(new FileReader(fileName));
int i=1;
while ((line = br1.readLine()) != null && (i<=lineNumber))
{
i++;
}
while ((line = br1.readLine()) != null )
{
if(line.contains("===="))
break;
for(Map.Entry<String,Integer> m:contents.entrySet())
{
if(line.contains(m.getKey()))
contents.put(m.getKey(),m.getValue()+1);
}
}
Map<String,Integer> result = new HashMap<>();
int maxCommitUser = 0;
String maxUser = ",";
for(Map.Entry<String,Integer> m:contents.entrySet())
{
if(m.getValue()>maxCommitUser)
{
maxCommitUser = m.getValue();
maxUser = m.getKey();
}
if(m.getValue()==maxCommitUser && !maxUser.contains(m.getKey()) )
{
maxCommitUser = m.getValue();
maxUser += m.getKey();
}
}
result.put(maxUser,maxCommitUser);
return result;
}
//Method that returns the earliest commits of files in the log
public Map<String,Date> earliestCommits() throws FileNotFoundException,IOException,ParseException
{
Map<String,Date> fileContents = new HashMap<>();
BufferedReader br = new BufferedReader(new FileReader(fileName));
String line,userName = "",file = new String();
int commits = 0;
String maxFile = new String();
ArrayList<String> users = new ArrayList<String>();
Date earlyDate = null,date;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
List<Date> temp_list = new ArrayList<Date>();
while ((line = br.readLine()) != null)
{
if(line.contains("RCS file:"))
{
String lines[] = line.split(":");
file = lines[1];
earlyDate = null;
fileContents.put(file,earlyDate);
}
if(line.contains("date:"))
{
String lines[] = line.split(";");
String dates[] =lines[0].split(" ");
String temp = dates[1]+" "+dates[2];
date = format.parse(temp);
if(earlyDate == null)
earlyDate = date;
if(earlyDate.after(date))
earlyDate = date;
fileContents.put(file,earlyDate);
}
}
return fileContents;
}
//Method that finds the last commits made to files
public Map<String,Date> LastCommits() throws FileNotFoundException,IOException,ParseException
{
Map<String,Date> fileContents = new HashMap<>();
BufferedReader br = new BufferedReader(new FileReader(fileName));
String line,userName = "",file = new String();
int commits = 0;
String maxFile = new String();
ArrayList<String> users = new ArrayList<String>();
Date lastDate = null,date;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
List<Date> temp_list = new ArrayList<Date>();
while ((line = br.readLine()) != null)
{
if(line.contains("RCS file:"))
{
String lines[] = line.split(":");
file = lines[1];
lastDate = null;
fileContents.put(file,lastDate);
}
if(line.contains("date:"))
{
String lines[] = line.split(";");
String dates[] =lines[0].split(" ");
String temp = dates[1]+" "+dates[2];
date = format.parse(temp);
if(lastDate == null)
lastDate = date;
if(lastDate.before(date))
lastDate = date;
fileContents.put(file,lastDate);
}
}
return fileContents;
}
//Method to fetch the years in the log file
public Map<String,List<String>> getYears() throws FileNotFoundException,IOException,ParseException
{
Map<String,List<String>> fileYears = new HashMap<String,List<String>>();
BufferedReader br = new BufferedReader(new FileReader(fileName));
String user,line,year = new String();
List<String> userValues = new ArrayList<String>();
//Initialises a hash with all years as keys
while ((line = br.readLine()) != null)
{
if(line.contains("date:"))
{
String lines[] = line.split(";");
String dates[] =lines[0].split(" ");
String temp1 = dates[1];
String years[] =temp1.split("-");
year = years[0];
userValues = new ArrayList<String>();
fileYears.put(year,userValues);
}
}
for(Map.Entry<String,List<String>> m:fileYears.entrySet())
{
BufferedReader br1 = new BufferedReader(new FileReader(fileName));
while ((line = br1.readLine()) != null)
{
if(line.contains("author:") && line.contains(m.getKey()))
{
List<String> usersList = m.getValue();
String users[] = line.split(";");
user = users[1].substring(9);
if(!usersList.contains(user))
usersList.add(user);
fileYears.put(m.getKey(),usersList);
}
}
}
// fileYears.add(temp);
return fileYears;
}
//Method that fetches the log file name from the user
public static String getFileName()
{
String file = new String();
Scanner in = new Scanner(System.in);
boolean flag = false;
while(!flag)
{
System.out.println("Enter the log file name");
file = in.nextLine();
if(file.contains(".log"))
flag = true;
}
return file;
}
public static void main(String args[])throws IOException,FileNotFoundException,ParseException
{
//Creates an object of P3A1_Yellajosyula_ryellajo class
P3A1_Yellajosyula_ryellajo x = new P3A1_Yellajosyula_ryellajo(getFileName());
//Hash Maps to hold th results
Map<String,Integer> result,result1,result2;
Map<String,Date> result3;
System.out.println("Total number of files in the log :\t "+x.getFileCount());
result = x.getMaxRevisions();
for(Map.Entry<String,Integer> m:result.entrySet())
{
System.out.println("File with the most number of revisions: "+m.getKey());
System.out.println("Number of revisions: "+m.getValue());
}
result1 = x.getMaxUserCommits();
for(Map.Entry<String,Integer> m:result1.entrySet())
{
System.out.println("File with the most user commits: "+m.getKey());
System.out.println("Number of user commits: "+m.getValue());
}
result2 = x.MaxUserCommits();
for(Map.Entry<String,Integer> m:result2.entrySet())
{
System.out.println("User with the most commits:"+m.getKey().substring(9));
System.out.println("Number of commits done by this user: "+m.getValue());
}
result3 =x.earliestCommits();
for(Map.Entry<String,Date> m:result3.entrySet())
{
System.out.println("File:"+m.getKey()+"\nEarliest commit date: "+m.getValue());
Map<String,Integer> Fresults = x.userMostCommits(m.getKey());
for(Map.Entry<String,Integer> f:Fresults.entrySet())
{
System.out.println("User(s) with most commits for this file is"+f.getKey());
System.out.println("Total commits\t"+f.getValue()+"\n");
}
}
}
}