Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature. re-architecutring pod-restart calendar on dashboard. #111

Merged
merged 1 commit into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions api/swagger/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3905,6 +3905,12 @@ const docTemplate = `{
"domain.ChartData": {
"type": "object",
"properties": {
"podCounts": {
"type": "array",
"items": {
"$ref": "#/definitions/domain.PodCount"
}
},
"series": {
"type": "array",
"items": {
Expand Down Expand Up @@ -5357,6 +5363,17 @@ const docTemplate = `{
}
}
},
"domain.PodCount": {
"type": "object",
"properties": {
"day": {
"type": "integer"
},
"value": {
"type": "integer"
}
}
},
"domain.Role": {
"type": "object",
"properties": {
Expand Down
17 changes: 17 additions & 0 deletions api/swagger/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -3898,6 +3898,12 @@
"domain.ChartData": {
"type": "object",
"properties": {
"podCounts": {
"type": "array",
"items": {
"$ref": "#/definitions/domain.PodCount"
}
},
"series": {
"type": "array",
"items": {
Expand Down Expand Up @@ -5350,6 +5356,17 @@
}
}
},
"domain.PodCount": {
"type": "object",
"properties": {
"day": {
"type": "integer"
},
"value": {
"type": "integer"
}
}
},
"domain.Role": {
"type": "object",
"properties": {
Expand Down
11 changes: 11 additions & 0 deletions api/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ definitions:
type: object
domain.ChartData:
properties:
podCounts:
items:
$ref: '#/definitions/domain.PodCount'
type: array
series:
items:
$ref: '#/definitions/domain.Unit'
Expand Down Expand Up @@ -1217,6 +1221,13 @@ definitions:
totalRows:
type: integer
type: object
domain.PodCount:
properties:
day:
type: integer
value:
type: integer
type: object
domain.Role:
properties:
createdAt:
Expand Down
166 changes: 19 additions & 147 deletions internal/usecase/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,101 +273,6 @@ func (u *DashboardUsecase) getChartFromPrometheus(organizationId string, chartTy
query = "avg by (taco_cluster) (rate(container_network_receive_bytes_total[1h]))"

case domain.ChartType_POD_CALENDAR.String():
/*
// 입력받은 년,월 을 date 형식으로
yearInt, _ := strconv.Atoi(year)
monthInt, _ := strconv.Atoi(month)
startDate := time.Date(yearInt, time.Month(monthInt), 1, 0, 0, 0, 0, time.UTC)
endDate := startDate.Add(time.Hour * 24 * 30)

start := 0
end := 0
if now.Year() < yearInt {
return res, fmt.Errorf("Invalid year")
} else if now.Year() == yearInt && int(now.Month()) < monthInt {
return res, fmt.Errorf("Invalid month")
} else if now.Year() == yearInt && int(now.Month()) == monthInt {
start = int(startDate.Unix())
end = int(now.Unix())
} else {
start = int(startDate.Unix())
end = int(endDate.Unix())
}

log.Debugf("S : %d E : %d", start, end)

query = "sum by (__name__) ({__name__=~\"kube_pod_container_status_restarts_total|kube_pod_status_phase\"})"

result, err := thanosClient.FetchRange(query, start, end, 60*60*24)
if err != nil {
return res, err
}

for _, val := range result.Data.Result {
xAxisData := []string{}
yAxisData := []string{}

for _, vals := range val.Values {
x := int(math.Round(vals.([]interface{})[0].(float64)))
y, err := strconv.ParseFloat(vals.([]interface{})[1].(string), 32)
if err != nil {
y = 0
}
xAxisData = append(xAxisData, strconv.Itoa(x))
yAxisData = append(yAxisData, fmt.Sprintf("%d", int(y)))
}

if val.Metric.Name == "kube_pod_container_status_restarts_total" {
chartData.XAxis.Data = xAxisData
chartData.Series = append(chartData.Series, domain.Unit{
Name: "date",
Data: xAxisData,
})
chartData.Series = append(chartData.Series, domain.Unit{
Name: "podRestartCount",
Data: yAxisData,
})
}

if val.Metric.Name == "kube_pod_status_phase" {
chartData.Series = append(chartData.Series, domain.Unit{
Name: "totalPodCount",
Data: yAxisData,
})
}
}


{
series : [
{
name : date,
data : [
"timestamp1",
"timestamp2"
"timestamp3"
]
},
{
name : podRestartCount,
data : [
"1",
"2"
"3"
]
},
{
name : totalPodCount,
data : [
"10",
"20"
"30"
]
},
]
}
*/

// 입력받은 년,월 을 date 형식으로
yearInt, _ := strconv.Atoi(year)
monthInt, _ := strconv.Atoi(month)
Expand All @@ -385,76 +290,41 @@ func (u *DashboardUsecase) getChartFromPrometheus(organizationId string, chartTy
return res, err
}

xAxisData := []string{}
yAxisData := []string{}
organization, err := u.organizationRepo.Get(organizationId)
if err != nil {
return res, err
}

log.Info(organization.CreatedAt.Format("2006-01-02"))

podCounts := []domain.PodCount{}
for day := rangeDate(startDate, endDate); ; {
d := day()
if d.IsZero() {
break
}
baseDate := d.Format("2006-01-02")

cntPodRestartStr := ""
baseDate := d.Format("2006-01-02")
cntPodRestart := 0

if baseDate <= now.Format("2006-01-02") {
if baseDate <= now.Format("2006-01-02") && baseDate >= organization.CreatedAt.Format("2006-01-02") {
for _, alert := range alerts {
strDate := alert.CreatedAt.Format("2006-01-02")

if strDate == baseDate {
cntPodRestart += 1
}
}
cntPodRestartStr = fmt.Sprintf("%d", int(cntPodRestart))
}

dd := time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, time.UTC)
xAxisData = append(xAxisData, strconv.Itoa(int(dd.Unix())))
yAxisData = append(yAxisData, cntPodRestartStr)
}

chartData.XAxis.Data = xAxisData
chartData.Series = append(chartData.Series, domain.Unit{
Name: "podRestartCount",
Data: yAxisData,
})

/*
for _, alert := range alerts {
xAxisData := []string{}
yAxisData := []string{}

for _, vals := range val.Values {
x := int(math.Round(vals.([]interface{})[0].(float64)))
y, err := strconv.ParseFloat(vals.([]interface{})[1].(string), 32)
if err != nil {
y = 0
}
xAxisData = append(xAxisData, strconv.Itoa(x))
yAxisData = append(yAxisData, fmt.Sprintf("%d", int(y)))
}

if val.Metric.Name == "kube_pod_container_status_restarts_total" {
chartData.XAxis.Data = xAxisData
chartData.Series = append(chartData.Series, domain.Unit{
Name: "date",
Data: xAxisData,
})
chartData.Series = append(chartData.Series, domain.Unit{
Name: "podRestartCount",
Data: yAxisData,
})
}

if val.Metric.Name == "kube_pod_status_phase" {
chartData.Series = append(chartData.Series, domain.Unit{
Name: "totalPodCount",
Data: yAxisData,
})
pd := domain.PodCount{
Day: d.Day(),
Value: int(cntPodRestart),
}
podCounts = append(podCounts, pd)
}
*/
}
chartData.XAxis = nil
chartData.YAxis = nil
chartData.PodCounts = podCounts

return domain.DashboardChart{
ChartType: domain.ChartType_POD_CALENDAR,
Expand Down Expand Up @@ -508,7 +378,9 @@ func (u *DashboardUsecase) getChartFromPrometheus(organizationId string, chartTy
Name: clusterName,
Data: yAxisData,
})

}
chartData.XAxis = &domain.Axis{}
chartData.XAxis.Data = xAxisData

return domain.DashboardChart{
Expand Down
12 changes: 9 additions & 3 deletions pkg/domain/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,16 @@ type Axis struct {
Data []string `json:"data"`
}

type PodCount struct {
Day int `json:"day"`
Value int `json:"value"`
}

type ChartData struct {
XAxis Axis `json:"xAxis"`
YAxis Axis `json:"yAxis"`
Series []Unit `json:"series"`
XAxis *Axis `json:"xAxis,omitempty"`
YAxis *Axis `json:"yAxis,omitempty"`
Series []Unit `json:"series,omitempty"`
PodCounts []PodCount `json:"podCounts,omitempty"`
}

type DashboardChartResponse struct {
Expand Down