From 107112b139eb69e771469f1e3d243780a39494e7 Mon Sep 17 00:00:00 2001 From: martin Date: Mon, 13 Feb 2023 13:46:10 +0100 Subject: [PATCH] feat: use goroutines Signed-off-by: martin --- src/immich/auth.go | 2 +- src/immich/data.go | 54 +++++++++++++++++++++++------------ src/immich/sendbackmessage.go | 6 ++-- src/models/api.go | 10 +++---- src/models/models.go | 4 +-- 5 files changed, 47 insertions(+), 29 deletions(-) diff --git a/src/immich/auth.go b/src/immich/auth.go index cb11915..ef9e221 100644 --- a/src/immich/auth.go +++ b/src/immich/auth.go @@ -45,7 +45,7 @@ func Auth() { if res.StatusCode == 400 { log.Fatalln("Incorrect login") } else { - var result models.Login + var result models.StructLogin if err := json.Unmarshal(body, &result); err != nil { log.Println("Can not unmarshal JSON") } diff --git a/src/immich/data.go b/src/immich/data.go index 1f71431..6c8cd41 100644 --- a/src/immich/data.go +++ b/src/immich/data.go @@ -7,28 +7,45 @@ import ( "io/ioutil" "log" "net/http" + "sync" "github.com/prometheus/client_golang/prometheus" ) +var wg sync.WaitGroup + func Allrequests(r *prometheus.Registry) { - serverversion(r) - - Analyze(r) + wg.Add(1) + go ServerVersion(r) + wg.Add(1) + go Analyze(r) + wg.Wait() } func Analyze(r *prometheus.Registry) { - allusers, err := GetAllUsers() - users, err2 := users() + defer wg.Done() + allusers := make(chan func() (*models.StructAllUsers, error)) + + serverinfo := make(chan func() (*models.StructServerInfo, error)) + wg.Add(1) + go GetAllUsers(allusers) + res1, err := (<-allusers)() + wg.Add(1) + go ServerInfo(serverinfo) + + res2, err2 := (<-serverinfo)() + if err != nil && err2 != nil { } else { - Sendbackmessagepreference(users, allusers, r) + Sendbackmessagepreference(res2, res1, r) } - + close(serverinfo) + close(allusers) } -func GetAllUsers() (*models.AllUsers, error) { +func GetAllUsers(c chan func() (*models.StructAllUsers, error)) { + defer wg.Done() resp, err := Apirequest("/api/user?isAll=true", "GET") if err != nil { if err.Error() == "403" { @@ -48,19 +65,20 @@ func GetAllUsers() (*models.AllUsers, error) { log.Fatalln(err) } else { - var result models.AllUsers + result := new(models.StructAllUsers) if err := json.Unmarshal(body, &result); err != nil { // Parse []byte to go struct pointer log.Println("Can not unmarshal JSON") } - return &result, nil + c <- (func() (*models.StructAllUsers, error) { return result, nil }) } } - return &models.AllUsers{}, err + } -func serverversion(r *prometheus.Registry) { +func ServerVersion(r *prometheus.Registry) { + defer wg.Done() resp, err := Apirequest("/api/server-info/version", "GET") if err != nil { if err.Error() == "403" { @@ -80,7 +98,7 @@ func serverversion(r *prometheus.Registry) { log.Fatalln(err) } else { - var result models.ServerVersion + var result models.StructServerVersion if err := json.Unmarshal(body, &result); err != nil { // Parse []byte to go struct pointer log.Println("Can not unmarshal JSON") } @@ -92,7 +110,8 @@ func serverversion(r *prometheus.Registry) { } -func users() (*models.Users, error) { +func ServerInfo(c chan func() (*models.StructServerInfo, error)) { + defer wg.Done() resp, err := Apirequest("/api/server-info/stats", "GET") if err != nil { if err.Error() == "403" { @@ -113,16 +132,15 @@ func users() (*models.Users, error) { log.Fatalln(err) } else { - var result models.Users + result := new(models.StructServerInfo) if err := json.Unmarshal(body, &result); err != nil { // Parse []byte to go struct pointer log.Println("Can not unmarshal JSON") } - - return &result, nil + c <- (func() (*models.StructServerInfo, error) { return result, nil }) } } - return &models.Users{}, err + } func Apirequest(uri string, method string) (*http.Response, error) { diff --git a/src/immich/sendbackmessage.go b/src/immich/sendbackmessage.go index b8d5271..9f26ead 100644 --- a/src/immich/sendbackmessage.go +++ b/src/immich/sendbackmessage.go @@ -8,7 +8,7 @@ import ( "github.com/prometheus/client_golang/prometheus" ) -func Sendbackmessagepreference(result *models.Users, result2 *models.AllUsers, r *prometheus.Registry) { +func Sendbackmessagepreference(result *models.StructServerInfo, result2 *models.StructAllUsers, r *prometheus.Registry) { total_photos := prometheus.NewGauge(prometheus.GaugeOpts{ Name: "immich_app_total_photos", Help: "The total number of photos", @@ -67,7 +67,7 @@ func Sendbackmessagepreference(result *models.Users, result2 *models.AllUsers, r } -func Sendbackmessageserverversion(result *models.ServerVersion, r *prometheus.Registry) { +func Sendbackmessageserverversion(result *models.StructServerVersion, r *prometheus.Registry) { version := prometheus.NewGauge(prometheus.GaugeOpts{ Name: "version", @@ -81,7 +81,7 @@ func Sendbackmessageserverversion(result *models.ServerVersion, r *prometheus.Re } -func GetName(result string, result2 *models.AllUsers) models.CustomUser { +func GetName(result string, result2 *models.StructAllUsers) models.CustomUser { var myuser models.CustomUser for i := 0; i < len(*result2); i++ { if (*result2)[i].ID == result { diff --git a/src/models/api.go b/src/models/api.go index bb7f468..db5367d 100644 --- a/src/models/api.go +++ b/src/models/api.go @@ -2,7 +2,7 @@ package models import "time" -type Login struct { +type StructLogin struct { AccessToken string `json:"accessToken"` UserID string `json:"userId"` UserEmail string `json:"userEmail"` @@ -12,7 +12,7 @@ type Login struct { ShouldChangePassword bool `json:"shouldChangePassword"` } -type Users struct { +type StructServerInfo struct { Photos int `json:"photos"` Videos int `json:"videos"` UsageByUser []struct { @@ -26,7 +26,7 @@ type Users struct { Usage string `json:"usage"` } -type ServerInfo struct { +type StructDiskInfo struct { DiskAvailable string `json:"diskAvailable"` DiskSize string `json:"diskSize"` DiskUse string `json:"diskUse"` @@ -36,13 +36,13 @@ type ServerInfo struct { DiskUsagePercentage float64 `json:"diskUsagePercentage"` } -type ServerVersion struct { +type StructServerVersion struct { Major int `json:"major"` Minor int `json:"minor"` Patch int `json:"patch"` } -type AllUsers []struct { +type StructAllUsers []struct { ID string `json:"id"` Email string `json:"email"` FirstName string `json:"firstName"` diff --git a/src/models/models.go b/src/models/models.go index ffc0020..97e5567 100644 --- a/src/models/models.go +++ b/src/models/models.go @@ -1,13 +1,13 @@ package models -type User struct { +type StructImmichUser struct { Username string Password string URL string accessToken string } -var myuser User +var myuser StructImmichUser func mask(input string) string { hide := ""