-
Notifications
You must be signed in to change notification settings - Fork 89
/
CreatePADS.R
281 lines (231 loc) · 8.46 KB
/
CreatePADS.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# Read a CSV file
# Classify the column types
# Determine measures and dimensions
# Generate meta data and store JSON
# Query Solr and store the information in SOLR
# Save the data as cache
# Author: Jitender Aswani, Co-Founder @datadolph.in
# Date: 3/15/2013
# Copyright (c) 2011, under the Creative Commons Attribution-NonCommercial 3.0 Unported (CC BY-NC 3.0) License
# For more information see: https://creativecommons.org/licenses/by-nc/3.0/
# All rights reserved.
require("RJSONIO")
require("plyr")
require("data.table")
require("stringr")
require("lubridate")
require("zoo")
source("UtilPADS.R")
source("ClassifyData.R")
source("MySQLFunctions.R")
source("MongoFns.R")
source("DefaultChartForPAD.R")
# initializeSystem <- function(stack=0){
#
# # turn off scientific formatting - getOption("scipen")
# options(scipen=999)
#
# #initialize solr docs
# #assign("solr.index.docs", list(), envir=.GlobalEnv)
#
# #mongo
# mongo <- getDefaultMongoDBCon()
# assign("mongo", mongo, envir=.GlobalEnv)
#
# #initialize stack
# mysql.db <- list(user="ddfin_dev", pass="BfNdW87Ym9FmcYj7", name="ddfin_dev", host="localhost")
# if(stack == 1) {
# #prod
# mysql.db <- list(user="ddfin_prod", pass="8MQ5CRDzQufHKXTx", name="ddfin_prod", host="localhost")
# }
# assign("mysql.db", mysql.db, envir=.GlobalEnv)
#
# #mysql
# mysql <- getMSDBCon()
# assign("mysql", mysql, envir=.GlobalEnv)
#
# #error counter
# assign("error.count", 0, envir=.GlobalEnv)
#
# #initialize log file
# assign("vec.log", vector(), envir=.GlobalEnv)
#
# #verbose mode
# assign("verbose", F, envir=.GlobalEnv)
#
# #min log
# assign("log.all", F, envir=.GlobalEnv)
# }
#
# loadStates
#
loadStates <- function(){
states <- data.table(readFile("./pads/raw-data/states_names.csv"))
setkey(states, state_code)
assign("states", states, envir=.GlobalEnv)
}
#
# Initialize Padification Process
#
initializeSystem <- function(stack=0){
# turn off scientific formatting - getOption("scipen")
options(scipen=999)
# day and months facors
lMonths <- c("January","February","March", "April","May","June","July","August","September", "October","November","December")
lDays <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
assign("lMonths", lMonths, envir=.GlobalEnv)
assign("lDays", lDays, envir=.GlobalEnv)
#loadStates
loadStates()
#initialize solr docs
#assign("solr.index.docs", list(), envir=.GlobalEnv)
#mongo
mongo <- getDefaultMongoDBCon()
assign("mongo", mongo, envir=.GlobalEnv)
#initialize stack
mysql.db <- list(user="ddfin_dev", pass="b31nd1$$", name="ddfin_dev", host="localhost")
if(stack == 1) {
#prod
mysql.db <- list(user="ddfin_prod", pass="g0f0r1t", name="ddfin_prod", host="localhost")
}
assign("mysql.db", mysql.db, envir=.GlobalEnv)
#mysql
mysql <- getMSDBCon()
assign("mysql", mysql, envir=.GlobalEnv)
#error counter
assign("error.count", 0, envir=.GlobalEnv)
#initialize log file
assign("vec.log", vector(), envir=.GlobalEnv)
#verbose mode
assign("verbose", F, envir=.GlobalEnv)
#min log
assign("log.all", F, envir=.GlobalEnv)
}
#
#cleanup
#
cleaupSystem <- function() {
#disconnect Mongo
disconnectMongoDB(mongo)
# disconnect msdb
disconnectMSDB(mysql)
#save the logs
dumpLogs()
# save the solr index to FS for later editing - this is now done through mysql
#persistSolrIndex(solr.index.docs, dataset)
}
#
# create pad - the padification process starts here
#
createPAD <- function(pad, title, desc, category, subcategory, data.source, source.file,
tags, pagetag) {
if(ncol(pad) == 0) stop('dolphy can not padify a pad that has zero columns')
#only continue if there is one or more colum
if(verbose) print(paste("Starting the padification process for source file", source.file, sep=":"))
logMessage(paste("Starting the padification process for source file", source.file, sep=":"))
#Very first task, classify pad
pad.classified <- classifyData(pad)
#if(verbose) print(pad.classified)
# Generate extra date columns
#if(length(which(col.classes == "Date")) > 0) {
# pad.classified <- generateExtraDateCols(pad.classified, which(col.classes == "Date"))
# col.classes <- laply(pad.classified, getClass)
#}
meta.data <- list(id="", "title"=title, "desc"=desc,
"records"=as.integer(0),
"columns"=as.integer(0),
"category"=category,
"subcategory"=subcategory,
"src"=data.source,
"src_file"= source.file,
"cache_location"="",
"dList"=list(), "mList"=list(),
"tags"=tags,
"pagetag"=pagetag
)
#Start filling up the meta data
meta.data$records <- nrow(pad.classified)
meta.data$columns <- ncol(pad.classified)
#Replace all meta chars from column names including spaces
colnames(pad.classified) <- replaceMetaChars(colnames(pad.classified))
col.classes <- laply(pad.classified, getClass)
col.names <- colnames(pad.classified)
#Create a dict (name=value pair)
cols <- list()
cols[col.names]=col.classes
meta.data$dList <- cols[which(unlist(cols)!="numeric")]
meta.data$mList <- cols[which(unlist(cols)=="numeric")]
#meta.data$mList <- names(which(sapply(pad.classified, is.numeric)))
#meta.data$dList <- setdiff(colnames(pad.classified), meta.data$mList)
# Get unique id for pad
padID <- getPadUID()
#print(paste("Assing pad id of ", padID, sep="::"))
logMessage(paste("Assigning pad id of ", padID, sep="::"))
meta.data$id <- padID
#meta.data$meta_location <- getMetaDataFileURL(padID)
meta.data$cache_location <- getCacheDataFileURL(padID)
#meta.data$data_location <- getDataFileURL(padID)
#Save transformed dataset as data.frame for later reading
assign(padID, pad.classified)
names <- c(eval(padID))
#get default chart info....
meta.data <- getDefaultChartInfo(pad.classified, meta.data)
#Save the cahce
if(verbose) print("saving cache")
if(log.all) logMessage("Saving cache...")
save(list=names, file=meta.data$cache_location)
#Save meta data
#jsonSt <- toJSON(meta.data)
#saveMetaData(meta.data$meta_location, jsonSt)
#Save underlying data into csv
#saveData(pad.classified, meta.data$data_location)
if(verbose) print("Padification Completed!")
logMessage("Padification Completed!")
return(meta.data)
}
#
# padify
#
padify <- function(series, series.data, x.plot.band=NULL, y.plot.band=NULL){
#Remove rows that have NAs
series.data <- series.data[rowSums(is.na(series.data)) != ncol(series.data),]
# Remove rows that have at least 1 NA
series.data <- series.data[complete.cases(series.data),]
if("data.table" %in% class(series.data))
series.data <- as.data.frame(series.data, , stringsAsFactors=F)
#create pad
pmd <- try(createPAD(series.data, series$title,
series$desc, series$category, series$subcategory,
series$source, paste(series$name, series$country, sep="."),
series$tags, series$pagetag), silent=T)
#assign("pmd", pmd, envir=.GlobalEnv)
#
# Add this pad to system pads table in the database, to mongo db and save the cache
#
#check to see if the padification went through
if(class(pmd) %in% c("try-error")) {
if(verbose) print("couldn't padify process.")
logMessage("couldn't padify the pad...")
assign("error.count", error.count+1, envir=.GlobalEnv)
} else {
if(!is.null(x.plot.band))
pmd$default$xaxis$plotbands <- x.plot.band
if(!is.null(y.plot.band))
pmd$default$yaxis$plotbands <- y.plot.band
if(verbose) print("adding the pad to mysql")
if(log.all) logMessage("Saving PAD meta data in the database...")
insertPadToMySQL(pmd)
# insert into mongo
if(verbose) print("adding the pad to mongo db")
if(log.all) print("adding the pad to mongo db")
pad <- fromJSON(toJSON(pmd))
insertPadToMongo(pmd$id, pad)
return (pmd$id)
# #generate solr doc
# if(verbose) print("adding the pad to solr vector")
# if(log.all) print("adding the pad to mongo")
# doc <- list("type"="pad", "id" = pmd$id, "title" = pmd$title, "desc"=pmd$desc, "category"=pmd$category,
# "subcategory"=pmd$subcategory, "tags"=pmd$tags, "author"="system")
# solr.index.docs[[length(solr.index.docs)+1]] <<- doc
}
}