#
# select companies by tags
#

library(RMySQL)
#library(sqldf)
library(feather)
library(dplyr)
library(reshape2)
library(lubridate)


# 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
# 3.1. Load one "table" function
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
df_entity <- db_load("wp_esi_entity")
df_tag <- db_load("wp_esi_tag")
df_tag_entity <- db_load("wp_esi_tag_entity")

# 3.3. Feature Engineering
df_entity <- df_entity %>% 
  subset(select = c(id, name, country, city))

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


#  ------------------------------------------------------------------------

# entity_id: 474
# tag_id: 56, 77

f <- read_feather("entities_tags_selected")

unique_tags <- unique(f$tag_id)


f_and <- f %>% 
  group_by(entity_id) %>% 
  summarise(n = n()) %>% 
  filter(n == length(unique_tags))


# # OR
# f_or <- f %>% 
#   filter(entity_id %in% f$entity_id)

# f_and <- df_entity %>% 
#   filter(id %in% f$entity_id)

  


