-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdatabase.go
583 lines (505 loc) · 19.7 KB
/
database.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
// Social Harvest is a social media analytics platform.
// Copyright (C) 2014 Tom Maiaroto, Shift8Creative, LLC (http://www.socialharvest.io)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// This file contains the database init and functions for querying it.
// Both Postgres and InfluxDB are supported.
package main
import (
"bytes"
"github.com/SocialHarvest/harvester/lib/config"
influxdb "github.com/influxdb/influxdb/client"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
//"github.com/mitchellh/mapstructure"
"log"
"net/http"
"regexp"
"strconv"
"strings"
)
type SocialHarvestDB struct {
Postgres *sqlx.DB
InfluxDB *influxdb.Client
Series []string
Schema struct {
Compact bool `json:"compact"`
}
}
var db = SocialHarvestDB{}
// Initializes the database and returns the client, setting it to `database.Postgres` in the current package scope
func newDatabase(c config.SocialHarvestConf) *SocialHarvestDB {
// A database is not required to use Social Harvest
if c.Database.Type == "" {
return &db
}
var err error
// Holds some options that will adjust the schema
db.Schema = c.Schema
// Now supporting Postgres OR InfluxDB
// (for now...may add more in the future...the re-addition of InfluxDB is to satisfy performance curiosities, it may go away. Postgres will ALWAYS be supported.)
// actually, if config.Database becomes an array, we can write to multiple databases...
switch c.Database.Type {
case "influxdb":
cfg := &influxdb.ClientConfig{
Host: c.Database.Host + ":" + strconv.Itoa(c.Database.Port),
Username: c.Database.User,
Password: c.Database.Password,
Database: c.Database.Database,
HttpClient: http.DefaultClient,
}
db.InfluxDB, err = influxdb.NewClient(cfg)
if err != nil {
log.Println(err)
return &db
}
case "postgres", "postgresql":
// Note that sqlx just wraps database/sql and `database.Postgres` gets a sqlx.DB which is essentially a wrapped sql.DB
db.Postgres, err = sqlx.Connect("postgres", "host="+c.Database.Host+" port="+strconv.Itoa(c.Database.Port)+" sslmode=disable dbname="+c.Database.Database+" user="+c.Database.User+" password="+c.Database.Password)
if err != nil {
log.Println(err)
return &db
}
}
// Keep a list of series (tables/collections/series - whatever the database calls them, we're going with series because we're really dealing with time with just about all our data)
// These do relate to structures in lib/config/series.go
db.Series = []string{"messages", "shared_links", "mentions", "hashtags", "contributor_growth"}
return &db
}
// Checks access to the database
func (db *SocialHarvestDB) HasAccess() bool {
var err error
if db.Postgres != nil {
var c int
err = db.Postgres.Get(&c, "SELECT COUNT(*) FROM messages")
if err == nil {
return true
} else {
return false
}
}
if db.InfluxDB != nil {
}
return false
}
// -------- GETTING STUFF BACK OUT ------------
// Note: We're a little stuck in the ORM and prepared statement department because our queries need to be pretty flexible.
// Table names are dynamic in some cases (rules out prepared statements) and we have special functions and "AS" keywords all over,
// so most ORMs are out because they are designed for basic CRUD. Upper.io wasn't the most robust ORM either, but it supported quite
// a few databases and worked well for the writes. The reading was always going to be a challenge. We luck out a little bit with using
// the CommonQueryParams struct because we know the Limit, for example, must be an int and therefore is sanitized already.
// Sanitizing data won't be so bad though because we're only allowing a limited amount of user input to begin with.
// Some common parameters to make passing them around a bit easier
type CommonQueryParams struct {
From string `json:"from"`
To string `json:"to"`
Territory string `json:"territory"`
Network string `json:"network,omitempty"`
Field string `json:"field,omitempty"`
Limit uint64 `json:"limit,omitempty"`
Series string `json:"series,omitempty"`
Skip uint64 `json:"skip,omitempty"`
}
type ResultCount struct {
Count int `json:"count"`
TimeFrom string `json:"timeFrom"`
TimeTo string `json:"timeTo"`
}
type ResultAggregateCount struct {
Count int `json:"count"`
Value string `json:"value"`
}
type ResultAggregateAverage struct {
Average int `json:"average"`
Value string `json:"value"`
}
type ResultAggregateFields struct {
Count map[string][]ResultAggregateCount `json:"counts,omitempty"`
Average map[string][]ResultAggregateAverage `json:"averages,omitempty"`
TimeFrom string `json:"timeFrom"`
TimeTo string `json:"timeTo"`
Total int `json:"total"`
Distinct int `json:"distinct"`
}
type BasicConditions struct {
Gender string `json:"contributor_gender,omitempty"`
Lang string `json:"contributor_lang,omitempty"`
Country string `json:"contributor_country,omitempty"`
IsQuestion int `json:"is_question,omitempty"`
Geohash string `json:"contributor_geohash,omitempty"`
}
// Sanitizes common query params to prevent SQL injection and to ensure proper formatting, etc.
func SanitizeCommonQueryParams(params CommonQueryParams) CommonQueryParams {
sanitizedParams := CommonQueryParams{}
// Just double check it's positive
if params.Limit > 0 {
sanitizedParams.Limit = params.Limit
}
if params.Skip > 0 {
sanitizedParams.Skip = params.Skip
}
// Prepared statements not so good when we let users dynamically chose the table to query (neither are any of the ORMs for Golang either unfortunately).
// Only allow tables speicfied in the series slice to be used in a query.
for _, v := range config.SeriesCollections {
if params.Series == v {
sanitizedParams.Series = params.Series
}
}
// Territory names can included spaces and are alphanumeric
pattern := `(?i)[A-z0-9\s]`
r, _ := regexp.Compile(pattern)
if r.MatchString(params.Territory) {
sanitizedParams.Territory = params.Territory
}
// Field (column) names and Network names can contain letters, numbers, and underscores
pattern = `(?i)[A-z0-9\_]`
r, _ = regexp.Compile(pattern)
if r.MatchString(params.Field) {
sanitizedParams.Field = params.Field
}
r, _ = regexp.Compile(pattern)
if r.MatchString(params.Network) {
sanitizedParams.Network = params.Network
}
// to/from are dates and there's only certain characters necessary there too. Fore xample, something like 2014-08-08 12:00:00 is all we need.
// TODO: Maybe timezone too? All dates should be UTC so there may really be no need.
// Look for anything other than numbers, a single dash, colons, and spaces. Then also trim a dash at the end of the string in case. It's an invalid query really, but let it work still (for now).
pattern = `\-{2,}|\"|\'|[A-z]|\#|\;|\*|\!|\\|\/|\(|\)|\|`
r, _ = regexp.Compile(pattern)
if !r.MatchString(params.To) {
sanitizedParams.To = strings.Trim(params.To, "-")
}
if !r.MatchString(params.From) {
sanitizedParams.From = strings.Trim(params.From, "-")
}
//log.Println(sanitizedParams)
return sanitizedParams
}
// Groups fields values and returns a count of occurences
func (db *SocialHarvestDB) FieldCounts(queryParams CommonQueryParams, fields []string, extraParams map[string]string) ([]ResultAggregateFields, ResultCount) {
var fieldCounts []ResultAggregateFields
var total ResultCount
sanitizedQueryParams := SanitizeCommonQueryParams(queryParams)
if db.Postgres != nil {
// The following query should work for pretty much any SQL database (at least any we're supporting)
var err error
// First get the overall total number of records
var buffer bytes.Buffer
buffer.WriteString("SELECT COUNT(*) AS count FROM ")
buffer.WriteString(sanitizedQueryParams.Series)
buffer.WriteString(" WHERE territory = '")
buffer.WriteString(sanitizedQueryParams.Territory)
buffer.WriteString("'")
// optional date range (can have either or both)
if sanitizedQueryParams.From != "" {
buffer.WriteString(" AND time >= '")
buffer.WriteString(sanitizedQueryParams.From)
buffer.WriteString("'")
}
if sanitizedQueryParams.To != "" {
buffer.WriteString(" AND time <= '")
buffer.WriteString(sanitizedQueryParams.To)
buffer.WriteString("'")
}
// optional extra params to further limit what gets counted (NOTE: the value must have the operater with it along with proper SQL, ie. if string, wrap in single quotes)
if len(extraParams) > 0 {
for k, v := range extraParams {
buffer.WriteString(" AND ")
buffer.WriteString(k)
buffer.WriteString(" ")
buffer.WriteString(v)
}
}
tQuery := buffer.String()
buffer.Reset()
err = db.Postgres.Get(&total, tQuery)
if err != nil {
log.Println(err)
}
for _, field := range fields {
if len(field) > 0 {
buffer.Reset()
buffer.WriteString("SELECT COUNT(")
buffer.WriteString(field)
buffer.WriteString(") AS count,")
buffer.WriteString(field)
buffer.WriteString(" AS value")
buffer.WriteString(" FROM ")
buffer.WriteString(sanitizedQueryParams.Series)
buffer.WriteString(" WHERE territory = '")
buffer.WriteString(sanitizedQueryParams.Territory)
buffer.WriteString("'")
// optional extra params to further limit what gets counted (NOTE: the value must have the operater with it along with proper SQL, ie. if string, wrap in single quotes)
if len(extraParams) > 0 {
for k, v := range extraParams {
buffer.WriteString(" AND ")
buffer.WriteString(k)
buffer.WriteString(" ")
buffer.WriteString(v)
}
}
// optional date range (can have either or both)
if sanitizedQueryParams.From != "" {
buffer.WriteString(" AND time >= '")
buffer.WriteString(sanitizedQueryParams.From)
buffer.WriteString("'")
}
if sanitizedQueryParams.To != "" {
buffer.WriteString(" AND time <= '")
buffer.WriteString(sanitizedQueryParams.To)
buffer.WriteString("'")
}
buffer.WriteString(" AND ")
buffer.WriteString(field)
buffer.WriteString(" != ''")
buffer.WriteString(" GROUP BY ")
buffer.WriteString(field)
buffer.WriteString(" ORDER BY count DESC")
//buffer.WriteString(", ")
//buffer.WriteString(field)
//buffer.WriteString(" DESC")
// optional limit (remember the date range limits results too)
if sanitizedQueryParams.Limit > 0 {
buffer.WriteString(" LIMIT ")
buffer.WriteString(strconv.FormatInt(int64(sanitizedQueryParams.Limit), 10))
}
// optional skip
if sanitizedQueryParams.Skip > 0 {
buffer.WriteString(" OFFSET ")
buffer.WriteString(strconv.FormatInt(int64(sanitizedQueryParams.Skip), 10))
}
query := buffer.String()
buffer.Reset()
var valueCounts []ResultAggregateCount
err = db.Postgres.Select(&valueCounts, query)
if err != nil {
log.Println(err)
continue
}
count := map[string][]ResultAggregateCount{}
count[field] = valueCounts
// Get distinct count
buffer.Reset()
buffer.WriteString("SELECT COUNT(DISTINCT ")
buffer.WriteString(field)
buffer.WriteString(") FROM ")
buffer.WriteString(sanitizedQueryParams.Series)
buffer.WriteString(" WHERE territory = '")
buffer.WriteString(sanitizedQueryParams.Territory)
buffer.WriteString("'")
// optional extra params to further limit what gets counted (NOTE: the value must have the operater with it along with proper SQL, ie. if string, wrap in single quotes)
if len(extraParams) > 0 {
for k, v := range extraParams {
buffer.WriteString(" AND ")
buffer.WriteString(k)
buffer.WriteString(" ")
buffer.WriteString(v)
}
}
// optional date range (can have either or both)
if sanitizedQueryParams.From != "" {
buffer.WriteString(" AND time >= '")
buffer.WriteString(sanitizedQueryParams.From)
buffer.WriteString("'")
}
if sanitizedQueryParams.To != "" {
buffer.WriteString(" AND time <= '")
buffer.WriteString(sanitizedQueryParams.To)
buffer.WriteString("'")
}
buffer.WriteString(" AND ")
buffer.WriteString(field)
buffer.WriteString(" != ''")
query = buffer.String()
buffer.Reset()
// SELECT COUNT(DISTINCT expanded_url) AS count FROM shared_links WHERE territory = 'theWalkingDead' AND TIME >= '2014-10-01' AND TIME <= '2014-11-02' AND TYPE IN('photo','image')
var dC int
err = db.Postgres.Get(&dC, query)
if err != nil {
log.Println(err)
}
fieldCount := ResultAggregateFields{Count: count, TimeFrom: sanitizedQueryParams.From, TimeTo: sanitizedQueryParams.To, Total: total.Count, Distinct: dC}
fieldCounts = append(fieldCounts, fieldCount)
}
}
}
return fieldCounts, total
}
// Returns total number of records for a given territory and series. Optional conditions for network, field/value, and date range. This is just a simple COUNT().
// However, since it accepts a date range, it could be called a few times to get a time series graph.
func (database *SocialHarvestDB) Count(queryParams CommonQueryParams, fieldValue string) ResultCount {
sanitizedQueryParams := SanitizeCommonQueryParams(queryParams)
var count = ResultCount{}
if db.Postgres != nil {
// The following query should work for pretty much any SQL database (at least any we're supporting)
var err error
var buffer bytes.Buffer
buffer.WriteString("SELECT COUNT(*) AS count FROM ")
buffer.WriteString(sanitizedQueryParams.Series)
buffer.WriteString(" WHERE territory = '")
buffer.WriteString(sanitizedQueryParams.Territory)
buffer.WriteString("'")
// optional date range (can have either or both)
if sanitizedQueryParams.From != "" {
buffer.WriteString(" AND time >= '")
buffer.WriteString(sanitizedQueryParams.From)
buffer.WriteString("'")
}
if sanitizedQueryParams.To != "" {
buffer.WriteString(" AND time <= '")
buffer.WriteString(sanitizedQueryParams.To)
buffer.WriteString("'")
}
// Because we're accepting user inuput, use a prepared statement. Sanitizing fieldValue could also be done in the future perhaps (if needed).
// The problem with prepared statements everywhere is that we can't put the tables through them. So only a few places will we be able to use them.
// Here is one though.
if sanitizedQueryParams.Field != "" && fieldValue != "" {
buffer.WriteString(" AND ")
buffer.WriteString(sanitizedQueryParams.Field)
buffer.WriteString(" = $1")
}
// Again for the network
if sanitizedQueryParams.Network != "" {
buffer.WriteString(" AND network")
// Must everything be so different?
buffer.WriteString(" = $2")
}
query := buffer.String()
buffer.Reset()
if err != nil {
}
// TODO: There has to be a better way to do this.... Need to pass a variable number of args
if fieldValue != "" && sanitizedQueryParams.Network == "" {
err = db.Postgres.Get(&count, query, fieldValue)
} else if fieldValue != "" && sanitizedQueryParams.Network != "" {
err = db.Postgres.Get(&count, query, fieldValue, sanitizedQueryParams.Network)
} else if fieldValue == "" && sanitizedQueryParams.Network != "" {
err = db.Postgres.Get(&count, query, sanitizedQueryParams.Network)
} else {
err = db.Postgres.Get(&count, query)
}
count.TimeFrom = sanitizedQueryParams.From
count.TimeTo = sanitizedQueryParams.To
}
return count
}
// Allows the messages series to be queried in some general ways.
func (database *SocialHarvestDB) Messages(queryParams CommonQueryParams, conds BasicConditions) ([]config.SocialHarvestMessage, uint64, uint64, uint64) {
sanitizedQueryParams := SanitizeCommonQueryParams(queryParams)
var results = []config.SocialHarvestMessage{}
var err error
// Must have a territory (for now)
if sanitizedQueryParams.Territory == "" {
return results, 0, sanitizedQueryParams.Skip, sanitizedQueryParams.Limit
}
var buffer bytes.Buffer
var bufferCount bytes.Buffer
var bufferQuery bytes.Buffer
bufferCount.WriteString("SELECT COUNT(*)")
bufferQuery.WriteString("SELECT *")
buffer.WriteString(" FROM messages WHERE territory = '")
buffer.WriteString(sanitizedQueryParams.Territory)
buffer.WriteString("'")
// optional date range (can have either or both)
if sanitizedQueryParams.From != "" {
buffer.WriteString(" AND time >= ")
buffer.WriteString(sanitizedQueryParams.From)
}
if sanitizedQueryParams.To != "" {
buffer.WriteString(" AND time <= ")
buffer.WriteString(sanitizedQueryParams.To)
}
if sanitizedQueryParams.Network != "" {
buffer.WriteString(" AND network = ")
buffer.WriteString(sanitizedQueryParams.Network)
}
// BasicConditions (various basic query conditions to be used explicitly, not in a loop, because not all fields will be available depending on the series)
if conds.Lang != "" {
buffer.WriteString(" AND contributor_lang = ")
buffer.WriteString(conds.Lang)
}
if conds.Country != "" {
buffer.WriteString(" AND contributor_country = ")
buffer.WriteString(conds.Country)
}
if conds.Geohash != "" {
// Ensure the goehash is alphanumeric.
// TODO: Pass these conditions through a sanitizer too, though the ORM should use prepared statements and take care of SQL injection....right? TODO: Check that too.
pattern := `(?i)[A-z0-9]`
r, _ := regexp.Compile(pattern)
if r.MatchString(conds.Geohash) {
buffer.WriteString(" AND contributor_geohash LIKE ")
buffer.WriteString(conds.Geohash)
buffer.WriteString("%")
}
}
if conds.Gender != "" {
switch conds.Gender {
case "-1", "f", "female":
buffer.WriteString(" AND contributor_gender = -1")
break
case "1", "m", "male":
buffer.WriteString(" AND contributor_gender = 1")
break
case "0", "u", "unknown":
buffer.WriteString(" AND contributor_gender = 0")
break
}
}
if conds.IsQuestion != 0 {
buffer.WriteString(" AND is_question = 1")
}
// Count here (before limit and order)
bufferCount.WriteString(buffer.String())
// Continue with query returning results
// TODO: Allow other sorting options? I'm not sure it matters because people likely want timely data. More important would be a search.
buffer.WriteString(" ORDER BY time DESC")
buffer.WriteString(" LIMIT ")
buffer.WriteString(strconv.FormatUint(sanitizedQueryParams.Limit, 10))
if (sanitizedQueryParams.Skip) > 0 {
buffer.WriteString(" OFFSET ")
buffer.WriteString(strconv.FormatUint(sanitizedQueryParams.Skip, 10))
}
bufferQuery.WriteString(buffer.String())
buffer.Reset()
query := bufferQuery.String()
bufferQuery.Reset()
countQuery := bufferCount.String()
bufferCount.Reset()
total := uint64(0)
if db.Postgres != nil {
var rows *sqlx.Rows
rows, err = db.Postgres.Queryx(query)
if err != nil {
log.Println(err)
return results, 0, sanitizedQueryParams.Skip, sanitizedQueryParams.Limit
}
// Map rows to array of struct
// TODO: Make slice of fixed size given we know limit?
var msg config.SocialHarvestMessage
for rows.Next() {
err = rows.StructScan(&msg)
if err != nil {
log.Println(err)
return results, 0, sanitizedQueryParams.Skip, sanitizedQueryParams.Limit
}
results = append(results, msg)
}
err = db.Postgres.Get(&total, countQuery)
if err != nil {
log.Println(err)
}
}
return results, total, sanitizedQueryParams.Skip, sanitizedQueryParams.Limit
}