This repository was archived by the owner on Nov 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnapshot.go
More file actions
93 lines (82 loc) · 2.3 KB
/
snapshot.go
File metadata and controls
93 lines (82 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package core
import (
"encoding/json"
"fmt"
"github.com/datatogether/sql_datastore"
"github.com/datatogether/sqlutil"
"github.com/ipfs/go-datastore"
"time"
)
// A snapshot is a record of a GET request to a url
// There can be many snapshots of a given url
type Snapshot struct {
// The url that was requested
Url string `json:"url"`
// Time this request was issued
Created time.Time `json:"date"`
// Returned Status
Status int `json:"status,omitempty"`
// Time to complete response in milliseconds
Duration int64 `json:"downloadTook,omitempty"`
// Record of all returned headers in [key,value,key,value...]
Headers []string `json:"headers,omitempty"`
// Multihash of response body (if any)
Hash string `json:"hash,omitempty"`
}
// SnapshotsForUrl returns all snapshots for a given url string
func SnapshotsForUrl(db sqlutil.Queryable, url string) ([]*Snapshot, error) {
res, err := db.Query(qSnapshotsByUrl, url)
if err != nil {
return nil, err
}
defer res.Close()
snapshots := make([]*Snapshot, 0)
for res.Next() {
c := &Snapshot{}
if err := c.UnmarshalSQL(res); err != nil {
return nil, err
}
snapshots = append(snapshots, c)
}
return snapshots, nil
}
// WriteSnapshot creates a snapshot record in the DB from a given Url struct
func WriteSnapshot(store datastore.Datastore, u *Url) error {
if sqld, ok := store.(*sql_datastore.Datastore); ok {
data, err := json.Marshal(u.Headers)
if err != nil {
return err
}
_, err = sqld.DB.Exec(qSnapshotInsert, u.Url, u.LastGet.In(time.UTC).Round(time.Second), u.Status, u.DownloadTook, data, u.Hash)
return err
}
return fmt.Errorf("write snapshot requires an sql db as it's datastore for url: %s", u.Url)
}
// UnmarshalSQL reads an SQL result into the snapshot receiver
func (s *Snapshot) UnmarshalSQL(row sqlutil.Scannable) error {
var (
url, hash string
created time.Time
duration int64
status int
headerData []byte
)
if err := row.Scan(&url, &created, &status, &duration, &hash, &headerData); err != nil {
return err
}
var headers []string
if headerData != nil {
if err := json.Unmarshal(headerData, &headers); err != nil {
return err
}
}
*s = Snapshot{
Url: url,
Created: created.In(time.UTC),
Status: status,
Duration: duration,
Hash: hash,
Headers: headers,
}
return nil
}