thunderhub/server/schema/widgets/resolvers/helpers.ts
Anthony Potdevin f80492b80a
refactor: ♻️ server structure (#52)
* refactor: ♻️ change schema

* chore: 🔧 small changes

* chore: 🔧 cleanup

* chore: 🔧 cleanup types

* chore: 🔧 change to absolute imports

Co-authored-by: apotdevin <apotdevincab@gmail.com>
2020-06-01 08:36:33 +02:00

113 lines
2.7 KiB
TypeScript

import { reduce, groupBy } from 'underscore';
import {
ForwardProps,
ReduceObjectProps,
ListProps,
InOutProps,
InOutListProps,
} from './interface';
export const reduceForwardArray = (list: ListProps) => {
const reducedOrder = [];
for (const key in list) {
if (Object.prototype.hasOwnProperty.call(list, key)) {
const element: ForwardProps[] = list[key];
const reducedArray: ReduceObjectProps = reduce(
element,
(a: ReduceObjectProps, b: ReduceObjectProps) => {
return {
fee: a.fee + b.fee,
tokens: a.tokens + b.tokens,
};
}
);
reducedOrder.push({
period: Number(key),
amount: element.length,
...reducedArray,
});
}
}
return reducedOrder;
};
export const reduceInOutArray = (list: InOutListProps) => {
const reducedOrder = [];
for (const key in list) {
if (Object.prototype.hasOwnProperty.call(list, key)) {
const element: InOutProps[] = list[key];
const reducedArray: InOutProps = reduce(
element,
(a: ReduceObjectProps, b: ReduceObjectProps) => ({
tokens: a.tokens + b.tokens,
})
);
reducedOrder.push({
period: Number(key),
amount: element.length,
tokens: reducedArray.tokens,
});
}
}
return reducedOrder;
};
export const countArray = (list: ForwardProps[], type: boolean) => {
const inOrOut = type ? 'incoming_channel' : 'outgoing_channel';
const grouped = groupBy(list, inOrOut);
const channelInfo = [];
for (const key in grouped) {
if (Object.prototype.hasOwnProperty.call(grouped, key)) {
const element = grouped[key];
const fee = element
.map(forward => forward.fee)
.reduce((p: number, c: number) => p + c);
const tokens = element
.map(forward => forward.tokens)
.reduce((p: number, c: number) => p + c);
channelInfo.push({
name: key,
amount: element.length,
fee,
tokens,
});
}
}
return channelInfo;
};
export const countRoutes = (list: ForwardProps[]) => {
const grouped = groupBy(list, 'route');
const channelInfo = [];
for (const key in grouped) {
if (Object.prototype.hasOwnProperty.call(grouped, key)) {
const element = grouped[key];
const fee = element
.map(forward => forward.fee)
.reduce((p: number, c: number) => p + c);
const tokens = element
.map(forward => forward.tokens)
.reduce((p: number, c: number) => p + c);
channelInfo.push({
route: key,
in: element[0].incoming_channel,
out: element[0].outgoing_channel,
amount: element.length,
fee,
tokens,
});
}
}
return channelInfo;
};