chore: more things

This commit is contained in:
AP 2019-11-26 07:22:58 +01:00
parent dce5795fa8
commit bf69ff995b
2 changed files with 40 additions and 32 deletions

View file

@ -6,12 +6,16 @@ import { subHours, subDays } from "date-fns";
import { countArray } from "./Helpers";
import { ForwardCompleteProps } from "./ForwardReport.interface";
import { ForwardChannelsType } from "../../../schemaTypes/query/report/ForwardChannels";
import { sortBy } from "underscore";
export const getForwardChannelsReport = {
type: ForwardChannelsType,
args: {
time: {
type: GraphQLString
},
order: {
type: GraphQLString
}
},
resolve: async (root: any, params: any, context: any) => {
@ -46,9 +50,16 @@ export const getForwardChannelsReport = {
const incomingCount = countArray(forwardsList.forwards, true);
const outgoingCount = countArray(forwardsList.forwards, false);
const sortedInCount = sortBy(incomingCount, params.order)
.reverse()
.slice(0, 5);
const sortedOutCount = sortBy(outgoingCount, params.order)
.reverse()
.slice(0, 5);
return {
incoming: JSON.stringify(incomingCount),
outgoing: JSON.stringify(outgoingCount)
incoming: JSON.stringify(sortedInCount),
outgoing: JSON.stringify(sortedOutCount)
};
} catch (error) {
logger.error("Error getting forward channel report: %o", error);

View file

@ -1,16 +1,12 @@
import { reduce, sortBy } from "underscore";
import { reduce, groupBy } from "underscore";
import {
ForwardProps,
ReduceObjectProps,
FinalProps,
FinalList,
ListProps,
CountProps,
ChannelCounts
ListProps
} from "./ForwardReport.interface";
export const reduceForwardArray = (list: ListProps): FinalList => {
let reducedOrder: FinalList = {};
export const reduceForwardArray = (list: ListProps) => {
const reducedOrder = [];
for (const key in list) {
if (list.hasOwnProperty(key)) {
const element: ForwardProps[] = list[key];
@ -20,39 +16,40 @@ export const reduceForwardArray = (list: ListProps): FinalList => {
tokens: a.tokens + b.tokens
};
});
reducedOrder[key] = {
reducedOrder.push({
period: parseInt(key),
amount: element.length,
...reducedArray
} as FinalProps;
});
}
}
return reducedOrder;
};
export const countArray = (
list: ForwardProps[],
type: boolean
): ChannelCounts[] => {
const count: CountProps = {};
list
.map(item => {
return type ? item.incoming_channel : item.outgoing_channel;
})
.forEach(channel => {
count[channel] = (count[channel] || 0) + 1;
});
export const countArray = (list: ForwardProps[], type: boolean) => {
const inOrOut = type ? "incoming_channel" : "outgoing_channel";
const grouped = groupBy(list, inOrOut);
const mapped: ChannelCounts[] = [];
for (const key in count) {
if (count.hasOwnProperty(key)) {
const element = count[key];
mapped.push({
const channelInfo = [];
for (const key in grouped) {
if (grouped.hasOwnProperty(key)) {
const element = grouped[key];
const fee = element.map(forward => forward.fee).reduce((p, c) => p + c);
const tokens = element
.map(forward => forward.tokens)
.reduce((p, c) => p + c);
channelInfo.push({
name: key,
count: element
amount: element.length,
fee: fee,
tokens: tokens
});
}
}
sortBy(mapped, "count");
return mapped;
return channelInfo;
};