98 lines
2.5 KiB
Go
98 lines
2.5 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"log"
|
||
|
"net/http"
|
||
|
"sync"
|
||
|
|
||
|
"gitea.urkob.com/urko/ess-etl-go/pkg/crono"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
cr := crono.New()
|
||
|
defer cr.Table()
|
||
|
host := "http://127.0.0.1:3458/graphql"
|
||
|
|
||
|
wg := &sync.WaitGroup{}
|
||
|
errChan := make(chan error, 1)
|
||
|
wg.Add(1)
|
||
|
go func(errChan chan error) {
|
||
|
defer wg.Done()
|
||
|
for i := 0; i < 1000; i++ {
|
||
|
wg.Add(1)
|
||
|
go doRequest(wg, host, 975153, errChan)
|
||
|
}
|
||
|
}(errChan)
|
||
|
|
||
|
go func(errChan chan error) {
|
||
|
if err := <-errChan; err != nil {
|
||
|
log.Fatalln("error process request", err)
|
||
|
}
|
||
|
}(errChan)
|
||
|
|
||
|
wg.Wait()
|
||
|
cr.MarkAndRestart("finish handle all response")
|
||
|
}
|
||
|
|
||
|
type GraphqlQuery struct {
|
||
|
Query string `json:"query"`
|
||
|
OperationName string `json:"operationName"`
|
||
|
Variables interface{} `json:"variables"`
|
||
|
}
|
||
|
|
||
|
type ByEmployeeNumberQueryVariables struct {
|
||
|
EmployeeNumber float64 `json:"employeeNumber"`
|
||
|
}
|
||
|
|
||
|
func doRequest(wg *sync.WaitGroup, host string, employeeID int, errChan chan error) error {
|
||
|
defer wg.Done()
|
||
|
var err error
|
||
|
query := GraphqlQuery{
|
||
|
Query: `query ByEmployeeNumber($employeeNumber: Float!) { byEmployeeNumber(employeeNumber: $employeeNumber) { EmployeeNumber Date WorkInformation { EmployeeNumber Date Shifts { Start End ActualStart ActualEnd RoleCode ShiftCategoryCode _ReferenceId } DaysOff { DayOff { DayOffTypeCode Note _ReferenceId } } } Baselines { Shifts { DayOff { DayOffTypeCode } } BaselineType DaysOff { DayOff { DayOffTypeCode } } FullDayAbsences { FullDayAbsence { AbsenceTypeCode } } } CustomFields { CustomField { FullDayAbsence { AbsenceTypeCode } } } DataVersion FullDayAbsences { FullDayAbsence { AbsenceTypeCode Note _ReferenceId } } } }`,
|
||
|
OperationName: "ByEmployeeNumber",
|
||
|
Variables: ByEmployeeNumberQueryVariables{
|
||
|
EmployeeNumber: float64(employeeID),
|
||
|
},
|
||
|
}
|
||
|
body, err := json.Marshal(query)
|
||
|
if err != nil {
|
||
|
err = fmt.Errorf("json.Marshal: %s", err)
|
||
|
errChan <- err
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
client := &http.Client{}
|
||
|
req, err := http.NewRequest("POST", host, bytes.NewReader(body))
|
||
|
|
||
|
if err != nil {
|
||
|
err = fmt.Errorf("http.NewRequest: %s", err)
|
||
|
errChan <- err
|
||
|
return err
|
||
|
}
|
||
|
req.Header.Add("Content-Type", "application/json")
|
||
|
|
||
|
res, err := client.Do(req)
|
||
|
if err != nil {
|
||
|
err = fmt.Errorf("client.Do: %s", err)
|
||
|
errChan <- err
|
||
|
return err
|
||
|
}
|
||
|
defer res.Body.Close()
|
||
|
|
||
|
//log.Println("readed", string(body))
|
||
|
|
||
|
// body, err = io.ReadAll(res.Body)
|
||
|
// if err != nil {
|
||
|
// err = fmt.Errorf("ioutil.ReadAll: %s", err)
|
||
|
// errChan <- err
|
||
|
// return err
|
||
|
// }
|
||
|
|
||
|
// log.Println("readed", string(body))
|
||
|
|
||
|
return nil
|
||
|
}
|