1
0
mirror of https://github.com/amir20/dozzle.git synced 2026-01-03 11:35:00 +01:00

Updates events to be more efficent

This commit is contained in:
Amir Raminfar
2020-12-14 15:26:37 -08:00
parent 8fe4f6bbd4
commit 5a7636c535
2 changed files with 27 additions and 11 deletions

View File

@@ -51,9 +51,12 @@ const mutations = {
state.settings = { ...state.settings, ...newValues };
storage.set(DOZZLE_SETTINGS_KEY, state.settings);
},
UPDATE_STAT(state, { container, stat }) {
UPDATE_STAT(_, { container, stat }) {
Vue.set(container, "stat", stat);
},
UPDATE_STATE(_, { container, state }) {
Vue.set(container, "state", state);
},
};
const actions = {
@@ -69,14 +72,23 @@ const actions = {
UPDATE_SETTING({ commit }, setting) {
commit("UPDATE_SETTINGS", setting);
},
UPDATE_STATS({ commit, getters }, stat) {
const { allContainersById } = getters;
UPDATE_STATS({ commit, getters: { allContainersById } }, stat) {
const container = allContainersById[stat.id];
if (container) {
commit("UPDATE_STAT", { container, stat });
}
},
CONTAINER_EVENT({ commit, getters: { allContainersById } }, event) {
switch (event.status) {
case "die":
const container = allContainersById[event.Actor.ID.substr(0, 12)];
commit("UPDATE_STATE", { container, state: "exited" });
break;
default:
}
},
};
const getters = {
allContainersById({ containers }) {
return containers.reduce((map, obj) => {
@@ -96,8 +108,9 @@ const getters = {
const es = new EventSource(`${config.base}/api/events/stream`);
es.addEventListener("containers-changed", (e) => store.commit("SET_CONTAINERS", JSON.parse(e.data)), false);
es.addEventListener("container-stat", (e) => store.dispatch("UPDATE_STATS", JSON.parse(e.data)), false);
es.addEventListener("container-event", (e) => store.dispatch("CONTAINER_EVENT", JSON.parse(e.data)), false);
mql.addListener((e) => store.commit("SET_MOBILE_WIDTH", e.matches));
mql.addEventListener("change", (e) => store.commit("SET_MOBILE_WIDTH", e.matches));
const store = new Vuex.Store({
state,

View File

@@ -177,10 +177,9 @@ func (h *handler) streamEvents(w http.ResponseWriter, r *http.Request) {
select {
case stat := <-stats:
bytes, _ := json.Marshal(stat)
_, err := fmt.Fprintf(w, "event: container-stat\ndata: %s\n\n", string(bytes))
if err != nil {
log.Debugf("Error while writing to event stream: %v", err)
break
if _, err := fmt.Fprintf(w, "event: container-stat\ndata: %s\n\n", string(bytes)); err != nil {
log.Debugf("Error writing stat to event stream: %v", err)
return
}
f.Flush()
case message, ok := <-messages:
@@ -195,11 +194,15 @@ func (h *handler) streamEvents(w http.ResponseWriter, r *http.Request) {
if err := h.client.ContainerStats(ctx, message.Actor.ID, stats); err != nil {
log.Errorf("Error when streaming new container stats: %v", err)
}
if err := sendContainersJSON(h.client, w); err != nil {
log.Errorf("Error encoding containers to stream: %v", err)
return
}
}
time.Sleep(time.Second)
if err := sendContainersJSON(h.client, w); err != nil {
log.Errorf("Error while encoding containers to stream: %v", err)
bytes, _ := json.Marshal(message)
if _, err := fmt.Fprintf(w, "event: container-event\ndata: %s\n\n", string(bytes)); err != nil {
log.Debugf("Error writing event to event stream: %v", err)
return
}