badguardhome/client/src/helpers/helpers.js

79 lines
2.2 KiB
JavaScript
Raw Normal View History

2018-08-30 14:25:33 +00:00
import dateParse from 'date-fns/parse';
import dateFormat from 'date-fns/format';
import subHours from 'date-fns/sub_hours';
2018-08-30 14:25:33 +00:00
import addHours from 'date-fns/add_hours';
import round from 'lodash/round';
import { STATS_NAMES } from './constants';
2018-08-30 14:25:33 +00:00
const formatTime = (time) => {
const parsedTime = dateParse(time);
return dateFormat(parsedTime, 'HH:mm:ss');
};
export const normalizeLogs = logs => logs.map((log) => {
const {
time,
question,
answer: response,
reason,
client,
rule,
2018-08-30 14:25:33 +00:00
} = log;
const { host: domain, type } = question;
const responsesArray = response ? response.map((response) => {
const { value, type, ttl } = response;
return `${type}: ${value} (ttl=${ttl})`;
}) : [];
return {
time: formatTime(time),
domain,
type,
response: responsesArray,
reason,
client,
rule,
2018-08-30 14:25:33 +00:00
};
});
export const normalizeHistory = history => Object.keys(history).map((key) => {
2018-10-10 14:54:21 +00:00
let id = STATS_NAMES[key];
if (!id) {
id = key.replace(/_/g, ' ').replace(/^\w/, c => c.toUpperCase());
}
2018-08-30 14:25:33 +00:00
const dayAgo = subHours(Date.now(), 24);
2018-08-30 14:25:33 +00:00
const data = history[key].map((item, index) => {
const formatHour = dateFormat(addHours(dayAgo, index), 'ddd HH:00');
2018-08-30 14:25:33 +00:00
const roundValue = round(item, 2);
return {
x: formatHour,
y: roundValue,
};
});
return {
id,
data,
};
});
export const normalizeFilteringStatus = (filteringStatus) => {
const { enabled, filters, user_rules: userRules } = filteringStatus;
const newFilters = filters ? filters.map((filter) => {
const {
url, enabled, last_updated: lastUpdated = Date.now(), name = 'Default name', rules_count: rulesCount = 0,
} = filter;
return {
url, enabled, lastUpdated: formatTime(lastUpdated), name, rulesCount,
};
}) : [];
const newUserRules = Array.isArray(userRules) ? userRules.join('\n') : '';
return { enabled, userRules: newUserRules, filters: newFilters };
};
export const getPercent = (amount, number) => round(100 / (amount / number));