-
Notifications
You must be signed in to change notification settings - Fork 4
/
answers.go
58 lines (48 loc) · 1.43 KB
/
answers.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
package statistics
import (
"context"
"time"
"cloud.google.com/go/bigquery"
"github.com/ansel1/merry/v2"
"github.com/bcc-code/bcc-media-platform/backend/sqlc"
"github.com/google/uuid"
"google.golang.org/api/iterator"
)
var getLatestExportedAnswerDateSQL = "SELECT MAX(updated) as updated FROM `bccm-k8s-main.rudderstack_prod.answers`"
type latestExportedAnswerDateRow struct {
Updated time.Time
}
// GetAnswersSinceBQRow is a row
type GetAnswersSinceBQRow struct {
Message string `bigquery:"message"`
ItemID uuid.UUID `bigquery:"item_id"`
Rating int32 `bigquery:"rating"`
AgeGroup string `bigquery:"age_group"`
OrgID int `bigquery:"org_id"`
Updated time.Time `bigquery:"updated"`
}
func answerRowToBQRow(r sqlc.GetAnswersSinceRow, _ int) GetAnswersSinceBQRow {
return GetAnswersSinceBQRow{
Message: r.Message,
ItemID: r.ItemID,
Rating: r.Rating,
AgeGroup: r.AgeGroup.String,
OrgID: int(r.OrgID.Int64),
Updated: r.Updated.Time,
}
}
func getLatestExportedAnswerDate(ctx context.Context, bqClient *bigquery.Client, bqDataset *bigquery.Dataset) (time.Time, error) {
row := latestExportedAnswerDateRow{
Updated: time.Now().AddDate(-1, 0, 0),
}
q := bqClient.Query(getLatestExportedAnswerDateSQL)
result, err := q.Read(ctx)
if err != nil {
return row.Updated, merry.Wrap(err)
}
err = result.Next(&row)
if err == iterator.Done {
err = nil
}
return row.Updated, merry.Wrap(err)
}