app_utils.R 9.74 KB
Newer Older
1
2
# DQAgui - A graphical user interface (GUI) to the functions implemented in the
# R package 'DQAstats'.
Jonathan Mang's avatar
Jonathan Mang committed
3
# Copyright (C) 2019-2020 Universitätsklinikum Erlangen
Lorenz Kapsner's avatar
Lorenz Kapsner committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#
# This program 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, either version 3 of the License, or
# (at your option) any later version.
#
# 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, see <http://www.gnu.org/licenses/>.

kapsner's avatar
kapsner committed
18
19

# create summary tables
20
21
22
23
24
25
26
27
28
summary_table <- function() {
  return(
    data.table::data.table(
      "variable" = character(),
      "distinct" = integer(),
      "valids" = integer(),
      "missings" = integer()
    )
  )
kapsner's avatar
kapsner committed
29
30
31
}

# render quick check tables
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
render_quick_checks <- function(dat_table) {
  out <-
    DT::datatable(
      dat_table,
      options = list(
        dom = "t",
        scrollY = "30vh",
        pageLength = nrow(dat_table)
      ),
      rownames = F
    ) %>%
    DT::formatStyle(columns = 2,
                    backgroundColor = DT::styleEqual(
                      c("passed", "failed"),
                      c("lightgreen", "red")
                    )) %>%
    DT::formatStyle(columns = 3,
                    backgroundColor = DT::styleEqual(
                      c("passed", "failed"),
                      c("lightgreen", "red")
                    )) %>%
    DT::formatStyle(columns = 4,
                    backgroundColor = DT::styleEqual(
                      c("passed", "failed"),
                      c("lightgreen", "red")
                    ))
kapsner's avatar
kapsner committed
58
59
60
  return(out)
}

61
62
#' @title get_db_settings
#'
63
#' @param input Shiny server input object
64
65
66
67
#' @param target A boolean (default: TRUE).
#'
#' @export
#'
68
get_db_settings <- function(input, target = T) {
kapsner's avatar
kapsner committed
69
70
71
72
  # create description of column selections
  vec <- c("dbname", "host", "port", "user", "password")

  tab <- lapply(vec, function(g) {
73
74
75
76
77
78
79
80
81
    if (target) {
      data.table::data.table("keys" = g, "value" = eval(parse(
        text = paste0("input[['moduleConfig-config_targetdb_", g, "']]")
      )))
    } else {
      data.table::data.table("keys" = g, "value" = eval(parse(
        text = paste0("input[['moduleConfig-config_sourcedb_", g, "']]")
      )))
    }
kapsner's avatar
kapsner committed
82
83
84
85
86
  })

  tab <- do.call(rbind, tab)

  # if one column is selected multiple times
87
88
89
90
91
92
93
94
  if ("" %in% tab[, get("value")] ||
      any(tab[, grepl("\\s", get("value"))])) {
    shiny::showModal(
      modalDialog(
        title = "Invalid values",
        "No empty strings or spaces allowed in database configurations."
      )
    )
kapsner's avatar
kapsner committed
95
96
97
    return(NULL)

  } else {
98
99
    outlist <- lapply(stats::setNames(vec, vec), function(g) {
      tab[get("keys") == g, get("value")]
kapsner's avatar
kapsner committed
100
101
102
103
    })
    return(outlist)
  }
}
104

105
106
#' @title This function is used in the config-tab and displays the selected
#'   system to the user.
107
108
#' @param system (String) e.g. "i2b2", "OMOP" or "CSV"
#' @param type (String) "source" or "target"
109
#' @return String containing the input params in a propper manner
110
111
#'
#'
112
113
114
115
116
feedback_txt <- function(system, type) {
  result <- paste0(
    "\U2714 ",
    system,
    " will be used as ",
Jonathan Mang's avatar
Jonathan Mang committed
117
    DQAstats::firstup(type),
118
119
120
121
122
123
124
125
    " system.",
    "\n\n",
    "To change, simply select and save another one."
  )
  return(result)
}


126
127
128
#' @title This function is called when the user clicks on the button
#' @description "Set target == source". It sets target settings = source
#'   settings.
129
130
#'
#' @inheritParams module_dashboard_server
131
132
#'
#'
133
134
135
136
137
138
set_target_equal_to_source <- function(rv) {
  rv$target$settings <- rv$source$settings
  rv$target$system_type <- rv$source$system_type
  rv$target$system_name <- rv$source$system_name
  return(rv)
}
139

