💮今日の作業を可視化する: Macのログをごにょる
大変面白い記事を見つけた。
管理願望の強い私は、さっそく試してみようと思ったのだけど、なにせGo言語について知識がなかったので挫折した。
というわけでパートナーであるRを使って同様のことを行ってみる。もちろん肝心の可視化もやる。
これらのパッケージを利用するので読み込んでおく。
library(ggplot2) library(gridExtra) library(ggalt) # devtools::install_github("hrbrmstr/ggalt") library(lubridate) library(dplyr)
library(SUmisc) # 俺々パッケージ。なくても問題ない
まずはログファイルを保存するディレクトリを作成し、pmset
コマンドでMacの起動時とスリープの情報について得る。
path <- "/Users/uri/Dropbox/log"
dir.create(path) system('pmset -g log|grep -e " Sleep " -e " Wake " -e " Start "', intern = TRUE) %>% write(paste(path, "df_log.dat", sep = "/"))
{dplyr}
で扱いやすいデータにする。
df_log <- read.table(paste(path, "df_log.dat", sep = "/"), header = FALSE, fill = TRUE) %>% dplyr::select(num_range("V", 1:4), -V3) %>% dplyr::rename(Date = V1, Time = V2, Status = V4) %>% dplyr::filter(Status %in% c("Sleep", "Start", "Wake")) %>% dplyr::mutate(Date = lubridate::ymd(Date, tz = "asia/tokyo"), Time = as.character(Time)) %>% droplevels()
こんな感じになる。
knitr::kable(head(df_log))
Date | Time | Status |
---|---|---|
2015-09-11 | 18:25:40 | Wake |
2015-09-11 | 18:44:49 | Sleep |
2015-09-11 | 19:14:39 | Wake |
2015-09-11 | 19:25:30 | Sleep |
2015-09-11 | 19:29:03 | Wake |
2015-09-11 | 19:36:02 | Sleep |
levels(df_log$Status)
## [1] "Sleep" "Start" "Wake"
起動時とスリープ前後の状態と時間が保存されているが、終了時のログは残っていない。というわけでシャットダウンの時刻についてはlast
コマンドで取得する
system('last shutdown', intern = TRUE) %>% write(paste(path, "df_log_down.dat", sep = "/"))
同様にデータをいじくる。
df_log_down <- read.table(paste(path, "df_log_down.dat", sep = "/"), header = FALSE, fill = TRUE) %>% dplyr::mutate(Date = lubridate::ymd(stringr::str_c("2015", V3, V4, V5, sep = " "), tz = "asia/tokyo")) %>% dplyr::mutate(Time = stringr::str_c(V6, "00", sep = ":")) %>% dplyr::filter(V1 == "shutdown") %>% dplyr::select(Date, Time, Status = V1)
2つのファイルを結合し、今日の日付のログを取得する。
df_log %<>% rbind(., df_log_down) %>% dplyr::arrange(Date, Time) df_log %<>% dplyr::filter(Date == as.character(lubridate::today())) %>% droplevels() df_log$Status <- factor(df_log$Status, levels = c("shutdown", "Sleep", "Wake", "Start"), labels = c("Shutdown", "Sleep", "Wake", "Start"))
最後に{ggplot2}
で視覚化。とりあえずという感じ。geom_line
だとカクカクした線になるので、キャプテンの新パッケージを使うことにした(いいタイミング!)。
ggplot(df_log, aes(Time, Status, group = Date)) + geom_point(aes(colour = Status, size = 3)) + geom_xspline(size = 0.5) + scale_colour_Publication() + guides(colour = FALSE, size = FALSE)
日付データと恋に落ちるの沼に落ちかけたが、なんとかできた。時間があればもうちょいなんとかする。
Enjoy!