OLD | NEW |
1 // Package activitylog implements utility for activity logging into database. | 1 // Package activitylog implements utility for activity logging into database. |
2 package activitylog | 2 package activitylog |
3 | 3 |
4 import ( | 4 import ( |
5 "fmt" | 5 "fmt" |
6 "time" | 6 "time" |
7 | 7 |
8 "github.com/golang/glog" | 8 "github.com/golang/glog" |
9 | 9 |
10 "skia.googlesource.com/buildbot.git/perf/go/db" | 10 "skia.googlesource.com/buildbot.git/perf/go/db" |
11 "skia.googlesource.com/buildbot.git/perf/go/types" | 11 "skia.googlesource.com/buildbot.git/perf/go/types" |
12 ) | 12 ) |
13 | 13 |
14 // Write writes a new activity record to the db table activitylog. | 14 // Write writes a new activity record to the db table activitylog. |
15 // Input is in types.Activity format, but ID and TS are ignored. Instead, always | 15 // Input is in types.Activity format, but ID and TS are ignored. Instead, always |
16 // use autoincrement ID and the current timestamp for the new record. | 16 // use autoincrement ID and the current timestamp for the new record. |
17 func Write(r *types.Activity) error { | 17 func Write(r *types.Activity) error { |
18 glog.Infof("Write activity: %s\n", r) | 18 glog.Infof("Write activity: %s\n", r) |
19 if r.UserID == "" || r.Action == "" { | 19 if r.UserID == "" || r.Action == "" { |
20 » » return fmt.Errorf("Activity UserID and Action cannot be empty: %
s\n", r) | 20 » » return fmt.Errorf("Activity UserID and Action cannot be empty: %
v\n", r) |
21 } | 21 } |
22 _, err := db.DB.Exec( | 22 _, err := db.DB.Exec( |
23 "INSERT INTO activitylog (timestamp, userid, action, url) VALUES
(?, ?, ?, ?)", | 23 "INSERT INTO activitylog (timestamp, userid, action, url) VALUES
(?, ?, ?, ?)", |
24 time.Now().Unix(), r.UserID, r.Action, r.URL) | 24 time.Now().Unix(), r.UserID, r.Action, r.URL) |
25 if err != nil { | 25 if err != nil { |
26 return fmt.Errorf("Failed to write to database: %s", err) | 26 return fmt.Errorf("Failed to write to database: %s", err) |
27 } | 27 } |
28 return nil | 28 return nil |
29 } | 29 } |
30 | 30 |
(...skipping 20 matching lines...) Expand all Loading... |
51 TS: timestamp, | 51 TS: timestamp, |
52 UserID: userid, | 52 UserID: userid, |
53 Action: action, | 53 Action: action, |
54 URL: url, | 54 URL: url, |
55 } | 55 } |
56 ret = append(ret, r) | 56 ret = append(ret, r) |
57 } | 57 } |
58 | 58 |
59 return ret, nil | 59 return ret, nil |
60 } | 60 } |
OLD | NEW |