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

[BUGFIX] Fix computed labels #129

Merged
merged 1 commit into from
Mar 1, 2019
Merged
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
47 changes: 30 additions & 17 deletions promgen/static/js/promgen.vue.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,6 @@ var app = new Vue({
}
this.showSilenceForm(event);
},
filterBy: function (source, label) {
let labels = new Set();
for (var a in source.filter(x => x.status.state == 'active')) {
for (var l in source[a].labels) {
if (l == label) {
labels.add(source[a].labels[label]);
}
}
}
return labels;
},
fetchSilences: function () {
let this_ = this;
fetch('/proxy/v1/silences')
Expand Down Expand Up @@ -103,23 +92,47 @@ var app = new Vue({
});

}

},
computed: {
alertLabelsService: function () {
return this.filterBy(this.globalAlerts, 'service');
return new Set(this.globalAlerts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think better to define this process to a method and call it from computed property.
So maintenance will be easily.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I previously wrote it as my own method, but I think that even though it's a few more lines in the computed properly itself, it's much more readable and more maintainable in this format

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like this

  methods: {
    filterByService: function() {
      return new Set(
        this.globalAlerts
          .filter(x => x.status.state == 'active')
          .filter(x => x.labels.service)
          .map(x => x.labels.service)
          .sort()
      )
    },
    filterByProject: function() {
      return new Set(
        this.globalAlerts
          .filter(x => x.status.state == 'active')
          .filter(x => x.labels.project)
          .map(x => x.labels.project)
          .sort()
      )
    },
    filterByAlertName: function() {
      return new Set(
        this.globalAlerts
          .filter(x => x.status.state == 'active')
          .filter(x => x.labels.alertname)
          .map(x => x.labels.alertname)
          .sort()
      )
    }
  },
  computed: {
    alertLabelsService: function() {
      return this.filterByService
    },
    alertLabelsProject: function() {
      return this.filterByProject
    },
    alertLabelsRule: function() {
      return this.filterByAlertName
    },
    silenceLabelsService: function() {
      return this.filterByService
    },
    silenceLabelsProject: function() {
      return this.filterByProject
    },
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry. This code was wrong.

.filter(x => x.status.state == 'active')
.filter(x => x.labels.service)
.map(x => x.labels.service)
.sort()
);
},
alertLabelsProject: function () {
return this.filterBy(this.globalAlerts, 'project');
return new Set(this.globalAlerts
.filter(x => x.status.state == 'active')
.filter(x => x.labels.project)
.map(x => x.labels.project)
.sort()
);
},
alertLabelsRule: function () {
return this.filterBy(this.globalAlerts, 'alertname');
return new Set(this.globalAlerts
.filter(x => x.status.state == 'active')
.filter(x => x.labels.alertname)
.map(x => x.labels.alertname)
.sort()
);
},
silenceLabelsService: function () {
return this.filterBy(this.globalSilences, 'service');
return new Set(this.globalSilences
.filter(x => x.status.state == 'active')
.filter(x => x.labels.service)
.map(x => x.labels.service)
.sort()
);
},
silenceLabelsProject: function () {
return this.filterBy(this.globalSilences, 'project');
return new Set(this.globalSilences
.filter(x => x.status.state == 'active')
.filter(x => x.labels.project)
.map(x => x.labels.project)
.sort()
);
},
filterActiveAlerts: function () {
return this.globalAlerts.filter(alert => alert.status.state == 'active');
Expand Down