-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactoring * fixing issues * fixing layout * admin stats scheduling * scheduler for admin stats --------- Co-authored-by: Maksym Bilan <>
- Loading branch information
1 parent
00bfb2d
commit f01e14b
Showing
7 changed files
with
158 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,15 @@ | ||
package app | ||
|
||
import "sync" | ||
|
||
type statFunc func(session *Session) *string | ||
type feedbackFunc func(session *Session) []string | ||
|
||
var wg sync.WaitGroup | ||
import ( | ||
"github.com/capymind/internal/helpers" | ||
) | ||
|
||
func handleStats(session *Session) { | ||
totalUserCount := waitForStatFunction(getTotalUserCount, session) | ||
totalActiveUserCount := waitForStatFunction(getTotalActiveUserCount, session) | ||
totalNoteCount := waitForStatFunction(getTotalNoteCount, session) | ||
feedback := waitForFeedback(prepareFeedback, session) | ||
|
||
wg.Wait() | ||
stats := helpers.GetStats(session.Context, session.Locale()) | ||
|
||
if totalUserCount != nil { | ||
setOutputText(*totalUserCount, session) | ||
var finalString string | ||
for _, stat := range stats { | ||
finalString += stat + "\n" | ||
} | ||
if totalActiveUserCount != nil { | ||
setOutputText(*totalActiveUserCount, session) | ||
} | ||
if totalNoteCount != nil { | ||
setOutputText(*totalNoteCount, session) | ||
} | ||
|
||
for _, item := range feedback { | ||
setOutputText(item, session) | ||
} | ||
} | ||
|
||
func waitForStatFunction(statFunc statFunc, session *Session) *string { | ||
wg.Add(1) | ||
ch := make(chan *string) | ||
go func() { | ||
defer wg.Done() | ||
result := statFunc(session) | ||
ch <- result | ||
}() | ||
result := <-ch | ||
return result | ||
} | ||
|
||
func waitForFeedback(feedbackFunc feedbackFunc, session *Session) []string { | ||
wg.Add(1) | ||
ch := make(chan []string) | ||
go func() { | ||
defer wg.Done() | ||
result := feedbackFunc(session) | ||
ch <- result | ||
}() | ||
result := <-ch | ||
return result | ||
setOutputText(finalString, session) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package helpers | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"sync" | ||
|
||
"github.com/capymind/internal/translator" | ||
"github.com/capymind/third_party/firestore" | ||
) | ||
|
||
type statFunc func(ctx *context.Context, locale translator.Locale) *string | ||
type feedbackFunc func(ctx *context.Context, locale translator.Locale) []string | ||
|
||
var wg sync.WaitGroup | ||
|
||
var adminStorage firestore.AdminStorage | ||
var feedbackStorage firestore.FeedbackStorage | ||
|
||
func GetTotalUserCount(ctx *context.Context, locale translator.Locale) *string { | ||
count, err := adminStorage.GetTotalUserCount(ctx) | ||
if err != nil { | ||
log.Printf("[Admin] Error during fetching total user count: %v", err) | ||
return nil | ||
} | ||
message := fmt.Sprintf(translator.Translate(locale, "total_user_count"), count) | ||
return &message | ||
} | ||
|
||
func GetTotalActiveUserCount(ctx *context.Context, locale translator.Locale) *string { | ||
count, err := adminStorage.GetActiveUserCount(ctx) | ||
if err != nil { | ||
log.Printf("[Admin] Error during fetching active user count: %v", err) | ||
return nil | ||
} | ||
message := fmt.Sprintf(translator.Translate(locale, "total_active_user_count"), count) | ||
return &message | ||
} | ||
|
||
func GetTotalNoteCount(ctx *context.Context, locale translator.Locale) *string { | ||
count, err := adminStorage.GetTotalNoteCount(ctx) | ||
if err != nil { | ||
log.Printf("[Admin] Error during fetching total note count: %v", err) | ||
return nil | ||
} | ||
message := fmt.Sprintf(translator.Translate(locale, "total_note_count"), count) | ||
return &message | ||
} | ||
|
||
func GetStats(ctx *context.Context, locale translator.Locale) []string { | ||
totalUserCount := waitForStatFunction(GetTotalUserCount, ctx, locale) | ||
totalActiveUserCount := waitForStatFunction(GetTotalActiveUserCount, ctx, locale) | ||
totalNoteCount := waitForStatFunction(GetTotalNoteCount, ctx, locale) | ||
feedback := waitForFeedback(PrepareFeedback, ctx, locale) | ||
|
||
wg.Wait() | ||
|
||
var array []string | ||
|
||
if totalUserCount != nil { | ||
array = append(array, *totalUserCount) | ||
} | ||
if totalActiveUserCount != nil { | ||
array = append(array, *totalActiveUserCount) | ||
} | ||
if totalNoteCount != nil { | ||
array = append(array, *totalNoteCount) | ||
} | ||
|
||
array = append(array, feedback...) | ||
|
||
return array | ||
} | ||
|
||
func PrepareFeedback(ctx *context.Context, locale translator.Locale) []string { | ||
var array []string | ||
array = append(array, "") | ||
array = append(array, translator.Translate(locale, "feedback_last_week")) | ||
array = append(array, "") | ||
|
||
feedback, err := feedbackStorage.GetFeedbackForLastWeek(ctx) | ||
if err != nil { | ||
log.Printf("[Bot] Error getting feedbacks from firestore, %s", err.Error()) | ||
} | ||
|
||
if len(feedback) == 0 { | ||
array = append(array, translator.Translate(locale, "no_feedback")) | ||
return array | ||
} | ||
|
||
for _, f := range feedback { | ||
array = append(array, *f.User.FirstName+" "+*f.User.LastName+":"+"\n"+f.Feedback.Text+"\n") | ||
} | ||
|
||
return array | ||
} | ||
|
||
func waitForStatFunction(statFunc statFunc, ctx *context.Context, locale translator.Locale) *string { | ||
wg.Add(1) | ||
ch := make(chan *string) | ||
go func() { | ||
defer wg.Done() | ||
result := statFunc(ctx, locale) | ||
ch <- result | ||
}() | ||
result := <-ch | ||
return result | ||
} | ||
|
||
func waitForFeedback(feedbackFunc feedbackFunc, ctx *context.Context, locale translator.Locale) []string { | ||
wg.Add(1) | ||
ch := make(chan []string) | ||
go func() { | ||
defer wg.Done() | ||
result := feedbackFunc(ctx, locale) | ||
ch <- result | ||
}() | ||
result := <-ch | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters