-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnalysis.R
190 lines (138 loc) · 5.35 KB
/
Analysis.R
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
##############################################################
########## Project ###########
# Database to effeciently store and retrieve timeseries data #
########## Submitted by Sarvani Vadali ###########
##############################################################
rm(list = ls())
library(mongolite)
# Aim of the script:
# Perform analysis on Timeseries data stored in MongoDB database.
# MondoDB is structured using wide table scheme.
# Wide table schema allows to get data points with simple queries
# Following script implements essential functions needed to understand
# stored (simulated temperature) data. There are a total of 1 million
# data points in sample data stored in MongoDB database.
##### Queries to get basic structure of Timeseries data #####
# Get number of distinct Timeseries objects in database
numberOfTimeseries <- function () {
mongoData <- mongo(collection = metadata, db = db)
return (mongoData$count())
}
# Total datapoints in database
totalDataPoints <- function() {
mongoData <- mongo(collection = points, db = db)
df <- mongoData$aggregate('[{"$group":{"_id":"$ID", "count": {"$sum":"$Count"}}}]')
return (sum(df$count))
}
# Gets ID of all Timeseries object stored in database
getIDs <- function() {
mongoData <- mongo(collection = metadata, db = db)
# We want to get IDs only.
fields <- '{"ID": 1, "_id": 0}'
df <- mongoData$find(fields = fields)
return (as.vector(df$ID))
}
# Get number of data points in a given Timeseries object
numberOfDataPoints <- function(ID) {
mongoData <- mongo(collection = points, db = db)
df <- mongoData$aggregate('[{"$group":{"_id":"$ID", "count": {"$sum":"$Count"}}}]')
return (df[ID, 'count'])
}
# Get average of data points for a given Timeseries
averageOfDataPonts <- function(ID) {
mongoData <- mongo(collection = points, db = db)
df <- mongoData$aggregate('[{"$group":{"_id":"$ID", "count": {"$sum":"$Count"}, "total": {"$sum":"$Total"}}}]')
return (df[ID, 'total'] / df[ID, 'count'])
}
##### Functionality to plot Timeseries data #####
# Helper function to get all the data points as a data.frame
getDataPoints <- function(ID) {
mongoData <- mongo(collection = points, db = db)
# We want to get only data points. Restrict data points.
fields <- '{"Count": 0, "Total": 0, "_id": 0, "ID": 0, "Time": 0}'
# Query only for given timeseries.
query <- sprintf('{"ID":%d}', ID)
df <- mongoData$find(query = query, fields = fields)
return (df)
}
# Plots all points in a given Timeseries.
plotATimeseries <- function(ID) {
df <- getDataPoints(ID)
# Now, plot given time series.
plot(as.vector(as.matrix(df)), type = "s")
}
# Helper to get count and totals for given Timeseries data.
getDataPointSummary <- function(ID) {
mongoData <- mongo(collection = points, db = db)
# We want to get only data points. Restrict data points.
fields <- '{"Count": 1, "Total": 1, "_id": 0}'
# Query only for given timeseries.
query <- sprintf('{"ID":%d}', ID)
df <- mongoData$find(query = query, fields = fields)
df$Average = (df$Total/df$Count)
return (df)
}
# Plot minute by minute averages for given Timeseries
plotMinutelyAverages <- function(ID) {
df <- getDataPointSummary(ID)
# Now, plot averages only.
plot(as.vector(df$Average), type="o", col="red")
}
##### Advanced queries related to single Timeseries #####
# Gets Times of given Timeseries data
getStartTimes <- function(ID) {
mongoData <- mongo(collection = points, db = db)
# We want to get only data points. Restrict data points.
fields <- '{"Time": 1, "_id": 0}'
# Query only for given timeseries.
query <- sprintf('{"ID":%d}', ID)
df <- mongoData$find(query = query, fields = fields)
df$Time <- as.POSIXct(df$Time, origin = "1970-01-01")
return (df)
}
# Get duration of Timeseries data
getDurationOfTimeseries <- function(ID) {
df <- getStartTimes(ID)
return (df[nrow(df), ] - df[1, ])
}
# Below function down samples given timeseries
downSampleTimeseries <- function(ID, numOfPoints = 100) {
totalPoints <- numberOfDataPoints(ID)
df <- getDataPoints(ID)
allPoints <- as.vector(as.matrix(df))
if (totalPoints > numOfPoints) {
pointsToPlot <- vector(mode = "numeric", length = numOfPoints)
# We need to down sample it.
k <- totalPoints / numOfPoints # Simply pick every k points
i <- 1
index <- 1
while (i <= numOfPoints) {
pointsToPlot[i] <- allPoints[index]
i <- i + 1
index <- index + k
}
return (pointsToPlot)
}
return (allPoints)
}
# Function to plot down sampled values
plotDownSampledValues <- function(ID) {
vals <- downSampleTimeseries(ID)
plot(vals, type="o", col="blue")
}
###################################
########## MAIN SCRIPT ##########
###################################
# Globals
db <- "Timeseries"
metadata <- "Metadata"
points <- "Points"
ID <- 5
print(sprintf("Total number of Timeseries object in database %d", numberOfTimeseries()))
print(sprintf("Total number of data points in database %d", totalDataPoints()))
print(sprintf("Number of data points in Timeseries (%d) = %d", ID, numberOfDataPoints(ID)))
print(sprintf("Average temperature of Timeseries (%d) = %s", ID, as.character(averageOfDataPonts(ID))))
# Sample plots
plotMinutelyAverages(ID)
plotATimeseries(ID)
plotDownSampledValues(ID)