Skip to content

Commit

Permalink
Merge 7c0df31 into e858a0b
Browse files Browse the repository at this point in the history
  • Loading branch information
vivekratnavel committed Apr 5, 2019
2 parents e858a0b + 7c0df31 commit 61c2bd0
Show file tree
Hide file tree
Showing 23 changed files with 2,754 additions and 2,550 deletions.
5 changes: 5 additions & 0 deletions web/config/jest/momentMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const moment = require.requireActual('moment-timezone');
jest.doMock('moment', () => {
moment.tz.setDefault('Atlantic/Reykjavik');
return moment;
});
3 changes: 3 additions & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@
"jest-canvas-mock",
"jest-localstorage-mock"
],
"setupFilesAfterEnv": [
"<rootDir>/config/jest/momentMock.js"
],
"testMatch": [
"<rootDir>/src/**/__tests__/**/*.{js,jsx,mjs}",
"<rootDir>/src/**/?(*.)(spec|test).{js,jsx,mjs}"
Expand Down
5 changes: 5 additions & 0 deletions web/src/appConstants/app.constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

export const SERVER_TIMEZONE = 'Atlantic/Reykjavik';
export const SETTING_TIMEZONE = 'timezone';
export const AUTO_REFRESH_INTERVAL = 'auto_refresh_interval';
export const DEFAULT_AUTO_REFRESH_INTERVAL = 30;
File renamed without changes.
43 changes: 27 additions & 16 deletions web/src/components/App/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import 'react-toastify/dist/ReactToastify.min.css';
import './style.scss';
import { SettingsModal } from "../SettingsModal/settingsModal";
import moment from 'moment-timezone';

export const SERVER_TIMEZONE = 'Atlantic/Reykjavik';
export const SETTING_TIMEZONE = 'timezone';
import {SETTING_TIMEZONE, AUTO_REFRESH_INTERVAL, DEFAULT_AUTO_REFRESH_INTERVAL} from "../../appConstants/app.constants";

class App extends Component {
constructor(props) {
Expand Down Expand Up @@ -74,6 +72,17 @@ class App extends Component {
});
};

_initializeSetting = (setting, value) => {
axios.post('/api/v1/Omniboard.Settings', {
name: setting,
value
}).then(response => {
if (response.status === 201) {
this._updateGlobalSettings([response.data]);
}
});
};

_fetchData = () => {
axios.all([
axios.get('/api/v1/database'),
Expand All @@ -84,20 +93,22 @@ class App extends Component {
dbName: dbResponse.data.name
});
}
// Write default settings to the database for the first time
// Guess the client timezone and set it as default
const userTimezone = moment.tz.guess();
if (settingsResponse && settingsResponse.data && settingsResponse.data.length) {
this._updateGlobalSettings(settingsResponse.data);
} else {
// Write default settings to the database for the first time
// Guess the client timezone and set it as default
const userTimezone = moment.tz.guess();
axios.post('/api/v1/Omniboard.Settings', {
name: SETTING_TIMEZONE,
value: userTimezone
}).then(response => {
if (response.status === 201) {
this._updateGlobalSettings(response.data);
}
});
const settingsResponseData = settingsResponse.data;
this._updateGlobalSettings(settingsResponseData);
if (!settingsResponseData.some(setting => setting.name === SETTING_TIMEZONE)) {
this._initializeSetting(SETTING_TIMEZONE, userTimezone);
}
if (!settingsResponseData.some(setting => setting.name === AUTO_REFRESH_INTERVAL)) {
this._initializeSetting(AUTO_REFRESH_INTERVAL, DEFAULT_AUTO_REFRESH_INTERVAL);
}
} else if (settingsResponse && settingsResponse.status === 200) {
// if empty response, then initialize all settings
this._initializeSetting(SETTING_TIMEZONE, userTimezone);
this._initializeSetting(AUTO_REFRESH_INTERVAL, DEFAULT_AUTO_REFRESH_INTERVAL);
}
})).catch(error => {
toast.error(parseServerError(error));
Expand Down
45 changes: 36 additions & 9 deletions web/src/components/App/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import App from './index';
import mockAxios from 'jest-mock-axios';
import { toast } from "react-toastify";
import {parseServerError} from "../Helpers/utils";
import * as appConstants from "../../appConstants/app.constants";

describe('App component', () => {
let wrapper = null;
Expand All @@ -18,6 +19,7 @@ describe('App component', () => {
jest.clearAllMocks();
// reset localStorage
localStorage.clear();
React.resetGlobal();
});

it('should render', () => {
Expand Down Expand Up @@ -79,17 +81,42 @@ describe('App component', () => {
});
});

it('should write default settings for the first time', async () => {
const setting = {name: 'timezone', value: 'Atlantic/Reykjavik', _id: 1};
mockAxios.mockResponse({status: 200, data: {name: 'test_db'}});
mockAxios.mockResponse({status: 200, data: []});
await tick();
describe('should write default settings for the first time', async () => {
it('when none of the settings are present', async () => {
const setting = {name: 'timezone', value: 'Atlantic/Reykjavik', _id: 1};
const autoRefreshSetting = {name: 'auto_refresh_interval', value: 30, _id: 2};
mockAxios.mockResponse({status: 200, data: {name: 'test_db'}});
mockAxios.mockResponse({status: 200, data: []});
await tick();

expect(mockAxios.post).toHaveBeenCalledTimes(1);
mockAxios.mockResponse({status: 201, data: [setting]});
expect(mockAxios.post).toHaveBeenCalledTimes(2);
mockAxios.mockResponse({status: 201, data: setting});
mockAxios.mockResponse({status: 201, data: autoRefreshSetting});

await tick();
await tick();

expect(wrapper.update().instance().global.settings[appConstants.SETTING_TIMEZONE]).toEqual(setting);
expect(wrapper.update().instance().global.settings[appConstants.AUTO_REFRESH_INTERVAL]).toEqual(autoRefreshSetting);
});

expect(wrapper.update().instance().global.settings.timezone).toEqual(setting);
it('when only auto refresh setting is present', async () => {
const setting = {name: 'timezone', value: 'Atlantic/Reykjavik', _id: 2};
const autoRefreshSetting = {
name: appConstants.AUTO_REFRESH_INTERVAL,
value: 60,
_id: 1
};
mockAxios.mockResponse({status: 200, data: {name: 'test_db'}});
mockAxios.mockResponse({status: 200, data: [autoRefreshSetting]});
await tick();

expect(mockAxios.post).toHaveBeenCalledTimes(1);
mockAxios.mockResponse({status: 201, data: setting});

await tick();

expect(wrapper.update().instance().global.settings[appConstants.SETTING_TIMEZONE]).toEqual(setting);
expect(wrapper.update().instance().global.settings[appConstants.AUTO_REFRESH_INTERVAL]).toEqual(autoRefreshSetting);
});
});
});
2 changes: 1 addition & 1 deletion web/src/components/CapturedOutView/capturedOutView.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, {Component} from 'react';
import {getRunStatus, parseServerError} from '../Helpers/utils';
import PropTypes from 'prop-types'
import { STATUS } from '../../constants/status.constants';
import { STATUS } from '../../appConstants/status.constants';
import { Glyphicon } from 'react-bootstrap';
import './capturedOutView.scss';
import axios from 'axios';
Expand Down
2 changes: 1 addition & 1 deletion web/src/components/CapturedOutView/capturedOutView.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { CapturedOutView, MAX_ERROR_COUNT } from './capturedOutView';
import mockAxios from 'jest-mock-axios';
import { STATUS } from '../../constants/status.constants';
import { STATUS } from '../../appConstants/status.constants';
import { toast } from "react-toastify";

describe('CapturedOutView', () => {
Expand Down
4 changes: 2 additions & 2 deletions web/src/components/DrillDownView/drillDownView.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import reactMixin from 'react-mixin';
import { ProgressWrapper } from '../Helpers/hoc';
import { parseServerError } from '../Helpers/utils';
import { toast } from 'react-toastify';
import { DRILLDOWN_VIEW } from '../../constants/drillDownView.constants';
import { DRILLDOWN_VIEW } from '../../appConstants/drillDownView.constants';
import { MetricsPlotView } from '../MetricsPlotView/metricsPlotView';
import { SourceFilesView } from '../SourceFilesView/sourceFilesView';
import { CapturedOutView } from '../CapturedOutView/capturedOutView';
import {STATUS} from "../../constants/status.constants";
import {STATUS} from "../../appConstants/status.constants";

class JsonView extends React.PureComponent {
static propTypes = {
Expand Down
4 changes: 2 additions & 2 deletions web/src/components/DrillDownView/drillDownView.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from 'react';
import { DrillDownView } from './drillDownView';
import mockAxios from 'jest-mock-axios';
import { DRILLDOWN_VIEW } from '../../constants/drillDownView.constants';
import { DRILLDOWN_VIEW } from '../../appConstants/drillDownView.constants';
import { toast } from 'react-toastify';
import { parseServerError } from '../Helpers/utils';
import * as events from 'dom-helpers/events';
import {STATUS} from "../../constants/status.constants";
import {STATUS} from "../../appConstants/status.constants";

describe('DrillDownView', () => {
let wrapper = null;
Expand Down
9 changes: 4 additions & 5 deletions web/src/components/Helpers/cells.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import axios from 'axios';
import { toast } from 'react-toastify';
import ms from 'ms';
import moment from 'moment-timezone';
import { SERVER_TIMEZONE, SETTING_TIMEZONE } from '../App/index';
import * as appConstants from "../../appConstants/app.constants";


const SortTypes = {
Expand Down Expand Up @@ -90,10 +90,9 @@ class DateCell extends React.PureComponent {
render() {
const {data, rowIndex, columnKey, ...props} = this.props;
let dateValue = data.getObjectAt(rowIndex)[columnKey];
if (this.global.settings && this.global.settings[SETTING_TIMEZONE]) {
const userTimezone = this.global.settings[SETTING_TIMEZONE].value;
dateValue = moment(moment.tz(moment.tz(dateValue, SERVER_TIMEZONE), userTimezone).toArray())
.format('YYYY-MM-DDTHH:mm:ss');
if (this.global.settings && this.global.settings[appConstants.SETTING_TIMEZONE]) {
const userTimezone = this.global.settings[appConstants.SETTING_TIMEZONE].value;
dateValue = moment.tz(moment.tz(dateValue, appConstants.SERVER_TIMEZONE), userTimezone).format('YYYY-MM-DDTHH:mm:ss');
}
return (
<Cell test-attr="date-cell" {...props}>
Expand Down
2 changes: 1 addition & 1 deletion web/src/components/Helpers/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {PROBABLY_DEAD_TIMEOUT, STATUS} from "../../constants/status.constants";
import {PROBABLY_DEAD_TIMEOUT, STATUS} from "../../appConstants/status.constants";

export const capitalize = (value) => {
// Capitalize each word
Expand Down
2 changes: 1 addition & 1 deletion web/src/components/MetricsPlotView/metricsPlotView.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, {Component} from 'react';
import { SCALE_VALUE, SCALE_VALUES, X_AXIS_VALUES } from '../../constants/drillDownView.constants';
import { SCALE_VALUE, SCALE_VALUES, X_AXIS_VALUES } from '../../appConstants/drillDownView.constants';
import { capitalize } from '../Helpers/utils';
import Plot from 'react-plotly.js';
import PropTypes from 'prop-types'
Expand Down
2 changes: 1 addition & 1 deletion web/src/components/MetricsPlotView/metricsPlotView.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { MetricsPlotView } from './metricsPlotView';
import mockAxios from 'jest-mock-axios';
import {X_AXIS_VALUE, SCALE_VALUE, X_AXIS_VALUES, SCALE_VALUES} from '../../constants/drillDownView.constants';
import {X_AXIS_VALUE, SCALE_VALUE, X_AXIS_VALUES, SCALE_VALUES} from '../../appConstants/drillDownView.constants';
import keyCode from 'rc-util/lib/KeyCode';

describe('MetricsPlotView', () => {
Expand Down

0 comments on commit 61c2bd0

Please sign in to comment.