feat(query_part): refactoring query part

This commit is contained in:
Torkel Ödegaard 2016-08-15 17:20:45 +02:00
parent befca9bb2f
commit 1418cbbd8b
9 changed files with 80 additions and 65 deletions

View File

@ -39,6 +39,7 @@ func (handler *DefaultResultHandler) Handle(ctx *EvalContext) {
} }
countSeverity(ctx.Rule.Severity) countSeverity(ctx.Rule.Severity)
if ctx.Rule.State != oldState { if ctx.Rule.State != oldState {
handler.log.Info("New state change", "alertId", ctx.Rule.Id, "newState", ctx.Rule.State, "oldState", oldState) handler.log.Info("New state change", "alertId", ctx.Rule.Id, "newState", ctx.Rule.State, "oldState", oldState)

View File

@ -27,18 +27,17 @@ const (
) )
type Item struct { type Item struct {
Id int64 `json:"id"` Id int64 `json:"id"`
OrgId int64 `json:"orgId"` OrgId int64 `json:"orgId"`
PanelLinkId string `json:"panelLinkId"` Type ItemType `json:"type"`
Type ItemType `json:"type"` Title string `json:"title"`
Title string `json:"title"` Text string `json:"text"`
Text string `json:"text"` Metric string `json:"metric"`
Metric string `json:"metric"` AlertId int64 `json:"alertId"`
AlertId int64 `json:"alertId"` UserId int64 `json:"userId"`
UserId int64 `json:"userId"` PrevState string `json:"prevState"`
PrevState string `json:"prevState"` NewState string `json:"newState"`
NewState string `json:"newState"` Timestamp time.Time `json:"timestamp"`
Timestamp time.Time `json:"timestamp"`
Data *simplejson.Json `json:"data"` Data *simplejson.Json `json:"data"`
} }

View File

@ -20,28 +20,30 @@ func addAlertMigrations(mg *Migrator) {
{Name: "frequency", Type: DB_BigInt, Nullable: false}, {Name: "frequency", Type: DB_BigInt, Nullable: false},
{Name: "handler", Type: DB_BigInt, Nullable: false}, {Name: "handler", Type: DB_BigInt, Nullable: false},
{Name: "severity", Type: DB_Text, Nullable: false}, {Name: "severity", Type: DB_Text, Nullable: false},
{Name: "enabled", Type: DB_Bool, Nullable: false}, {Name: "paused", Type: DB_Bool, Nullable: false},
{Name: "silenced", Type: DB_Bool, Nullable: false},
{Name: "execution_error", Type: DB_Text, Nullable: false},
{Name: "last_eval_data", Type: DB_Text, Nullable: false},
{Name: "last_eval_time", Type: DB_DateTime, Nullable: false},
{Name: "created", Type: DB_DateTime, Nullable: false}, {Name: "created", Type: DB_DateTime, Nullable: false},
{Name: "updated", Type: DB_DateTime, Nullable: false}, {Name: "updated", Type: DB_DateTime, Nullable: false},
{Name: "updated_by", Type: DB_BigInt, Nullable: false}, {Name: "updated_by", Type: DB_BigInt, Nullable: false},
{Name: "created_by", Type: DB_BigInt, Nullable: false}, {Name: "created_by", Type: DB_BigInt, Nullable: false},
}, },
Indices: []*Index{
{Cols: []string{"org_id", "id"}, Type: IndexType},
{Cols: []string{"state"}, Type: IndexType},
{Cols: []string{"dashboard_id"}, Type: IndexType},
},
} }
// create table // create table
mg.AddMigration("create alert table v1", NewAddTableMigration(alertV1)) mg.AddMigration("create alert table v1", NewAddTableMigration(alertV1))
alert_heartbeat := Table{ // create indices
Name: "alert_heartbeat", mg.AddMigration("add index alert org_id & id ", NewAddIndexMigration(alertV1, alertV1.Indices[0]))
Columns: []*Column{ mg.AddMigration("add index alert state", NewAddIndexMigration(alertV1, alertV1.Indices[1]))
{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true}, mg.AddMigration("add index alert dashboard_id", NewAddIndexMigration(alertV1, alertV1.Indices[2]))
{Name: "server_id", Type: DB_NVarchar, Length: 50, Nullable: false},
{Name: "created", Type: DB_DateTime, Nullable: false},
{Name: "updated", Type: DB_DateTime, Nullable: false},
},
}
mg.AddMigration("create alert_heartbeat table v1", NewAddTableMigration(alert_heartbeat))
alert_notification := Table{ alert_notification := Table{
Name: "alert_notification", Name: "alert_notification",
@ -54,7 +56,11 @@ func addAlertMigrations(mg *Migrator) {
{Name: "created", Type: DB_DateTime, Nullable: false}, {Name: "created", Type: DB_DateTime, Nullable: false},
{Name: "updated", Type: DB_DateTime, Nullable: false}, {Name: "updated", Type: DB_DateTime, Nullable: false},
}, },
Indices: []*Index{
{Cols: []string{"org_id", "name"}, Type: UniqueIndex},
},
} }
mg.AddMigration("create alert_notification table v1", NewAddTableMigration(alert_notification)) mg.AddMigration("create alert_notification table v1", NewAddTableMigration(alert_notification))
mg.AddMigration("add index alert_notification org_id & name", NewAddIndexMigration(alert_notification, alert_notification.Indices[0]))
} }