140
141
#' @title This function checks if all necessary input parameters
#'   for source and target exist and are valid.
142
143
#'
#' @inheritParams module_dashboard_server
144
145
#'
#'
146
147
148
149
150
151
152
153
154
155
validate_inputs <- function(rv) {
  error_tmp <- F
  if (!is.null(rv$source$system_type) &&
      !is.null(rv$target$system_type)) {
    # Check source setting:
    if (rv$source$system_type == "csv") {
      # Check if source-path is valid:
      if (typeof(rv$source$settings$dir) == "character" &&
          !is.null(rv$source$settings$dir) &&
          length(rv$source$settings$dir) > 0) {
156
        DQAstats::feedback("Source settings seem valid.",
157
158
159
160
161
162
163
164
165
                 findme = "c0bcc9aa31")
        # valid path, so check if files exist:
        test_source_csv <- DQAstats::test_csv(
          settings = rv$source$settings,
          source_db = rv$source$system_name,
          mdr = rv$mdr,
          headless = F
        )
        if (isTRUE(test_source_csv)) {
166
          DQAstats::feedback("All source csv-files were found.",
167
168
                   findme = "794c6f3160")
        } else{
169
          DQAstats::feedback("Some source csv-files are MISSING.",
170
171
172
173
174
175
                   type = "Error",
                   findme = "926b0c567c")
          error_tmp <- T
        }
      } else {
        # invalid path:
176
        DQAstats::feedback(
177
178
179
180
181
          print_this = "Source settings not valid.",
          type = "warning",
          findme = "10d5e79d44",
          ui = T
        )
182
        DQAstats::feedback(
183
          print_this = paste0(
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
            "rv$source$settings$dir = ",
            rv$source$settings$dir,
            "(d9b43110bb)"
          )
        )
        error_tmp <- T
      }
    } else if (rv$source$system_type == "postgres") {
      # Check if source-db settings are valid:
      if (!is.null(rv$source$settings)) {
        rv$source$db_con <-
          DQAstats::test_db(settings = rv$source$settings,
                            headless = rv$headless)
        if (!is.null(rv$source$db_con)) {
          # valid
199
          DQAstats::feedback(print_this = "Source db-settings seem valid.",
200
                   findme = "29cc920472")
201
202
        } else {
          # invalid:
203
          DQAstats::feedback(
204
205
206
207
208
            print_this = "Source db-settings not valid.",
            type = "Warning",
            findme = "c63e1ccaf0",
            ui = T
          )
209
          DQAstats::feedback(
210
211
212
213
            print_this = paste0("rv$source$settings = ",
                                rv$source$settings),
            findme = "2d47f163a9"
          )
214
215
216
217
          error_tmp <- T
        }
      } else {
        # invalid 2:
218
        DQAstats::feedback(
219
220
221
222
223
224
225
226
          print_this = "Source db-settings are empty.",
          type = "Warning",
          findme = "127deaebca",
          ui = T
        )
        error_tmp <- T
      }
    } else {
227
      DQAstats::feedback(
228
229
230
231
232
233
234
235
236
237
238
239
240
241
        print_this = "Source system not yet implemented.",
        type = "Warning",
        findme = "d0f0bfa2f3",
        ui = T
      )
      error_tmp <- T
    }

    # Check target setting:
    if (rv$target$system_type == "csv") {
      # Check if target-path is valid:
      if (typeof(rv$target$settings$dir) == "character" &&
          !is.null(rv$target$settings$dir) &&
          length(rv$target$settings$dir) > 0) {
242
        DQAstats::feedback("target settings seem valid.",
243
244
245
246
247
248
249
250
251
                 findme = "9979bb57ef")
        # valid path, so check if files exist:
        test_target_csv <- DQAstats::test_csv(
          settings = rv$target$settings,
          source_db = rv$target$system_name,
          mdr = rv$mdr,
          headless = F
        )
        if (isTRUE(test_target_csv)) {
252
          DQAstats::feedback("All target csv-files were found.",
253
254
                   findme = "ff8203c831")
        } else{
255
          DQAstats::feedback("Some target csv-files are MISSING.",
256
257
258
259
260
261
                   type = "Error",
                   findme = "079525a7de")
          error_tmp <- T
        }
      } else {
        # invalid path:
262
        DQAstats::feedback(
263
264
265
266
267
          print_this = "Target settings not valid.",
          type = "Warning",
          findme = "f4cc32e068",
          ui = T
        )
268
        DQAstats::feedback(
269
270
271
272
          print_this = paste0("rv$target$settings$dir = ",
                              rv$target$dir),
          findme = "(43c81cb723)"
        )
273
274
275
276
277
278
279
280
281
282
        error_tmp <- T
      }
    } else if (rv$target$system_type == "postgres") {
      # Check if target-db settings are valid:
      if (!is.null(rv$target$settings)) {
        rv$target$db_con <-
          DQAstats::test_db(settings = rv$target$settings,
                            headless = rv$headless)
        if (!is.null(rv$target$db_con)) {
          # valid
283
          DQAstats::feedback("Target db-settings seem valid. (79234d2ba0)")
284
285
        } else {
          # invalid:
286
          DQAstats::feedback(
287
288
289
290
291
            print_this = "Target db-settings not valid.",
            type = "Warning",
            findme = "096341c4c1",
            ui = T
          )
292
          DQAstats::feedback(paste0(
293
294
295
296
297
298
299
300
            "rv$target$settings = ",
            rv$target$settings,
            "(2d47f163a9)"
          ))
          error_tmp <- T
        }
      } else {
        # invalid 2:
301
        DQAstats::feedback(
302
303
304
305
306
307
308
309
          print_this = "Target db-settings are empty.",
          type = "Warning",
          findme = "8440a9e683",
          ui = T
        )
        error_tmp <- T
      }
    } else {
310
      DQAstats::feedback(
311
312
313
314
315
316
317
318
        print_this = "Target system not yet implemented.",
        type = "Warning",
        findme = "57b314a1a3",
        ui = T
      )
      error_tmp <- T
    }
  } else {
319
    DQAstats::feedback(
320
321
322
323
324
325
326
      print_this = "Either source or target system is not set.",
      type = "Warning",
      findme = "4e9400f8c9",
      ui = T
    )
    error_tmp <- T
  }
327
  return(!error_tmp)
328
}
Jonathan Mang's avatar
Jonathan Mang committed
329

330
331
332
333
334
335
fix_sql_display <- function(text) {
  t <- text
  t <- gsub("\\\n", "<br>\n", t)
  t <- gsub("\\\t", "&nbsp;&nbsp;&nbsp;&nbsp;", t)
  return(t)
}