-
Notifications
You must be signed in to change notification settings - Fork 4
/
item-bq-types.go
293 lines (268 loc) · 10.9 KB
/
item-bq-types.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
package statistics
import (
"encoding/json"
"fmt"
"time"
"cloud.google.com/go/bigquery"
"github.com/bcc-code/bcc-media-platform/backend/common"
"github.com/bcc-code/bcc-media-platform/backend/sqlc"
"github.com/bcc-code/bcc-media-platform/backend/utils"
"github.com/google/uuid"
"github.com/samber/lo"
)
var statsLanguages = []string{"no", "en", "de", "nl"}
func nullStr(s *string) bigquery.NullString {
str := ""
if s != nil {
str = *s
}
return bigquery.NullString{
StringVal: str,
Valid: s != nil,
}
}
func asJsonString[T any](o T) string {
out, _ := json.Marshal(o)
return string(out)
}
// Show is the definition of the Show object
type Show struct {
ID int `json:"id"`
Type string `json:"type"`
Tags string `json:"tags"`
PublicTitle bigquery.NullString `json:"publicTitle"`
Title bigquery.NullString `json:"title"`
Description bigquery.NullString `json:"description"`
ImageIcon bigquery.NullString `json:"image_icon"`
ImageFeatured bigquery.NullString `json:"image_featured"`
ImagePoster bigquery.NullString `json:"image_poster"`
Updated time.Time
Deleted *time.Time
}
func ShowFromCommon(s common.Show, tags []common.Tag) Show {
return Show{
ID: s.ID,
Type: s.Type,
Tags: tagsToJson(tags),
PublicTitle: nullStr(s.PublicTitle.Ptr()),
Title: nullStr(s.Title.GetValueOrNil(statsLanguages)),
Description: nullStr(s.Description.GetValueOrNil(statsLanguages)),
ImageIcon: nullStr(s.Images.GetDefault(statsLanguages, common.ImageStyleIcon)),
ImageFeatured: nullStr(s.Images.GetDefault(statsLanguages, common.ImageStyleFeatured)),
ImagePoster: nullStr(s.Images.GetDefault(statsLanguages, common.ImageStylePoster)),
Updated: time.Now(),
}
}
// Season is the definition of the Season object
type Season struct {
ID int `json:"id"`
Tags string `json:"tags"`
Number int
AgeRating string
PublicTitle bigquery.NullString `json:"publicTitle"`
Title bigquery.NullString `json:"title"`
Description bigquery.NullString `json:"description"`
ShowID int
ImageIcon bigquery.NullString `json:"image_icon"`
ImageFeatured bigquery.NullString `json:"image_featured"`
ImagePoster bigquery.NullString `json:"image_poster"`
Updated time.Time
Deleted *time.Time
}
func SeasonFromCommon(s common.Season, tags []common.Tag) Season {
return Season{
ID: s.ID,
Tags: tagsToJson(tags),
Number: s.Number,
AgeRating: s.AgeRating,
ShowID: s.ShowID,
PublicTitle: nullStr(s.PublicTitle.Ptr()),
Title: nullStr(s.Title.GetValueOrNil(statsLanguages)),
Description: nullStr(s.Description.GetValueOrNil(statsLanguages)),
ImageIcon: nullStr(s.Images.GetDefault(statsLanguages, common.ImageStyleIcon)),
ImageFeatured: nullStr(s.Images.GetDefault(statsLanguages, common.ImageStyleFeatured)),
ImagePoster: nullStr(s.Images.GetDefault(statsLanguages, common.ImageStylePoster)),
Updated: time.Now(),
}
}
type Episode struct {
ID int `json:"id"`
UUID uuid.UUID `json:"uuid"`
Unlisted bool `json:"unlisted"`
Type string `json:"type"`
PreventPublicIndexing bool `json:"preventPublicIndexing"`
SeasonID bigquery.NullInt64 `json:"seasonId"`
PublishDate time.Time `json:"publishDate"`
ProductionDate time.Time `json:"productionDate"`
AvailableFrom time.Time `json:"availableFrom"`
AvailableTo time.Time `json:"availableTo"`
Number int `json:"number"`
ImageIcon bigquery.NullString `json:"image_icon"`
ImageFeatured bigquery.NullString `json:"image_featured"`
ImagePoster bigquery.NullString `json:"image_poster"`
Duration int `json:"duration"`
AgeRating string `json:"ageRating"`
Tags string `json:"tags"`
PublicTitle bigquery.NullString `json:"publicTitle"`
Title bigquery.NullString `json:"title"`
Description bigquery.NullString `json:"description"`
ExtraDescription bigquery.NullString `json:"extraDescription"`
Updated time.Time
Deleted *time.Time
LegacyEpisodeID bigquery.NullString
LegacyProgramID bigquery.NullString
Audience bigquery.NullString `json:"audience"`
ContentType bigquery.NullString `json:"contentType"`
}
func EpisodeFromCommon(e common.Episode, tags []common.Tag) Episode {
legacyEpisodeID := bigquery.NullString{
Valid: e.LegacyID.Valid,
StringVal: fmt.Sprintf("E%d", e.LegacyID.ValueOrZero()),
}
legacyProgramID := bigquery.NullString{
Valid: e.LegacyProgramID.Valid,
StringVal: fmt.Sprintf("P%d", e.LegacyProgramID.ValueOrZero()),
}
return Episode{
ID: e.ID,
UUID: e.UUID,
Unlisted: e.Unlisted(),
Type: e.Type,
PreventPublicIndexing: e.PreventPublicIndexing,
SeasonID: bigquery.NullInt64(e.SeasonID.NullInt64),
PublishDate: e.PublishDate,
ProductionDate: e.ProductionDate,
AvailableFrom: e.AvailableFrom,
AvailableTo: e.AvailableTo,
Number: int(e.Number.Int64),
ImageIcon: nullStr(e.Images.GetDefault(statsLanguages, common.ImageStyleIcon)),
ImageFeatured: nullStr(e.Images.GetDefault(statsLanguages, common.ImageStyleFeatured)),
ImagePoster: nullStr(e.Images.GetDefault(statsLanguages, common.ImageStylePoster)),
Duration: e.Duration,
AgeRating: e.AgeRating,
Tags: tagsToJson(tags),
PublicTitle: nullStr(e.PublicTitle.Ptr()),
Title: nullStr(e.Title.GetValueOrNil(statsLanguages)),
Description: nullStr(e.Description.GetValueOrNil(statsLanguages)),
ExtraDescription: nullStr(e.ExtraDescription.GetValueOrNil(statsLanguages)),
LegacyEpisodeID: legacyEpisodeID,
LegacyProgramID: legacyProgramID,
Updated: time.Now(),
Audience: nullStr(e.Audience.Ptr()),
ContentType: nullStr(e.ContentType.Ptr()),
}
}
type TimedMetadata struct {
ID string `json:"id"`
Type string `json:"type"`
Timestamp float64 `json:"timestamp"`
Title bigquery.NullString `json:"title"`
Description bigquery.NullString `json:"description"`
ChapterType bigquery.NullString `json:"chapterType"`
PersonIDs string `json:"personIds"`
SongID uuid.NullUUID `json:"songId"`
}
func TimedMetadataFromCommon(tm common.TimedMetadata, _ int) TimedMetadata {
return TimedMetadata{
ID: tm.ID.String(),
Type: tm.Type,
Title: nullStr(tm.Title.GetValueOrNil(statsLanguages)),
Description: nullStr(tm.Description.GetValueOrNil(statsLanguages)),
Timestamp: tm.Timestamp,
ChapterType: nullStr(&tm.ChapterType.Value),
PersonIDs: asJsonString(tm.PersonIDs),
SongID: tm.SongID,
}
}
type MediaItem struct {
ID string `json:"id"`
UserCreated string `json:"userCreated" bigquery:"user_created"`
DateCreated time.Time `json:"dateCreated" bigquery:"date_created"`
UserUpdated string `json:"userUpdated" bigquery:"user_updated"`
DateUpdated time.Time `json:"dateUpdated" bigquery:"date_updated"`
Label string `json:"label"`
Title bigquery.NullString `json:"title"`
Description bigquery.NullString `json:"description"`
Type string `json:"type"`
AssetID string `json:"assetId" bigquery:"asset_id"`
ParentEpisode bigquery.NullInt64 `json:"parentEpisode" bigquery:"parent_episode"`
ParentStarts bigquery.NullFloat64 `json:"parentStarts" bigquery:"parent_starts"`
ParentEnds bigquery.NullFloat64 `json:"parentEnds" bigquery:"parent_ends"`
}
func MediaItemFromDb(mi sqlc.Mediaitem, _ int) MediaItem {
return MediaItem{
ID: mi.ID.String(),
UserCreated: mi.UserCreated.UUID.String(),
DateCreated: mi.DateCreated.Time,
UserUpdated: mi.UserUpdated.UUID.String(),
DateUpdated: mi.DateUpdated.Time,
Label: mi.Label,
Title: nullStr(mi.Title.Ptr()),
Description: nullStr(mi.Description.Ptr()),
Type: mi.Type,
AssetID: fmt.Sprint(mi.AssetID.Int64),
ParentEpisode: bigquery.NullInt64(mi.ParentEpisodeID.NullInt64),
ParentStarts: bigquery.NullFloat64(mi.ParentStartsAt),
ParentEnds: bigquery.NullFloat64(mi.ParentEndsAt),
}
}
type Short struct {
ID string `bigquery:"id"`
MediaID string `bigquery:"media_id"`
AssetID string `bigquery:"asset_id"`
Label string `bigquery:"label"`
EpisodeID string `bigquery:"episode_id"`
Status string `bigquery:"status"`
StartsAt bigquery.NullFloat64 `bigquery:"starts_at"`
EndsAt bigquery.NullFloat64 `bigquery:"ends_at"`
Image bigquery.NullString `bigquery:"image"`
DateUpdated time.Time `bigquery:"date_updated"`
Tags string `bigquery:"tags"`
}
func ShortFromCommon(s common.Short, tags []common.Tag) Short {
return Short{
ID: s.ID.String(),
MediaID: s.MediaID.String(),
AssetID: fmt.Sprint(s.AssetID.Int64),
Label: s.Label,
EpisodeID: fmt.Sprint(s.EpisodeID.Int64),
StartsAt: bigquery.NullFloat64(s.StartsAt.NullFloat64),
EndsAt: bigquery.NullFloat64(s.EndsAt.NullFloat64),
Image: nullStr(s.Images.GetDefault([]string{"no"}, common.ImageStyleDefault)),
DateUpdated: s.DateUpdated,
Status: string(s.Status),
Tags: tagsToJson(tags),
}
}
type CalendarEntry struct {
ID string `bigquery:"id"`
EventID bigquery.NullString `bigquery:"event_id"`
Title string `bigquery:"title"`
Start time.Time `bigquery:"start"`
End time.Time `bigquery:"end"`
Type bigquery.NullString `bigquery:"type"`
IsReplay bool `bigquery:"is_replay"`
ItemID bigquery.NullString `bigquery:"item_id"`
}
func CalendarEntryFromCommon(c common.CalendarEntry, _ int) CalendarEntry {
var eventID *string
if c.EventID.Valid {
e := fmt.Sprint(c.EventID.Int64)
eventID = &e
}
return CalendarEntry{
ID: fmt.Sprint(c.ID),
EventID: nullStr(eventID),
Title: c.Title.Get(*utils.FallbackLanguages()),
Start: c.Start,
End: c.End,
Type: nullStr(c.Type.Ptr()),
IsReplay: c.IsReplay,
ItemID: nullIntToBQNullString(c.ItemID),
}
}
func tagsToJson(tags []common.Tag) string {
return asJsonString(lo.Map(tags, func(t common.Tag, _ int) string {
return t.Code
}))
}