Skip to content
Snippets Groups Projects
Commit 05c2efcb authored by Odin K. Henriksen's avatar Odin K. Henriksen
Browse files

Added a new check to see if filehash data is complete or not, to provide better cached results

parent 6ac6ada2
No related branches found
No related tags found
No related merge requests found
...@@ -16,6 +16,7 @@ func HashIntelligence(c *gin.Context) { ...@@ -16,6 +16,7 @@ func HashIntelligence(c *gin.Context) {
var hashInt []byte var hashInt []byte
var err error var err error
var completeInt bool
hash := strings.TrimSpace(c.Query("hash")) hash := strings.TrimSpace(c.Query("hash"))
...@@ -28,21 +29,23 @@ func HashIntelligence(c *gin.Context) { ...@@ -28,21 +29,23 @@ func HashIntelligence(c *gin.Context) {
fmt.Println("No Cache hit") fmt.Println("No Cache hit")
// Perform the request // Perform the request
hashInt, err = hashSearch(hash) hashInt, err, completeInt = hashSearch(hash)
if err != nil { if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"Error ": "Invalid response from third party API's."}) c.JSON(http.StatusInternalServerError, gin.H{"Error ": "Invalid response from third party API's."})
return return
} }
// Add the data to the database if completeInt {
response, err := utils.Conn.Do("SETEX", "hash:"+hash, utils.CacheDurationHash, hashInt) // Add the data to the database
if err != nil { response, err := utils.Conn.Do("SETEX", "hash:"+hash, utils.CacheDurationHash, hashInt)
fmt.Println("Error adding data to redis:" + err.Error()) if err != nil {
logging.Logerror(err, "Error adding data to redis, hash-intelligence") fmt.Println("Error adding data to redis:" + err.Error())
logging.Logerror(err, "Error adding data to redis, hash-intelligence")
} }
fmt.Println(response) fmt.Println(response)
}
} else { } else {
...@@ -76,7 +79,7 @@ func HashIntelligence(c *gin.Context) { ...@@ -76,7 +79,7 @@ func HashIntelligence(c *gin.Context) {
c.Data(http.StatusOK, "application/json", hashInt) c.Data(http.StatusOK, "application/json", hashInt)
} }
func hashSearch(hash string) (data []byte, err error) { func hashSearch(hash string) (data []byte, err error, complete bool) {
var wg sync.WaitGroup var wg sync.WaitGroup
var responseData [2]utils.FrontendResponse2 var responseData [2]utils.FrontendResponse2
...@@ -98,12 +101,25 @@ func hashSearch(hash string) (data []byte, err error) { ...@@ -98,12 +101,25 @@ func hashSearch(hash string) (data []byte, err error) {
utils.SetResultHash(resultPointer, len(responseData)) utils.SetResultHash(resultPointer, len(responseData))
complete = checkIfIntelligenceCompleteHash(resultResponse, len(responseData))
hashInt, err := json.Marshal(resultResponse) hashInt, err := json.Marshal(resultResponse)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
logging.Logerror(err, "") logging.Logerror(err, "")
return nil, err return nil, err, complete
}
return hashInt, nil, complete
}
func checkIfIntelligenceCompleteHash(jsonData utils.ResultFrontendResponse, size int) (complete bool) {
complete = true
for i := 0; i <= size-1; i++ {
if jsonData.FrontendResponse[i].EN.Status == "Awaiting analysis" || jsonData.FrontendResponse[i].EN.Status == "Error" {
complete = false
}
} }
return hashInt, nil return complete
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment