Skip to content

Commit

Permalink
fix(api): add x-nullable annotation and fix documentaion in config ha…
Browse files Browse the repository at this point in the history
…ndler (#903)
  • Loading branch information
almostinf authored Oct 16, 2023
1 parent 0b379cb commit 586cc22
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 39 deletions.
2 changes: 1 addition & 1 deletion api/dto/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (p *ProblemOfTarget) hasError() bool {

type TreeOfProblems struct {
SyntaxOk bool `json:"syntax_ok" example:"true"`
TreeOfProblems *ProblemOfTarget `json:"tree_of_problems,omitempty"`
TreeOfProblems *ProblemOfTarget `json:"tree_of_problems,omitempty" extensions:"x-nullable"`
}

// TargetVerification validates trigger targets.
Expand Down
24 changes: 12 additions & 12 deletions api/dto/triggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ var targetNameRegex = regexp.MustCompile("t(\\d+)")
var asteriskPattern = "*"

type TriggersList struct {
Page *int64 `json:"page,omitempty" format:"int64"`
Size *int64 `json:"size,omitempty" format:"int64"`
Total *int64 `json:"total,omitempty" format:"int64"`
Pager *string `json:"pager,omitempty"`
Page *int64 `json:"page,omitempty" format:"int64" extensions:"x-nullable"`
Size *int64 `json:"size,omitempty" format:"int64" extensions:"x-nullable"`
Total *int64 `json:"total,omitempty" format:"int64" extensions:"x-nullable"`
Pager *string `json:"pager,omitempty" extensions:"x-nullable"`
List []moira.TriggerCheck `json:"list"`
}

Expand All @@ -46,23 +46,23 @@ type TriggerModel struct {
// Trigger name
Name string `json:"name" example:"Not enough disk space left"`
// Description string
Desc *string `json:"desc,omitempty" example:"check the size of /var/log"`
Desc *string `json:"desc,omitempty" example:"check the size of /var/log" extensions:"x-nullable"`
// Graphite-like targets: t1, t2, ...
Targets []string `json:"targets" example:"devOps.my_server.hdd.freespace_mbytes"`
// WARN threshold
WarnValue *float64 `json:"warn_value" example:"500"`
WarnValue *float64 `json:"warn_value" example:"500" extensions:"x-nullable"`
// ERROR threshold
ErrorValue *float64 `json:"error_value" example:"1000"`
ErrorValue *float64 `json:"error_value" example:"1000" extensions:"x-nullable"`
// Could be: rising, falling, expression
TriggerType string `json:"trigger_type" example:"rising"`
// Set of tags to manipulate subscriptions
Tags []string `json:"tags" example:"server,disk"`
// When there are no metrics for trigger, Moira will switch metric to TTLState state after TTL seconds
TTLState *moira.TTLState `json:"ttl_state,omitempty" example:"NODATA"`
TTLState *moira.TTLState `json:"ttl_state,omitempty" example:"NODATA" extensions:"x-nullable"`
// When there are no metrics for trigger, Moira will switch metric to TTLState state after TTL seconds
TTL int64 `json:"ttl,omitempty" example:"600" format:"int64"`
// Determines when Moira should monitor trigger
Schedule *moira.ScheduleData `json:"sched,omitempty"`
Schedule *moira.ScheduleData `json:"sched,omitempty" extensions:"x-nullable"`
// Used if you need more complex logic than provided by WARN/ERROR values
Expression string `json:"expression" example:""`
// Graphite patterns for trigger
Expand All @@ -78,9 +78,9 @@ type TriggerModel struct {
// A list of targets that have only alone metrics
AloneMetrics map[string]bool `json:"alone_metrics" example:"t1:true"`
// Datetime when the trigger was created
CreatedAt *time.Time `json:"created_at"`
CreatedAt *time.Time `json:"created_at" extensions:"x-nullable"`
// Datetime when the trigger was updated
UpdatedAt *time.Time `json:"updated_at"`
UpdatedAt *time.Time `json:"updated_at" extensions:"x-nullable"`
// Username who created trigger
CreatedBy string `json:"created_by"`
// Username who updated trigger
Expand Down Expand Up @@ -396,7 +396,7 @@ func (*MetricsMaintenance) Bind(*http.Request) error {
}

type TriggerMaintenance struct {
Trigger *int64 `json:"trigger" example:"1594225165" format:"int64"`
Trigger *int64 `json:"trigger" example:"1594225165" format:"int64" extensions:"x-nullable"`
Metrics map[string]int64 `json:"metrics"`
}

Expand Down
7 changes: 5 additions & 2 deletions api/handler/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package handler
import "net/http"

type ContactExample struct {
Type string `json:"type" example:"telegram"`
Label string `json:"label" example:"Telegram"`
Type string `json:"type" example:"webhook kontur"`
Label string `json:"label" example:"Webhook Kontur"`
Validation string `json:"validation" example:"^(http|https):\\/\\/.*(testkontur.ru|kontur.host|skbkontur.ru)(:[0-9]{2,5})?\\/"`
Placeholder string `json:"placeholder" example:"https://service.testkontur.ru/webhooks/moira"`
Help string `json:"help" example:"### Domains whitelist:\n - skbkontur.ru\n - testkontur.ru\n - kontur.host"`
}

type ConfigurationResponse struct {
Expand Down
7 changes: 4 additions & 3 deletions api/handler/triggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func getAllTriggers(writer http.ResponseWriter, request *http.Request) {
// @success 200 {object} dto.TriggersList "Fetched unused triggers"
// @failure 422 {object} api.ErrorRenderExample "Render error"
// @failure 500 {object} api.ErrorInternalServerExample "Internal server error"
// @router /trigger [get]
// @router /trigger/unused [get]
func getUnusedTriggers(writer http.ResponseWriter, request *http.Request) {
triggersList, errorResponse := controller.GetUnusedTriggerIDs(database)
if errorResponse != nil {
Expand Down Expand Up @@ -288,14 +288,15 @@ func searchTriggers(writer http.ResponseWriter, request *http.Request) {
// nolint: gofmt,goimports
//
// @summary Delete triggers pager
// @id delete-pager
// @tags trigger
// @produce json
// @param pagerID path string true "Pager ID to delete" default(bcba82f5-48cf-44c0-b7d6-e1d32c64a88c)
// @param pagerID query string false "Pager ID" default(bcba82f5-48cf-44c0-b7d6-e1d32c64a88c)
// @success 200 {object} dto.TriggersSearchResultDeleteResponse "Successfully deleted pager"
// @failure 404 {object} api.ErrorNotFoundExample "Resource not found"
// @failure 422 {object} api.ErrorRenderExample "Render error"
// @failure 500 {object} api.ErrorInternalServerExample "Internal server error"
// @router /trigger/pagers/{pagerID} [delete]
// @router /trigger/search/pager [delete]
func deletePager(writer http.ResponseWriter, request *http.Request) {
pagerID := middleware.GetPagerID(request)

Expand Down
42 changes: 21 additions & 21 deletions datatypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ type NotificationEvent struct {
IsTriggerEvent bool `json:"trigger_event,omitempty" example:"true"`
Timestamp int64 `json:"timestamp" example:"1590741878" format:"int64"`
Metric string `json:"metric" example:"carbon.agents.*.metricsReceived"`
Value *float64 `json:"value,omitempty" example:"70"`
Value *float64 `json:"value,omitempty" example:"70" extensions:"x-nullable"`
Values map[string]float64 `json:"values,omitempty"`
State State `json:"state" example:"OK"`
TriggerID string `json:"trigger_id" example:"5ff37996-8927-4cab-8987-970e80d8e0a8"`
SubscriptionID *string `json:"sub_id,omitempty"`
SubscriptionID *string `json:"sub_id,omitempty" extensions:"x-nullable"`
ContactID string `json:"contact_id,omitempty"`
OldState State `json:"old_state" example:"ERROR"`
Message *string `json:"msg,omitempty"`
MessageEventInfo *EventInfo `json:"event_message"`
Message *string `json:"msg,omitempty" extensions:"x-nullable"`
MessageEventInfo *EventInfo `json:"event_message" extensions:"x-nullable"`
}

// NotificationEventHistoryItem is in use to store notifications history of channel
Expand All @@ -70,8 +70,8 @@ type NotificationEventHistoryItem struct {

// EventInfo - a base for creating messages.
type EventInfo struct {
Maintenance *MaintenanceInfo `json:"maintenance,omitempty"`
Interval *int64 `json:"interval,omitempty" example:"0" format:"int64"`
Maintenance *MaintenanceInfo `json:"maintenance,omitempty" extensions:"x-nullable"`
Interval *int64 `json:"interval,omitempty" example:"0" format:"int64" extensions:"x-nullable"`
}

// CreateMessage - creates a message based on EventInfo.
Expand Down Expand Up @@ -274,30 +274,30 @@ const (
type Trigger struct {
ID string `json:"id" example:"292516ed-4924-4154-a62c-ebe312431fce"`
Name string `json:"name" example:"Not enough disk space left"`
Desc *string `json:"desc,omitempty" example:"check the size of /var/log"`
Desc *string `json:"desc,omitempty" example:"check the size of /var/log" extensions:"x-nullable"`
Targets []string `json:"targets" example:"devOps.my_server.hdd.freespace_mbytes"`
WarnValue *float64 `json:"warn_value" example:"5000"`
ErrorValue *float64 `json:"error_value" example:"1000"`
WarnValue *float64 `json:"warn_value" example:"5000" extensions:"x-nullable"`
ErrorValue *float64 `json:"error_value" example:"1000" extensions:"x-nullable"`
TriggerType string `json:"trigger_type" example:"rising"`
Tags []string `json:"tags" example:"server,disk"`
TTLState *TTLState `json:"ttl_state,omitempty" example:"NODATA"`
TTLState *TTLState `json:"ttl_state,omitempty" example:"NODATA" extensions:"x-nullable"`
TTL int64 `json:"ttl,omitempty" example:"600" format:"int64"`
Schedule *ScheduleData `json:"sched,omitempty"`
Expression *string `json:"expression,omitempty" example:""`
PythonExpression *string `json:"python_expression,omitempty"`
Schedule *ScheduleData `json:"sched,omitempty" extensions:"x-nullable"`
Expression *string `json:"expression,omitempty" example:"" extensions:"x-nullable"`
PythonExpression *string `json:"python_expression,omitempty" extensions:"x-nullable"`
Patterns []string `json:"patterns" example:""`
TriggerSource TriggerSource `json:"trigger_source,omitempty" example:"graphite_local"`
MuteNewMetrics bool `json:"mute_new_metrics" example:"false"`
AloneMetrics map[string]bool `json:"alone_metrics" example:"t1:true"`
CreatedAt *int64 `json:"created_at" format:"int64"`
UpdatedAt *int64 `json:"updated_at" format:"int64"`
CreatedAt *int64 `json:"created_at" format:"int64" extensions:"x-nullable"`
UpdatedAt *int64 `json:"updated_at" format:"int64" extensions:"x-nullable"`
CreatedBy string `json:"created_by"`
UpdatedBy string `json:"updated_by"`
}

type TriggerSource string

const (
var (
TriggerSourceNotSet TriggerSource = ""
GraphiteLocal TriggerSource = "graphite_local"
GraphiteRemote TriggerSource = "graphite_remote"
Expand Down Expand Up @@ -395,7 +395,7 @@ type MetricState struct {
Suppressed bool `json:"suppressed" example:"false"`
SuppressedState State `json:"suppressed_state,omitempty"`
Timestamp int64 `json:"timestamp" example:"1590741878" format:"int64"`
Value *float64 `json:"value,omitempty" example:"70"`
Value *float64 `json:"value,omitempty" example:"70" extensions:"x-nullable"`
Values map[string]float64 `json:"values,omitempty"`
Maintenance int64 `json:"maintenance,omitempty" example:"0" format:"int64"`
MaintenanceInfo MaintenanceInfo `json:"maintenance_info"`
Expand All @@ -418,10 +418,10 @@ func (metricState *MetricState) GetMaintenance() (MaintenanceInfo, int64) {

// MaintenanceInfo represents user and time set/unset maintenance
type MaintenanceInfo struct {
StartUser *string `json:"setup_user"`
StartTime *int64 `json:"setup_time" example:"0" format:"int64"`
StopUser *string `json:"remove_user"`
StopTime *int64 `json:"remove_time" example:"0" format:"int64"`
StartUser *string `json:"setup_user" extensions:"x-nullable"`
StartTime *int64 `json:"setup_time" example:"0" format:"int64" extensions:"x-nullable"`
StopUser *string `json:"remove_user" extensions:"x-nullable"`
StopTime *int64 `json:"remove_time" example:"0" format:"int64" extensions:"x-nullable"`
}

// Set maintanace start and stop users and times
Expand Down

0 comments on commit 586cc22

Please sign in to comment.