diff --git a/packages/unleash-api/lib/client-metrics/service.js b/packages/unleash-api/lib/client-metrics/service.js index e60a88a685..cb87582ff1 100644 --- a/packages/unleash-api/lib/client-metrics/service.js +++ b/packages/unleash-api/lib/client-metrics/service.js @@ -8,7 +8,7 @@ module.exports = class UnleashClientMetrics extends EventEmitter { super(); this.db = metricsDb; this.highestIdSeen = 0; - this.db.getMetricsLastWeek().then(metrics => { + this.db.getMetricsLastHour().then(metrics => { this.addMetrics(metrics); this.startPoller(); }); diff --git a/packages/unleash-api/lib/db/client-metrics.js b/packages/unleash-api/lib/db/client-metrics.js index 9efd26d9f5..760dc17208 100644 --- a/packages/unleash-api/lib/db/client-metrics.js +++ b/packages/unleash-api/lib/db/client-metrics.js @@ -10,12 +10,12 @@ module.exports = function (db) { } // Used at startup to load all metrics last week into memory! - function getMetricsLastWeek () { + function getMetricsLastHour () { return db .select(METRICS_COLUMNS) .from(TABLE) .limit(2000) - .whereRaw('created_at > now() - interval \'7 day\'') + .whereRaw('created_at > now() - interval \'1 hour\'') .orderBy('created_at', 'asc') .map(mapRow); } @@ -39,5 +39,5 @@ module.exports = function (db) { }; } - return { insert, getMetricsLastWeek, getNewMetrics }; + return { insert, getMetricsLastHour, getNewMetrics }; }; diff --git a/packages/unleash-frontend-next/src/component/nav.jsx b/packages/unleash-frontend-next/src/component/nav.jsx index a6106e6e3d..24328ed1d8 100644 --- a/packages/unleash-frontend-next/src/component/nav.jsx +++ b/packages/unleash-frontend-next/src/component/nav.jsx @@ -19,7 +19,7 @@ export default class UnleashNav extends Component { {createListItem('/history', 'Event history')} {createListItem('/archive', 'Archived toggles')} {createListItem('/metrics', 'Client metrics')} - {createListItem('/Clients', 'Client strategies')} + {createListItem('/client-strategies', 'Client strategies')} diff --git a/packages/unleash-frontend-next/src/data/archive-api.js b/packages/unleash-frontend-next/src/data/archive-api.js index 82ce715b8d..29f6ae3743 100644 --- a/packages/unleash-frontend-next/src/data/archive-api.js +++ b/packages/unleash-frontend-next/src/data/archive-api.js @@ -1,19 +1,7 @@ +import { throwIfNotSuccess, headers } from './helper'; + const URI = '/archive'; -const headers = { - 'Accept': 'application/json', - 'Content-Type': 'application/json', -}; - -function throwIfNotSuccess (response) { - if (!response.ok) { - let error = new Error('API call failed'); - error.status = response.status; - throw error; - } - return response; -} - function fetchAll () { return fetch(`${URI}/features`) .then(throwIfNotSuccess) diff --git a/packages/unleash-frontend-next/src/data/client-strategy-api.js b/packages/unleash-frontend-next/src/data/client-strategy-api.js index dc8bd31aff..d97642f233 100644 --- a/packages/unleash-frontend-next/src/data/client-strategy-api.js +++ b/packages/unleash-frontend-next/src/data/client-strategy-api.js @@ -1,16 +1,9 @@ +import { throwIfNotSuccess, headers } from './helper'; + const URI = '/client/strategies'; -function throwIfNotSuccess (response) { - if (!response.ok) { - let error = new Error('API call failed'); - error.status = response.status; - throw error; - } - return response; -} - function fetchAll () { - return fetch(URI) + return fetch(URI, { headers }) .then(throwIfNotSuccess) .then(response => response.json()); } diff --git a/packages/unleash-frontend-next/src/data/feature-api.js b/packages/unleash-frontend-next/src/data/feature-api.js new file mode 100644 index 0000000000..8d367b8a25 --- /dev/null +++ b/packages/unleash-frontend-next/src/data/feature-api.js @@ -0,0 +1,38 @@ +import { throwIfNotSuccess, headers } from './helper'; + +const URI = '/features'; + +function fetchAll () { + return fetch(URI) + .then(throwIfNotSuccess) + .then(response => response.json()); +} + +function create (featureToggle) { + return fetch(URI, { + method: 'POST', + headers, + body: JSON.stringify(featureToggle), + }).then(throwIfNotSuccess); +} + +function update (featureToggle) { + return fetch(`${URI}/${featureToggle.name}`, { + method: 'PUT', + headers, + body: JSON.stringify(featureToggle), + }).then(throwIfNotSuccess); +} + +function remove (featureToggleName) { + return fetch(`${URI}/${featureToggleName}`, { + method: 'DELETE', + }).then(throwIfNotSuccess); +} + +module.exports = { + fetchAll, + create, + update, + remove, +}; diff --git a/packages/unleash-frontend-next/src/data/helper.js b/packages/unleash-frontend-next/src/data/helper.js new file mode 100644 index 0000000000..fc88df79be --- /dev/null +++ b/packages/unleash-frontend-next/src/data/helper.js @@ -0,0 +1,25 @@ +const defaultErrorMessage = 'Unexptected exception when talking to unleash-api'; + +export function throwIfNotSuccess (response) { + if (!response.ok) { + if (response.status > 400 && response.status < 404) { + return new Promise((resolve, reject) => { + response.json().then(body => { + const errorMsg = body && body.length > 0 ? body[0].msg : defaultErrorMessage; + let error = new Error(errorMsg); + error.statusCode = response.status; + reject(error); + }); + }); + } else { + return Promise.reject(new Error(defaultErrorMessage)); + } + } + return Promise.resolve(response); +}; + + +export const headers = { + 'Accept': 'application/json', + 'Content-Type': 'application/json', +}; diff --git a/packages/unleash-frontend-next/src/data/history-api.js b/packages/unleash-frontend-next/src/data/history-api.js index 68f953c38a..48037fa52a 100644 --- a/packages/unleash-frontend-next/src/data/history-api.js +++ b/packages/unleash-frontend-next/src/data/history-api.js @@ -1,13 +1,6 @@ -const URI = '/events'; +import { throwIfNotSuccess } from './helper'; -function throwIfNotSuccess (response) { - if (!response.ok) { - let error = new Error('API call failed'); - error.status = response.status; - throw error; - } - return response; -} +const URI = '/events'; function fetchAll () { return fetch(URI) diff --git a/packages/unleash-frontend-next/src/data/metrics-api.js b/packages/unleash-frontend-next/src/data/metrics-api.js index cb06ad9443..dbbdf19b76 100644 --- a/packages/unleash-frontend-next/src/data/metrics-api.js +++ b/packages/unleash-frontend-next/src/data/metrics-api.js @@ -1,13 +1,6 @@ -const URI = '/metrics'; +import { throwIfNotSuccess } from './helper'; -function throwIfNotSuccess (response) { - if (!response.ok) { - let error = new Error('API call failed'); - error.status = response.status; - throw error; - } - return response; -} +const URI = '/metrics'; function fetchAll () { return fetch(URI) diff --git a/packages/unleash-frontend-next/src/data/strategy-api.js b/packages/unleash-frontend-next/src/data/strategy-api.js index f88dde5ec8..9aa061fe10 100644 --- a/packages/unleash-frontend-next/src/data/strategy-api.js +++ b/packages/unleash-frontend-next/src/data/strategy-api.js @@ -1,19 +1,7 @@ +import { throwIfNotSuccess, headers } from './helper'; + const URI = '/strategies'; -const headers = { - 'Accept': 'application/json', - 'Content-Type': 'application/json', -}; - -function throwIfNotSuccess (response) { - if (!response.ok) { - let error = new Error('API call failed'); - error.status = response.status; - throw error; - } - return response; -} - function fetchAll () { return fetch(URI) .then(throwIfNotSuccess) diff --git a/packages/unleash-frontend-next/src/index.jsx b/packages/unleash-frontend-next/src/index.jsx index 52b3350856..0f494d1fd0 100644 --- a/packages/unleash-frontend-next/src/index.jsx +++ b/packages/unleash-frontend-next/src/index.jsx @@ -16,7 +16,7 @@ import CreateStrategies from './page/strategies/create'; import HistoryPage from './page/history'; import Archive from './page/archive'; import Metrics from './page/metrics'; -import Clients from './page/clients'; +import ClientStrategies from './page/client-strategies'; const unleashStore = createStore( store, @@ -38,7 +38,7 @@ ReactDOM.render( - + , document.getElementById('app')); diff --git a/packages/unleash-frontend-next/src/page/clients/index.js b/packages/unleash-frontend-next/src/page/client-strategies/index.js similarity index 100% rename from packages/unleash-frontend-next/src/page/clients/index.js rename to packages/unleash-frontend-next/src/page/client-strategies/index.js diff --git a/packages/unleash-frontend-next/src/store/feature-actions.js b/packages/unleash-frontend-next/src/store/feature-actions.js index 935ec7a4f8..06db2b5ef1 100644 --- a/packages/unleash-frontend-next/src/store/feature-actions.js +++ b/packages/unleash-frontend-next/src/store/feature-actions.js @@ -1,4 +1,4 @@ -import api from './feature-api'; +import api from '../data/feature-api'; const debug = require('debug')('unleash:feature-actions'); export const ADD_FEATURE_TOGGLE = 'ADD_FEATURE_TOGGLE'; diff --git a/packages/unleash-frontend-next/src/store/feature-api.js b/packages/unleash-frontend-next/src/store/feature-api.js deleted file mode 100644 index 13e44b430e..0000000000 --- a/packages/unleash-frontend-next/src/store/feature-api.js +++ /dev/null @@ -1,61 +0,0 @@ -const URI = '/features'; - -const headers = { - 'Accept': 'application/json', - 'Content-Type': 'application/json', -}; - -const defaultErrorMessage = 'Unexptected exception when talking to unleash-api'; - -function throwIfNotSuccess (response) { - if (!response.ok) { - if (response.status > 400 && response.status < 404) { - return new Promise((resolve, reject) => { - response.json().then(body => { - const errorMsg = body && body.length > 0 ? body[0].msg : defaultErrorMessage; - let error = new Error(errorMsg); - error.statusCode = response.status; - reject(error); - }); - }); - } else { - return Promise.reject(new Error(defaultErrorMessage)); - } - } - return Promise.resolve(response); -} - -function fetchAll () { - return fetch(URI) - .then(throwIfNotSuccess) - .then(response => response.json()); -} - -function create (featureToggle) { - return fetch(URI, { - method: 'POST', - headers, - body: JSON.stringify(featureToggle), - }).then(throwIfNotSuccess); -} - -function update (featureToggle) { - return fetch(`${URI}/${featureToggle.name}`, { - method: 'PUT', - headers, - body: JSON.stringify(featureToggle), - }).then(throwIfNotSuccess); -} - -function remove (featureToggleName) { - return fetch(`${URI}/${featureToggleName}`, { - method: 'DELETE', - }).then(throwIfNotSuccess); -} - -module.exports = { - fetchAll, - create, - update, - remove, -};