1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-01 00:08:27 +01:00

some cleanup

This commit is contained in:
ivaosthu 2016-11-04 22:14:58 +01:00
parent ade8df4946
commit f89535f359
14 changed files with 82 additions and 125 deletions

View File

@ -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();
});

View File

@ -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 };
};

View File

@ -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')}
<ListDivider />

View File

@ -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)

View File

@ -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());
}

View File

@ -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,
};

View File

@ -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',
};

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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(
<Route path="/history" component={HistoryPage} />
<Route path="/archive" component={Archive} />
<Route path="/metrics" component={Metrics} />
<Route path="/clients" component={Clients} />
<Route path="/client-strategies" component={ClientStrategies} />
</Route>
</Router>
</Provider>, document.getElementById('app'));

View File

@ -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';

View File

@ -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,
};