Skip to content

Commit 29f4a4c

Browse files
committed
added average vulnEvents statistic
1 parent 60620fe commit 29f4a4c

4 files changed

Lines changed: 65 additions & 1 deletion

File tree

controllers/statistics_controller.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,32 @@ func (c *StatisticsController) GetOrgStatistics(ctx shared.Context) error {
267267

268268
topCVEs, err := c.statisticsRepository.GetMostCommonCVEsInOrg(org.ID, 10)
269269
if err != nil {
270-
return err
270+
return echo.NewHTTPError(500, "could not get most common CVEs across org")
271+
}
272+
273+
vulnEventAverages, err := c.statisticsRepository.GetWeeklyAveragePerVulnEventType(org.ID)
274+
if err != nil {
275+
return echo.NewHTTPError(500, "could not get weekly average for vuln events")
276+
}
277+
278+
vulnEventAverageDistribution := dtos.AverageVulnEventsPerWeek{}
279+
for _, average := range vulnEventAverages {
280+
switch average.VulnEventType {
281+
case dtos.EventTypeDetected:
282+
vulnEventAverageDistribution.AverageDetectedEvents = average.Average
283+
case dtos.EventTypeAccepted:
284+
vulnEventAverageDistribution.AverageAcceptedEvents = average.Average
285+
case dtos.EventTypeFalsePositive:
286+
vulnEventAverageDistribution.AverageFalsePositiveEvents = average.Average
287+
case dtos.EventTypeFixed:
288+
vulnEventAverageDistribution.AverageFixedEvents = average.Average
289+
case dtos.EventTypeReopened:
290+
vulnEventAverageDistribution.AverageReopenedEvents = average.Average
291+
}
271292
}
272293

273294
orgStatistics := dtos.OrgOverview{
295+
VulnEventAverage: vulnEventAverageDistribution,
274296
VulnDistribution: distribution,
275297
OrgStructure: structure,
276298
TopProjects: projects,

database/repositories/statistics_repository.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,3 +575,29 @@ func (r *statisticsRepository) GetMostCommonCVEsInOrg(orgID uuid.UUID, limit int
575575
LIMIT 10;`).Find(&topCVEs).Error
576576
return topCVEs, err
577577
}
578+
579+
func (r *statisticsRepository) GetWeeklyAveragePerVulnEventType(orgID uuid.UUID) ([]dtos.VulnEventAverage, error) {
580+
averageByType := []dtos.VulnEventAverage{}
581+
err := r.db.Raw(`
582+
SELECT
583+
type, AVG(count) as weekly_average
584+
FROM(
585+
SELECT
586+
weeks.week,
587+
types.type,
588+
COALESCE(counts.count, 0) AS count
589+
FROM
590+
(SELECT DISTINCT date_trunc('week', created_at) AS week FROM vuln_events) weeks
591+
CROSS JOIN (SELECT DISTINCT type FROM vuln_events) types
592+
LEFT JOIN (
593+
SELECT date_trunc('week', a.created_at) AS week, a.type, COUNT(*)
594+
FROM vuln_events a
595+
LEFT JOIN dependency_vulns b ON a.vuln_id = b.id
596+
LEFT JOIN assets c ON b.asset_id = c.id
597+
LEFT JOIN projects d ON c.project_id = d.id
598+
WHERE d.organization_id = ?
599+
GROUP BY week, a.type
600+
) counts USING (week, type)
601+
) GROUP BY type;`, orgID).Find(&averageByType).Error
602+
return averageByType, err
603+
}

dtos/statistics_dto.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,19 @@ type CVEOccurrencesAcrossOrg struct {
108108
TotalAmountInOrg string `json:"totalAmount" gorm:"column:total_amount"`
109109
}
110110

111+
type VulnEventAverage struct {
112+
VulnEventType VulnEventType `gorm:"column:type"`
113+
Average float32 `gorm:"column:weekly_average"`
114+
}
115+
116+
type AverageVulnEventsPerWeek struct {
117+
AverageDetectedEvents float32 `json:"averageDetectedEvents"`
118+
AverageReopenedEvents float32 `json:"averageReopenedEvents"`
119+
AverageFalsePositiveEvents float32 `json:"averageFalsePositiveEvents"`
120+
AverageAcceptedEvents float32 `json:"averageAcceptedEvents"`
121+
AverageFixedEvents float32 `json:"averageFixedEvents"`
122+
}
123+
111124
type OrgOverview struct {
112125
VulnDistribution VulnDistribution `json:"vulnDistribution"`
113126

@@ -118,4 +131,6 @@ type OrgOverview struct {
118131

119132
TopComponents []ComponentUsageAcrossOrg `json:"topComponents"`
120133
TopCVEs []CVEOccurrencesAcrossOrg `json:"topCVEs"`
134+
135+
VulnEventAverage AverageVulnEventsPerWeek `json:"vulnEventAverage"`
121136
}

shared/common_interfaces.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@ type StatisticsRepository interface {
537537
GetMostVulnerableAssetsInOrg(orgID uuid.UUID, limit int) ([]dtos.VulnDistributionInStructure, error)
538538
GetMostUsedComponentsInOrg(orgID uuid.UUID, limit int) ([]dtos.ComponentUsageAcrossOrg, error)
539539
GetMostCommonCVEsInOrg(orgID uuid.UUID, limit int) ([]dtos.CVEOccurrencesAcrossOrg, error)
540+
GetWeeklyAveragePerVulnEventType(orgID uuid.UUID) ([]dtos.VulnEventAverage, error)
540541
}
541542

542543
type ArtifactRiskHistoryRepository interface {

0 commit comments

Comments
 (0)