-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from cypherpepe/patch-1
refactor(honey): cleared some code redundancy
- Loading branch information
Showing
3 changed files
with
70 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,111 +1,56 @@ | ||
import { type HoneyEntry } from "~/app/type"; | ||
|
||
export function fullHourTimestamps(endUnix: number): number[] { | ||
function generateTimestamps(endUnix: number, step: number): number[] { | ||
const timestamps: number[] = []; | ||
const currentTimeInseconds = Math.floor(Date.now() / 1000); | ||
let current = currentTimeInseconds - (currentTimeInseconds % 3600); | ||
const currentTimeInSeconds = Math.floor(Date.now() / 1000); | ||
let current = currentTimeInSeconds - (currentTimeInSeconds % step); | ||
while (current >= endUnix) { | ||
timestamps.push(current); | ||
current -= 3600; | ||
current -= step; | ||
} | ||
return timestamps; | ||
} | ||
|
||
export function fullDayTimestamps(endUnix: number): number[] { | ||
const timestamps: number[] = []; | ||
const currentTimeInseconds = Math.floor(Date.now() / 1000); | ||
let current = currentTimeInseconds - (currentTimeInseconds % 86400); | ||
while (current >= endUnix) { | ||
timestamps.push(current); | ||
current -= 86400; | ||
} | ||
return timestamps; | ||
} | ||
|
||
export function fillVolumeDataByHour(list: HoneyEntry[], endUnix: number) { | ||
function fillData( | ||
list: HoneyEntry[], | ||
endUnix: number, | ||
step: number, | ||
typename: "HoneyVolumeHourData" | "HoneyVolumeDayData" | "HoneySupplyHourData" | "HoneySupplyDayData" | ||
) { | ||
const data = [...list]; | ||
const timeList = fullHourTimestamps(endUnix + 3600); | ||
const timeList = generateTimestamps(endUnix + step, step); | ||
let lastAmount = list.length > 0 ? data[0]!.amount : "0"; | ||
|
||
timeList.forEach((time: number) => { | ||
const entry = data.find((entry: HoneyEntry) => entry.timestamp === time); | ||
if (!entry) { | ||
data.push({ | ||
//@ts-ignore | ||
__typename: "HoneyVolumeHourData", | ||
id: (time / 3600).toString(), | ||
__typename: typename, | ||
id: (time / step).toString(), | ||
timestamp: time, | ||
amount: "0", | ||
amount: lastAmount, | ||
}); | ||
} else { | ||
lastAmount = entry.amount; | ||
} | ||
}); | ||
|
||
return data.sort((a: HoneyEntry, b: HoneyEntry) => a.timestamp - b.timestamp); | ||
} | ||
|
||
export function fillVolumeDataByHour(list: HoneyEntry[], endUnix: number) { | ||
return fillData(list, endUnix, 3600, "HoneyVolumeHourData"); | ||
} | ||
|
||
export function fillVolumeDataByDay(list: HoneyEntry[], endUnix: number) { | ||
const data = [...list]; | ||
const timeList = fullDayTimestamps(endUnix + 24 * 3600); | ||
timeList.forEach((time: number) => { | ||
const entry = data.find((entry: HoneyEntry) => entry.timestamp === time); | ||
if (!entry) { | ||
data.push({ | ||
//@ts-ignore | ||
__typename: "HoneyVolumeDayData", | ||
id: (time / 86400).toString(), | ||
timestamp: time, | ||
amount: "0", | ||
}); | ||
} | ||
}); | ||
return data.sort((a: HoneyEntry, b: HoneyEntry) => a.timestamp - b.timestamp); | ||
return fillData(list, endUnix, 86400, "HoneyVolumeDayData"); | ||
} | ||
|
||
export function fillSupplyDataByHour(list: HoneyEntry[], endUnix: number) { | ||
if (list && list.length > 0) { | ||
const data = [...list]; | ||
const timeList = fullHourTimestamps(endUnix + 3600); | ||
let supply = data[0]!.amount; | ||
timeList.forEach((time: number) => { | ||
const entry = data.find((entry: HoneyEntry) => entry.timestamp === time); | ||
if (!entry) { | ||
data.push({ | ||
//@ts-ignore | ||
__typename: "HoneySupplyHourData", | ||
id: (time / 3600).toString(), | ||
timestamp: time, | ||
amount: supply, | ||
}); | ||
} else { | ||
supply = entry.amount; | ||
} | ||
}); | ||
return data.sort( | ||
(a: HoneyEntry, b: HoneyEntry) => a.timestamp - b.timestamp, | ||
); | ||
} | ||
return []; | ||
return fillData(list, endUnix, 3600, "HoneySupplyHourData"); | ||
} | ||
|
||
export function fillSupplyDataByDay(list: HoneyEntry[], endUnix: number) { | ||
if (list && list.length > 0) { | ||
const data = [...list]; | ||
const timeList = fullDayTimestamps(endUnix + 24 * 3600); | ||
let supply = data[0]!.amount; | ||
timeList.forEach((time: number) => { | ||
const entry = data.find((entry: HoneyEntry) => entry.timestamp === time); | ||
if (!entry) { | ||
data.push({ | ||
//@ts-ignore | ||
__typename: "HoneySupplyDayData", | ||
id: (time / 86400).toString(), | ||
timestamp: time, | ||
amount: supply, | ||
}); | ||
} else { | ||
supply = entry.amount; | ||
} | ||
}); | ||
return data.sort( | ||
(a: HoneyEntry, b: HoneyEntry) => a.timestamp - b.timestamp, | ||
); | ||
} | ||
return []; | ||
return fillData(list, endUnix, 86400, "HoneySupplyDayData"); | ||
} |