This repository has been archived by the owner on May 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from LF-Engineering/clean-up
Add job logging functionalities
- Loading branch information
Showing
8 changed files
with
402 additions
and
6 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package elasticgap | ||
|
||
// ElasticResponse ... | ||
type ElasticResponse struct { | ||
Took int | ||
Errors bool | ||
Items []ElasticResponseItem | ||
} | ||
|
||
// ElasticResponseItem ... | ||
type ElasticResponseItem struct { | ||
Index ESResponseIndex | ||
} | ||
|
||
// ESResponseIndex ... | ||
type ESResponseIndex struct { | ||
ID string `json:"_id"` | ||
Status int | ||
} |
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,92 @@ | ||
package elasticgap | ||
|
||
import ( | ||
b64 "encoding/base64" | ||
"fmt" | ||
|
||
"github.com/LF-Engineering/insights-datasource-shared/elastic" | ||
jsoniter "github.com/json-iterator/go" | ||
) | ||
|
||
// HTTPClient used in connecting to remote http server | ||
type HTTPClient interface { | ||
Request(url string, method string, header map[string]string, body []byte, params map[string]string) (statusCode int, resBody []byte, err error) | ||
} | ||
|
||
// Auth0Client ... | ||
type Auth0Client interface { | ||
GetToken() (string, error) | ||
} | ||
|
||
// GapHandler ... | ||
type GapHandler struct { | ||
gapURL string | ||
httpClient HTTPClient | ||
auth0Client Auth0Client | ||
} | ||
|
||
// NewGapHandler ... | ||
func NewGapHandler(gapURL string, httpClient HTTPClient, auth0Client Auth0Client) *GapHandler { | ||
return &GapHandler{ | ||
gapURL: gapURL, | ||
httpClient: httpClient, | ||
auth0Client: auth0Client, | ||
} | ||
} | ||
|
||
// Send unsaved data to data-gap handler | ||
func (g *GapHandler) Send(data []elastic.BulkData) error { | ||
token, err := g.auth0Client.GetToken() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
byteData, err := jsoniter.Marshal(data) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
dataEnc := b64.StdEncoding.EncodeToString(byteData) | ||
gapBody := map[string]map[string]string{"index": {"content": dataEnc}} | ||
bData, err := jsoniter.Marshal(gapBody) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
header := make(map[string]string) | ||
header["Authorization"] = fmt.Sprintf("Bearer %s", token) | ||
|
||
if g.gapURL != "" { | ||
_, _, err = g.httpClient.Request(g.gapURL, "POST", header, bData, nil) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// HandleFailedData ... | ||
func (g *GapHandler) HandleFailedData(data []elastic.BulkData, byteResponse []byte) (failedIndexes []elastic.BulkData, err error) { | ||
var esRes ElasticResponse | ||
err = jsoniter.Unmarshal(byteResponse, &esRes) | ||
if err != nil { | ||
return failedIndexes, err | ||
} | ||
|
||
// loop throw elastic response to get failed indexes | ||
for _, item := range esRes.Items { | ||
if item.Index.Status != 200 { | ||
var singleBulk elastic.BulkData | ||
// loop throw real data to get failed ones | ||
for _, el := range data { | ||
if el.ID == item.Index.ID { | ||
singleBulk = el | ||
break | ||
} | ||
} | ||
failedIndexes = append(failedIndexes, singleBulk) | ||
} | ||
} | ||
return failedIndexes, nil | ||
} |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package ingestjob | ||
|
||
import "time" | ||
|
||
// Log ... | ||
type Log struct { | ||
Connector string `json:"connector"` | ||
Configuration []map[string]string `json:"configuration"` | ||
Status string `json:"status"` | ||
CreatedAt time.Time `json:"created_at"` | ||
UpdatedAt time.Time `json:"updated_at"` | ||
Message string `json:"message"` | ||
From *time.Time `json:"from,omitempty"` | ||
To *time.Time `json:"to,omitempty"` | ||
} | ||
|
||
// TopHits result | ||
type TopHits struct { | ||
Hits Hits `json:"hits"` | ||
} | ||
|
||
// Hits result | ||
type Hits struct { | ||
Hits []NestedHits `json:"hits"` | ||
} | ||
|
||
// NestedHits is the actual hit data | ||
type NestedHits struct { | ||
ID string `json:"_id"` | ||
Source Log `json:"_source"` | ||
} |
Oops, something went wrong.