View File

@ -12,7 +12,6 @@ func addAnnotationMig(mg *Migrator) {
{Name: "org_id", Type: DB_BigInt, Nullable: false}, {Name: "org_id", Type: DB_BigInt, Nullable: false},
{Name: "alert_id", Type: DB_BigInt, Nullable: true}, {Name: "alert_id", Type: DB_BigInt, Nullable: true},
{Name: "user_id", Type: DB_BigInt, Nullable: true}, {Name: "user_id", Type: DB_BigInt, Nullable: true},
{Name: "panel_link_id", Type: DB_NVarchar, Length: 32, Nullable: false},
{Name: "type", Type: DB_NVarchar, Length: 25, Nullable: false}, {Name: "type", Type: DB_NVarchar, Length: 25, Nullable: false},
{Name: "title", Type: DB_Text, Nullable: false}, {Name: "title", Type: DB_Text, Nullable: false},
{Name: "text", Type: DB_Text, Nullable: false}, {Name: "text", Type: DB_Text, Nullable: false},
@ -25,7 +24,6 @@ func addAnnotationMig(mg *Migrator) {
Indices: []*Index{ Indices: []*Index{
{Cols: []string{"org_id", "alert_id"}, Type: IndexType}, {Cols: []string{"org_id", "alert_id"}, Type: IndexType},
{Cols: []string{"org_id", "type"}, Type: IndexType}, {Cols: []string{"org_id", "type"}, Type: IndexType},
{Cols: []string{"org_id", "panel_link_id"}, Type: IndexType},
{Cols: []string{"timestamp"}, Type: IndexType}, {Cols: []string{"timestamp"}, Type: IndexType},
}, },
} }
@ -35,6 +33,5 @@ func addAnnotationMig(mg *Migrator) {
// create indices // create indices
mg.AddMigration("add index annotation org_id & alert_id ", NewAddIndexMigration(table, table.Indices[0])) mg.AddMigration("add index annotation org_id & alert_id ", NewAddIndexMigration(table, table.Indices[0]))
mg.AddMigration("add index annotation org_id & type", NewAddIndexMigration(table, table.Indices[1])) mg.AddMigration("add index annotation org_id & type", NewAddIndexMigration(table, table.Indices[1]))
mg.AddMigration("add index annotation org_id & panel_link_id ", NewAddIndexMigration(table, table.Indices[2])) mg.AddMigration("add index annotation timestamp", NewAddIndexMigration(table, table.Indices[2]))
mg.AddMigration("add index annotation timestamp", NewAddIndexMigration(table, table.Indices[3]))
} }

View File

@ -0,0 +1,16 @@
package migrations
//
// alert_heartbeat := Table{
// Name: "alert_heartbeat",
// Columns: []*Column{
// {Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
// {Name: "server_id", Type: DB_NVarchar, Length: 50, Nullable: false},
// {Name: "created", Type: DB_DateTime, Nullable: false},
// {Name: "updated", Type: DB_DateTime, Nullable: false},
// },
// }
//
// mg.AddMigration("create alert_heartbeat table v1", NewAddTableMigration(alert_heartbeat))
//
//

View File

@ -64,7 +64,9 @@ export function queryPartEditorDirective($compile, templateSrv) {
$link.html(templateSrv.highlightVariablesAsHtml(newValue)); $link.html(templateSrv.highlightVariablesAsHtml(newValue));
part.updateParam($input.val(), paramIndex); part.updateParam($input.val(), paramIndex);
$scope.$apply($scope.partUpdated); $scope.$apply(() => {
$scope.handleEvent({$event: {name: 'part-param-changed'}});
});
} }
$input.hide(); $input.hide();

View File

