Skip to content

Commit fdd3d6a

Browse files
committed
Extract fillTimeVector()
1 parent 7755619 commit fdd3d6a

File tree

2 files changed

+64
-57
lines changed

2 files changed

+64
-57
lines changed

R/setGlobalInspectionID.R

+46-57
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ setGlobalInspectionID <- function(
3333
}
3434

3535
# Just a shortcut
36-
removeEmpty <- function(df) kwb.utils::removeEmptyColumns(df, dbg = FALSE)
37-
36+
removeEmpty <- function(x) kwb.utils::removeEmptyColumns(x, dbg = FALSE)
37+
3838
inspections <- removeEmpty(get_elements(inspection.data, "inspections"))
3939
observations <- removeEmpty(get_elements(inspection.data, "observations"))
4040

@@ -48,87 +48,76 @@ setGlobalInspectionID <- function(
4848
snake = "inspection_time"
4949
))
5050

51-
inspections <- kwb.utils::hsAddMissingCols(inspections, timeColumn)
52-
53-
hasNoTime <- kwb.utils::isNaOrEmpty(inspections[[timeColumn]])
54-
55-
if (any(hasNoTime)) {
56-
57-
n_missing <- sum(hasNoTime)
58-
59-
message(sprintf(
60-
"Setting %d missing inspection times to '%s' (plus random seconds).",
61-
n_missing,
62-
default.time
63-
))
64-
65-
# We have to fix the random number generator otherwise the times are not
66-
# reproducible!
67-
set.seed(123L)
68-
69-
# Generate a random number for the seconds
70-
inspections[[timeColumn]][hasNoTime] <- sprintf(
71-
"%s:%02d",
72-
default.time,
73-
sample(0:59, size = n_missing, replace = TRUE)
74-
)
75-
}
76-
77-
# Columns from which to generate the hash code
78-
columns <- get_elements(elements = name.convention, list(
79-
norm = c(
80-
"project",
81-
"ABF",
82-
"ABG",
83-
"AAD",
84-
"AAF"
85-
),
86-
camel = c(
87-
"project",
88-
"InspDate",
89-
"InspTime",
90-
"Node1Ref",
91-
"Node2Ref"
92-
),
93-
snake = c(
94-
"project",
95-
"inspection_date",
96-
"inspection_time",
97-
"node_1_ref",
98-
"node_2_ref"
99-
)
100-
))
51+
inspections[[timeColumn]] <- fillTimeVector(
52+
x = if (timeColumn %in% names(inspections)) {
53+
inspections[[timeColumn]]
54+
} else {
55+
character(nrow(inspections))
56+
},
57+
hhmm = default.time,
58+
seed = 123L
59+
)
10160

10261
# Create the inspection IDs and store them in column "inspection_id"
10362
hashes <- createHashFromColumns(
10463
data = inspections,
105-
columns = columns,
64+
# Columns from which to generate the hash code
65+
columns = get_elements(elements = name.convention, list(
66+
norm = c("project", "ABF", "ABG", "AAD", "AAF"),
67+
camel = c("project", "InspDate", "InspTime", "Node1Ref", "Node2Ref"),
68+
snake = c("project", "inspection_date", "inspection_time", "node_1_ref", "node_2_ref")
69+
)),
10670
silent = TRUE
10771
)
10872

10973
# Check for duplicates in the hashes
11074
stop_on_hash_duplicates(hashes, error.file = error.file)
111-
75+
11276
inspections[["inspection_id"]] <- hashes
113-
77+
11478
i <- kwb.utils::selectColumns(observations, "inspno")
11579

11680
observations[["inspection_id"]] <- kwb.utils::selectColumns(
11781
inspections, "inspection_id"
11882
)[i]
11983

12084
observations <- kwb.utils::removeColumns(observations, "inspno")
121-
85+
12286
# Just a shortcut
12387
id_first <- function(x) kwb.utils::moveColumnsToFront(x, "inspection_id")
124-
88+
12589
list(
12690
header.info = get_elements(inspection.data, "header.info"),
12791
inspections = id_first(inspections),
12892
observations = id_first(observations)
12993
)
13094
}
13195

96+
# fillTimeVector ---------------------------------------------------------------
97+
fillTimeVector <- function(x, hhmm = "22:22", seed = NULL, silent = FALSE)
98+
{
99+
if (any(isEmpty <- kwb.utils::isNaOrEmpty(x))) {
100+
101+
if (!silent) {
102+
message(sprintf(
103+
"Setting %d missing inspection times to '%s' (plus random seconds).",
104+
sum(isEmpty), hhmm
105+
))
106+
}
107+
108+
# Fix the random number generator to be reproducible
109+
if (!is.null(seed)) {
110+
set.seed(seed)
111+
}
112+
113+
# Generate a random number for the seconds.
114+
seconds <- sample(0:59, size = sum(isEmpty), replace = TRUE)
115+
x[isEmpty] <- sprintf("%s:%02d", hhmm, seconds)
116+
}
117+
118+
x
119+
}
120+
132121
# stop_on_hash_duplicates ------------------------------------------------------
133122

134123
#'@importFrom utils capture.output

tests/testthat/test-function-setGlobalInspectionID.R

+18
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,22 @@ test_that("setGlobalInspectionID() works", {
2727
))
2828

2929
expect_true(file.exists(error.file))
30+
expect_true(length(readLines(error.file)) > 0L)
31+
32+
expect_message(regexp = "Setting .* inspection times", f(
33+
inspection.data = list(
34+
inspections = data.frame(
35+
inspection_date = c("2024-01-03", "2024-01-03"),
36+
node_1_ref = c("A", "B"),
37+
node_2_ref = c("B", "A")
38+
),
39+
observations = data.frame(
40+
inspno = 1L
41+
),
42+
header.info = list()
43+
),
44+
project = "Lausanne",
45+
name.convention = "snake"
46+
))
47+
3048
})

0 commit comments

Comments
 (0)