Changed interface of StatForTime and added functions for DB.
Added AddStatTimeToDb that save one StatForTime in DB. Added Init() for StatForTime that initialize time with current Unix time and keys with empty map. Application now ssae keys to DB.
This commit is contained in:
70
src/main.go
70
src/main.go
@@ -4,10 +4,10 @@ package main
|
|||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -19,14 +19,19 @@ const (
|
|||||||
SLEEP_TIME = 3 * time.Second
|
SLEEP_TIME = 3 * time.Second
|
||||||
KEYBOARD_BUFER_SIZE = 10000
|
KEYBOARD_BUFER_SIZE = 10000
|
||||||
DATABASE_NAME = "./gokeystat.db"
|
DATABASE_NAME = "./gokeystat.db"
|
||||||
CAPTURE_TIME = 5 * time.Second // time between capturing keyboard to db
|
CAPTURE_TIME = 5 // time in seconds between capturing keyboard to db
|
||||||
)
|
)
|
||||||
|
|
||||||
type StatForTime struct {
|
type StatForTime struct {
|
||||||
time int
|
time int64
|
||||||
keys map[uint8]int
|
keys map[uint8]int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (stat *StatForTime) Init() {
|
||||||
|
stat.time = time.Now().Unix()
|
||||||
|
stat.keys = make(map[uint8]int)
|
||||||
|
}
|
||||||
|
|
||||||
// Return map from key numbers to key names like "F1", "Tab", "d"
|
// Return map from key numbers to key names like "F1", "Tab", "d"
|
||||||
func GetKeymap() map[uint8]string {
|
func GetKeymap() map[uint8]string {
|
||||||
return GetKeymapFromOutput(GetKeymapOutput())
|
return GetKeymapFromOutput(GetKeymapOutput())
|
||||||
@@ -76,11 +81,17 @@ func GetKeyNumsFromOutput(buf []byte) []uint8 {
|
|||||||
return keyNums
|
return keyNums
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitDb(db *sql.DB, keyMap map[uint8]string) {
|
func GetKeyNumsFromKeyMap(keyMap map[uint8]string) []int {
|
||||||
keyNums := make([]int, 0, len(keyMap))
|
res := make([]int, 0, len(keyMap))
|
||||||
for keyNum := range keyMap {
|
for keyNum := range keyMap {
|
||||||
keyNums = append(keyNums, int(keyNum))
|
res = append(res, int(keyNum))
|
||||||
}
|
}
|
||||||
|
sort.Ints(res)
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
func InitDb(db *sql.DB, keyMap map[uint8]string) {
|
||||||
|
keyNums := GetKeyNumsFromKeyMap(keyMap)
|
||||||
|
|
||||||
sqlInit := `CREATE TABLE IF NOT EXISTS keylog (
|
sqlInit := `CREATE TABLE IF NOT EXISTS keylog (
|
||||||
time INTEGER primary key`
|
time INTEGER primary key`
|
||||||
@@ -104,6 +115,19 @@ func InitDb(db *sql.DB, keyMap map[uint8]string) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("%q: %s\n", err, sqlInit)
|
log.Fatalf("%q: %s\n", err, sqlInit)
|
||||||
}
|
}
|
||||||
|
rows, err := db.Query("select COUNT(*) from keymap")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var rowsCount int
|
||||||
|
rows.Next()
|
||||||
|
rows.Scan(&rowsCount)
|
||||||
|
if rowsCount > 0 {
|
||||||
|
// already inserted keymap
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
tx, err := db.Begin()
|
tx, err := db.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -124,6 +148,25 @@ func InitDb(db *sql.DB, keyMap map[uint8]string) {
|
|||||||
tx.Commit()
|
tx.Commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func AddStatTimeToDb(db *sql.DB, statTime StatForTime, keyMap map[uint8]string) {
|
||||||
|
keyNums := GetKeyNumsFromKeyMap(keyMap)
|
||||||
|
sqlStmt := "insert into keylog(time"
|
||||||
|
for keyNum := range keyNums {
|
||||||
|
sqlStmt += ",\n" + "KEY" + strconv.Itoa(keyNum)
|
||||||
|
}
|
||||||
|
sqlStmt += ") values "
|
||||||
|
sqlStmt += "(" + strconv.FormatInt(statTime.time, 10)
|
||||||
|
for keyNum := range keyNums {
|
||||||
|
keyNumber, _ := statTime.keys[uint8(keyNum)]
|
||||||
|
sqlStmt += ",\n" + strconv.Itoa(keyNumber)
|
||||||
|
}
|
||||||
|
sqlStmt += ")"
|
||||||
|
_, err := db.Exec(sqlStmt)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("%q: %s\n", err, sqlStmt)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
keyboardId := flag.Int("id", -1, "Your keyboard id")
|
keyboardId := flag.Int("id", -1, "Your keyboard id")
|
||||||
@@ -144,6 +187,7 @@ func main() {
|
|||||||
keyMap := GetKeymap()
|
keyMap := GetKeymap()
|
||||||
|
|
||||||
InitDb(db, keyMap)
|
InitDb(db, keyMap)
|
||||||
|
|
||||||
cmd := exec.Command("xinput", "test", strconv.Itoa(*keyboardId))
|
cmd := exec.Command("xinput", "test", strconv.Itoa(*keyboardId))
|
||||||
|
|
||||||
stdout, err := cmd.StdoutPipe()
|
stdout, err := cmd.StdoutPipe()
|
||||||
@@ -155,13 +199,25 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
buf := make([]byte, KEYBOARD_BUFER_SIZE)
|
buf := make([]byte, KEYBOARD_BUFER_SIZE)
|
||||||
|
var curStat StatForTime
|
||||||
|
curStat.Init()
|
||||||
for {
|
for {
|
||||||
n, err := stdout.Read(buf)
|
n, err := stdout.Read(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
// processing buf here
|
// processing buf here
|
||||||
fmt.Println(n)
|
for _, keyNum := range GetKeyNumsFromOutput(buf[:n]) {
|
||||||
|
oldKeyCount, _ := curStat.keys[keyNum]
|
||||||
|
curStat.keys[keyNum] = oldKeyCount + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
// Every CAPTURE_TIME seconds save to BD
|
||||||
|
if time.Now().Unix()-curStat.time > CAPTURE_TIME {
|
||||||
|
AddStatTimeToDb(db, curStat, keyMap)
|
||||||
|
curStat.Init()
|
||||||
|
}
|
||||||
|
|
||||||
time.Sleep(SLEEP_TIME)
|
time.Sleep(SLEEP_TIME)
|
||||||
}
|
}
|
||||||
case *outputPath != "":
|
case *outputPath != "":
|
||||||
|
|||||||
Reference in New Issue
Block a user