-
Notifications
You must be signed in to change notification settings - Fork 3
/
helpers.R
1203 lines (1093 loc) · 55.7 KB
/
helpers.R
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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
library(RMySQL)
# println + concatenation wrapper
println <- function(...) {
cat(paste(..., "\n", sep = ""))
}
plain <- function(x,...) {
format(x, ..., scientific = FALSE, trim = TRUE)
}
paste.path <- function(folder, filename) {
paste(folder, filename, sep = "/")
}
# heatmaps ------------------------------------------------------------------------------------------------------------
createHeatmap <- function(datasetName, datasetPath, y_axis = "commits") {
data = read.csv(paste(datasetPath, "/heatmap.csv", sep=""), header = F, col.names = c("pid", "stars", "commits", "files", "originalFiles","containsClones"))
# create the matrix
len_x = ceiling(log10(max(data$files) + 1) / 0.2) + 1
len_y = ceiling(log10(max(data[[y_axis]]) + 1) / 0.2) + 1
# because the hashmaps for the paper are cropped and we want to use the same function
len_x = max(25, len_x)
len_y = max(25, len_y)
density = matrix(0, len_x, len_y)
sumFiles = matrix(0, len_x, len_y)
sumOriginalFiles = matrix(0, len_x, len_y)
sumContainsClones = matrix(0, len_x, len_y)
total = 0
for (i in 1:length(data$files)) {
x = ceiling(log10(data$files[[i]] + 1) / 0.2) + 1
y = ceiling(log10(data[[y_axis]][[i]] + 1) / 0.2) + 1
density[x,y] = density[x,y] + 1
sumFiles[x,y] = sumFiles[x,y] + data$files[[i]]
sumOriginalFiles[x,y] = sumOriginalFiles[x,y] + data$originalFiles[[i]]
sumContainsClones[x,y] = sumContainsClones[x,y] + data$containsClones[[i]]
total = total + 1
}
# convert the matrix to data.frame
files = double()
stars = double()
d = double()
sf = double()
sof = double()
scc = double()
originality = double()
avgClones = double()
#for (i in 1:len_x) {
# for (j in 1:len_y) {
for (i in 3:25) {
for (j in 3:25) {
files = c(files, (i - 1) * 0.2)
stars = c(stars, (j - 1) * 0.2)
d = c(d, density[i,j])
sf = c(sf, sumFiles[i,j])
sof = c(sof, sumOriginalFiles[i,j])
scc = c(scc, sumContainsClones[i,j])
originality = c(originality, sumOriginalFiles[i, j] / sumFiles[i,j])
avgClones = c(avgClones, sumContainsClones[i, j] / density[i,j])
}
}
heat_data = data.frame(files = files, yaxis = stars, density = d, Clones = originality, avgClones = avgClones, sumFiles = sf, sumOriginalFiles = sof, sumContainsClones = scc)
# do the graph
g = ggplot(heat_data, aes(files, yaxis))
g = g + geom_raster(aes(fill = Clones))
#g = g + scale_fill_gradient(low = "red", high = "green", limits = c(1, 0), breaks = c(0.25, 0.5, 0.75))
g = g + scale_fill_gradient(low = "white", high = "red", limits = c(1, 0), breaks = c(0.25, 0.5, 0.75))
g = g + scale_x_continuous("Files per Project", labels = function(x) plain(10**x))
g = g + scale_y_continuous("Commits", labels = function(x) plain(10**x))
g = g + geom_text(aes(label = round(100 - Clones * 100, 0)), size = 3)
g = g + theme(panel.background = element_rect(color = "black", fill="white"), panel.spacing = unit(c(0,0,0,0), "points"))
g = g + coord_cartesian(c(0.45, 4.75), c(0.45, 4.75))
#g = g + scale_fill_continuous(limits=c(1, 0), breaks=seq(1,0,by=-0.25))
g = g + theme(axis.title.x=element_blank(), axis.title.y = element_blank())
ggsave(paste("graphs/", datasetName, "/heatmap.pdf", sep = ""), width = 68 * 2.5, height = 68 * 2.5, units = "mm")
g
}
# graphs --------------------------------------------------------------------------------------------------------------
logHistogram <- function(dbname, query, title, xtitle, ytitle, filename = NULL) {
linetypes = c("Median" = "solid", "Mean" = "dashed")
sql.connect(username = DB_USER, password = DB_PASSWORD, dbname = dbname, host = DB_HOST)
query = sql.query(query)[[1]]
sql.disconnect()
# because we are log hist, do log
#query = log10(query + 1)
data = data.frame(x = query)
# calculate the breaks so that we fill the range
breaks = 1
i = 1
m = max(query)
repeat {
i = i * 10
breaks = c(breaks, i + 1)
if (log10(i + 1) > m)
break
}
breaks = log10(breaks)
# draw the graph
g = ggplot(data)
g = g + geom_histogram(binwidth = 0.2, boundary = 0, aes(x = log10(x + 1), y=..count../sum(..count..)))
# add mean & median vertical lines
x_mean = log10(mean(query) + 1)
x_median = log10(median(query) + 1)
g <- g + geom_vline(aes(xintercept = x_mean, linetype = "Mean"), alpha = 1)
g <- g + geom_vline(aes(xintercept = x_median, linetype = "Median"), alpha = 1)
g = g + scale_x_continuous(xtitle, labels = function(x) plain(10**x - 1), breaks = breaks) + theme(axis.text.y = element_text(angle=90, hjust = 0.5)) + scale_y_continuous(ytitle, labels=function(x) x * 100)
g = g + scale_linetype_manual(name="Statistics", values = linetypes)
g = g + ggtitle(title)
g = g + theme(plot.title = element_text(hjust = 0.5))
if (!is.null(filename))
ggsave(paste("graphs/", dbname, "/", filename, sep=""), width = 68 * 2.5, height = 55 * 2.5, units = "mm")
g
}
normalHistogram <- function(dbname, query, title, xtitle, ytitle, filename = NULL) {
linetypes = c("Median" = "solid", "Mean" = "dashed")
sql.connect(username = DB_USER, password = DB_PASSWORD, dbname = dbname, host = DB_HOST)
query = sql.query(query)[[1]]
sql.disconnect()
data = data.frame(x = query)
# draw the graph
g = ggplot(data)
g = g + geom_histogram(aes(x = x, y=..count../sum(..count..)))
# add mean & median vertical lines
x_mean = mean(query)
x_median = median(query)
g <- g + geom_vline(aes(xintercept = x_mean, linetype = "Mean"), alpha = 1)
g <- g + geom_vline(aes(xintercept = x_median, linetype = "Median"), alpha = 1)
g = g + scale_x_continuous(paste(xtitle)) + theme(axis.text.y = element_text(angle=90, hjust = 0.5)) + scale_y_continuous(ytitle, labels=function(x) x * 100)
g = g + scale_linetype_manual(name="Statistics", values = linetypes)
g = g + ggtitle(title)
g = g + theme(plot.title = element_text(hjust = 0.5))
if (!is.null(filename))
ggsave(paste("graphs/", dbname, "/", filename, sep=""), width = 68 * 2.5, height = 55 * 2.5, units = "mm")
g
}
normalHistogramLogY <- function(dbname, query, title, xtitle, ytitle, filename = NULL) {
linetypes = c("Median" = "solid", "Mean" = "dashed")
sql.connect(username = DB_USER, password = DB_PASSWORD, dbname = dbname, host = DB_HOST)
query = sql.query(query)[[1]]
sql.disconnect()
data = data.frame(x = query)
# draw the graph
g = ggplot(data)
g = g + geom_histogram(aes(x = x), bins = 50)
# add mean & median vertical lines
x_mean = mean(query, na.rm = T)
x_median = median(query, na.rm = T)
g <- g + geom_vline(aes(xintercept = x_mean, linetype = "Mean"), alpha = 1)
g <- g + geom_vline(aes(xintercept = x_median, linetype = "Median"), alpha = 1)
g = g + scale_x_continuous(paste(xtitle)) + theme(axis.text.y = element_text(angle=90, hjust = 0.5))
g = g + scale_linetype_manual(name="Statistics", values = linetypes)
g = g + ggtitle(title)
g = g + theme(plot.title = element_text(hjust = 0.5))
g = g + scale_y_log10(ytitle)
if (!is.null(filename))
ggsave(paste("graphs/", dbname, "/", filename, sep=""), width = 68 * 2.5, height = 55 * 2.5, units = "mm")
g
}
logHistogramFromDF <- function(data, column, title, xtitle, ytitle, filename = NULL, summary = T) {
linetypes = c("Median" = "solid", "Mean" = "dashed")
data = data.frame(x = data[[column]])
# calculate the breaks so that we fill the range
breaks = 1
i = 1
m = max(data$x)
repeat {
i = i * 10
breaks = c(breaks, i + 1)
if (log10(i + 1) > m)
break
}
breaks = log10(breaks)
# draw the graph
g = ggplot(data)
if (summary) {
g = g + geom_histogram(binwidth = 0.2, boundary = 0, aes(x = log10(x + 1), y=..count../sum(..count..)))
g = g + scale_x_continuous(xtitle, labels = function(x) plain(10**x - 1), breaks = breaks) + theme(axis.text.y = element_text(angle=90, hjust = 0.5)) + scale_y_continuous(ytitle, labels=function(x) x * 100)
} else {
g = g + geom_histogram(binwidth = 0.2, boundary = 0, aes(x = log10(x + 1)))
g = g + scale_x_continuous(xtitle, labels = function(x) plain(10**x - 1), breaks = breaks) + theme(axis.text.y = element_text(angle=90, hjust = 0.5)) + scale_y_continuous(ytitle, labels=function(x) plain(x))
}
# add mean & median vertical lines
x_mean = log10(mean(data$x) + 1)
x_median = log10(median(data$x) + 1)
g <- g + geom_vline(aes(xintercept = x_mean, linetype = "Mean"), alpha = 1)
g <- g + geom_vline(aes(xintercept = x_median, linetype = "Median"), alpha = 1)
g = g + scale_linetype_manual(name="Statistics", values = linetypes)
g = g + ggtitle(title)
g = g + theme(plot.title = element_text(hjust = 0.5))
if (!is.null(filename))
ggsave(paste("graphs/", title, "/", filename, sep=""), width = 68 * 2.5, height = 55 * 2.5, units = "mm")
g
}
logHistogramDouble = function(db1, query, db1Title, db2Title, title, xtitle, ytitle, filename = NULL, query2 = query) {
db2 = paste(db1, "_nonpm", sep="")
colors = c("red", "blue")
names(colors) = c(db1Title, db2Title)
linetypes = c("Median" = "solid", "Mean" = "dashed")
# get the input data
sql.connect(username = DB_USER, password = DB_PASSWORD, dbname = db1, host = DB_HOST)
sql.query("USE ", db1)
first = sql.query(query)[[1]];
sql.query("USE ", db2)
second = sql.query(query2)[[1]];
sql.disconnect()
# make lengths the same
maxl = max(length(first), length(second))
length(first) = maxl
length(second) = maxl
# create the dataframe
data = data.frame(first = first, second = second)
# calculate the breaks so that we fill the range
breaks = 1
i = 1
m = max(max(first, na.rm = T), max(second, na.rm = T))
repeat {
i = i * 10
breaks = c(breaks, i + 1)
if (log10(i + 1) > m)
break
}
breaks = log10(breaks)
# create the graph
g = ggplot(data)
g = g + geom_histogram(binwidth = 0.2, boundary = 0, aes(x = log10(first + 1), y=..count../sum(..count..), fill = db1Title), alpha = 0.5)
g = g + geom_histogram(binwidth = 0.2, boundary = 0, aes(x = log10(second + 1), y=..count../sum(..count..), fill = db2Title), alpha = 0.5, show.legend = T)
first_mean = log10(mean(first, na.rm = T) + 1)
first_median = log10(median(first, na.rm = T) + 1)
g <- g + geom_vline(aes(xintercept = first_mean, linetype = "Mean", color = db1Title), alpha = 0.7)
g <- g + geom_vline(aes(xintercept = first_median, linetype = "Median", color = db1Title), alpha = 0.7)
second_mean = log10(mean(second, na.rm = T) + 1)
second_median = log10(median(second, na.rm = T) + 1)
g <- g + geom_vline(aes(xintercept = second_mean, linetype = "Mean", color = db2Title), alpha = 0.7)
g <- g + geom_vline(aes(xintercept = second_median, linetype = "Median", color = db2Title), alpha = 0.7)
g = g + scale_x_continuous(xtitle, labels = function(x) plain(10**x - 1), breaks = breaks) + theme(axis.text.y = element_text(angle=90, hjust = 0.5)) + scale_y_continuous(ytitle, labels=function(x) x * 100)
g = g + scale_fill_manual(name=" ",values = colors)
g = g + scale_linetype_manual(name="Statistics", values = linetypes)
g = g + scale_color_manual(values=colors, guide = "none")
g = g + ggtitle(title)
g = g + theme(plot.title = element_text(hjust = 0.5))
if (!is.null(filename))
ggsave(paste("graphs/", db1, "/", filename, sep=""), width = 68 * 2.5, height = 55 * 2.5, units = "mm")
g
}
filesOverTime = function(dname, js_aggregate, filename) {
cols <- c(
"non-test duplicates"="#a0a0a0",
"test duplicates"="#808080",
"npm non-test"="#606060",
"npm test"="#404040",
"unique tests"="#202020",
"unique files"="#000000"
)
data = data.frame(
time = js_aggregate$time,
unique = sum(js_aggregate, npm = F, thUnique = T, tests = F),
uniqueTests = sum(js_aggregate, npm = F, thUnique = T),
npmTests = sum(js_aggregate, npm = T, tests = T),
npmNonTest = sum(js_aggregate, npm = T),
dupTests = sum(js_aggregate, npm = F, thUnique = F, tests = T),
all = sum(js_aggregate))
g = ggplot(data, aes(x = time))
g = g + geom_area(aes(y = all, fill = "non-test duplicates"))
g = g + geom_area(aes(y = dupTests + npmNonTest + uniqueTests, fill = "test duplicates"))
g = g + geom_area(aes(y = npmNonTest + uniqueTests, fill = "npm non-test"))
g = g + geom_area(aes(y = npmTests + uniqueTests, fill = "npm test"))
g = g + geom_area(aes(y = uniqueTests, fill = "unique tests"))
g = g + geom_area(aes(y = unique, fill = "unique files"))
g = g + scale_x_continuous("Date", limits = c(87, 212), labels = function(x) sapply(x, month.text))
g = g + scale_y_continuous("Files", labels = plain)
#g = g + scale_fill_identity(guide = 'legend', labels = c("haha", "bubu", "gaga"))
#g = g + guides(fill=guide_legend(title="New Legend Title"))
g = g + scale_fill_manual(name = "Legend", values=cols, breaks = names(cols))
g = g + ggtitle("Files over time")
g = g + theme(plot.title = element_text(hjust = 0.5))
ggsave(paste("graphs/", dname, "/", filename, sep = ""), width = 68 * 2.5, height = 55 * 2.5, units = "mm")
g
}
nonNpmFilesOverTime = function(dname, js_aggregate, filename) {
cols <- c(
"non-test duplicates"="#c0c0c0",
"test duplicates"="#808080",
"unique tests"="#404040",
"unique files"="#000000",
"NPM files" = "dashed"
)
data = data.frame(
time = js_aggregate$time,
unique = sum(js_aggregate, npm = F, thUnique = T, tests = F),
uniqueTests = sum(js_aggregate, npm = F, thUnique = T),
dupTests = sum(js_aggregate, npm = F, thUnique = F, tests = T),
dup = sum(js_aggregate, npm = F),
npm = sum(js_aggregate, npm = T))
g = ggplot(data, aes(x = time))
g = g + geom_area(aes(y = dup, fill = "non-test duplicates"))
g = g + geom_area(aes(y = dupTests + uniqueTests, fill = "test duplicates"))
g = g + geom_area(aes(y = uniqueTests, fill = "unique tests"))
g = g + geom_area(aes(y = unique, fill = "unique files"))
g = g + geom_line(aes(y = npm, linetype = "NPM files"))
g = g + scale_x_continuous("Date", limits = c(87, 212), labels = function(x) sapply(x, month.text))
g = g + scale_y_continuous("Files", limits = c(0, max(data$dup)), labels = plain)
g = g + scale_fill_manual(name = "Legend", values=cols, breaks = names(cols))
g = g + scale_linetype_manual(name=" ", values = cols)
g = g + ggtitle("Non - NPM Files over time")
g = g + theme(plot.title = element_text(hjust = 0.5))
ggsave(paste("graphs/", dname, "/", filename, sep = ""), width = 68 * 2.5, height = 55 * 2.5, units = "mm")
g
}
nonUniqueFilesOverTime = function(dname, js_aggregate, filename, bounds = c(70, 100)) {
cols <- c(
"# of all files"="#c0c0c0",
"# of NPM files"="#a0a0a0",
"all files" = "solid",
"NPM" = "dotted",
"non-NPM" = "dashed",
"non-NPM tests" = "dotdash"
)
x = sum(js_aggregate)
maxx = max(x)
minx = min(x)
data = data.frame(
time = js_aggregate$time,
all = sum(js_aggregate) * (bounds[[2]] - bounds[[1]]) / maxx,
npm = sum(js_aggregate, npm = T) * (bounds[[2]] - bounds[[1]]) / maxx,
pctAll = 100 - sum(js_aggregate, thUnique = T) * 100 / sum(js_aggregate),
pctNPM = 100 - sum(js_aggregate, npm = T, thUnique = T) * 100 / sum(js_aggregate,npm = T),
pctNonNPM = 100 - sum(js_aggregate, npm = F, thUnique = T) * 100 / sum(js_aggregate, npm = F),
pctNonNPMTest = 100 - sum(js_aggregate, npm = F, tests = T, thUnique = T) * 100 / sum(js_aggregate, npm = F, tests = T))
g = ggplot(data, aes(x = time))
g = g + geom_area(aes(y = all, fill = "# of all files"), position = position_nudge(y = bounds[[1]]))
g = g + geom_area(aes(y = npm, fill = "# of NPM files"), position = position_nudge(y = bounds[[1]]))
g = g + geom_line(aes(y = pctAll, linetype = "all files"))
g = g + geom_line(aes(y = pctNPM, linetype = "NPM"))
g = g + geom_line(aes(y = pctNonNPM, linetype = "non-NPM"))
g = g + geom_line(aes(y = pctNonNPMTest, linetype = "non-NPM tests"))
g = g + scale_x_continuous("Date", limits = c(109, 212), labels = function(x) sapply(x, month.text), breaks = c(106,118, 130, 142, 154, 166, 178, 190, 202, 212))
g = g + scale_y_continuous("%", labels = plain)
g = g + coord_cartesian(ylim = bounds)
g = g + scale_fill_manual(name = "Legend", values=cols, breaks = names(cols))
g = g + scale_linetype_manual(name=" ", values = cols)
g = g + ggtitle("% of non-unique files")
g = g + theme(plot.title = element_text(hjust = 0.5))
ggsave(paste("graphs/", dname, "/", filename, sep = ""), width = 68 * 2.5, height = 55 * 2.5, units = "mm")
g
}
# aggregation helpers -------------------------------------------------------------------------------------------------
is.true <- function (x) {
! is.na(x) && x == T
}
sum <- function(from, npm = NA, tests = NA, minjs = NA, thUnique = NA, sccUnique = NA) {
result = rep(0L, length(from$time))
for (row in 1:length(from$time)) {
rowSum = 0L
for (col in 0:31) {
use = T
rc = as.raw(col)
if (is.true((as.integer(rc & as.raw(1)) != 0) == ! npm))
use = F;
if (is.true((as.integer(rc & as.raw(2)) != 0) == ! tests))
use = F;
if (is.true((as.integer(rc & as.raw(4)) != 0) == ! minjs))
use = F;
if (is.true((as.integer(rc & as.raw(8)) != 0) == ! thUnique))
use = F;
if (is.true((as.integer(rc & as.raw(16)) != 0) == ! sccUnique))
use = F;
if (use)
rowSum = rowSum + from[[2 + col]][[row]]
}
result[[row]] = rowSum
}
result
#data.frame(time = from$time, sums = result)
}
month.text = function(x) {
m = 3
y = 1999
m = m + x
while (m > 12) {
y = y + 1
m = m - 12
}
paste(m, y, sep = "/")
}
# database import & data processing -----------------------------------------------------------------------------------
# Imports the dataset (files, stats and projects tables) from csv files produced by the downloader & tokenizer
importDataset <- function(dbName, inputFolder) {
sql.connect(username = DB_USER, password = DB_PASSWORD, dbname = dbName, host = DB_HOST)
importCommonData(inputFolder)
createCommonIndices()
calculateProjectSizes()
sql.disconnect()
}
# creates projects, files and stats tables in given database and populates them from the selected folder, which must contain the appropriate output files from the downloader (projects.txt, files.txt and stats.txt)
importCommonData <- function(inputFolder) {
println(" removing existing tables...")
println(" ", sql.dropTable("projects"))
println(" ", sql.dropTable("files"))
println(" ", sql.dropTable("stats"))
println(" creating tables...")
println(" ", sql.createTable("projects", "
projectId INT UNSIGNED NOT NULL,
projectPath VARCHAR(4000) NOT NULL,
projectUrl VARCHAR(4000) NOT NULL,
PRIMARY KEY (projectId)"))
println(" ", sql.createTable("files", "
fileId BIGINT UNSIGNED NOT NULL,
projectId INT UNSIGNED NOT NULL,
relativeUrl VARCHAR(4000) NOT NULL,
fileHash BIGINT NOT NULL,
PRIMARY KEY (fileId)"))
println(" ", sql.createTable("stats","
fileHash BIGINT NOT NULL,
fileBytes INT NOT NULL,
fileLines INT NOT NULL,
fileSLOC INT NOT NULL,
fileLOC INT NOT NULL,
totalTokens INT NOT NULL,
uniqueTokens INT NOT NULL,
tokenHash BIGINT NOT NULL,
PRIMARY KEY (fileHash)"))
println(" loading tables...")
println(" ", sql.loadTable("projects", paste(inputFolder, "projects.csv", sep = "/")))
println(" ", sql.loadTable("files", paste(inputFolder, "files.csv.h2i", sep = "/")))
println(" ", sql.loadTable("stats", paste(inputFolder, "stats.csv.h2i", sep = "/")))
}
# creates indices on projects, files and stats tables. Assumes the database containing the tables has already been selected
createCommonIndices <- function() {
println(" creating indices...")
println(" ", sql.createIndex("projects", "projectId"))
println(" ", sql.createIndex("files", "fileId"))
println(" ", sql.createIndex("files", "projectId", unique = F))
println(" ", sql.createIndex("stats", "fileHash"))
println(" ", sql.createIndex("stats", "tokenHash", unique = F))
}
# augments the projects table with files column and counts for each project number of files it contains
calculateProjectSizes <- function() {
println("calculating project sizes...")
sql.query("ALTER TABLE projects ADD COLUMN files INT UNSIGNED NOT NULL DEFAULT 0")
println(" added column files to projects table")
sql.query("CREATE TABLE projects_files SELECT COUNT(*) AS files, projectId AS pid FROM files GROUP BY files.projectId")
println(" file counts calculated")
sql.query("UPDATE projects JOIN projects_files ON projects.projectId = projects_files.pid SET projects.files = projects_files.files")
println(" projects table updated")
sql.query("DROP TABLE projects_files;")
println(" deleted temporary tables")
}
# imports the results of sourcererCC
importSourcerer <- function(dbName, inputFolder) {
sql.connect(username = DB_USER, password = DB_PASSWORD, dbname = dbName, host = DB_HOST)
println(" removing existing tables...")
println(" ", sql.dropTable("CCPairs"))
println(" creating tables...")
println(" ", sql.createTable("CCPairs", "
projectId1 INT UNSIGNED NOT NULL,
fileId1 INT UNSIGNED NOT NULL,
projectId2 INT UNSIGNED NOT NULL,
fileId2 INT UNSIGNED NOT NULL,
PRIMARY KEY (fileId1, fileId2)"))
println(" loading tables...")
println(" ", sql.loadTable("CCPairs", paste(inputFolder, "sourcerer.csv", sep = "/")))
println(" creating indices...")
println(" ", sql.createIndex("CCPairs", "projectId1", unique = F))
println(" ", sql.createIndex("CCPairs", "fileId1", unique = F))
println(" ", sql.createIndex("CCPairs", "projectId2", unique = F))
println(" ", sql.createIndex("CCPairs", "fileId2", unique = F))
sql.disconnect()
}
# Imports Javascript extra data. Alters projects with time of creation and commit at which the project has been tokenized. Alters files with the time of creation, which can be used for originals detection. Loads the data from files_extra and projects_extra files produced by the JS tokenizer. The data is added as extra columns to the files and projects table, which saves space in the database (as opposed to having an extra table) while not breaking any compatibility, the extra columns simply do not have to be used.
importJSExtras <- function(dbName, inputFolder) {
sql.connect(username = DB_USER, password = DB_PASSWORD, dbname = dbName, host = DB_HOST)
println("importing JS specific data...")
println(" creating tables...")
println(" ", sql.createTable("projects_extra", "
projectId INT UNSIGNED NOT NULL,
createdAt INT UNSIGNED NOT NULL,
commit CHAR(40) NOT NULL,
PRIMARY KEY (projectId)"))
println(" ", sql.createTable("files_extra", "
fileId BIGINT NOT NULL,
createdAt INT UNSIGNED NOT NULL,
PRIMARY KEY (fileId)"))
println(" loading tables...")
println(" ", sql.loadTable("projects_extra", paste(inputFolder, "projects_extra.csv", sep = "/")))
println(" ", sql.loadTable("files_extra", paste(inputFolder, "files_extra.csv", sep = "/")))
println(" merging information...")
sql.query("ALTER TABLE projects ADD COLUMN createdAt INT UNSIGNED NOT NULL")
println(" createdAt added to projects")
sql.query("ALTER TABLE projects ADD COLUMN commit CHAR(40) NOT NULL")
println(" commit added to projects")
sql.query("UPDATE projects JOIN projects_extra ON projects.projectId = projects_extra.projectId
SET projects.createdAt = projects_extra.createdAt, projects.commit = projects_extra.commit")
println(" projects table updated")
sql.query("ALTER TABLE files ADD COLUMN createdAt INT UNSIGNED NOT NULL")
println(" createdAt added to files")
sql.query("UPDATE files JOIN files_extra ON files.fileId = files_extra.fileId SET files.createdAt = files_extra.createdAt")
println(" files table updated")
println(" deleting temporary tables")
println(" ", sql.dropTable("projects_extra"))
println(" ", sql.dropTable("files_extra"))
sql.disconnect()
}
# Imports the infromation about npm files (and extra information about the files in the dataset)
importAndCreateJS_NPM <- function(dbName, dbOrigin, inputFolder) {
sql.connect(username = DB_USER, password = DB_PASSWORD, dbname = dbOrigin, host = DB_HOST)
importNPMInfo(inputFolder)
sql.switchDb(dbName)
createNonNPMDataset(dbOrigin)
sql.disconnect()
}
# Augments the files table with the information whether the file belongs to an NPM package or not.
importNPMInfo <- function(inputFolder) {
println("importing NPM & file origin information")
println(" creating files_nm table...")
sql.dropTable("files_nm")
sql.createTable("files_nm", "
fileId BIGINT UNSIGNED NOT NULL,
pathDepth SMALLINT UNSIGNED NOT NULL,
npmDepth SMALLINT UNSIGNED NOT NULL,
test TINYINT NOT NULL,
locale TINYINT NOT NULL,
moduleName VARCHAR(255) NOT NULL,
blameModule VARCHAR(255) NOT NULL,
fileName VARCHAR(1000) NOT NULL,
fileExt VARCHAR(255) NOT NULL,
inModuleName VARCHAR(4000) NOT NULL,
PRIMARY KEY (fileId)")
println(" loading table...")
println(" ", sql.loadTable("files_nm", paste(inputFolder, "files_nm.csv", sep = "/")))
# now alter the files table, add package and test categories
println(" altering files table")
sql.query("ALTER TABLE files ADD COLUMN npm TINYINT NOT NULL")
println(" npm column")
sql.query("ALTER TABLE files ADD COLUMN test TINYINT NOT NULL")
# and merge the information from the files_nm table
println(" updating the files table...")
sql.query("UPDATE files JOIN files_nm ON files.fileId = files_nm.fileId SET files.npm = IF(files_nm.npmDepth > 0, 1, 0), files.test = files_nm.test")
# we keep everything in the files_nm table as well, it might be useful in the future?
}
# Takes the origin database (which should have npm info already present) and creates projects files & stats tables in current database containing only those not in npm modules
createNonNPMDataset <- function(origin) {
println("copying only NPM data...")
sql.query("CREATE TABLE projects AS SELECT projectId, projectUrl, createdAt, commit FROM ", origin, ".projects")
println(" projects")
sql.query("CREATE TABLE files AS SELECT fileId, projectId, relativeUrl, fileHash, createdAt, test FROM ", origin, ".files WHERE npm = 0")
println(" files")
sql.query("CREATE TABLE stats AS SELECT * FROM ", origin, ".stats WHERE fileHash IN (SELECT DISTINCT fileHash FROM files)")
println(" stats")
createCommonIndices()
calculateProjectSizes()
}
# exports the data about files and projects required to calculate project level cloning
exportCloneFinderData <- function(dbName, outputFolder, threshold = 0) {
sql.connect(username = DB_USER, password = DB_PASSWORD, dbname = dbName, host = DB_HOST)
println("exporting clone finder input data")
println(" creating clone finder's input...")
sql.query("SELECT projectId, totalTokens, tokenHash FROM files JOIN stats ON files.fileHash = stats.fileHash WHERE totalTokens > ",threshold," INTO OUTFILE \"", outputFolder, "/clone_finder.csv\" FIELDS TERMINATED BY ','");
sql.disconnect()
}
exportHeatmapData <- function(dbName, outputFolder) {
sql.connect(username = DB_USER, password = DB_PASSWORD, dbname = dbName, host = DB_HOST)
println("exporting projects_heat.csv")
sql.query("SELECT projectId, stars, commits FROM projects INTO OUTFILE \"", outputFolder, "/projects_heat.csv\" FIELDS TERMINATED BY ','");
sql.disconnect();
}
exportAggregationData <- function(dbName, outputFolder) {
sql.connect(username = DB_USER, password = DB_PASSWORD, dbname = dbName, host = DB_HOST)
println("exporting files_statistics.csv")
sql.query("SELECT files.fileId, createdAt, npm, files.test, fileExt, tokenHash FROM files JOIN files_nm ON files.fileId = files_nm.fileId JOIN stats ON files.fileHash = stats.fileHash INTO OUTFILE \"", outputFolder, "/files_statistics.csv\" FIELDS TERMINATED BY ','");
sql.disconnect();
}
# when project level cloning is calculated, loads it in the database
importCloneFinderData <- function(dbName, inputFolder, numThreads) {
sql.connect(username = DB_USER, password = DB_PASSWORD, dbname = dbName, host = DB_HOST)
println(" creating cf table projectClones")
println(" dropping if exists", sql.dropTable("projectClones"))
println(" creating", sql.createTable("projectClones","
cloneId INT UNSIGNED NOT NULL,
cloneClonedFiles INT UNSIGNED NOT NULL,
cloneTotalFiles INT UNSIGNED NOT NULL,
cloneCloningPercent DECIMAL(6,3) NOT NULL,
hostId INT UNSIGNED NOT NULL,
hostAffectedFiles INT UNSIGNED NOT NULL,
hostTotalFiles INT UNSIGNED NOT NULL,
hostAffectedPercent DECIMAL(6,3) NOT NULL,
PRIMARY KEY (cloneId, hostId)"))
println(" loading chunks...")
for (i in 0:(numThreads - 1)) {
filename = paste(inputFolder, "/project_clones.", i, ".csv", sep = "")
println(" ", sql.loadTable("projectClones", filename))
}
println(" creating indices...")
println(" ", sql.createIndex("projectClones", "cloneId", unique = F))
println(" ", sql.createIndex("projectClones", "cloneTotalFiles", unique = F))
println(" ", sql.createIndex("projectClones", "cloneCloningPercent", unique = F))
println(" ", sql.createIndex("projectClones", "hostId", unique = F))
println(" ", sql.createIndex("projectClones", "hostTotalFiles", unique = F))
println(" ", sql.createIndex("projectClones", "hostAffectedPercent", unique = F))
sql.disconnect()
}
downloadMetadata <- function(dataset, outputDir, secrets, stride = 1, strides = 1) {
library(RCurl)
library(rjson)
library(bitops)
sql.connect(user = DB_USER, password = DB_PASSWORD, dbname = dataset, host = DB_HOST)
# now get all the projects we want to get metadata for
projects = sql.query("SELECT projectId, projectUrl FROM projects ORDER BY projectId")
sql.disconnect()
numProjects = length(projects$projectId)
println("total projects: ", numProjects)
f = file(paste(outputDir, "/projects_metadata-",stride,".csv", sep = ""), "wt")
si = 1L
errors = 0
i = stride
while (i <= numProjects) {
secret = secrets[[si]]
si = si + 1L
if (si > length(secrets))
si = 1L
pid = projects$projectId[[i]]
tryCatch({
x = getProjectMetadata(pid, projects$projectUrl[[i]], secret)
if (x$stars == "NULL") {
cat(paste(x$id, "-1,-1,-1,-1\n", sep = ","), file = f)
errors = errors + 1
} else {
cat(paste(x$id, x$stars, x$subscribers, x$forks, x$openIssues, sep = ","), file = f)
cat("\n", file = f)
}
}, error = function(e) errors = errors + 1)
i = i + strides
if (i %% 1000 == stride)
println(" ", i, " errors: ", errors)
}
println(" TOTAL ERRORS: ", errors)
close(f)
}
getProjectMetadata <- function(pid, url, secret) {
url = paste("https://api.github.com/repos/", url, sep = "")
#println(pid)
result = list(id = pid)
x = fromJSON(getURL(url, USERAGENT = "prl-prg", FOLLOWLOCATION = T, HTTPHEADER = paste("Authorization: token ", secret, sep = "")))
result$stars = x["stargazers_count"]
result$subscribers = x["subscribers_count"]
result$forks = x["forks_count"]
result$openIssues = x["open_issues_count"]
result
}
importMetadata <- function(dataset, inputFolder, strides = 1) {
sql.connect(user = DB_USER, password = DB_PASSWORD, dbname = dataset, host = DB_HOST)
sql.dropTable("projects_metadata")
sql.createTable("projects_metadata", "
projectId INT NOT NULL,
stars INT NOT NULL,
subscribers INT NOT NULL,
forks INT NOT NULL,
openIssues INT NOT NULL,
PRIMARY KEY (projectId)")
println(" loading chunks...")
for (i in 1:(strides)) {
filename = paste(inputFolder, "/projects_metadata-", i, ".csv", sep = "")
println(" ", sql.loadTable("projects_metadata", filename))
}
println(" altering projects table...")
sql.query("ALTER TABLE projects ADD COLUMN stars INT NOT NULL DEFAULT 0")
sql.query("ALTER TABLE projects ADD COLUMN subscribers INT NOT NULL DEFAULT 0")
sql.query("ALTER TABLE projects ADD COLUMN forks INT NOT NULL DEFAULT 0")
sql.query("ALTER TABLE projects ADD COLUMN openIssues INT NOT NULL DEFAULT 0")
sql.query("UPDATE projects JOIN projects_metadata ON projects.projectId = projects_metadata.projectId SET
projects.stars = projects_metadata.stars,
projects.subscribers = projects_metadata.subscribers,
projects.forks = projects_metadata.forks,
projects.openIssues = projects_metadata.openIssues")
sql.dropTable("projects_metadata");
}
importCommits <- function(dataset, inputFolder) {
sql.connect(user = DB_USER, password = DB_PASSWORD, dbname = dataset, host = DB_HOST)
sql.dropTable("projects_commits")
sql.createTable("projects_commits", "
projectId INT NOT NULL,
commits INT NOT NULL,
PRIMARY KEY (projectId)")
println(" loading data...")
filename = paste(inputFolder, "/project_commits.csv", sep = "")
println(" ", sql.loadTable("projects_commits", filename))
println(" altering projects table...")
sql.query("ALTER TABLE projects ADD COLUMN commits INT NOT NULL DEFAULT 0")
sql.query("UPDATE projects JOIN projects_commits ON projects.projectId = projects_commits.projectId SET
projects.commits = projects_commits.commits")
sql.dropTable("projects_commits");
}
# SQL functions -------------------------------------------------------------------------------------------------------
DB_CONNECTION_ = NULL
LAST_SQL_TIME_ = NULL
# connects to the given db server and opens the database name, if the database name does not exist, creates it. Keeps the connection alive
sql.connect <- function(username = DB_USER, password = DB_PASSWORD, dbname, host = DB_HOST) {
# disconnect first, if we have existing connection
sql.disconnect()
tryCatch({
# now connect to the database
DB_CONNECTION_ <<- dbConnect(MySQL(), user = username, password = password, host = host, dbname = dbname)
}, error = function(e) {
# if the error is the databse does not exist, create it
if (length(grep("Failed to connect to database: Error: Unknown database", e$message)) > 0) {
DB_CONNECTION_ <<- dbConnect(MySQL(), user = username, password = password, host = host)
sql.query("CREATE DATABASE ", dbname)
println("Creating database ", dbname)
sql.disconnect()
sql.connect(username, password, dbname, host)
} else {
stop(e)
}
})
}
# disconnects from the database
sql.disconnect <- function() {
if (! is.null(DB_CONNECTION_)) {
dbDisconnect(DB_CONNECTION_)
DB_CONNECTION_ <<- NULL
}
}
# concatenates the arguments into one string and executes it as query, if updateTime is T, stores the time the query took on server
sql.query <- function(..., updateTime = T) {
result <- 0
f <- function() {
res <- dbSendQuery(DB_CONNECTION_, paste(..., sep = ""))
result <<- dbFetch(res, n = -1)
dbClearResult(res)
}
if (updateTime) {
LAST_SQL_TIME_ <<- system.time({
f()
})
} else {
f()
}
result
}
# returns the time in seconds it took the last query to execute on the server
sql.lastTime <- function() {
if (is.null(LAST_SQL_TIME_))
0
else
LAST_SQL_TIME_[["elapsed"]]
}
# creates (recreates) index on given table and column
sql.createIndex <- function(table, column, unique = T) {
index = gsub(",", "", column)
index = gsub(" ", "", index)
index = paste("index_", index, sep="")
x <- sql.query("SHOW INDEX FROM ", table, " WHERE KEY_NAME=\"", index, "\"")$Key_name
if (length(x) > 0)
sql.query("DROP INDEX ", index, " ON ", table)
if (unique)
sql.query("CREATE UNIQUE INDEX ", index, " ON ", table, "(", column, ")")
else
sql.query("CREATE INDEX ", index, " ON ", table, "(", column, ")")
paste("created index ", column, " on table ", table, " in ", sql.lastTime(), "[s]", sep = "")
}
sql.dropTable <- function(name) {
sql.query("DROP TABLE IF EXISTS ", name)
name
}
sql.createTable <- function(name, contents) {
sql.query("CREATE TABLE ", name, " (", contents, ")")
name
}
sql.loadTable <- function(name, file) {
sql.query("LOAD DATA LOCAL INFILE \"", file,"\" INTO TABLE ", name, " FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"'")
file
}
# switches the database, creating a new one if the given database does not exist
sql.switchDb <- function(dbName) {
tryCatch({
sql.query("USE ", dbName)
}, error = function(e) {
# if the error is the databse does not exist, create it
if (length(grep("Unknown database", e$message)) > 0) {
sql.query("CREATE DATABASE ", dbName)
sql.query("USE ", dbName)
} else {
stop(e)
}
})
}
# wrapper function for creating all graphs shown in the paper ---------------------------------------------------------
# prints table 1 in the paper - the corpus statistics
tableCorpus <- function(dbname) {
sql.connect(dbname = dbname)
println(" Counts:")
println(" Projects downloaded ", sql.query("SELECT COUNT(*) FROM projects"))
println(" Projects analyzed ", sql.query("SELECT COUNT(*) FROM projects WHERE files > 0"))
println(" Files analyzed ", sql.query("SELECT COUNT(*) FROM files"))
println(" Means:")
x = sql.query("SELECT files FROM projects")[[1]]
println(" Files per project ",median(x), " sd: ", sd(x))
x = sql.query("SELECT fileSLOC FROM files JOIN stats ON files.fileHash = stats.fileHash")[[1]]
println(" SLOC per file ",median(x), " sd: ", sd(x))
x = sql.query("SELECT stars FROM projects")[[1]]
println(" Stars per project ",median(x), " sd: ", sd(x))
x = sql.query("SELECT commits FROM projects")[[1]]
println(" Commits per project ",median(x), " sd: ", sd(x))
sql.disconnect()
}
fileLevelDup <- function(dbname) {
sql.connect(dbname = dbname)
totalFiles = sql.query("SELECT COUNT(*) FROM files")[[1]]
fileHashes = sql.query("SELECT COUNT(*) FROM stats")[[1]]
tokenHashes = sql.query("SELECT COUNT(DISTINCT tokenHash) FROM stats")[[1]]
sccDup = sql.query("SELECT COUNT(id) FROM (SELECT fileId1 AS id FROM CCPairs UNION SELECT fileId2 AS id FROM CCPairs) AS x")[[1]]
println(" Total files ", totalFiles)
println(" File hashes ", fileHashes, " (", fileHashes/totalFiles, "%)")
println(" Token hashes ", tokenHashes, " (", tokenHashes/totalFiles, "%)")
println(" SCCdupfiles ", sccDup)
println(" SCCuniquefiles ", tokenHashes - sccDup, "(", (tokenHashes - sccDup) / totalFiles, "%)")
sql.disconnect()
}
fileLevelDupNoSmall <- function(dbname) {
sql.connect(dbname = dbname)
totalFiles = sql.query("SELECT COUNT(*) FROM files JOIN stats ON files.fileHash = stats.fileHash WHERE totalTokens >= 50")[[1]]
fileHashes = sql.query("SELECT COUNT(*) FROM stats WHERE totalTokens >= 50")[[1]]
tokenHashes = sql.query("SELECT COUNT(DISTINCT tokenHash) FROM stats WHERE totalTokens >= 50")[[1]]
sccDup = sql.query("SELECT COUNT(id) FROM (SELECT fileId1 AS id FROM CCPairs UNION SELECT fileId2 AS id FROM CCPairs) AS x JOIN files ON id = fileId JOIN stats ON files.fileHash = stats.fileHash WHERE totalTokens >=50")[[1]]
println(" Total files ", totalFiles)
println(" File hashes ", fileHashes, " (", fileHashes/totalFiles, "%)")
println(" Token hashes ", tokenHashes, " (", tokenHashes/totalFiles, "%)")
println(" SCCdupfiles ", sccDup)
println(" SCCuniquefiles ", tokenHashes - sccDup, "(", (tokenHashes - sccDup) / totalFiles, "%)")
sql.disconnect()
}
interProjectCloning <- function(dbname) {
sql.connect(dbname = dbname)
println(" Projects analyzed ", sql.query("SELECT COUNT(*) FROM projects WHERE files > 0"))
println(" Clones >=50% ", sql.query("select count(*) from (select distinct cloneId from projectClones where cloneCloningPercent >= 50 union select distinct hostId from projectClones where hostAffectedPercent >= 50) as combined"))
println(" Clones >=80% ", sql.query("select count(*) from (select distinct cloneId from projectClones where cloneCloningPercent >= 80 union select distinct hostId from projectClones where hostAffectedPercent >= 80) as combined"))
println(" Clones 100% ", sql.query("select count(*) from (select distinct cloneId from projectClones where cloneCloningPercent = 100 union select distinct hostId from projectClones where hostAffectedPercent = 100) as combined"))
println(" exact dups ", sql.query("select count(*) from (select distinct cloneId from projectClones where cloneCloningPercent = 100 and hostAffectedPercent = 100 union select distinct hostId from projectClones where cloneCloningPercent = 100 and hostAffectedPercent = 100) as combined"))
println(" exact dups (>= files) ", sql.query("select count(*) from (select distinct cloneId from projectClones where cloneCloningPercent = 100 and hostAffectedPercent = 100 and cloneTotalFiles >= 10 union select distinct hostId from projectClones where cloneCloningPercent = 100 and hostAffectedPercent = 100 and hostTotalFiles >= 10) as combined"))
sql.disconnect()
}
tokensPerFileQuantiles <- function(dbname) {
sql.connect(dbname = dbname)
x = sql.query("SELECT totalTokens FROM files JOIN stats ON files.fileHash = stats.fileHash")[[1]]
result = list()
result$q = quantile(x, c(.20,0.30,.45,.55,.70,.80,.90))
q1 = result$q[[1]]
q2 = result$q[[2]]
q3 = result$q[[3]]
q4 = result$q[[4]]
q5 = result$q[[5]]
q6 = result$q[[6]]
q7 = result$q[[7]]
s1 = sql.query("SELECT COUNT(*) FROM files JOIN stats ON files.fileHash = stats.fileHash WHERE totalTokens >= ",q1, " AND totalTokens <=", q2)[[1]]
s2 = sql.query("SELECT COUNT(*) FROM files JOIN stats ON files.fileHash = stats.fileHash WHERE totalTokens >= ",q3, " AND totalTokens <=", q4)[[1]]
s3 = sql.query("SELECT COUNT(*) FROM files JOIN stats ON files.fileHash = stats.fileHash WHERE totalTokens >= ",q5, " AND totalTokens <=", q6)[[1]]
s4 = sql.query("SELECT COUNT(*) FROM files JOIN stats ON files.fileHash = stats.fileHash WHERE totalTokens >= ",q7)[[1]]
s = length(x) #sql.query("SELECT COUNT(*) FROM files")[[1]]
result$sums = c(s1, s2, s3, s4)
result$pct = c(s1/s, s2/s, s3/s, s4/s)
result$total = s
sql.disconnect()
result
}
metadataCorpus <- function(dbname) {
sql.connect(dbname = dbname)
println(" Projects analyzed ", sql.query("SELECT COUNT(*) FROM projects WHERE files > 0"))
println(" Projects with 1+ commits ", sql.query("SELECT COUNT(*) FROM projects WHERE files > 0 AND commits > 0"))
sql.disconnect()
}
filesPerProjectDist <- function(dbname, path, title = dbname) {
heatmap = read.csv(paste(path, "/heatmap.csv", sep=""), header = F, col.names = c("pid", "stars", "commits", "files", "originalFiles","containsClones"))
logHistogramFromDF(heatmap, "files", title, "Files per Project", "% of Projects", "Hist_files_per_project.pdf")
}
slocPerFileDist <- function(dbname, title = dbname) {
logHistogram(dbname, "SELECT fileSLOC FROM files JOIN stats ON files.fileHash = stats.fileHash ORDER BY RAND() LIMIT 1000000", title, "SLOC", "% of projects", "Hist_sloc_per_file.pdf")
}
starsPerProjectDist <- function(dbname, path, title = dbname) {
heatmap = read.csv(paste(path, "/heatmap.csv", sep=""), header = F, col.names = c("pid", "stars", "commits", "files", "originalFiles","containsClones"))
logHistogramFromDF(heatmap, "stars", title, "Stars", "% of Projects", "Hist_stars_per_project.pdf")
}
commitsPerProjectDist <- function(dbname, path, title = dbname) {
heatmap = read.csv(paste(path, "/heatmap.csv", sep=""), header = F, col.names = c("pid", "stars", "commits", "files", "originalFiles","containsClones"))
logHistogramFromDF(heatmap, "commits", title, "Commits", "% of Projects", "Hist_commits_per_project.pdf")
}
summaryStats <- function(dbname) {
sql.connect(dbname = dbname)
println("Files per project")
print(summary(sql.query("SELECT files FROM projects")[[1]]))
println("Bytes per file")
print(summary(sql.query("SELECT fileBytes FROM files JOIN stats ON files.fileHash = stats.fileHash")[[1]]))
println("Lines per file")
print(summary(sql.query("SELECT fileLines FROM files JOIN stats ON files.fileHash = stats.fileHash")[[1]]))
println("LOC per file")
print(summary(sql.query("SELECT fileLOC FROM files JOIN stats ON files.fileHash = stats.fileHash")[[1]]))
println("SLOC per file")
print(summary(sql.query("SELECT fileSLOC FROM files JOIN stats ON files.fileHash = stats.fileHash")[[1]]))
println("Distinct tokens per file")
print(summary(sql.query("SELECT uniqueTokens FROM files JOIN stats ON files.fileHash = stats.fileHash")[[1]]))
sql.disconnect()
}
summaryStatsTokenHash <- function(dbname) {
sql.connect(dbname = dbname)
println("Files per project")
print(summary(sql.query("SELECT COUNT(*) FROM (SELECT MIN(projectId) AS pid, COUNT(*) AS cnt FROM files JOIN stats ON files.fileHash = stats.fileHash GROUP BY tokenHash) AS x WHERE x.cnt = 1 GROUP BY x.pid")[[1]]))
println("Bytes per file")
print(summary(sql.query("SELECT AVG(fileBytes) FROM stats GROUP BY tokenHash")[[1]]))
println("Lines per file")
print(summary(sql.query("SELECT AVG(fileLines) FROM stats GROUP BY tokenHash")[[1]]))
println("LOC per file")
print(summary(sql.query("SELECT AVG(fileLOC) FROM stats GROUP BY tokenHash")[[1]]))
println("SLOC per file")
print(summary(sql.query("SELECT AVG(fileSLOC) FROM stats GROUP BY tokenHash")[[1]]))
println("Distinct tokens per file")
print(summary(sql.query("SELECT AVG(uniqueTokens) FROM stats GROUP BY tokenHash")[[1]]))