forked from se-sic/coronet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util-data.R
2416 lines (2046 loc) · 113 KB
/
util-data.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
## This file is part of coronet, which is free software: you
## can redistribute it and/or modify it under the terms of the GNU General
## Public License as published by the Free Software Foundation, version 2.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License along
## with this program; if not, write to the Free Software Foundation, Inc.,
## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
##
## Copyright 2016-2019 by Claus Hunsen <[email protected]>
## Copyright 2017-2019 by Thomas Bock <[email protected]>
## Copyright 2020-2021, 2023 by Thomas Bock <[email protected]>
## Copyright 2017 by Raphael Nömmer <[email protected]>
## Copyright 2017-2018 by Christian Hechtl <[email protected]>
## Copyright 2020 by Christian Hechtl <[email protected]>
## Copyright 2017 by Felix Prasse <[email protected]>
## Copyright 2017 by Ferdinand Frank <[email protected]>
## Copyright 2018-2019 by Jakob Kronawitter <[email protected]>
## Copyright 2019-2020 by Anselm Fehnker <[email protected]>
## Copyright 2020-2021, 2023 by Niklas Schneider <[email protected]>
## Copyright 2021 by Johannes Hostert <[email protected]>
## Copyright 2021 by Mirabdulla Yusifli <[email protected]>
## Copyright 2022 by Jonathan Baumann <[email protected]>
## Copyright 2022-2023 by Maximilian Löffler <[email protected]>
## All Rights Reserved.
## / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
## Libraries ---------------------------------------------------------------
requireNamespace("R6") # for R6 classes
requireNamespace("logging") # for logging
requireNamespace("parallel") # for parallel computation
requireNamespace("lubridate") # for date conversion
## / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
## Constants ---------------------------------------------------------------
## untracked file
UNTRACKED.FILE = "<untracked.file>"
## the empty string which resides in the artifact column when artifact == feature or artifact == function
## in the 'ProjectConf'
UNTRACKED.FILE.EMPTY.ARTIFACT = ""
UNTRACKED.FILE.EMPTY.ARTIFACT.TYPE = ""
## base artifacts (which one actually applies, depends on the artifact parameter in the 'ProjectConf')
BASE.ARTIFACTS = c(
"Base_Feature", ## when artifact == feature
"File_Level", ## when artifact == function
UNTRACKED.FILE ## when artifact == file
)
## mapping of data source to artifact column (for commits: filter artifacts based on the configuration options
## 'commits.filter.base.artifact' and 'commits.filter.untracked.files' of the corresponding 'ProjectConf' object)
DATASOURCE.TO.ARTIFACT.FUNCTION = list(
"commits" = "get.commits",
"mails" = "get.mails",
"issues" = "get.issues"
)
## mapping of data source to the appropiate getter for unfiltered date
## compared to \code{DATASOURCE.TO.ARTIFACT.FUNCTION}, which yields the getter for filtered data.
DATASOURCE.TO.UNFILTERED.ARTIFACT.FUNCTION = list(
"commits" = "get.commits.unfiltered",
"mails" = "get.mails.unfiltered",
"issues" = "get.issues.unfiltered"
)
## Yields the getters associated with additional data sources, e.g. author data.
DATASOURCE.TO.ADDITIONAL.ARTIFACT.FUNCTION = list(
"authors" = "get.authors",
"commit.messages" = "get.commit.messages",
"synchronicity" = "get.synchronicity",
"pasta" = "get.pasta",
"gender" = "get.gender",
"custom.event.timestamps" = "get.custom.event.timestamps"
)
#' Applies a function to list keys
#' Given a list, this function returns a modified list where the list keys have been
#' changed by \code{map.function}, while the values are left unchanged.
#'
#' @param lst the list to rename
#' @param map.function the function applied to \code{lst}'s keys
#'
#' @return \code{lst}, with the keys changed
rename.list.keys = function(lst, map.function) {
names(lst) = lapply(names(lst), map.function)
return (lst)
}
## Combine \code{DATASOURCE.TO.ARTIFACT.FUNCTION}, \code{DATASOURCE.TO.UNFILTERED.ARTIFACT.FUNCTION}
## and \code{DATASOURCE.TO.ADDITIONAL.ARTIFACT.FUNCTION} to build a list giving the getter associated with every
## possible data source stored in ProjectData.
DATASOURCE.FIELDS.TO.ARTIFACT.GLOBAL.FUNCTION = c(
DATASOURCE.TO.ADDITIONAL.ARTIFACT.FUNCTION,
DATASOURCE.TO.ARTIFACT.FUNCTION,
rename.list.keys(DATASOURCE.TO.UNFILTERED.ARTIFACT.FUNCTION, function(x) paste0(x, ".unfiltered"))
)
## mapping of data source to artifact column
DATASOURCE.TO.ARTIFACT.COLUMN = list(
"commits" = "artifact",
"mails" = "thread",
"issues" = "issue.id"
)
## the maximum time difference between subsequent mails of a patchstack
PATCHSTACK.MAIL.DECAY.THRESHOLD = "30 seconds"
## configuration parameters that do not reset the environment when changed
CONF.PARAMETERS.NO.RESET.ENVIRONMENT = c("commit.messages",
"pasta",
"gender",
"synchronicity",
"synchronicity.time.window",
"commits.locked",
"issues.locked",
"mails.locked",
"custom.event.timestamps",
"custom.event.timestamps.locked")
## / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
## ProjectData -------------------------------------------------------------
#' The class \code{ProjectData} provides convenient data handling for a project's data
#' in a lazy fashion.
#'
#' To configure an object of this data-handling class, a configuration object of class
#' \code{ProjectConf} must be passed to the initialization function. Data paths and
#' other configuration options are read from it.
#'
#' The following main data sources are handled:
#' - commits,
#' - mails, and
#' - issues.
#'
#' Furthermore, the following additional and orthogonal data sources are handled:
#' - synchronicity data and
#' - PaStA data.
#'
#' @seealso ProjectConf
ProjectData = R6::R6Class("ProjectData",
## * private -----------------------------------------------------------
private = list(
## * * configuration -----------------------------------------------
project.conf = NULL, # list
## * * raw data ----------------------------------------------------
## commits and commit data
commits = create.empty.commits.list(), # data.frame
commits.unfiltered = create.empty.commits.list(), # data.frame
commit.messages = create.empty.commit.message.list(), # data.frame
## mails
mails.unfiltered = create.empty.mails.list(), # data.frame
mails = create.empty.mails.list(), # data.frame
mails.patchstacks = list(), # list
## issues
issues.unfiltered = create.empty.issues.list(), #data.frame
issues = create.empty.issues.list(), #data.frame
## authors
authors = create.empty.authors.list(), # data.frame
## additional data sources
gender = create.empty.gender.list(), # data.frame
synchronicity = create.empty.synchronicity.list(), # data.frame
pasta = create.empty.pasta.list(), # data.frame
pasta.mails = create.empty.pasta.list(), # data.frame
pasta.commits = create.empty.pasta.list(), # data.frame
## timestamps of mail, issue and commit data
data.timestamps = data.frame(start = numeric(0), end = numeric(0)), # data.frame
## custom timestamps for splitting
custom.event.timestamps = list(), # list
## * * commit filtering --------------------------------------------
#' Filter commits by potentially removing commits to untracked files or to the base artifact (see parameters).
#'
#' @param commits the data.frame of commits on which filtering will be applied
#' @param remove.untracked.files flag whether untracked files are kept or removed
#' @param remove.base.artifact flag whether the base artifact is kept or removed
#' @param filter.bots flag whether commits by bots should be removed
#'
#' @return the commits after all filters have been applied
filter.commits = function(commits, remove.untracked.files, remove.base.artifact, filter.bots) {
logging::logdebug("filter.commits: starting.")
## filter out the untracked files
if (remove.untracked.files) {
commits = subset(commits, file != UNTRACKED.FILE)
}
## filter out the base artifacts (i.e., Base_Feature, File_Level)
if (remove.base.artifact) {
commits = subset(commits, !(artifact %in% BASE.ARTIFACTS))
}
## filter out all commits made by bots
if (filter.bots) {
commits = self$filter.bots(commits)
}
logging::logdebug("filter.commits: finished.")
return(commits)
},
## * * issue filtering --------------------------------------------
#' Filter issue by potentially removing all issue events that are not comments.
#'
#' @param issues the data.frame of issues on which filtering will be applied
#' @param issues.only.comments flag whether non-comment issue events are removed
#' @param filter.bots flag whether bot issues are to be removed
#'
#' @return the issues after all filters have been applied
filter.issues = function(issues, issues.only.comments, filter.bots) {
logging::logdebug("filter.issues: starting.")
if (issues.only.comments) {
issues = issues[issues[["event.name"]] == "commented", ]
}
## filter out all issues made by bots
if (filter.bots) {
issues = self$filter.bots(issues)
}
logging::logdebug("filter.issues: finished.")
return(issues)
},
## * * mail filtering ----------------------------------------------
#' Filter patchstack mails from the mails that are currently cached in the field \code{mails} and return them.
#' Store detected patchstacks in the field \code{patchstack.mails}. They are used later in the
#' function \code{filter.pasta.data} to also accommodate for the deleted mails in the PaStA data.
#'
#' In a thread, a patchstack spans the first sequence of mails where each mail has been authored by the thread
#' creator and has been sent within a short time window (see \code{PATCHSTACK.MAIL.DECAY.THRESHOLD}) after the
#' preceding mail.
#' The mails spanned by a patchstack are called 'patchstack mails'.
#'
#' For each patchstack, all patchstack mails but the first one are filtered.
#'
#' @return the mail data after filtering patchstack mails
filter.patchstack.mails = function() {
logging::logdebug("filter.patchstack.mails: starting.")
## return immediately if no mails are available
if (!self$is.data.source.cached("mails.unfiltered")) {
private$mails.patchstacks = NULL
return(private$mails)
}
## retrieve mails grouped by thread IDs
thread.data = self$group.authors.by.data.column("mails", "thread")
## extract the patchstack mails and the filtered mails for each thread
result = parallel::mclapply(thread.data, function(thread) {
## ensure that all mails within the thread are ordered correctly
thread = thread[order(thread[["date"]]), ]
running = TRUE
i = 1
## find the largest index 'i' for which holds that each mail up to index 'i' has been authored by the
## thread creator and that all mails up to index 'i' have been received within a succesive time window
## of 'PATCHSTACK.MAIL.DECAY.THRESHOLD'
while (i < nrow(thread) && running) {
if (thread[1, "author.name"] == thread[i + 1, "author.name"] &&
thread[i + 1, "date"] - thread[i, "date"] <=
lubridate::as.duration(PATCHSTACK.MAIL.DECAY.THRESHOLD)) {
i = i + 1
} else {
running = FALSE
}
}
## return the mails of the thread with all patchstack mails but the first one being removed
return (list(keep = thread[setdiff(seq_len(nrow(thread)), seq_len(i)[-1]), ],
patchstack = thread[seq_len(i), ]))
})
## override thread data with filtered thread data
thread.data = lapply(result, function(x) x[["keep"]])
## flatten the list of mail-dataframes (i.e. thread.data) to a single mail-dataframe
mails = plyr::rbind.fill(thread.data)
## Retrieve patchstacks from the result above which are used to manipulate the PaStA data. This needs to be
## done because the PaStA data relates to some of the filtered mails and must be adjusted accordingly.
patchstacks = lapply(result, function(x) x[["patchstack"]])
## only patchstacks that contain at least two mails are considered patchstacks
patchstacks = patchstacks[lapply(patchstacks, nrow) > 1]
## store patchstack information
private$mails.patchstacks = patchstacks
logging::logdebug("filter.patchstack.mails: finished.")
return(mails)
},
#' Filter mail data by for example removing all mails that have been sent by bots.
#'
#' @param mails the data.frame of mails on which filtering will be applied
#' @param filter.bots flag whether bot mails are to be removed
#'
#' @return the mails after all filters have been applied
filter.mails = function(mails, filter.bots) {
logging::logdebug("filter.mails: starting.")
## filter out all mails made by bots
if (filter.bots) {
mails = self$filter.bots(mails)
}
logging::logdebug("filter.mails: finished.")
return(mails)
},
## * * commit message data ------------------------------------------
#' Add the columns \code{title} and \code{message} to commits using the selected
#' configuration option of \code{commit.messages} and the results of the function \code{get.commit.messages}.
update.commit.message.data = function() {
logging::loginfo("Merging commit messages into commit data.")
if (self$is.data.source.cached("commits.unfiltered")) {
## remove the columns that are present in case that the parameter was changed to from "messages" to
## "none" or "title"
private$commits.unfiltered = private$commits.unfiltered[ , colnames(private$commits.unfiltered) != "message"]
private$commits.unfiltered = private$commits.unfiltered[ , colnames(private$commits.unfiltered) != "title"]
## when 'none' is selected, we are ready here, else merge the possibly new data to the cleaned commits
if (private$project.conf$get.value("commit.messages") != "none") {
## get commit messages
commit.messages = private$commit.messages
## now there are only three columns left: commit.id, title, message
## check whether to include only title or also the messages
if (private$project.conf$get.value("commit.messages") == "title") {
commit.messages = commit.messages[ , colnames(commit.messages) != "message"]
}
## get a vector with the column names in the right order
col.names = unique(c(colnames(private$commits.unfiltered), colnames(commit.messages)))
## merge them into the commit data
private$commits.unfiltered = merge(private$commits.unfiltered, commit.messages,
by = c("commit.id", "hash"), all.x = TRUE, sort = FALSE)
## adjust the column order
private$commits.unfiltered = private$commits.unfiltered[col.names]
}
}
if (self$is.data.source.cached("commits")) {
## remove the columns that are present in case that the parameter was changed to from "messages" to
## "none" or "title"
private$commits = private$commits[ , colnames(private$commits) != "message"]
private$commits = private$commits[ , colnames(private$commits) != "title"]
## when 'none' is selected, we are ready here, else merge the possibly new data to the cleaned commits
if (private$project.conf$get.value("commit.messages") != "none") {
## get commit messages
commit.messages = private$commit.messages
## now there are only three columns left: commit.id, title, message
## check whether to include only title or also the messages
if (private$project.conf$get.value("commit.messages") == "title") {
commit.messages = commit.messages[ , colnames(commit.messages) != "message"]
}
## get a vector with the column names in the right order
col.names = unique(c(colnames(private$commits), colnames(commit.messages)))
## merge them into the commit data
private$commits = merge(private$commits, commit.messages,
by = c("commit.id", "hash"), all.x = TRUE, sort = FALSE)
## adjust the column order
private$commits = private$commits[col.names]
}
}
## get the caller function as a string
stacktrace = get.stacktrace(sys.calls())
caller = get.second.last.element(stacktrace)
## only print warning if this function has not been called by 'cleanup.commit.message.data' including the
## case that it is called manually, i.e. the stack is too short.
if (is.na(caller) || paste(caller, collapse = " ") != "cleanup.commit.message.data()") {
logging::logwarn("There might be commit message data that does not appear in the commit data.
To clean this up you can call the function 'cleanup.commit.message.data()'.")
}
},
## * * Gender data --------------------------------------------------
#' Update the gender related fields of: \code{authors}
#'
#' This method should be called whenever the field \code{gender} is changed.
update.gender.data = function() {
logging::logdebug("update.gender.data: starting.")
## update author data by attaching gender data
if (!is.null(private$authors)) {
## remove previous gender data
private$authors["gender"] = NULL
## merge gender data
private$authors = merge(private$authors, private$gender,
by = "author.name", all.x = TRUE, sort = FALSE)
}
logging::logdebug("update.gender.data: finished.")
},
## * * PaStA data --------------------------------------------------
#' Use the information about the deleted patchstack mails that are stored in the field \code{patchstack.mails}
#' to also filter out PaStA information that relates to the deleted mails.
#'
#' The PaStA information is not discarded completely however but instead is gathered for each patchstack and is
#' assigned to the first mail in each patchstack because this very first mail has not been filtered and
#' represents the patchstack.
#'
#' @return the filtered PaStA data
filter.pasta.data = function() {
logging::logdebug("filter.pasta.data: starting.")
new.pasta = parallel::mclapply(private$mails.patchstacks, function(patchstack) {
## get all PaStA data that relates to the current patchstack (do not drop data.frame structure!)
pasta.tmp = private$pasta[private$pasta[["message.id"]] %in% patchstack[["message.id"]], , drop = FALSE]
## override all old message IDs with the message ID of the first mail in the patchstack since it
## is the only one that is kept (if any data is available in 'pasta.tmp')
if (nrow(pasta.tmp) > 0) {
pasta.tmp["message.id"] = patchstack[1, "message.id"]
}
return(pasta.tmp)
})
## combine new re-written PaStA data
new.pasta = plyr::rbind.fill(new.pasta)
## remove potential duplicates
new.pasta = unique(new.pasta)
## remove old items from PaStA data
## 1) flatten the list of mail-dataframes (i.e. patchstacks) to a single mail-dataframe
patchstack.mails = plyr::rbind.fill(private$mails.patchstacks)
## 2) delete any PaStA information that relate to message IDs of mails that will be discarded
pasta = private$pasta[!(private$pasta[["message.id"]] %in% patchstack.mails[["message.id"]]), ]
## append the new pasta data to the old pasta data
pasta = plyr::rbind.fill(pasta, new.pasta)
## reestablish ordering using the 'revision.set.id' column of the PaStA data
pasta = pasta[order(pasta[["revision.set.id"]]), ]
logging::logdebug("filter.pasta.data: finished.")
return(pasta)
},
#' Aggregate PaStA data for convenient merging to main data sources.
#'
#' In detail, the given PaStA data is independently aggregated by both the
#' columns \code{commit.hash} and \code{message.id}. The resulting data.frames
#' are stored in the fields \code{pasta.commits} and \code{pasta.mails},
#' respectively.
#'
#' **Note**: The column \code{commit.hash} gets renamed to \code{hash} to match
#' the corresponding column in the commit data (see \code{read.commits}).
aggregate.pasta.data = function() {
logging::logdebug("aggregate.pasta.data: starting.")
## check for data first
if (!self$is.data.source.cached("pasta")) {
## take (empty) input data and no rows from it
private$pasta.mails = create.empty.pasta.list()
private$pasta.commits = create.empty.pasta.list()
} else {
## compute aggregated data.frames for easier merging
## 1) define group function (determines result in aggregated data.frame cells)
group.fun = unname # unique
## 2) aggregate by message ID
group.col = "message.id"
private$pasta.mails = aggregate(
as.formula(sprintf(". ~ %s", group.col)), private$pasta,
group.fun, na.action = na.pass
)
## 3) aggregate by commit hash
group.col = "commit.hash"
private$pasta.commits = aggregate(
as.formula(sprintf(". ~ %s", group.col)), private$pasta,
group.fun, na.action = na.pass
)
}
## set column names consistent to main data
colnames(private$pasta.mails) = c("message.id", "pasta", "revision.set.id")
colnames(private$pasta.commits) = c("hash", "pasta", "revision.set.id")
logging::logdebug("aggregate.pasta.data: finished.")
},
#' Update the PaStA-related columns \code{pasta} and \code{revision.set.id} that are appended to \code{commits}
#' using the currently available PaStA data from the field \code{pasta.commits}.
update.pasta.commit.data = function() {
logging::logdebug("update.pasta.commit.data: starting.")
## remove previous PaStA data
private$commits.unfiltered["pasta"] = NULL
private$commits.unfiltered["revision.set.id"] = NULL
## only merge new data if pasta has been configured (it could also be changed to 'FALSE' in which case
## we want to just remove the columns above)
if (private$project.conf$get.value("pasta")) {
## merge PaStA data
private$commits.unfiltered = merge(private$commits.unfiltered, private$pasta.commits,
by = "hash", all.x = TRUE, sort = FALSE)
## sort by date again because 'merge' disturbs the order
private$commits.unfiltered = private$commits.unfiltered[order(private$commits.unfiltered[["date"]], decreasing = FALSE), ]
## remove duplicated revision set ids
private$commits.unfiltered[["revision.set.id"]] = lapply(private$commits.unfiltered[["revision.set.id"]], function(rev.id) {
return(unique(rev.id))
})
}
## remove previous PaStA data
private$commits["pasta"] = NULL
private$commits["revision.set.id"] = NULL
## only merge new data if pasta has been configured (it could also be changed to 'FALSE' in which case
## we want to just remove the columns above)
if (private$project.conf$get.value("pasta")) {
## merge PaStA data
private$commits = merge(private$commits, private$pasta.commits,
by = "hash", all.x = TRUE, sort = FALSE)
## sort by date again because 'merge' disturbs the order
private$commits = private$commits[order(private$commits[["date"]], decreasing = FALSE), ]
## remove duplicated revision set ids
private$commits[["revision.set.id"]] = lapply(private$commits[["revision.set.id"]], function(rev.id) {
return(unique(rev.id))
})
}
logging::logdebug("update.pasta.commit.data: finished.")
},
#' Update the PaStA-related columns \code{pasta} and \code{revision.set.id} that are appended to \code{mails}
#' using the currently available PaStA data from the field \code{pasta.mails}.
update.pasta.mail.data = function() {
logging::logdebug("update.pasta.mail.data: starting.")
## remove previous PaStA data
private$mails.unfiltered["pasta"] = NULL
private$mails.unfiltered["revision.set.id"] = NULL
## only merge new data if pasta has been configured (it could also be changed to 'FALSE' in which case
## we want to just remove the columns above)
if (private$project.conf$get.value("pasta")) {
## merge PaStA data
private$mails.unfiltered = merge(private$mails.unfiltered, private$pasta.mails,
by = "message.id", all.x = TRUE, sort = FALSE)
## sort by date again because 'merge' disturbs the order
private$mails.unfiltered = private$mails.unfiltered[order(private$mails.unfiltered[["date"]], decreasing = FALSE), ]
## remove duplicated revision set ids
private$mails.unfiltered[["revision.set.id"]] = lapply(private$mails.unfiltered[["revision.set.id"]], function(rev.id) {
return(unique(rev.id))
})
}
## remove previous PaStA data
private$mails["pasta"] = NULL
private$mails["revision.set.id"] = NULL
## only merge new data if pasta has been configured (it could also be changed to 'FALSE' in which case
## we want to just remove the columns above)
if (private$project.conf$get.value("pasta")) {
## merge PaStA data
private$mails = merge(private$mails, private$pasta.mails,
by = "message.id", all.x = TRUE, sort = FALSE)
## sort by date again because 'merge' disturbs the order
private$mails = private$mails[order(private$mails[["date"]], decreasing = FALSE), ]
## remove duplicated revision set ids
private$mails[["revision.set.id"]] = lapply(private$mails[["revision.set.id"]], function(rev.id) {
return(unique(rev.id))
})
}
logging::logdebug("update.pasta.mail.data: finished.")
},
#' Recompute the values of the cached fields \code{pasta.mails} and \code{pasta.commits} using the currrently
#' available PaStA information of the field \code{pasta} and also assign/update this PaStA information to
#' \code{mails} and \code{commits}.
#'
#' This method should be called whenever the field \code{pasta} is changed.
update.pasta.data = function() {
logging::logdebug("update.pasta.data: starting.")
## filter patchstack mails from PaStA data if configured
if (private$project.conf$get.value("mails.filter.patchstack.mails")) {
private$pasta = private$filter.pasta.data()
}
## aggregate by message IDs and commit hashes
private$aggregate.pasta.data()
## update mail data by attaching PaStA data
private$update.pasta.mail.data()
## update commit data by attaching PaStA data
private$update.pasta.commit.data()
## get the caller function as a string
stacktrace = get.stacktrace(sys.calls())
caller = get.second.last.element(stacktrace)
## only print warning if this function has not been called by 'cleanup.pasta.data' including the case
## that it is called manually, i.e. the stack is too short.
if (all(is.na(caller)) || paste(caller, collapse = " ") != "cleanup.pasta.data()") {
logging::logwarn("There might be PaStA data that does not appear in the mail or commit data.
To clean this up you can call the function 'cleanup.pasta.data()'.")
}
logging::logdebug("update.pasta.data: finished.")
},
## * * synchronicity data ------------------------------------------
#' Update the column \code{synchronicity} that is appended to commits using the currently available
#' synchronicity data from the field \code{synchronicity}.
#'
#' This method should be called whenever the field \code{synchronicity} is changed.
update.synchronicity.data = function() {
logging::logdebug("update.synchronicity.data: starting.")
## update commit data by attaching synchronicity data
## do not check whether synchronicity is available in order to remove the columns if it is not
## remove previous synchronicity data
private$commits.unfiltered["synchronicity"] = NULL
## only merge new data if synchronicity has been configured (it could also be changed to 'FALSE' in
## which case we want to just remove the columns above)
if (private$project.conf$get.value("synchronicity")) {
## merge synchronicity data
private$commits.unfiltered = merge(private$commits.unfiltered, private$synchronicity,
by = "hash", all.x = TRUE, sort = FALSE)
## sort by date again because 'merge' disturbs the order
private$commits.unfiltered = private$commits.unfiltered[order(private$commits.unfiltered[["date"]], decreasing = FALSE), ]
}
## remove previous synchronicity data
private$commits["synchronicity"] = NULL
## only merge new data if synchronicity has been configured (it could also be changed to 'FALSE' in
## which case we want to just remove the columns above)
if (private$project.conf$get.value("synchronicity")) {
## merge synchronicity data
private$commits = merge(private$commits, private$synchronicity,
by = "hash", all.x = TRUE, sort = FALSE)
## sort by date again because 'merge' disturbs the order
private$commits = private$commits[order(private$commits[["date"]],
decreasing = FALSE), ]
}
## get the caller function as a string
stacktrace = get.stacktrace(sys.calls())
caller = get.second.last.element(stacktrace)
## only print warning if this function has not been called by 'cleanup.synchronicity.data' including the case
## that it is called manually, i.e. the stack is too short.
if (all(is.na(caller)) || paste(caller, collapse = " ") != "cleanup.synchronicity.data()") {
logging::logwarn("There might be synchronicity data that does not appear in the commit data.
To clean this up you can call the function 'cleanup.synchronicity.data()'.")
}
logging::logdebug("update.synchronicity.data: finished.")
},
## * * timestamps --------------------------------------------------
#' Call the getters of the specified data sources in order to
#' initialize the sources and extract the timestamps.
#'
#' @param data.sources the data sources to be prepared
prepare.timestamps = function(data.sources) {
for (source in data.sources) {
self[[ DATASOURCE.TO.UNFILTERED.ARTIFACT.FUNCTION[[source]] ]]()
}
},
#' Extract the earliest and the latest date from the specified data source
#' and store it to the timestamps data.frame.
#'
#' @param name the name the timestamps are registered as
#' @param source the specified data source
extract.timestamps = function(name, source) {
## initialize data structure for timestamp
if (is.null(private$data.timestamps)) {
## let this stay as a sanity-check; this is actually initialized this way when creating the object
private$data.timestamps = data.frame(start = numeric(0), end = numeric(0))
}
## collect minimum and maximum date for data source
## 1) if we have data available
if (nrow(private[[source]]) > 0) {
source.date.min = min(private[[source]][, "date"])
source.date.max = max(private[[source]][, "date"])
}
## NAs otherwise
else {
source.date.min = NA
source.date.max = NA
}
## remove old line if existing
private$data.timestamps = subset(
private$data.timestamps,
!(rownames(private$data.timestamps) == name)
)
## store the data in the timestamp data set
private$data.timestamps = rbind(
private$data.timestamps,
data.frame(
start = source.date.min,
end = source.date.max,
row.names = name
)
)
}
),
## * public ------------------------------------------------------------
public = list(
#' The constructor of the class.
#'
#' @param project.conf the given \code{ProjectConf} object for this instance of the class
initialize = function(project.conf) {
## check arguments
private$project.conf = verify.argument.for.parameter(project.conf, "ProjectConf", "ProjectData$new")
## if we have a direct subclass of ProjectData here,
## log this accordingly
if (class(self)[1] == "ProjectData") {
logging::loginfo("Initialized data object %s", self$get.class.name())
}
},
## * * printing ----------------------------------------------------
#' The toString method of the class.
get.class.name = function() {
return(
sprintf("ProjectData<%s>", private$project.conf$get.value("repo"))
)
},
## * * resetting environment ---------------------------------------
#' Reset the current environment in order to rebuild it.
#' Has to be called whenever the project configuration or data gets
#' changed.
reset.environment = function() {
private$authors = create.empty.authors.list()
private$commits.unfiltered = create.empty.commits.list()
private$commits = create.empty.commits.list()
private$commit.messages = create.empty.commit.message.list()
private$data.timestamps = data.frame(start = numeric(0), end = numeric(0))
private$issues = create.empty.issues.list()
private$issues.unfiltered = create.empty.issues.list()
private$mails.unfiltered = create.empty.mails.list()
private$mails = create.empty.mails.list()
private$mails.patchstacks = list()
private$pasta = create.empty.pasta.list()
private$pasta.mails = create.empty.pasta.list()
private$pasta.commits = create.empty.pasta.list()
private$gender = create.empty.gender.list()
private$synchronicity = create.empty.synchronicity.list()
},
## * * configuration -----------------------------------------------
#' Get the current project configuration.
#'
#' @return the \code{ProjectConf} object of the current instance of the class
get.project.conf = function() {
return(private$project.conf)
},
#' Set the current project configuration to the given one.
#'
#' @param project.conf the new project configuration.
#' @param reset.environment parameter to determine whether the environment
#' has to be reset or not [default: FALSE]
set.project.conf = function(project.conf, reset.environment = FALSE) {
private$project.conf = verify.argument.for.parameter(project.conf, "ProjectConf",
"ProjectData$set.project.conf")
if (reset.environment) {
self$reset.environment()
}
},
#' Get a value of the project configuration.
#'
#' @return the value of the given entry name
get.project.conf.entry = function(entry) {
return(private$project.conf$get.value(entry))
},
#' Set a value of the project configuration and, in some cases (i.e. when the parameters allow it)
#' resets the environment.
#'
#' @param entry the configuration option to set. The environment will not be reset, if and only if the entry to
#' change affects a parameter controlling whether or not to read additional data sources, that is,
#' if the parameter is one of the elements of the vector \code{CONF.PARAMETERS.NO.RESET.ENVIRONMENT}.
#' @param value the new value that is assigned to the configuration parameter
set.project.conf.entry = function(entry, value) {
private$project.conf$update.value(entry, value)
## only reset the environment when the 'entry' parameter is not one of the keys where it should not be reset
if (!(entry %in% CONF.PARAMETERS.NO.RESET.ENVIRONMENT)) {
self$reset.environment()
} else {
## if the 'commit.messages' parameter has been changed, update the commit message data, since we want to
## merge the data again if the parameter e.g. changed from "title" to "messages" or vice versa
if (entry == "commit.messages") {
private$update.commit.message.data()
}
## if the 'pasta' parameter has been changed, update the pasta data, since we want to
## merge the data again if the parameter changed
if (entry == "pasta") {
private$update.pasta.data()
}
## if the 'synchronicity' or 'synchronicity.time.window' parameter has been changed, update the
## synchronicity data, since we want to merge the data again if the parameter changed
if (entry == "synchronicity" || entry == "synchronicity.time.window") {
## re-read synchronicity data if the time window is changed and the data is cached because the
## change invalidates the cache
if (entry == "synchronicity.time.window" && self$is.data.source.cached("synchronicity")) {
self$set.synchronicity(NULL)
self$get.synchronicity()
} else {
private$update.synchronicity.data()
}
}
## if the 'custom.event.timestamps.file' parameter has changed, we want to clear them to trigger a re-read.
if (entry == "custom.event.timestamps.file") {
self$clear.custom.event.timestamps()
}
}
},
#' Update the project configuration based on the given list
#' of values and, in some cases (i.e. when the parameters allow it), reset the environment afterwards.
#'
#' @param updated.values the new values for the project configuration.
#' If at least one of the configuration parameters is not an element of the vector
#' \code{CONF.PARAMETERS.NO.RESET.ENVIRONMENT}, the environment will be reset.
#' [default: list()]
update.project.conf = function(updated.values = list()) {
private$project.conf$update.values(updated.values = updated.values)
## get the names of the parameters that are updated and a logical vector indicating whether the
## parameters cause a reset or not
params = names(updated.values)
params.keep.environment = params %in% CONF.PARAMETERS.NO.RESET.ENVIRONMENT
## only reset if at least one of them should cause a reset
if(!all(params.keep.environment)) {
self$reset.environment()
} else {
## if the 'commit.messages' parameter has been changed, update the commit message data, since we want to
## merge the data again if the parameter e.g. changed from "title" to "messages" or vice versa
if (c("commit.messages") %in% params) {
private$update.commit.message.data()
}
## if the 'pasta' parameter has been changed, update the pasta data, since we want to
## merge the data again if the parameter changed
if (c("pasta") %in% params) {
private$update.pasta.data()
}
## if the 'synchronicity' or 'synchronicity.time.window' parameter has been changed, update the
## synchronicity data, since we want to merge the data again if the parameter changed
if (c("synchronicity") %in% params || c("synchronicity.time.window") %in% params) {
## re-read synchronicity data if the time window is changed and the data is cached because the
## change invalidates the cache
if (c("synchronicity.time.window") %in% params && self$is.data.source.cached("synchronicity")) {
self$set.synchronicity(NULL)
self$get.synchronicity()
} else {
private$update.synchronicity.data()
}
}
## if the 'custom.event.timestamps.file' parameter has changed, we want to clear them to trigger a re-read.
if (c("custom.event.timestamps.file") %in% params) {
self$clear.custom.event.timestamps()
}
}
},
## * * backups -----------------------------------------------------
#' Backup the current environment to a file on the disk.
#'
#' @param file the path to the backup file
save.to.disk = function(file) {
save(self, file = file)
},
## * * path retrieval ----------------------------------------------
#' Get the absolute path to the project's result folder.
#'
#' @return the path to the result folder
get.data.path = function() {
data.path = private$project.conf$get.value("datapath")
return(data.path)
},
#' Get the absolute path to the result folder for gender data.
#'
#' @return the path to the gender data
get.data.path.gender = function() {
data.path = private$project.conf$get.value("datapath.gender")
return(data.path)
},
#' Get the absolute path to the range's result folder for synchronicity data.
#'
#' @return the path to the synchronicity files
get.data.path.synchronicity = function() {
data.path = private$project.conf$get.value("datapath.synchronicity")
return(data.path)
},
#' Get the absolute path to the result folder for PaStA data.
#'
#' @return the path to the PaStA data
get.data.path.pasta = function() {
data.path = private$project.conf$get.value("datapath.pasta")
return(data.path)
},
#' Get the absolute path to the result folder for issue data.
#'
#' @return the path to the issue data
get.data.path.issues = function() {
data.path = private$project.conf$get.value("datapath.issues")
return(data.path)
},
## * * raw data ----------------------------------------------------
#' Return the commits retrieved by the method \code{get.commits.unfiltered} by removing untracked files and removing the
#' base artifact (if configured in the \code{project.conf}, see parameters \code{commits.filter.untracked.files}
#' and \code{commits.filter.base.artifact}).
#'
#' This method caches the filtered commits to the field \code{commits.filtered}.
#'
#' @return the commits retrieved by the method \code{get.commits.unfiltered} after all filters have been applied
#'
#' @seealso get.commits.uncached
get.commits = function() {
logging::loginfo("Getting commit data.")
if (!self$is.data.source.cached("commits")) {
private$commits = private$filter.commits(
self$get.commits.unfiltered(),
private$project.conf$get.value("commits.filter.untracked.files"),
private$project.conf$get.value("commits.filter.base.artifact"),
private$project.conf$get.value("filter.bots")
)