SaveStats records click stats for links. The provided map includes incremental clicks that have occurred since the last time SaveStats was called.
(stats ClickStats)
| 188 | // incremental clicks that have occurred since the last time SaveStats |
| 189 | // was called. |
| 190 | func (s *SQLiteDB) SaveStats(stats ClickStats) error { |
| 191 | s.mu.Lock() |
| 192 | defer s.mu.Unlock() |
| 193 | |
| 194 | tx, err := s.db.BeginTx(context.TODO(), nil) |
| 195 | if err != nil { |
| 196 | return err |
| 197 | } |
| 198 | now := time.Now().Unix() |
| 199 | for short, clicks := range stats { |
| 200 | _, err := tx.Exec("INSERT INTO Stats (ID, Created, Clicks) VALUES (?, ?, ?)", linkID(short), now, clicks) |
| 201 | if err != nil { |
| 202 | tx.Rollback() |
| 203 | return err |
| 204 | } |
| 205 | } |
| 206 | return tx.Commit() |
| 207 | } |
| 208 | |
| 209 | // DeleteStats deletes click stats for a link. |
| 210 | func (s *SQLiteDB) DeleteStats(short string) error { |