-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrack.go
451 lines (391 loc) · 13.9 KB
/
track.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
// Copyright 2024 Cisco Systems, Inc. and its affiliates
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package instruqt
import (
"fmt"
"time"
graphql "github.com/hasura/go-graphql-client"
)
// trackQuery represents the GraphQL query structure for retrieving a single
// track by its track ID.
type trackQuery struct {
Track `graphql:"track(trackID: $trackId)"`
}
// sandboxTrackQuery represents the GraphQL query structure for retrieving
// a user's specific track along with its challenges by track ID, user ID, and organization slug.
type sandboxTrackQuery struct {
Track SandboxTrack `graphql:"track(trackID: $trackId, userID: $userId, organizationSlug: $organizationSlug)"`
}
// trackQueryBySlug represents the GraphQL query structure for retrieving a single
// track by its slug and team slug.
type trackQueryBySlug struct {
Track `graphql:"track(trackSlug: $trackSlug, teamSlug: $teamSlug)"`
}
// tracksQuery represents the GraphQL query structure for retrieving all tracks
// associated with a specific organization slug.
type tracksQuery struct {
Tracks []Track `graphql:"tracks(organizationSlug: $organizationSlug)"`
}
// Track represents the data structure for an Instruqt track.
type Track struct {
Slug string // The slug identifier for the track.
Id string // The unique identifier for the track.
Icon string // The icon associated with the track.
Title string // The title of the track.
Description string // The description of the track.
Teaser string // A teaser or short description of the track.
Level string // The difficulty level of the track.
Embed_Token string // The token used for embedding the track.
CreatedAt time.Time // Timestamp of when track was created.
DeletedAt time.Time // Timestamp of when track was deleted.
Last_Update time.Time // Timestamp of when track has been last updated.
Statistics struct { // Statistics about the track.
Average_review_score float32 // The average review score of the track.
}
TrackTags []TrackTag // A list of tags associated with the track.
TrackReviews struct { // A collection of reviews for the track.
TotalCount int
Nodes []Review
} `graphql:"-"` // Not queried
Challenges []Challenge `graphql:"-"` // A list of challenges associated with the sandbox track, not queried.
}
// TrackTag represents a tag associated with an Instruqt track.
type TrackTag struct {
Value string // The value of the tag.
}
// SandboxTrack represents a track in a sandbox environment, including its details
// and associated challenges.
type SandboxTrack struct {
Id string // The unique identifier for the sandbox track.
Slug string // The slug identifier for the sandbox track.
Icon string // The icon associated with the sandbox track.
Title string // The title of the sandbox track.
Description string // The description of the sandbox track.
Teaser string // A teaser or short description of the sandbox track.
Level string // The difficulty level of the sandbox track.
Embed_Token string // The token used for embedding the sandbox track.
CreatedAt time.Time // Timestamp of when track was created.
DeletedAt time.Time // Timestamp of when track was deleted.
Last_Update time.Time // Timestamp of when track has been last updated.
Statistics struct { // Statistics about the sandbox track.
Average_review_score float32 // The average review score of the sandbox track.
}
TrackTags []TrackTag // A list of tags associated with the track.
TrackReviews struct { // A collection of reviews for the sandbox track.
TotalCount int
Nodes []Review
} `graphql:"-"` /* Not queried */
Challenges []Challenge `graphql:"-"` // A list of challenges associated with the sandbox track, not queried.
Status string // The current status of the sandbox track.
Started time.Time // The timestamp when the sandbox track was started.
Completed time.Time // The timestamp when the sandbox track was completed.
Participant struct { // Information about the participant of the sandbox track.
Id string
}
SandboxConfig *SandboxConfig // The SandboxConfig associated the track.
}
// GetTrackById retrieves a track from Instruqt using its unique track ID.
//
// Parameters:
// - trackId: The unique identifier of the track to retrieve.
// - opts (...Option): Variadic functional options to modify the query behavior.
//
// Returns:
// - Track: The track details if found.
// - error: Any error encountered while retrieving the track.
func (c *Client) GetTrackById(trackId string, opts ...Option) (t Track, err error) {
if trackId == "" {
return t, nil
}
// Initialize default options.
options := &options{}
for _, opt := range opts {
opt(options)
}
var q trackQuery
variables := map[string]interface{}{
"trackId": graphql.String(trackId),
}
if err := c.GraphQLClient.Query(c.Context, &q, variables); err != nil {
return t, err
}
if options.includeChallenges {
challenges, err := c.GetChallenges(trackId)
if err != nil {
return t, fmt.Errorf("failed to fetch challenges for track: %v", err)
}
q.Track.Challenges = challenges
}
if options.includeReviews {
count, reviews, err := c.GetReviews(trackId, opts...)
if err != nil {
return t, fmt.Errorf("failed to fetch reviews for track: %v", err)
}
q.Track.TrackReviews.TotalCount = count
q.Track.TrackReviews.Nodes = reviews
}
return q.Track, nil
}
// GetUserTrackById retrieves a track for a specific user, including its challenges,
// using the user's ID and the track's ID.
//
// Parameters:
// - userId: The unique identifier of the user.
// - trackId: The unique identifier of the track.
// - opts (...Option): Variadic functional options to modify the query behavior.
//
// Returns:
// - SandboxTrack: The track details with challenges if found.
// - error: Any error encountered while retrieving the track.
func (c *Client) GetUserTrackById(userId string, trackId string, opts ...Option) (t SandboxTrack, err error) {
if trackId == "" {
return t, nil
}
// Initialize default options.
options := &options{}
for _, opt := range opts {
opt(options)
}
var q sandboxTrackQuery
variables := map[string]interface{}{
"trackId": graphql.String(trackId),
"userId": graphql.String(userId),
"organizationSlug": graphql.String(c.TeamSlug),
}
if err := c.GraphQLClient.Query(c.Context, &q, variables); err != nil {
return t, err
}
if options.includeChallenges {
challenges, err := c.GetChallenges(trackId)
if err != nil {
return t, fmt.Errorf("failed to fetch challenges for track: %v", err)
}
q.Track.Challenges = challenges
}
if options.includeReviews {
count, reviews, err := c.GetReviews(trackId, opts...)
if err != nil {
return t, fmt.Errorf("failed to fetch reviews for track: %v", err)
}
q.Track.TrackReviews.TotalCount = count
q.Track.TrackReviews.Nodes = reviews
}
return q.Track, nil
}
// GetTrackBySlug retrieves a track from Instruqt using its slug and team slug.
//
// Parameters:
// - trackSlug: The slug identifier of the track to retrieve.
// - opts (...Option): Variadic functional options to modify the query behavior.
//
// Returns:
// - Track: The track details if found.
// - error: Any error encountered while retrieving the track.
func (c *Client) GetTrackBySlug(trackSlug string, opts ...Option) (t Track, err error) {
if trackSlug == "" {
return t, nil
}
// Initialize default options.
options := &options{}
for _, opt := range opts {
opt(options)
}
var q trackQueryBySlug
variables := map[string]interface{}{
"trackSlug": graphql.String(trackSlug),
"teamSlug": graphql.String(c.TeamSlug),
}
if err := c.GraphQLClient.Query(c.Context, &q, variables); err != nil {
return t, err
}
if options.includeChallenges {
challenges, err := c.GetChallenges(q.Track.Id)
if err != nil {
return t, fmt.Errorf("failed to fetch challenges for track: %v", err)
}
q.Track.Challenges = challenges
}
if options.includeReviews {
count, reviews, err := c.GetReviews(q.Track.Id, opts...)
if err != nil {
return t, fmt.Errorf("failed to fetch reviews for track: %v", err)
}
q.Track.TrackReviews.TotalCount = count
q.Track.TrackReviews.Nodes = reviews
}
return q.Track, nil
}
// GetTrackUnlockedChallenge retrieves the first unlocked challenge for a specific
// user's track.
//
// Parameters:
// - userId: The unique identifier of the user.
// - trackId: The unique identifier of the track.
//
// Returns:
// - Challenge: The first unlocked challenge found.
// - error: Any error encountered while retrieving the challenge.
func (c *Client) GetTrackUnlockedChallenge(userId string, trackId string) (challenge Challenge, err error) {
track, err := c.GetUserTrackById(userId, trackId, WithChallenges())
if err != nil {
return challenge, fmt.Errorf("[instruqt.GetTrackUnlockedChallenge] failed to get user track: %v", err)
}
for _, chllg := range track.Challenges {
switch chllg.Status {
case "unlocked", "creating", "created", "started":
return chllg, nil
}
}
return
}
// GetTracks retrieves all tracks associated with the client's team slug.
// Parameters:
// - opts (...Option): Variadic functional options to modify the query behavior.
//
// Returns:
// - []Track: A list of tracks for the team.
// - error: Any error encountered while retrieving the tracks.
func (c *Client) GetTracks(opts ...Option) (tt []Track, err error) {
// Initialize default options.
options := &options{}
for _, opt := range opts {
opt(options)
}
var q tracksQuery
variables := map[string]interface{}{
"organizationSlug": graphql.String(c.TeamSlug),
}
if err := c.GraphQLClient.Query(c.Context, &q, variables); err != nil {
return tt, err
}
if options.includeChallenges {
for _, t := range q.Tracks {
challenges, err := c.GetChallenges(t.Id)
if err != nil {
return tt, fmt.Errorf("failed to fetch challenges for track: %v", err)
}
t.Challenges = challenges
}
}
if options.includeReviews {
for _, t := range q.Tracks {
count, reviews, err := c.GetReviews(t.Id, opts...)
if err != nil {
return tt, fmt.Errorf("failed to fetch reviews for track: %v", err)
}
t.TrackReviews.TotalCount = count
t.TrackReviews.Nodes = reviews
}
}
return q.Tracks, nil
}
// GenerateOneTimePlayToken generates a one-time play token for a specific track.
//
// Parameters:
// - trackId: The unique identifier of the track.
//
// Returns:
// - string: The generated one-time play token.
// - error: Any error encountered while generating the token.
func (c *Client) GenerateOneTimePlayToken(trackId string) (token string, err error) {
var m struct {
GenerateOneTimePlayToken string `graphql:"generateOneTimePlayToken(trackID: $trackID)"`
}
variables := map[string]any{
"trackID": graphql.String(trackId),
}
if err := c.GraphQLClient.Mutate(c.Context, &m, variables); err != nil {
return "", err
}
return m.GenerateOneTimePlayToken, nil
}
// GetReviews retrieves all reviews for a Track
// It accepts optional functional options to include additional fields like 'play'.
//
// Parameters:
// - trackId (string): The unique identifier of the track.
// - opts (...Option): Variadic functional options to modify the query behavior.
//
// Returns:
// - []Review: A list retrieved Reviews. Includes Play if specified.
// - error: An error object if the query fails or the review is not found.
func (c *Client) GetReviews(trackId string, opts ...Option) (count int, reviews []Review, err error) {
// Initialize default options.
options := &options{}
for _, opt := range opts {
opt(options)
}
// Prepare GraphQL variables.
variables := map[string]interface{}{
"trackId": graphql.String(trackId),
}
if options.includePlay {
// Define the extended query struct with 'play'.
var q struct {
TrackReviews struct {
TotalCount int
Nodes []Review
} `graphql:"trackReviews(trackID: $trackId)"`
}
// Execute the query.
if err := c.GraphQLClient.Query(c.Context, &q, variables); err != nil {
return 0, nil, fmt.Errorf("GraphQL query with play failed: %w", err)
}
// Return the fetched Review, which includes Play.
return q.TrackReviews.TotalCount, q.TrackReviews.Nodes, nil
}
// Define the base query struct without 'play'.
var q struct {
TrackReviews struct {
TotalCount int
Nodes []baseReview
} `graphql:"trackReviews(trackID: $trackId)"`
}
// Execute the query.
if err := c.GraphQLClient.Query(c.Context, &q, variables); err != nil {
return 0, nil, fmt.Errorf("GraphQL query failed: %w", err)
}
// Construct the Reviews without Play.
for _, r := range q.TrackReviews.Nodes {
reviews = append(reviews, Review{
baseReview: r,
Play: nil,
})
}
return q.TrackReviews.TotalCount, reviews, nil
}
type challengesQuery struct {
Challenges []Challenge `graphql:"challenges(trackID: $trackId, teamSlug: $teamSlug)"`
}
// GetChallenges retrieves all challenges for a Track using its unique track ID.
//
// Parameters:
// - trackId: The unique identifier of the track to retrieve.
//
// Returns:
// - []Challenge: The list of challenges.
// - error: Any error encountered while retrieving the challenge.
func (c *Client) GetChallenges(trackId string) (ch []Challenge, err error) {
if trackId == "" {
return ch, nil
}
var q challengesQuery
variables := map[string]interface{}{
"trackId": graphql.String(trackId),
"teamSlug": graphql.String(c.TeamSlug),
}
if err := c.GraphQLClient.Query(c.Context, &q, variables); err != nil {
return ch, err
}
return q.Challenges, nil
}