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

fix(ui): reduce webpack bundle size #1790

Merged
merged 7 commits into from
May 30, 2020
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
6 changes: 0 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,7 @@ jobs:
name: Webpack bundle size compare
if: (repo = prymitive/karma AND type = pull_request)
<<: *DEFAULTS_JS
cache:
directories:
- ui/node_modules
- ui/build
env:
# custom cache key
- JOB=bundle
# GITHUB_TOKEN for CI Bundle size diff comments and gist uploads
- secure: "Or5fiXZfgIsXvzoOdEprRwJ0uwUjMvxGHE3LG+h3OsIBO6WA8vgOqVOjqVHV2dgC9cSjm5A7MX52S7cDqfkQDkgnVHpVxRwDC9n9O4vnaFdCZ4nC+d18z8dikbiwgdeWQ+Wi6RhZENye1Lu5sBaAJ09wYgx9lNdEVpRaTUvUw6grSlESJZSXoxfxWWpmTyx+yPH4sxuWjZ7gCspDX9s9k4fjpY4LkhQQwLlk8wPc2hfDg48e+K1OR6sYB8uRS33Xc4fQtzElzazmaZ0fn77h5ysDgC1g/ko+E2j8HHMbZvFpzYpm1bCpIv1G/0A2ItH7gT3HsuwkDvfH/it56JTCbBWJJ+hTDeswCQNu0h797QM6jv0o5wgKpHR1t+AeM9vDe4Ds0pAXouJz0LJewNOdNvi5O1BZA9OooKc34hwTJs/zj5NwiZuOyPSMhDBGa++Vhsr9K3rPD9+97M2hac6NO6TBVWZjvqJilmkjJs+bKrl//ClBvdhDGkJNDbB+2emdD1/wzpPVJPp3IRhzeEF89IVE58qE+OQnIwtbEZ2W1ct6Ep7ZJdrXWc/VBdJB1ELfUtNmkvWFZD5IJfnb/Z5MS6iespXlV5alPQ7eZ2jNl3tn7uDaCStuQN1tO2wthNnsSU/OkfFRch/Ks3gYC5+v7n8aJMkTYFmHr4Y/xlXBsrA="
script:
Expand Down
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ include make/lint-versions.mk

.PHONY: lint
lint: lint-versions
make -f go.mk test
make -C ui lint-js
make -C ui lint-docs

.PHONY: test
test: lint
make -f go.mk test-go
make test-go
make -C ui test-js

.PHONY: clean
Expand Down
6 changes: 3 additions & 3 deletions internal/models/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,21 @@ type Alert struct {
Alertmanager []AlertmanagerInstance `json:"alertmanager"`
Receiver string `json:"receiver"`
// fingerprints are precomputed for speed
labelsFP string `hash:"-"`
LabelsFP string `json:"id" hash:"-"`
contentFP string `hash:"-"`
}

// UpdateFingerprints will generate a new set of fingerprints for this alert
// it should be called after modifying any field that isn't tagged with hash:"-"
func (a *Alert) UpdateFingerprints() {
a.labelsFP = fmt.Sprintf("%x", structhash.Sha1(a.Labels, 1))
a.LabelsFP = fmt.Sprintf("%x", structhash.Sha1(a.Labels, 1))
a.contentFP = fmt.Sprintf("%x", structhash.Sha1(a, 1))
}

// LabelsFingerprint is a checksum computed only from labels which should be
// unique for every alert
func (a *Alert) LabelsFingerprint() string {
return a.labelsFP
return a.LabelsFP
}

// ContentFingerprint is a checksum computed from entire alert object
Expand Down
9 changes: 6 additions & 3 deletions internal/models/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ func TestDedupSharedMaps(t *testing.T) {
"inhibitedBy": null
}
],
"receiver": ""
"receiver": "",
"id": ""
},
{
"annotations": [],
Expand Down Expand Up @@ -158,7 +159,8 @@ func TestDedupSharedMaps(t *testing.T) {
"inhibitedBy": null
}
],
"receiver": ""
"receiver": "",
"id": ""
},
{
"annotations": [],
Expand Down Expand Up @@ -194,7 +196,8 @@ func TestDedupSharedMaps(t *testing.T) {
"inhibitedBy": null
}
],
"receiver": ""
"receiver": "",
"id": ""
}
],
"id": "",
Expand Down
45 changes: 23 additions & 22 deletions ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"bricks.js": "1.8.0",
"copy-to-clipboard": "3.3.1",
"csshake": "1.5.3",
"fast-deep-equal": "3.1.1",
"favico.js": "0.3.10",
"fontfaceobserver": "2.1.0",
"lodash": "4.17.15",
Expand All @@ -34,7 +33,6 @@
"mobx-react-lite": "2.0.7",
"mobx-stored": "1.1.0",
"moment": "2.26.0",
"object-hash": "2.0.3",
"promise-retry": "1.1.1",
"prop-types": "15.7.2",
"qs": "6.9.4",
Expand Down
25 changes: 25 additions & 0 deletions ui/src/Common/Hash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// https://stackoverflow.com/a/58208791/1154047
const normalize = (sortingFunction) => {
return function (key, value) {
if (typeof value === "object" && !Array.isArray(value)) {
return Object.entries(value)
.sort(sortingFunction || undefined)
.reduce((acc, entry) => {
acc[entry[0]] = entry[1];
return acc;
}, {});
}
return value;
};
};

// https://stackoverflow.com/a/15710692/1154047
const hashString = (s) =>
s.split("").reduce((a, b) => {
a = (a << 5) - a + b.charCodeAt(0);
return a & a;
}, 0);

const hashObject = (o) => hashString(JSON.stringify(o, normalize(), 2));

