-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobal.R
More file actions
72 lines (54 loc) · 1.71 KB
/
Global.R
File metadata and controls
72 lines (54 loc) · 1.71 KB
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
library(shiny)
library(shinySignals)
library(dplyr)
library(shinydashboard)
library(bubbles)
source("bloomfilter.R")
prototype <- data.frame(date = character(), time = character(),
amount = numeric(), r_version = character(),
r_os = character(), mercahnt_name = character(), product = character(),
country = character(), ip_id = character(), received = numeric())
packageStream <- function(session) {
sock <- socketConnection("cransim.rstudio.com", 6789, blocking = FALSE, open = "r")
session$onSessionEnded(function() {
close(sock)
})
newLines <- reactive({
invalidateLater(1000, session)
readLines(sock)
})
reactive({
if (length(newLines()) == 0)
return()
read.csv(textConnection(newLines()), header=FALSE, stringsAsFactors=FALSE,
col.names = names(prototype)
) %>% mutate(received = as.numeric(Sys.time()))
})
}
packageData <- function(pkgStream, timeWindow) {
shinySignals::reducePast(pkgStream, function(memo, value) {
rbind(memo, value) %>%
filter(received > as.numeric(Sys.time()) - timeWindow)
}, prototype)
}
downloadCount <- function(pkgStream) {
shinySignals::reducePast(pkgStream, function(memo, df) {
if (is.null(df))
return(memo)
memo + nrow(df)
}, 0)
}
userCount <- function(pkgStream) {
bloomFilter <- BloomFilter$new(5000, 0.01)
total <- 0
reactive({
df <- pkgStream()
if (!is.null(df) && nrow(df) > 0) {
ids <- paste(df$date, df$ip_id) %>% unique()
newIds <- !sapply(ids, bloomFilter$has)
total <<- total + length(newIds)
sapply(ids[newIds], bloomFilter$set)
}
total
})
}