From c776278913d9760165906f7a2b4863518d250a14 Mon Sep 17 00:00:00 2001 From: dtookey Date: Fri, 22 Apr 2022 09:04:43 -0400 Subject: [PATCH] started work on a library for pulling project insight data --- .gitignore | 1 + src/insight/insight-connect.go | 86 ++++++++++++++++++++++++++++++++++ src/mercury.go | 10 ++++ 3 files changed, 97 insertions(+) create mode 100644 .gitignore create mode 100644 src/insight/insight-connect.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e995d5d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/tokens/ diff --git a/src/insight/insight-connect.go b/src/insight/insight-connect.go new file mode 100644 index 0000000..62effbd --- /dev/null +++ b/src/insight/insight-connect.go @@ -0,0 +1,86 @@ +package insight + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "net/http" + "os" + "strconv" +) + +const ( + BaseApiUrl = "https://jds.projectinsight.net/api/" +) + +type ( + TimeEntry struct { + ActualHours float64 + ActualHoursFormattedString string + ActualTimeString string + ActualTotal float64 + BillableHours float64 + BillableHoursFormattedString string + BillableTimeString string + BillableTotal float64 + Date string + Description string + ProjectId string `json:"Project_Id,omitempty"` + RateBill float64 + RateBurden float64 + TaskId string `json:"Task_Id,omitempty"` + TimeSheetId string `json:"TimeSheet_Id,omitempty"` + UserId string `json:"User_Id,omitempty"` + } +) + +func Get(urlEndpoint string) { + client := http.Client{} + reqUrl := BaseApiUrl + urlEndpoint + token := getToken("insight.token") + deserializeContainer := make([]TimeEntry, 0, 20000) + + request, err := http.NewRequest("GET", reqUrl, nil) + if err != nil { + log.Fatal(err) + } + + request.Header.Add("api-token", token) + + resp, err := client.Do(request) + if err != nil { + log.Fatal(err) + } + + bodyBytes, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Fatal(err) + } + + err = json.Unmarshal(bodyBytes, &deserializeContainer) + if err != nil { + log.Fatal(err) + } + + for _, entry := range deserializeContainer { + log.Printf("%#v\n", entry) + } + log.Println("Received Entries (qty): " + strconv.Itoa(len(deserializeContainer))) +} + +func getToken(tokenName string) string { + filePath := fmt.Sprintf("./tokens/%s", tokenName) + file, err := os.OpenFile(filePath, os.O_RDONLY, 0755) + if err != nil { + log.Fatal(err) + } + defer file.Close() + + rawBytes, err := ioutil.ReadAll(file) + if err != nil { + log.Fatal(err) + } + + return string(rawBytes) +} diff --git a/src/mercury.go b/src/mercury.go index 9dda677..4b653de 100644 --- a/src/mercury.go +++ b/src/mercury.go @@ -2,16 +2,26 @@ package main import ( "log" + "mercury/src/insight" "mercury/src/mercury" "os" ) func main() { + fetchInsightData() +} + +func processQbBilling() { reportBase := os.Getenv("mercury_path") if len(reportBase) == 0 { log.Fatalln("please set the mercury_path env var. we don't know where to look otherwise") } mercury.ProcessCSVsFromPath(reportBase, "/home/dtookey/work/clarity-reporting/qb_all_bc.csv") +} +func fetchInsightData() { + //jcrouch id = e779e0a9-0e56-4dbe-98dd-e8d0048d109f + //id := "e779e0a9-0e56-4dbe-98dd-e8d0048d109f" + insight.Get("time-entry/search?startDate=2018-01-01&endDate=2022-04-01") }