| OLD | NEW |
| 1 // shortcut handles storing and retrieving shortcuts. | 1 // shortcut handles storing and retrieving shortcuts. |
| 2 package shortcut | 2 package shortcut |
| 3 | 3 |
| 4 import ( | 4 import ( |
| 5 "encoding/json" | 5 "encoding/json" |
| 6 "fmt" | 6 "fmt" |
| 7 "io" | 7 "io" |
| 8 "io/ioutil" | 8 "io/ioutil" |
| 9 | 9 |
| 10 "skia.googlesource.com/buildbot.git/perf/go/db" | 10 "skia.googlesource.com/buildbot.git/perf/go/db" |
| 11 ) | 11 ) |
| 12 | 12 |
| 13 type Shortcut struct { | 13 type Shortcut struct { |
| 14 Scale int `json:"scale"` | 14 Scale int `json:"scale"` |
| 15 Tiles []int `json:"tiles"` | 15 Tiles []int `json:"tiles"` |
| 16 Keys []string `json:"keys"` | 16 Keys []string `json:"keys"` |
| 17 Hash string `json:"hash"` | 17 Hash string `json:"hash"` |
| 18 Issue string `json:"issue"` | 18 Issue string `json:"issue"` |
| 19 } | 19 } |
| 20 | 20 |
| 21 // Insert adds the shortcut content into the database. The id of the shortcut | 21 // Insert adds the shortcut content into the database. The id of the shortcut |
| 22 // is returned. | 22 // is returned. |
| 23 func Insert(r io.Reader) (string, error) { | 23 func Insert(r io.Reader) (string, error) { |
| 24 b, err := ioutil.ReadAll(r) | 24 b, err := ioutil.ReadAll(r) |
| 25 if err != nil { | 25 if err != nil { |
| 26 return "", fmt.Errorf("Unable to read shortcut body: %s", err) | 26 return "", fmt.Errorf("Unable to read shortcut body: %s", err) |
| 27 } | 27 } |
| 28 result, err := db.DB.Exec(`INSERT INTO shortcuts (traces) VALUES (?)`, s
tring(b)) | 28 result, err := db.DB.Exec(`INSERT INTO shortcuts (traces) VALUES (?)`, s
tring(b)) |
| 29 if err != nil { | 29 if err != nil { |
| 30 » » return "", fmt.Errorf("Error while inserting shortcut: err", err
) | 30 » » return "", fmt.Errorf("Error while inserting shortcut: %v", err) |
| 31 } | 31 } |
| 32 id, err := result.LastInsertId() | 32 id, err := result.LastInsertId() |
| 33 if err != nil { | 33 if err != nil { |
| 34 » » return "", fmt.Errorf("Error retrieving ID of new shortcut:", er
r) | 34 » » return "", fmt.Errorf("Error retrieving ID of new shortcut: %v",
err) |
| 35 } | 35 } |
| 36 return fmt.Sprintf("%d", id), nil | 36 return fmt.Sprintf("%d", id), nil |
| 37 } | 37 } |
| 38 | 38 |
| 39 // Get retrieves a parsed shortcut for the given id. | 39 // Get retrieves a parsed shortcut for the given id. |
| 40 func Get(id string) (*Shortcut, error) { | 40 func Get(id string) (*Shortcut, error) { |
| 41 var s string | 41 var s string |
| 42 if err := db.DB.QueryRow(`SELECT traces FROM shortcuts WHERE id =?`, id)
.Scan(&s); err != nil { | 42 if err := db.DB.QueryRow(`SELECT traces FROM shortcuts WHERE id =?`, id)
.Scan(&s); err != nil { |
| 43 return nil, fmt.Errorf("Error retrieving shortcut from db: %s",
err) | 43 return nil, fmt.Errorf("Error retrieving shortcut from db: %s",
err) |
| 44 } | 44 } |
| 45 ret := &Shortcut{} | 45 ret := &Shortcut{} |
| 46 if err := json.Unmarshal([]byte(s), ret); err != nil { | 46 if err := json.Unmarshal([]byte(s), ret); err != nil { |
| 47 return nil, fmt.Errorf("Error decoding shortcut: %s", err) | 47 return nil, fmt.Errorf("Error decoding shortcut: %s", err) |
| 48 } | 48 } |
| 49 return ret, nil | 49 return ret, nil |
| 50 } | 50 } |
| OLD | NEW |