feat: use goroutines

Signed-off-by: martin <martin.labat92@gmail.com>
This commit is contained in:
martin 2023-02-13 13:46:10 +01:00
parent 3749b1eef9
commit 107112b139
No known key found for this signature in database
GPG key ID: 4544CC55835A3B9E
5 changed files with 47 additions and 29 deletions

View file

@ -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")
}

View file

@ -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) {

View file

@ -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 {

View file

@ -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"`

View file

@ -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 := ""