#
# select companies by tech
#

library(shinydashboard)
library(RMySQL)
#library(sqldf)
library(feather)
library(ggplot2)
library(dplyr)
library(reshape2)
library(lubridate)
library(plotly) 


# 1. Connect to db
mydb <-  dbConnect(MySQL(), user='analyst', password='exa_analyst1&', 
                   dbname='esi_management', 
                   host='lecanaldb.c12hbxfn3xzn.eu-west-1.rds.amazonaws.com',
                   port=3306)

# 2. List of tables
dbListTables(mydb)

# 3. Load tables

db_load <- function(table){
  rs = dbSendQuery(mydb, paste0("select * from ", table))
  df = fetch(rs, n=-1)
  write_feather(df, paste0("data/", table))  
}

# 3.2. Load all tables
# <main>
df_entity <- db_load("wp_esi_entity")

# basic for cross
df_tag <- db_load("wp_esi_tag")
df_technology <- db_load("wp_esi_technology")
df_industry <- db_load("wp_esi_industry")
df_function <- db_load("wp_esi_function")

# cross-tables
df_entity_tag <- db_load("wp_esi_tag_entity")
df_entity_technology <- db_load("wp_esi_technology_entity")
df_entity_industry <- db_load("wp_esi_entity_industry")

# stats
nrow(df_technology)
write.csv(df_technology, "technology.csv", row.names = F)

nrow(df_industry)
write.csv(df_industry, "industry.csv", row.names = F)

nrow(df_entity_technology)
write.csv(df_entity_technology, "entity_technology.csv", row.names = F)

nrow(df_functon)
write.csv(df_functon, "functon.csv", row.names = F)

nrow(df_entity_industry)
write.csv(df_entity_industry, "entity_industry.csv", row.names = F)

# Merge ------------------------------------------------------------------

# Add "source" feature
df_tag$source <- "tag"

df_technology$source <- "technology"

df_industry$description <- NA
df_industry$source <- "industry"

df_function$description <- NA
df_function$source <- "function"

# Merge all
df <- df_tag %>% 
  rbind(df_technology) %>% 
  rbind(df_industry) %>% 
  rbind(df_function) %>% 
  arrange(name)

write.csv(df, "tag_tech_ind_func.csv", row.names = F)

# # 4. Save as a chached file
# write_feather(df, paste0("data/", table))  
