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
/
Copy pathprimer.go
292 lines (256 loc) · 6.58 KB
/
primer.go
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package core
import (
"database/sql"
"encoding/json"
"fmt"
"github.com/datatogether/sql_datastore"
"github.com/datatogether/sqlutil"
"github.com/ipfs/go-datastore"
"github.com/pborman/uuid"
"time"
)
// Primer is tracking information about an abstract group of content.
// For example a government agency is a primer
type Primer struct {
// version 4 uuid
Id string `json:"id"`
// Created timestamp rounded to seconds in UTC
Created time.Time `json:"created"`
// Updated timestamp rounded to seconds in UTC
Updated time.Time `json:"updated"`
// shortest possible expression of this primer's name, usually an acronym
// called shortTitle b/c acronyms collide often & users should feel free to
// expand on acronyms
ShortTitle string `json:"shortTitle"`
// human-readable title of this primer.
Title string `json:"title"`
// long-form description of this primer.
// TODO - Maybe we should store this in markdown format?
Description string `json:"description"`
// parent primer (if any)
Parent *Primer `json:"parent"`
// child-primers list
SubPrimers []*Primer `json:"subPrimers,omitempty"`
// metadata to associate with this primer
Meta map[string]interface{} `json:"meta"`
// statistics about this primer
Stats *PrimerStats `json:"stats"`
// collection of child sources
Sources []*Source `json:"sources,omitempty"`
}
// TODO - finish
type PrimerStats struct {
UrlCount int `json:"urlCount"`
ArchivedUrlCount int `json:"archivedUrlCount"`
ContentUrlCount int `json:"contentUrlCount"`
ContentMetadataCount int `json:"contentMetadataCount"`
SourcesUrlCount int `json:"sourcesUrlCount"`
SourcesArchivedUrlCount int `json:"sourcesArchivedUrlCount"`
}
func (p Primer) DatastoreType() string {
return "Primer"
}
func (p Primer) GetId() string {
return p.Id
}
func (p Primer) Key() datastore.Key {
return datastore.NewKey(fmt.Sprintf("%s:%s", p.DatastoreType(), p.GetId()))
}
// ReadSubPrimers reads child primers of this primer
func (p *Primer) ReadSubPrimers(db sqlutil.Queryable) error {
rows, err := db.Query(qPrimerSubPrimers, p.Id)
if err != nil {
return err
}
defer rows.Close()
sps := make([]*Primer, 0)
for rows.Next() {
c := &Primer{}
if err := c.UnmarshalSQL(rows); err != nil {
return err
}
sps = append(sps, c)
}
p.SubPrimers = sps
return nil
}
func (p *Primer) CalcStats(db *sql.DB) error {
p.Stats = &PrimerStats{}
if err := p.ReadSources(db); err != nil {
return err
}
for _, s := range p.Sources {
if err := s.CalcStats(db); err != nil {
return err
}
p.Stats.UrlCount += s.Stats.UrlCount
p.Stats.ContentMetadataCount += s.Stats.ContentMetadataCount
p.Stats.ContentUrlCount += s.Stats.ContentUrlCount
}
if err := p.ReadSubPrimers(db); err != nil {
return err
}
for _, sp := range p.SubPrimers {
if err := sp.CalcStats(db); err != nil {
return err
}
p.Stats.UrlCount += sp.Stats.UrlCount
p.Stats.ContentMetadataCount += sp.Stats.ContentMetadataCount
p.Stats.ContentUrlCount += sp.Stats.ContentUrlCount
}
store := sql_datastore.NewDatastore(db)
if err := store.Register(&Primer{}); err != nil {
return err
}
return p.Save(store)
}
// ReadSources reads child sources of this primer
func (p *Primer) ReadSources(db sqlutil.Queryable) error {
rows, err := db.Query(qPrimerSources, p.Id)
if err != nil {
return err
}
defer rows.Close()
s := make([]*Source, 0)
for rows.Next() {
c := &Source{}
if err := c.UnmarshalSQL(rows); err != nil {
return err
}
s = append(s, c)
}
p.Sources = s
return nil
}
func (p *Primer) Read(store datastore.Datastore) error {
pi, err := store.Get(p.Key())
if err != nil {
if err == datastore.ErrNotFound {
return ErrNotFound
}
return err
}
got, ok := pi.(*Primer)
if !ok {
return ErrInvalidResponse
}
*p = *got
return nil
}
func (p *Primer) Save(store datastore.Datastore) (err error) {
var exists bool
if p.Id != "" {
exists, err = store.Has(p.Key())
if err != nil {
return err
}
}
if !exists {
p.Id = uuid.New()
p.Created = time.Now().Round(time.Second)
p.Updated = p.Created
} else {
p.Updated = time.Now().Round(time.Second)
}
return store.Put(p.Key(), p)
}
func (p *Primer) Delete(store datastore.Datastore) error {
return store.Delete(p.Key())
}
func (p *Primer) NewSQLModel(key datastore.Key) sql_datastore.Model {
return &Primer{Id: key.Name()}
}
func (p *Primer) SQLQuery(cmd sql_datastore.Cmd) string {
switch cmd {
case sql_datastore.CmdCreateTable:
return qPrimerCreateTable
case sql_datastore.CmdExistsOne:
return qPrimerExists
case sql_datastore.CmdSelectOne:
return qPrimerById
case sql_datastore.CmdInsertOne:
return qPrimerInsert
case sql_datastore.CmdUpdateOne:
return qPrimerUpdate
case sql_datastore.CmdDeleteOne:
return qPrimerDelete
case sql_datastore.CmdList:
return qPrimersList
default:
return ""
}
}
func (p *Primer) SQLParams(cmd sql_datastore.Cmd) []interface{} {
switch cmd {
case sql_datastore.CmdSelectOne, sql_datastore.CmdExistsOne, sql_datastore.CmdDeleteOne:
return []interface{}{p.Id}
case sql_datastore.CmdList:
return []interface{}{}
default:
parentId := ""
if p.Parent != nil {
parentId = p.Parent.Id
}
metaBytes, err := json.Marshal(p.Meta)
if err != nil {
panic(err)
}
statBytes, err := json.Marshal(p.Stats)
if err != nil {
panic(err)
}
return []interface{}{
p.Id,
p.Created.In(time.UTC),
p.Updated.In(time.UTC),
p.ShortTitle,
p.Title,
p.Description,
parentId,
statBytes,
metaBytes,
}
}
}
func (p *Primer) UnmarshalSQL(row sqlutil.Scannable) error {
var (
parent *Primer
id, title, description, short, parentId string
created, updated time.Time
statsBytes, metaBytes []byte
meta map[string]interface{}
stats *PrimerStats
)
if err := row.Scan(&id, &created, &updated, &short, &title, &description, &parentId, &statsBytes, &metaBytes); err != nil {
if err == sql.ErrNoRows {
return ErrNotFound
}
return err
}
if parentId != "" {
parent = &Primer{Id: parentId}
}
if metaBytes != nil {
if err := json.Unmarshal(metaBytes, &meta); err != nil {
return err
}
}
if statsBytes != nil {
stats = &PrimerStats{}
if err := json.Unmarshal(statsBytes, stats); err != nil {
return err
}
}
*p = Primer{
Id: id,
Created: created.In(time.UTC),
Updated: updated.In(time.UTC),
ShortTitle: short,
Title: title,
Description: description,
Parent: parent,
Meta: meta,
Stats: stats,
}
return nil
}