feat(models): add source metadata to analysis result

Closes #7
This commit is contained in:
Mikhail Chusavitin
2026-02-04 10:09:15 +03:00
parent d38d0c9d30
commit 596eda709c
4 changed files with 221 additions and 33 deletions

View File

@@ -62,6 +62,7 @@ func (s *Server) handleUpload(w http.ResponseWriter, r *http.Request) {
}
result := p.Result()
applyArchiveSourceMetadata(result)
s.SetResult(result)
s.SetDetectedVendor(p.DetectedVendor())
@@ -114,18 +115,31 @@ func (s *Server) handleGetSensors(w http.ResponseWriter, r *http.Request) {
func (s *Server) handleGetConfig(w http.ResponseWriter, r *http.Request) {
result := s.GetResult()
if result == nil || result.Hardware == nil {
if result == nil {
jsonResponse(w, map[string]interface{}{})
return
}
response := map[string]interface{}{
"source_type": result.SourceType,
"protocol": result.Protocol,
"target_host": result.TargetHost,
"collected_at": result.CollectedAt,
}
if result.Hardware == nil {
response["hardware"] = map[string]interface{}{}
response["specification"] = []SpecLine{}
jsonResponse(w, response)
return
}
// Build specification summary
spec := buildSpecification(result)
jsonResponse(w, map[string]interface{}{
"hardware": result.Hardware,
"specification": spec,
})
response["hardware"] = result.Hardware
response["specification"] = spec
jsonResponse(w, response)
}
// SpecLine represents a single line in specification
@@ -495,9 +509,13 @@ func (s *Server) handleGetStatus(w http.ResponseWriter, r *http.Request) {
}
jsonResponse(w, map[string]interface{}{
"loaded": true,
"filename": result.Filename,
"vendor": s.GetDetectedVendor(),
"loaded": true,
"filename": result.Filename,
"vendor": s.GetDetectedVendor(),
"source_type": result.SourceType,
"protocol": result.Protocol,
"target_host": result.TargetHost,
"collected_at": result.CollectedAt,
"stats": map[string]int{
"events": len(result.Events),
"sensors": len(result.Sensors),
@@ -574,6 +592,8 @@ func (s *Server) handleCollectStart(w http.ResponseWriter, r *http.Request) {
}
job := s.jobManager.CreateJob(req)
s.SetResult(newAPIResult(req))
s.SetDetectedVendor("")
s.startMockCollectionJob(job.ID, req)
w.Header().Set("Content-Type", "application/json")
@@ -726,6 +746,28 @@ func generateJobID() string {
return fmt.Sprintf("job_%x", buf)
}
func applyArchiveSourceMetadata(result *models.AnalysisResult) {
if result == nil {
return
}
result.SourceType = models.SourceTypeArchive
result.Protocol = ""
result.TargetHost = ""
result.CollectedAt = time.Now().UTC()
}
func newAPIResult(req CollectRequest) *models.AnalysisResult {
return &models.AnalysisResult{
SourceType: models.SourceTypeAPI,
Protocol: req.Protocol,
TargetHost: req.Host,
CollectedAt: time.Now().UTC(),
Events: make([]models.Event, 0),
FRU: make([]models.FRUInfo, 0),
Sensors: make([]models.SensorReading, 0),
}
}
func jsonResponse(w http.ResponseWriter, data interface{}) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(data)