export { normalize, hashString, hashObject };
4 changes: 1 addition & 3 deletions ui/src/Components/Grid/AlertGrid/AlertGroup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import PropTypes from "prop-types";
import { observer } from "mobx-react";
import { observable, action, toJS } from "mobx";

import hash from "object-hash";

import { Fade } from "react-reveal";

import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
Expand Down Expand Up @@ -260,7 +258,7 @@ const AlertGroup = observer(
.slice(0, this.renderConfig.alertsToRender)
.map((alert) => (
<Alert
key={hash(alert.labels)}
key={alert.id}
group={group}
alert={alert}
showAlertmanagers={
Expand Down
9 changes: 4 additions & 5 deletions ui/src/Components/LabelSetList/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import PropTypes from "prop-types";

import { useLocalStore, useObserver } from "mobx-react";

import hash from "object-hash";

import { AlertStore } from "Stores/AlertStore";
import { IsMobile } from "Common/Device";
import { hashObject } from "Common/Hash";
import { StaticLabel } from "Components/Labels/StaticLabel";
import { PageSelect } from "Components/Pagination";

Expand All @@ -22,7 +21,7 @@ const GroupListToUniqueLabelsList = (groups) => {
group.shared.labels,
alert.labels
);
const alertHash = hash(alertLabels);
const alertHash = hashObject(alertLabels);
alerts[alertHash] = alertLabels;
}
}
Expand Down Expand Up @@ -50,9 +49,9 @@ const LabelSetList = ({ alertStore, labelsList }) => {
(pagination.activePage - 1) * maxPerPage,
pagination.activePage * maxPerPage
)
.map((labels) => (
.map((labels, index) => (
<li
key={hash(labels)}
key={`${index}/${labels.length}`}
className="list-group-item px-0 pt-2 pb-1"
>
{Object.entries(labels).map(([name, value]) => (
Expand Down
6 changes: 2 additions & 4 deletions ui/src/Components/ManagedSilence/SilenceDetails.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import React, { useState } from "react";
import PropTypes from "prop-types";

import hash from "object-hash";

import moment from "moment";
import Moment from "react-moment";

Expand Down Expand Up @@ -139,9 +137,9 @@ const SilenceDetails = ({
className="flex-shrink-1 flex-grow-1 mw-1p"
style={{ minWidth: "0px" }}
>
{silence.matchers.map((matcher) => (
{silence.matchers.map((matcher, index) => (
<span
key={hash(matcher)}
key={`${index}/${matcher.name}/${matcher.isRegex}/${matcher.value}`}
className="badge badge-primary px-1 mr-1 components-label"
>
{matcher.name}
Expand Down
6 changes: 2 additions & 4 deletions ui/src/Components/NavBar/FilterInput/History.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import PropTypes from "prop-types";
import { useObserver, useLocalStore } from "mobx-react";
import { localStored } from "mobx-stored";

import hash from "object-hash";

import { Manager, Reference, Popper } from "react-popper";

import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
Expand Down Expand Up @@ -79,10 +77,10 @@ const HistoryMenu = ({
{filters.length === 0 ? (
<h6 className="dropdown-header text-muted text-center">Empty</h6>
) : (
filters.slice(0, maxItems).map((historyFilters) => (
filters.slice(0, maxItems).map((historyFilters, index) => (
<button
className="dropdown-item cursor-pointer px-3"
key={hash(historyFilters)}
key={`${index}/${historyFilters.length}`}
onClick={() => {
alertStore.filters.setFilters(historyFilters.map((f) => f.raw));
afterClick();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import PropTypes from "prop-types";

import { observer } from "mobx-react";

import hash from "object-hash";

import { components } from "react-select";

import Creatable from "react-select/creatable";
Expand All @@ -13,12 +11,13 @@ import { FormatBackendURI } from "Stores/AlertStore";
import { SilenceFormStore } from "Stores/SilenceFormStore";
import { SilenceFormMatcher } from "Models/SilenceForm";
import { useFetchGet } from "Hooks/useFetchGet";
import { hashObject } from "Common/Hash";
import { ValidationError } from "Components/ValidationError";
import { ThemeContext } from "Components/Theme";
import { MatchCounter } from "./MatchCounter";

const GenerateHashFromMatchers = (silenceFormStore, matcher) =>
hash({
hashObject({
alertmanagers: silenceFormStore.data.alertmanagers,
matcher: {
name: matcher.name,
Expand Down
6 changes: 2 additions & 4 deletions ui/src/Components/SilenceModal/SilencePreview/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import React from "react";
import PropTypes from "prop-types";

import { useObserver } from "mobx-react";

import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faArrowLeft } from "@fortawesome/free-solid-svg-icons/faArrowLeft";
import { faCheckCircle } from "@fortawesome/free-solid-svg-icons/faCheckCircle";
Expand Down Expand Up @@ -48,7 +46,7 @@ const SilencePreview = ({ alertStore, silenceFormStore }) => {
FormatBackendURI("alerts.json?") + FormatAlertsQ(filters)
);

return useObserver(() => (
return (
<React.Fragment>
<div className="mb-3">
{isLoading ? (
Expand Down Expand Up @@ -83,7 +81,7 @@ const SilencePreview = ({ alertStore, silenceFormStore }) => {
</button>
</div>
</React.Fragment>
));
);
};
SilencePreview.propTypes = {
alertStore: PropTypes.instanceOf(AlertStore).isRequired,
Expand Down
1 change: 1 addition & 0 deletions ui/src/Models/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const APIAlertAlertmanagerState = PropTypes.exact({
});

const APIAlert = PropTypes.exact({
id: PropTypes.string.isRequired,
annotations: PropTypes.arrayOf(Annotation).isRequired,
labels: PropTypes.object.isRequired,
startsAt: PropTypes.string.isRequired,
Expand Down
Loading