feat: возможность отражения информации действий по базам данных
This commit is contained in:
108
app/logs.R
108
app/logs.R
@@ -17,5 +17,113 @@ server <- function(id, values, scheme, mhcs) {
|
||||
|
||||
moduleServer(id, function(input, output, session) {
|
||||
|
||||
# отображение DT-таблицы со списком последних действий
|
||||
observeEvent(input$show_last_actions, {
|
||||
|
||||
con <- db$make_db_connection(scheme(),"show_last_actions")
|
||||
on.exit(db$close_db_connection(con, "show_last_actions"), add = TRUE)
|
||||
|
||||
log_df <- DBI::dbReadTable(con, "log")
|
||||
|
||||
# новые записи вначале, формат даты с временем
|
||||
log_df <- log_df |>
|
||||
dplyr::arrange(dplyr::desc(date)) |>
|
||||
dplyr::mutate(date = format(as.POSIXct(date), "%d.%m.%Y %H:%M"))
|
||||
|
||||
output$dt_logs <- DT::renderDataTable(
|
||||
DT::datatable(
|
||||
log_df,
|
||||
# caption = 'Table 1: This is a simple caption for the table.',
|
||||
rownames = FALSE,
|
||||
colnames = logs_colnames,
|
||||
extensions = c('KeyTable', "FixedColumns"),
|
||||
# editable = 'cell',
|
||||
class = 'cell-border stripe',
|
||||
selection = "single",
|
||||
options = list(
|
||||
# dom = 'tipf',
|
||||
scrollX = TRUE,
|
||||
fixedColumns = list(leftColumns = 1),
|
||||
keys = TRUE,
|
||||
autoWidth = TRUE,
|
||||
columnDefs = list(
|
||||
list(width = "150px", targets = c(0,6)),
|
||||
list(width = "110px", targets = c(1:4))
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
showModal(modalDialog(
|
||||
DT::dataTableOutput(ns("dt_logs")),
|
||||
size = "xl",
|
||||
# footer = tagList(
|
||||
# actionButton("nested_form_dt_save", "сохранить изменения")
|
||||
# ),
|
||||
easyClose = TRUE
|
||||
))
|
||||
|
||||
})
|
||||
|
||||
# observe({
|
||||
# print(input$dt_logs_rows_selected)
|
||||
# })
|
||||
|
||||
output$display_log <- renderUI({
|
||||
req(values$main_key)
|
||||
|
||||
# получение логов
|
||||
con <- db$make_db_connection(scheme(),"display_log")
|
||||
on.exit(db$close_db_connection(con, "display_log"), add = TRUE)
|
||||
|
||||
query <- sprintf("SELECT * FROM \"log\" WHERE key = '%s'", values$main_key)
|
||||
log_df_for_id <- DBI::dbGetQuery(con, query)
|
||||
|
||||
if (nrow(log_df_for_id) > 0) {
|
||||
|
||||
lines <- log_df_for_id |>
|
||||
dplyr::mutate(
|
||||
date = as.POSIXct(date),
|
||||
date = date + lubridate::hours(3), # fix datetime
|
||||
date_day = as.Date(date)
|
||||
) |>
|
||||
dplyr::mutate(cons_actions = dplyr::consecutive_id(action, user)) |>
|
||||
dplyr::mutate(n_actions = dplyr::row_number(), .by = c(cons_actions, user, action, date_day)) |>
|
||||
dplyr::slice(which.max(n_actions), .by = c(user, action, date_day)) |>
|
||||
dplyr::arrange(date) |>
|
||||
dplyr::mutate(string_to_print = sprintf(
|
||||
"<b>[%s %s]</b> %s: %s (%s)",
|
||||
format(date, "%d.%m.%y"),
|
||||
format(date, "%H:%M"),
|
||||
user,
|
||||
action,
|
||||
n_actions
|
||||
)) |>
|
||||
dplyr::pull(string_to_print) |>
|
||||
paste(collapse = "</br>")
|
||||
|
||||
} else {
|
||||
lines <- ""
|
||||
}
|
||||
|
||||
div(
|
||||
strong("Последние действия:"),
|
||||
br(),
|
||||
HTML(lines),
|
||||
style = "font-size:10px;"
|
||||
)
|
||||
|
||||
})
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
logs_colnames <- c(
|
||||
"время" = "date",
|
||||
"пользователь" = "user",
|
||||
"приложение" = "app_id",
|
||||
"версия" = "app_ver",
|
||||
"ip" = "remote_addr",
|
||||
"ID записи" = "key",
|
||||
"действие" = "action"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user