Skip to content

Commit

Permalink
GPTUBE 37: Refactor the code for youtube analysis endpoints (#61)
Browse files Browse the repository at this point in the history
* feat: adding initial performance enhancements

* feat: refactoring the code and handling more accurate number of error messages

* feat: enhanced api handlers logic still having troubles with errors counting for roberta model

* fix: fixed issue with correct number of successfull and errors comments from Analysis for both models

* feat: adding final changes to the Analyze function and deleting some debug logs

* feat: testing

* fea: adding git ignore files

* feat: adding final cases

* feat: handling the final errors handling
  • Loading branch information
webtaken authored Oct 19, 2023
1 parent d4cf2ef commit 004abe4
Show file tree
Hide file tree
Showing 19 changed files with 669 additions and 386 deletions.
7 changes: 6 additions & 1 deletion go-server/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ gptube-firebase-sdk-prod.json
acme.json

# VSCODE
.vscode
.vscode

# VEGETA tests
landing.json
pre_analysis.json
target.list
30 changes: 25 additions & 5 deletions go-server/database/firebase.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ func AddYoutubeResult(results *models.YoutubeAnalyzerRespBody) error {
defer client.Close()

currentTime := time.Now().UTC()
existingResult, err := GetYoutubeResult(results.OwnerEmail, results.VideoID)
existingResult, err := GetYoutubeResult(results.AccountEmail, results.VideoID)
if err != nil {
results.CreatedAt = currentTime
} else {
results.CreatedAt = existingResult.CreatedAt
}
results.LastUpdate = currentTime

userDoc := client.Collection("users").Doc(results.OwnerEmail)
userDoc := client.Collection("users").Doc(results.AccountEmail)
if existingResult != nil {
_, err = userDoc.Update(Ctx, []firestore.Update{
{
Expand All @@ -102,7 +102,7 @@ func AddYoutubeResult(results *models.YoutubeAnalyzerRespBody) error {
})
} else {
_, err = userDoc.Set(Ctx, map[string]interface{}{
"email": results.OwnerEmail,
"email": results.AccountEmail,
"usageLimitYoutube": 1,
})
}
Expand All @@ -119,11 +119,31 @@ func AddYoutubeResult(results *models.YoutubeAnalyzerRespBody) error {

negativeCommentsColl := youtubeDoc.Collection("NegativeComments")
for _, comment := range results.Results.NegativeComments {
_, err = negativeCommentsColl.Doc(comment.CommentID).Set(Ctx, comment)
_, err = negativeCommentsColl.Doc(comment.Id).Get(Ctx)
if err != nil {
log.Printf("Failed to add negative comment: %v", err)
// Comment doesn't exists we need to insert it
fmt.Printf("[AddYoutubeResult] Comment with ID %v doesn't exists inserting...\n", comment.Id)
_, err = negativeCommentsColl.Doc(comment.Id).Set(Ctx, comment)
if err != nil {
log.Printf("[AddYoutubeResult] Failed to add negative comment %v: %v",
comment.Id, err)
}
continue
}
}

// aggregationQuery := negativeCommentsColl.NewAggregationQuery().WithCount("all")
// res, err := aggregationQuery.Get(Ctx)
// if err != nil {
// return err
// }

// count, ok := res["all"]
// if !ok {
// return errors.New("firestore: couldn't get alias for COUNT from results")
// }
// countValue := count.(*firestorepb.Value)
// fmt.Printf("Number of Negative Comments after insertions: %d\n", countValue.GetIntegerValue())
return nil
}

Expand Down
58 changes: 46 additions & 12 deletions go-server/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ const docTemplate = `{
"$ref": "#/definitions/models.YoutubeAnalyzerRespBody"
}
},
"204": {
"description": "No Content",
"schema": {
"$ref": "#/definitions/utils.HandleError.errorResponse"
}
},
"400": {
"description": "Bad Request",
"schema": {
Expand Down Expand Up @@ -265,11 +271,12 @@ const docTemplate = `{
"models.YoutubeAnalyzerReqBody": {
"type": "object",
"properties": {
"email": {
"account_email": {
"description": "The email of the account sending the request",
"type": "string"
},
"owner_email": {
"description": "The email of the account sending the request",
"email": {
"description": "The email that will be used to send the results",
"type": "string"
},
"video_id": {
Expand All @@ -280,6 +287,9 @@ const docTemplate = `{
"models.YoutubeAnalyzerRespBody": {
"type": "object",
"properties": {
"account_email": {
"type": "string"
},
"created_at": {
"type": "string"
},
Expand All @@ -289,16 +299,10 @@ const docTemplate = `{
"last_update": {
"type": "string"
},
"owner_email": {
"type": "string"
},
"results_id": {
"description": "firestore results id",
"type": "string"
},
"snippet": {
"$ref": "#/definitions/youtube.VideoSnippet"
},
"video_id": {
"type": "string"
}
Expand All @@ -315,15 +319,15 @@ const docTemplate = `{
"models.YoutubePreAnalyzerRespBody": {
"type": "object",
"properties": {
"number_of_comments": {
"type": "integer"
},
"requires_email": {
"type": "boolean"
},
"snippet": {
"$ref": "#/definitions/youtube.VideoSnippet"
},
"statistics": {
"$ref": "#/definitions/youtube.VideoStatistics"
},
"video_id": {
"type": "string"
}
Expand Down Expand Up @@ -476,6 +480,36 @@ const docTemplate = `{
"type": "string"
}
}
},
"youtube.VideoStatistics": {
"type": "object",
"properties": {
"commentCount": {
"description": "CommentCount: The number of comments for the video.",
"type": "string",
"example": "0"
},
"dislikeCount": {
"description": "DislikeCount: The number of users who have indicated that they\ndisliked the video by giving it a negative rating.",
"type": "string",
"example": "0"
},
"favoriteCount": {
"description": "FavoriteCount: The number of users who currently have the video\nmarked as a favorite video.",
"type": "string",
"example": "0"
},
"likeCount": {
"description": "LikeCount: The number of users who have indicated that they liked the\nvideo by giving it a positive rating.",
"type": "string",
"example": "0"
},
"viewCount": {
"description": "ViewCount: The number of times the video has been viewed.",
"type": "string",
"example": "0"
}
}
}
}
}`
Expand Down
58 changes: 46 additions & 12 deletions go-server/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
"$ref": "#/definitions/models.YoutubeAnalyzerRespBody"
}
},
"204": {
"description": "No Content",
"schema": {
"$ref": "#/definitions/utils.HandleError.errorResponse"
}
},
"400": {
"description": "Bad Request",
"schema": {
Expand Down Expand Up @@ -259,11 +265,12 @@
"models.YoutubeAnalyzerReqBody": {
"type": "object",
"properties": {
"email": {
"account_email": {
"description": "The email of the account sending the request",
"type": "string"
},
"owner_email": {
"description": "The email of the account sending the request",
"email": {
"description": "The email that will be used to send the results",
"type": "string"
},
"video_id": {
Expand All @@ -274,6 +281,9 @@
"models.YoutubeAnalyzerRespBody": {
"type": "object",
"properties": {
"account_email": {
"type": "string"
},
"created_at": {
"type": "string"
},
Expand All @@ -283,16 +293,10 @@
"last_update": {
"type": "string"
},
"owner_email": {
"type": "string"
},
"results_id": {
"description": "firestore results id",
"type": "string"
},
"snippet": {
"$ref": "#/definitions/youtube.VideoSnippet"
},
"video_id": {
"type": "string"
}
Expand All @@ -309,15 +313,15 @@
"models.YoutubePreAnalyzerRespBody": {
"type": "object",
"properties": {
"number_of_comments": {
"type": "integer"
},
"requires_email": {
"type": "boolean"
},
"snippet": {
"$ref": "#/definitions/youtube.VideoSnippet"
},
"statistics": {
"$ref": "#/definitions/youtube.VideoStatistics"
},
"video_id": {
"type": "string"
}
Expand Down Expand Up @@ -470,6 +474,36 @@
"type": "string"
}
}
},
"youtube.VideoStatistics": {
"type": "object",
"properties": {
"commentCount": {
"description": "CommentCount: The number of comments for the video.",
"type": "string",
"example": "0"
},
"dislikeCount": {
"description": "DislikeCount: The number of users who have indicated that they\ndisliked the video by giving it a negative rating.",
"type": "string",
"example": "0"
},
"favoriteCount": {
"description": "FavoriteCount: The number of users who currently have the video\nmarked as a favorite video.",
"type": "string",
"example": "0"
},
"likeCount": {
"description": "LikeCount: The number of users who have indicated that they liked the\nvideo by giving it a positive rating.",
"type": "string",
"example": "0"
},
"viewCount": {
"description": "ViewCount: The number of times the video has been viewed.",
"type": "string",
"example": "0"
}
}
}
}
}
50 changes: 41 additions & 9 deletions go-server/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,28 @@ definitions:
type: object
models.YoutubeAnalyzerReqBody:
properties:
email:
type: string
owner_email:
account_email:
description: The email of the account sending the request
type: string
email:
description: The email that will be used to send the results
type: string
video_id:
type: string
type: object
models.YoutubeAnalyzerRespBody:
properties:
account_email:
type: string
created_at:
type: string
email:
type: string
last_update:
type: string
owner_email:
type: string
results_id:
description: firestore results id
type: string
snippet:
$ref: '#/definitions/youtube.VideoSnippet'
video_id:
type: string
type: object
Expand All @@ -85,12 +84,12 @@ definitions:
type: object
models.YoutubePreAnalyzerRespBody:
properties:
number_of_comments:
type: integer
requires_email:
type: boolean
snippet:
$ref: '#/definitions/youtube.VideoSnippet'
statistics:
$ref: '#/definitions/youtube.VideoStatistics'
video_id:
type: string
type: object
Expand Down Expand Up @@ -215,6 +214,35 @@ definitions:
youtube.videos.update
type: string
type: object
youtube.VideoStatistics:
properties:
commentCount:
description: 'CommentCount: The number of comments for the video.'
example: "0"
type: string
dislikeCount:
description: |-
DislikeCount: The number of users who have indicated that they
disliked the video by giving it a negative rating.
example: "0"
type: string
favoriteCount:
description: |-
FavoriteCount: The number of users who currently have the video
marked as a favorite video.
example: "0"
type: string
likeCount:
description: |-
LikeCount: The number of users who have indicated that they liked the
video by giving it a positive rating.
example: "0"
type: string
viewCount:
description: 'ViewCount: The number of times the video has been viewed.'
example: "0"
type: string
type: object
host: localhost:8000
info:
contact:
Expand Down Expand Up @@ -257,6 +285,10 @@ paths:
description: OK
schema:
$ref: '#/definitions/models.YoutubeAnalyzerRespBody'
"204":
description: No Content
schema:
$ref: '#/definitions/utils.HandleError.errorResponse'
"400":
description: Bad Request
schema:
Expand Down
Loading

1 comment on commit 004abe4

@vercel
Copy link

@vercel vercel bot commented on 004abe4 Oct 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

gptube-subscriptions – ./subscriptions-server

gptube-subscriptions-git-main-luckly083-gmailcom.vercel.app
gptube-subscriptions-luckly083-gmailcom.vercel.app
gptube-subscriptions.vercel.app

Please sign in to comment.