Skip to content

Commit

Permalink
#45 Created a separate RequestReport struct
Browse files Browse the repository at this point in the history
  • Loading branch information
roma-glushko committed Jan 29, 2023
1 parent 78f5b26 commit bbe6341
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 27 deletions.
4 changes: 2 additions & 2 deletions pkg/adapters/writer/request_report_csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewRequestReportCsvWriter() *RequestReportCsvWriter {
}

// Save request report to CSV file
func (w *RequestReportCsvWriter) Save(filePath string, requestReport map[string]*report.RequestReportItem) {
func (w *RequestReportCsvWriter) Save(filePath string, requestReport *report.RequestReport) {
file, err := os.Create(filePath)

if err != nil {
Expand All @@ -40,7 +40,7 @@ func (w *RequestReportCsvWriter) Save(filePath string, requestReport map[string]
}

// Body
for _, requestReportItem := range requestReport {
for _, requestReportItem := range requestReport.Report() {
err := writer.Write([]string{
requestReportItem.Path,
strconv.FormatUint(requestReportItem.Requests, 10),
Expand Down
67 changes: 42 additions & 25 deletions pkg/services/report/request_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,48 @@ type RequestReportItem struct {
RefererURLs map[string]bool
}

type RequestReport struct {
report map[string]*RequestReportItem
mu sync.Mutex
}

func (r *RequestReport) AddRequest(path string, refererURL string, logRecord entity.AccessLogRecord) {
r.mu.Lock()
defer r.mu.Unlock()

if requestRecord, exists := r.report[path]; exists {
requestRecord.Requests++

// collect referer URLs
if _, found := requestRecord.RefererURLs[refererURL]; !found {
requestRecord.RefererURLs[refererURL] = true
}

return
}

r.report[path] = &RequestReportItem{
Path: path,
Requests: 1,
ResponseCode: logRecord.ResponseCode,
RefererURLs: map[string]bool{refererURL: true},
}
}

func (r *RequestReport) Report() map[string]*RequestReportItem {
return r.report
}

func NewRequestReport() *RequestReport {
return &RequestReport{
report: make(map[string]*RequestReportItem),
mu: sync.Mutex{},
}
}

// RequestReportWriter
type RequestReportWriter interface {
Save(reportPath string, browserReport map[string]*RequestReportItem)
Save(reportPath string, requestReport *RequestReport)
}

// RequestReportService
Expand All @@ -35,8 +74,7 @@ func NewRequestReportService(requestReportWriter RequestReportWriter) *RequestRe

// GenerateReport processes access logs and collect request reports
func (u *RequestReportService) GenerateReport(reportPath string, logChan <-chan entity.AccessLogRecord) {
var requestReport = make(map[string]*RequestReportItem)
var mutex sync.Mutex // TODO: try to use sync.Map
requestReport := NewRequestReport()

// todo: move to configs
queryPatterns := []string{
Expand Down Expand Up @@ -85,28 +123,7 @@ func (u *RequestReportService) GenerateReport(reportPath string, logChan <-chan
path = filter.ReplaceAllString(path, "")
}

mutex.Lock()

if _, exists := requestReport[path]; exists {
requestReport[path].Requests++

// collect referer URLs
if _, found := requestReport[path].RefererURLs[refererURL]; !found {
requestReport[path].RefererURLs[refererURL] = true
}

mutex.Unlock()
continue
}

requestReport[path] = &RequestReportItem{
Path: path,
Requests: 1,
ResponseCode: accessRecord.ResponseCode,
RefererURLs: map[string]bool{refererURL: true},
}

mutex.Unlock()
requestReport.AddRequest(path, refererURL, accessRecord)
}
}()
}
Expand Down

0 comments on commit bbe6341

Please sign in to comment.