File: usage.R

package info (click to toggle)
r-cran-rsconnect 1.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,044 kB
  • sloc: python: 185; sh: 13; makefile: 5
file content (137 lines) | stat: -rw-r--r-- 5,206 bytes parent folder | download
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
#' Show Application Usage
#'
#' Show application usage of a currently deployed application
#' @param appName Name of application
#' @param appDir Directory containing application. Defaults to
#'   current working directory.
#' @inheritParams deployApp
#' @param usageType Use metric to retreive (for example: "hours")
#' @param from Date range starting timestamp (Unix timestamp or relative time
#'   delta such as "2d" or "3w").
#' @param until Date range ending timestamp (Unix timestamp or relative time
#'   delta such as "2d" or "3w").
#' @param interval Summarization interval. Data points at intervals less then this
#'   will be grouped. (Relative time delta e.g. "120s" or "1h" or "30d").
#' @note This function only works for ShinyApps servers.
#' @export
showUsage <- function(appDir = getwd(), appName = NULL, account = NULL, server = NULL,
                      usageType = "hours", from = NULL, until = NULL, interval = NULL) {

  accountDetails <- accountInfo(account, server)
  checkShinyappsServer(accountDetails$server)

  api <- clientForAccount(accountDetails)

  # resolve application
  if (is.null(appName))
    appName <- basename(appDir)
  application <- resolveApplication(accountDetails, appName)

  # get application usage
  data <- api$getAccountUsage(accountDetails$accountId,
                              usageType,
                              application$id,
                              from,
                              until,
                              interval)

  if (length(data$points) < 1) {
    stop("No data.", call. = FALSE)
  }

  # get data points
  points <- data$points[[1]]
  points <- lapply(points, function(X) {
    X[[1]] <- X[[1]] / 1000 # convert from milliseconds to seconds
    X
  })

  # convert to data frame
  df <- data.frame(matrix(unlist(points), nrow = length(points), byrow = TRUE), stringsAsFactors = FALSE)
  colnames(df) <- c("timestamp", usageType)
  return(df)
}

#' Show Application Metrics
#'
#' Show application metrics of a currently deployed application.
#' This function only works for ShinyApps servers.
#'
#' @param metricSeries Metric series to query. Refer to the
#'   [shinyapps.io documentation](<https://docs.posit.co/shinyapps.io/metrics.html#ApplicationMetrics>)
#'   for available series.
#' @param metricNames Metric names in the series to query. Refer to the
#'   [shinyapps.io documentation](<https://docs.posit.co/shinyapps.io/metrics.html#ApplicationMetrics>)
#'   for available metrics.
#' @inheritParams deployApp
#' @param from Date range starting timestamp (Unix timestamp or relative time
#'   delta such as "2d" or "3w").
#' @param until Date range ending timestamp (Unix timestamp or relative time
#'   delta such as "2d" or "3w").
#' @param interval Summarization interval. Data points at intervals less then this
#'   will be grouped. (Relative time delta e.g. "120s" or "1h" or "30d").
#' @export
showMetrics <- function(metricSeries,
                        metricNames,
                        appDir = getwd(),
                        appName = NULL,
                        account = NULL,
                        server = "shinyapps.io",
                        from = NULL,
                        until = NULL,
                        interval = NULL) {

  accountDetails <- accountInfo(account, server)
  api <- clientForAccount(accountDetails)

  # resolve application
  if (is.null(appName))
    appName <- basename(appDir)
  application <- resolveApplication(accountDetails, appName)

  # get application usage
  data <- api$getApplicationMetrics(application$id,
                                    metricSeries,
                                    metricNames,
                                    from,
                                    until,
                                    interval)

  if (length(data$points) < 1) {
    stop("No data.", call. = FALSE)
  }

  points <- lapply(data$points, as.data.frame, stringsAsFactors = FALSE)
  points <- do.call(rbind, points)
  points$time <- .POSIXct(points$time / 1000)
  points
}

#' Show Account Usage
#'
#' Show account usage
#' @inheritParams deployApp
#' @param usageType Use metric to retreive (for example: "hours")
#' @param from Date range starting timestamp (Unix timestamp or relative time
#'   delta such as "2d" or "3w").
#' @param until Date range ending timestamp (Unix timestamp or relative time
#'   delta such as "2d" or "3w").
#' @param interval Summarization interval. Data points at intervals less then this
#'   will be grouped. (Number of seconds or relative time delta e.g. "1h").
#' @note This function only works for ShinyApps servers.
#' @export
accountUsage <- function(account = NULL, server = NULL, usageType = "hours",
                         from = NULL, until = NULL, interval = NULL) {
  accountDetails <- accountInfo(account, server)
  checkShinyappsServer(accountDetails$server)

  api <- clientForAccount(accountDetails)

  # get application usage
  data <- api$getAccountUsage(accountDetails$accountId,
                              usageType,
                              NULL,
                              from,
                              until,
                              interval)
}