Skip to content

Instantly share code, notes, and snippets.

@leafo
Last active August 29, 2015 14:02
Show Gist options
  • Save leafo/4fd93235a4502db7de1f to your computer and use it in GitHub Desktop.
Save leafo/4fd93235a4502db7de1f to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"fmt"
"os"
"sort"
"strconv"
"strings"
)
type action struct {
id string
time int
path string
}
type sortTuple struct {
id string
count int
}
type sortTuples []sortTuple
func (self sortTuples) Len() int {
return len(self)
}
func (self sortTuples) Swap(i, j int) {
self[i], self[j] = self[j], self[i]
}
func (self sortTuples) Less(i, j int) bool {
return self[i].count >= self[j].count
}
type pathList []string
type userSessions struct {
id string
lastActionTime int
sessions []pathList
}
func topCounts(counter map[string]int, num int) {
tuples := make([]sortTuple, 0)
for id, count := range counter {
tuples = append(tuples, sortTuple{
id, count,
})
}
sort.Sort(sortTuples(tuples))
for i := 0; i < num; i++ {
fmt.Println(tuples[i])
}
}
func buildSessions(actions []action) map[string]*userSessions {
sessionsById := make(map[string]*userSessions)
for _, action := range actions {
session := sessionsById[action.id]
if session == nil {
session = &userSessions{
id: action.id,
}
sessionsById[action.id] = session
}
// one hour
if action.time-session.lastActionTime > 60*60 {
current := pathList{ action.path }
session.sessions = append(session.sessions, current)
} else {
current := &session.sessions[len(session.sessions)-1]
*current = append(*current, action.path)
}
session.lastActionTime = action.time
}
return sessionsById
}
func main() {
file, _ := os.Open("action_log.log")
defer file.Close()
scanner := bufio.NewScanner(file)
actions := make([]action, 0)
actionCount := make(map[string]int)
pathCount := make(map[string]int)
count := 0
for scanner.Scan() {
count += 1
// if count > 100000 {
// break
// }
parts := strings.SplitN(scanner.Text(), "::", 3)
time, _ := strconv.Atoi(parts[1])
row := action{
id: parts[0],
time: time,
path: parts[2],
}
// funky 404 requester
if row.id == "0812c2b7a1636a9bfa98328af60a8499" {
continue
}
actionCount[row.id] += 1
pathCount[row.path] += 1
actions = append(actions, row)
}
fmt.Println("Collected actions...", len(actions))
sessions := buildSessions(actions)
for _, session := range sessions {
fmt.Println(session.id, session.sessions)
}
}
fname = "action_log.log"
file = assert io.open fname
users = {}
lines = 0
for line in file\lines!
uuid, time, path = line\match "^(.-)::(.-)::(.-)$"
continue unless uuid
users[uuid] = (users[uuid] or 0) + 1
lines += 1
print "Processed #{lines} lines"
tuples = for uuid, count in pairs users
{uuid, count}
print "Made #{#tuples} tuples"
table.sort tuples, (a,b) ->
a[2] > b[2]
print "Sorted"
require("moon").p tuples[1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment