Skip to content

Commit

Permalink
Merge pull request #7 from jubilee2/report
Browse files Browse the repository at this point in the history
add queryLogSummary
  • Loading branch information
jubilee2 authored Dec 18, 2024
2 parents 0f3bdb8 + 1480ae2 commit 9ef7db4
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 6 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: rcUtils
Type: Package
Title: REDCap Utility Functions
Version: 0.3.0
Version: 0.4.0
Author: Jubilee
Maintainer: Jubilee<[email protected]>
Description: A collection of reusable functions to streamline and enhance the functionality of REDCap projects. This package includes functions for parsing logging strings, converting data types, and more.
Expand Down
4 changes: 4 additions & 0 deletions R/logParse.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ logDetailsParseDQ <- function(details) {
pairs <- strsplit(info, pattern, perl = TRUE)

lapply(pairs, function(x) {
if (length(x) == 1 && x == "") {
return(list())
}

kv <- strsplit(x, ': (?=(?:[^"]|"[^"]*")*$)', perl = TRUE)
kv <- lapply(kv, function(x) gsub('^"|"$', "", x))

Expand Down
13 changes: 13 additions & 0 deletions R/logReport.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
queryLogSummary <- function(logs) {
# Function implementation
o <- logDetailsParseDQ(logs$details)
logs$Action <- sapply(o, function(x) if (is.null(x$Action)) NA else x$Action)
logs$Record <- sapply(o, function(x) if (is.null(x$Record)) NA else x$Record)
logs$Event <- sapply(o, function(x) if (is.null(x$Event)) NA else x$Event)
logs$Field <- sapply(o, function(x) if (is.null(x$Field)) NA else x$Field)
logs$Comment <- sapply(o, function(x) if (is.null(x$Comment)) NA else x$Comment)
logs$`Data Quality Rule` <- sapply(o, function(x) if (is.null(x$`Data Quality Rule`)) NA else x$`Data Quality Rule`)

selector <- sapply(o, length) > 0
logs[selector,c("timestamp", "username", "Action","Record","Event","Field","Data Quality Rule","Comment")]
}
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# rcUtils

## Overview
rcUtils is an R library providing utility functions for parsing and analyzing log data. It includes:


rcUtils is an R library providing utility functions for parsing and analyzing log data.
## Functions
### logDetailsParseDQ
Parses data query log details into a structured format.
Expand All @@ -17,23 +15,33 @@ Extracts instance numbers from log details.
### logFilter
Filters log actions based on specified types.


### queryLogSummary
Summarizes query logs by extracting relevant information.
## Installation
```r
# Install from GitHub
devtools::install_github("jubilee2/rcUtils")
```
## Usage
```R
library(rcUtils)

# Example log data
log <- data.frame(
timestamp = Sys.time(),
username = "user123",
details = c(
"Open data query",
"Send data query back for further attention",
"Open data query (Record: 1, Event: \"Event 1\")",
"[instance = 123] name = 'Jane', age = '25', hobbies(2) = unchecked"
)
)

# Summarize query logs
summary <- queryLogSummary(log)
print(summary)

# Parse log details
log$detailObj <- logDetailsParseRecord(log$details)
log$detailObj <- logDetailsParseDQ(log$details)
Expand All @@ -52,7 +60,7 @@ log[selected,]
* logDetailsParseRecord: Extracts key-value pairs from log details.
* logDetailsParseRecordInstance: Extracts instance numbers from log details.
* logFilter: Filters log actions based on specified types.

* queryLogSummary: Summarizes query logs.
## Documentation
For detailed documentation, visit our [Wiki](https://github.com/jubilee2/rcUtils/wiki).

Expand Down
30 changes: 30 additions & 0 deletions man/queryLogSummary.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
\name{queryLogSummary}
\alias{queryLogSummary}
\title{Summarize Data Query Logs}
\description{
Summarize query logs by extracting relevant information from log details.
}
\usage{
queryLogSummary(logs)
}
\arguments{
\item{logs}{Data frame containing query logs with a 'timestamp', 'username', 'details' column.}
}
\value{
Data frame with summarized query log information.
}
\examples{
logs <- data.frame(
timestamp = Sys.time(),
username = "user123",
details = c("Open data query", "Send data query back for further attention")
)
summary <- queryLogSummary(logs)
print(summary)
}
\seealso{
\code{logDetailsParseDQ()}
}
\author{
Jubilee
}
24 changes: 24 additions & 0 deletions tests/testthat/test-logReport.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

test_that("queryLogSummary returns data frame", {
logs <- data.frame(timestamp = Sys.time(), username = "user123", details = "Open data query")
result <- queryLogSummary(logs)
expect_s3_class(result, "data.frame")
})

test_that("queryLogSummary has correct columns", {
logs <- data.frame(timestamp = Sys.time(), username = "user123", details = "Open data query")
result <- queryLogSummary(logs)
expect_setequal(names(result), c("timestamp", "username", "Action", "Record", "Event", "Field", "Data Quality Rule", "Comment"))
})

test_that("queryLogSummary handles empty logs", {
logs <- data.frame(timestamp = Sys.time(), username = 'foo', details = '')
result <- queryLogSummary(logs)
expect_equal(nrow(result), 0)
})

test_that("queryLogSummary handles NA details", {
logs <- data.frame(timestamp = Sys.time(), username = "user123", details = NA)
result <- queryLogSummary(logs)
expect_equal(nrow(result), 0)
})

0 comments on commit 9ef7db4

Please sign in to comment.