From 7d955b81628e6f8e550cc609b28ff319691cb4c0 Mon Sep 17 00:00:00 2001 From: Maxime Wack Date: Wed, 17 Jan 2018 18:01:39 +0000 Subject: [PATCH] Insert in db using R --- geoip.R | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/geoip.R b/geoip.R index 21a16ce..e3a6ff2 100644 --- a/geoip.R +++ b/geoip.R @@ -3,16 +3,16 @@ library(RSQLite) library(ipapi) library(leaflet) -# sqlite3 access.db -# create table access(ip, null1, user, timestamp, zone, req, status, size, referer, agent, null2); -# .separator " " -# .import access.log access - -dbConnect(SQLite(), "access.db") %>% - dbGetQuery("delete from access where ip = '192.168.0.254' or user = 'maxx' or agent like '%bot%' or ip = '164.2.255.244';") - -dbConnect(SQLite(), "access.db") %>% - dbGetQuery("vacuum;") +dbConnect(SQLite(), "access.db") -> db + +read_delim("access.log", delim = " ", col_names = c("ip", "null1", "user", "timestamp", "zone", "req", "status", "size", "referer", "agent", "null2")) %>% + mutate_all(na_if, "-") %>% + filter(user %>% is.na) %>% + filter(!ip %in% c("192.168.0.254", "164.2.255.244")) %>% + mutate(timestamp = timestamp %>% as.POSIXct(format = "[%d/%b/%Y:%H:%M:%S")) %>% + separate(req, into = c("method", "url", "version"), sep = " ") %>% + select(ip, timestamp, url, status, referer, agent) %>% + dbWriteTable(conn = db, name = "access", value = ., append = T) known_ips <- character(0)