From 1d5d0db60b4107712173d7b703033baeba4df23b Mon Sep 17 00:00:00 2001 From: Raiden Shogun Date: Mon, 2 Oct 2023 12:04:29 +0200 Subject: [PATCH] feat: support show growth within 7 days --- .gitignore | 1 + pkg/db/db.go | 2 +- pkg/db/postgres.go | 6 ++---- pkg/db/sqlite.go | 2 +- pkg/handler/flake_chart.html | 42 +++++++++++++++++++++++++++++++----- pkg/handler/handler.go | 9 ++++++-- 6 files changed, 49 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 7f11ee6..1e7c54b 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ testdata/docker-test/testout.json testdata/docker-test/testout.html ./gopogh cmd/gopogh-server/gopogh-server +.vscode \ No newline at end of file diff --git a/pkg/db/db.go b/pkg/db/db.go index 3ab11db..8f8d0c5 100644 --- a/pkg/db/db.go +++ b/pkg/db/db.go @@ -34,7 +34,7 @@ type Datab interface { GetEnvCharts(string, int) (map[string]interface{}, error) - GetOverview() (map[string]interface{}, error) + GetOverview(dataRange int) (map[string]interface{}, error) GetTestCharts(string, string) (map[string]interface{}, error) } diff --git a/pkg/db/postgres.go b/pkg/db/postgres.go index 97454ef..f9af77e 100644 --- a/pkg/db/postgres.go +++ b/pkg/db/postgres.go @@ -424,7 +424,8 @@ func (m *Postgres) GetEnvCharts(env string, testsInTop int) (map[string]interfac } // GetOverview writes the overview charts to a map with the keys summaryAvgFail and summaryTable -func (m *Postgres) GetOverview() (map[string]interface{}, error) { +func (m *Postgres) GetOverview(dateRange int) (map[string]interface{}, error) { + // dateRange is the number of days to use to look for "flaky-est" envs. start := time.Now() // Filters out old data and calculates the average number of failures and average duration per day per environment sqlQuery := ` @@ -442,9 +443,6 @@ func (m *Postgres) GetOverview() (map[string]interface{}, error) { } log.Printf("\nduration metric: took %f seconds to execute SQL query for summary duration and failure charts since start of handler", time.Since(start).Seconds()) - // Number of days to use to look for "flaky-est" envs. - const dateRange = 15 - // Filters out data from prior to 90 days // Then computes average number of fails for each environment for each time frame // Then calculates the change in the average number of fails between the time frames diff --git a/pkg/db/sqlite.go b/pkg/db/sqlite.go index 6e30d50..9a9ae15 100644 --- a/pkg/db/sqlite.go +++ b/pkg/db/sqlite.go @@ -133,6 +133,6 @@ func (m *sqlite) GetTestCharts(_ string, _ string) (map[string]interface{}, erro // GetOverview writes the overview charts to a map with the keys summaryAvgFail and summaryTable // This is not yet supported for sqlite -func (m *sqlite) GetOverview() (map[string]interface{}, error) { +func (m *sqlite) GetOverview(int) (map[string]interface{}, error) { return nil, nil } diff --git a/pkg/handler/flake_chart.html b/pkg/handler/flake_chart.html index 0948c1d..c2c0794 100644 --- a/pkg/handler/flake_chart.html +++ b/pkg/handler/flake_chart.html @@ -38,8 +38,38 @@ return [unescape(keyValue[0]), unescape(keyValue[1])]; })); } - -function createRecentNumberOfFailTable(summaryTable) { +function createDateRangeSelectForFailTable(table){ + let tableCapturedByLambda = table + let dateSelect = document.createElement("select") + dateSelect.innerHTML = " " + dateSelect.addEventListener('click', async function (event) { + // stop the onClick event from propagation to its parent + event.stopPropagation(); + }, false); + dateSelect.addEventListener("change", async function(event){ + // when a new date range is selected + // fetch new data and replace old table with new table + const parent = tableCapturedByLambda.parentElement + const dateRange = dateSelect.options[dateSelect.selectedIndex].value + const response = await fetch("/summary?date_range="+dateRange); + if (!response.ok) { + throw new Error('Network response was not ok'); + } + const data = await response.json(); + let newTable = createRecentNumberOfFailTable(data.summaryTable, dateRange) + parent.replaceChild(newTable, tableCapturedByLambda) + tableCapturedByLambda = newTable + }) + let wrapper = document.createElement("div") + wrapper.style.width = "100%" + wrapper.style.textAlign = "center" + wrapper.innerHTML = "Show Growth within " + wrapper.insertAdjacentElement("beforeend", dateSelect) + wrapper.insertAdjacentHTML("beforeend", " days") + + return wrapper +} +function createRecentNumberOfFailTable(summaryTable, dateRange=15) { const createCell = (elementType, text) => { const element = document.createElement(elementType); element.innerHTML = text; @@ -50,7 +80,7 @@ tableHeaderRow.appendChild(createCell("th", "Rank")); tableHeaderRow.appendChild(createCell("th", "Env Name")).style.textAlign = "left"; tableHeaderRow.appendChild(createCell("th", "Recent Number of Fails")); - tableHeaderRow.appendChild(createCell("th", "Growth (since last 15 days)")); + tableHeaderRow.appendChild(createCell("th", `Growth (since last ${dateRange} days)`)); table.appendChild(tableHeaderRow); const tableBody = document.createElement("tbody"); for (let i = 0; i < summaryTable.length; i++) { @@ -521,8 +551,10 @@ const durationChart = new google.visualization.LineChart(summaryDurContainer); durationChart.draw(durChart, durOptions); - - chartsContainer.appendChild(createRecentNumberOfFailTable(data.summaryTable)) + const table=createRecentNumberOfFailTable(data.summaryTable) + const select=createDateRangeSelectForFailTable(table) + chartsContainer.appendChild(select) + chartsContainer.appendChild(table) } function displayEnvironmentChart(data, query) { diff --git a/pkg/handler/handler.go b/pkg/handler/handler.go index 79ca4e5..93cfdb8 100644 --- a/pkg/handler/handler.go +++ b/pkg/handler/handler.go @@ -120,8 +120,13 @@ func (m *DB) ServeEnvCharts(w http.ResponseWriter, r *http.Request) { } // ServeOverview writes the overview chart for all of the environments to a JSON HTTP response -func (m *DB) ServeOverview(w http.ResponseWriter, _ *http.Request) { - data, err := m.Database.GetOverview() +func (m *DB) ServeOverview(w http.ResponseWriter, r *http.Request) { + dateRange, err := strconv.Atoi(r.URL.Query().Get("date_range")) + if err != nil { + dateRange = 15 + } + + data, err := m.Database.GetOverview(dateRange) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return