@ -167,7 +167,7 @@ export class AlertTabCtrl {
addCondition(type) { addCondition(type) {
var condition = this.buildDefaultCondition(); var condition = this.buildDefaultCondition();
// add to persited model // add to persited model
this.alert.conditions.push(condition); this.panelCtrl.conditions.push(condition);
// add to view model // add to view model
this.conditionModels.push(this.buildConditionModel(condition)); this.conditionModels.push(this.buildConditionModel(condition));
} }

View File

@ -57,11 +57,9 @@
<span>GROUP BY</span> <span>GROUP BY</span>
</label> </label>
<query-part-editor <query-part-editor ng-repeat="part in ctrl.queryModel.groupByParts"
ng-repeat="part in ctrl.queryModel.groupByParts" part="part" class="gf-form-label query-part"
part="part" handle-event="ctrl.handleGroupByPartEvent(part, $index, $event)">
class="gf-form-label query-part"
remove-action="ctrl.removeGroupByPart(part, $index)" part-updated="ctrl.refresh();" get-options="ctrl.getPartOptions(part)">
</query-part-editor> </query-part-editor>
</div> </div>

View File

@ -106,19 +106,11 @@ export class InfluxQueryCtrl extends QueryCtrl {
this.panelCtrl.refresh(); this.panelCtrl.refresh();
} }
removeGroupByPart(part, index) {
this.queryModel.removeGroupByPart(part, index);
this.panelCtrl.refresh();
}
addSelectPart(selectParts, cat, subitem) { addSelectPart(selectParts, cat, subitem) {
this.queryModel.addSelectPart(selectParts, subitem.value); this.queryModel.addSelectPart(selectParts, subitem.value);
this.panelCtrl.refresh(); this.panelCtrl.refresh();
} }
removeSelectPart(selectParts, part) {
}
handleSelectPartEvent(selectParts, part, evt) { handleSelectPartEvent(selectParts, part, evt) {
switch (evt.name) { switch (evt.name) {
case "get-param-options": { case "get-param-options": {
@ -127,9 +119,14 @@ export class InfluxQueryCtrl extends QueryCtrl {
.then(this.transformToSegments(true)) .then(this.transformToSegments(true))
.catch(this.handleQueryError.bind(this)); .catch(this.handleQueryError.bind(this));
} }
case "part-param-changed": {
this.panelCtrl.refresh();
break;
}
case "action-remove-part": { case "action-remove-part": {
this.queryModel.removeSelectPart(selectParts, part); this.queryModel.removeSelectPart(selectParts, part);
this.panelCtrl.refresh(); this.panelCtrl.refresh();
break;
} }
case "get-part-actions": { case "get-part-actions": {
return this.$q.when([{text: 'Remove', value: 'remove-part'}]); return this.$q.when([{text: 'Remove', value: 'remove-part'}]);
@ -137,8 +134,27 @@ export class InfluxQueryCtrl extends QueryCtrl {
} }
} }
selectPartUpdated() { handleGroupByPartEvent(part, index, evt) {
this.panelCtrl.refresh(); switch (evt.name) {
case "get-param-options": {
var tagsQuery = this.queryBuilder.buildExploreQuery('TAG_KEYS');
return this.datasource.metricFindQuery(tagsQuery)
.then(this.transformToSegments(true))
.catch(this.handleQueryError.bind(this));
}
case "part-param-changed": {
this.panelCtrl.refresh();
break;
}
case "action-remove-part": {
this.queryModel.removeGroupByPart(part, index);
this.panelCtrl.refresh();
break;
}
case "get-part-actions": {
return this.$q.when([{text: 'Remove', value: 'remove-part'}]);
}
}
} }
fixTagSegments() { fixTagSegments() {
@ -183,21 +199,6 @@ export class InfluxQueryCtrl extends QueryCtrl {
.catch(this.handleQueryError.bind(this)); .catch(this.handleQueryError.bind(this));
} }
getPartOptions(part) {
if (part.def.type === 'field') {
var fieldsQuery = this.queryBuilder.buildExploreQuery('FIELDS');
return this.datasource.metricFindQuery(fieldsQuery)
.then(this.transformToSegments(true))
.catch(this.handleQueryError.bind(this));
}
if (part.def.type === 'tag') {
var tagsQuery = this.queryBuilder.buildExploreQuery('TAG_KEYS');
return this.datasource.metricFindQuery(tagsQuery)
.then(this.transformToSegments(true))
.catch(this.handleQueryError.bind(true));
}
}
handleQueryError(err) { handleQueryError(err) {
this.error = err.message || 'Failed to issue metric query'; this.error = err.message || 'Failed to issue metric query';
return []; return [];
@ -259,11 +260,6 @@ export class InfluxQueryCtrl extends QueryCtrl {
.catch(this.handleQueryError); .catch(this.handleQueryError);
} }
setFill(fill) {
this.target.fill = fill;
this.panelCtrl.refresh();
}
tagSegmentUpdated(segment, index) { tagSegmentUpdated(segment, index) {
this.tagSegments[index] = segment; this.tagSegments[index] = segment;