|
|
@ -41,7 +41,6 @@ import { |
|
|
parse, |
|
|
parse, |
|
|
parseISO |
|
|
parseISO |
|
|
} from 'date-fns'; |
|
|
} from 'date-fns'; |
|
|
import { first, last } from 'lodash'; |
|
|
|
|
|
import ms from 'ms'; |
|
|
import ms from 'ms'; |
|
|
import { DeviceDetectorService } from 'ngx-device-detector'; |
|
|
import { DeviceDetectorService } from 'ngx-device-detector'; |
|
|
import { parse as csvToJson } from 'papaparse'; |
|
|
import { parse as csvToJson } from 'papaparse'; |
|
|
@ -123,75 +122,78 @@ export class GfHistoricalMarketDataEditorComponent |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public ngOnChanges() { |
|
|
public ngOnChanges() { |
|
|
if (this.dateOfFirstActivity) { |
|
|
const startDate = this.dateOfFirstActivity ?? this.marketData()[0]?.date; |
|
|
let date = this.dateOfFirstActivity; |
|
|
|
|
|
|
|
|
if (!startDate) { |
|
|
const missingMarketData: { date: Date; marketPrice?: number }[] = []; |
|
|
return; |
|
|
|
|
|
} |
|
|
if (this.historicalDataItems()?.[0]?.date) { |
|
|
|
|
|
while ( |
|
|
let date = startDate; |
|
|
isBefore( |
|
|
|
|
|
date, |
|
|
const missingMarketData: { date: Date; marketPrice?: number }[] = []; |
|
|
parse(this.historicalDataItems()[0].date, DATE_FORMAT, new Date()) |
|
|
|
|
|
) |
|
|
|
|
|
) { |
|
|
|
|
|
missingMarketData.push({ |
|
|
|
|
|
date, |
|
|
|
|
|
marketPrice: undefined |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
date = addDays(date, 1); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const marketDataItems = [...missingMarketData, ...this.marketData()]; |
|
|
if (this.historicalDataItems()?.[0]?.date) { |
|
|
|
|
|
while ( |
|
|
|
|
|
isBefore( |
|
|
|
|
|
date, |
|
|
|
|
|
parse(this.historicalDataItems()[0].date, DATE_FORMAT, new Date()) |
|
|
|
|
|
) |
|
|
|
|
|
) { |
|
|
|
|
|
missingMarketData.push({ |
|
|
|
|
|
date, |
|
|
|
|
|
marketPrice: undefined |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
const lastDate = last(marketDataItems)?.date; |
|
|
date = addDays(date, 1); |
|
|
if (!lastDate || !isToday(lastDate)) { |
|
|
|
|
|
marketDataItems.push({ date: new Date() }); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
this.marketDataByMonth = {}; |
|
|
const marketDataItems = [...missingMarketData, ...this.marketData()]; |
|
|
|
|
|
|
|
|
for (const marketDataItem of marketDataItems) { |
|
|
const lastDate = marketDataItems.at(-1)?.date; |
|
|
const currentDay = parseInt(format(marketDataItem.date, 'd'), 10); |
|
|
if (!lastDate || !isToday(lastDate)) { |
|
|
const key = format(marketDataItem.date, 'yyyy-MM'); |
|
|
marketDataItems.push({ date: new Date() }); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if (!this.marketDataByMonth[key]) { |
|
|
this.marketDataByMonth = {}; |
|
|
this.marketDataByMonth[key] = {}; |
|
|
|
|
|
} |
|
|
for (const marketDataItem of marketDataItems) { |
|
|
|
|
|
const currentDay = parseInt(format(marketDataItem.date, 'd'), 10); |
|
|
|
|
|
const key = format(marketDataItem.date, 'yyyy-MM'); |
|
|
|
|
|
|
|
|
this.marketDataByMonth[key][ |
|
|
if (!this.marketDataByMonth[key]) { |
|
|
currentDay < 10 ? `0${currentDay}` : currentDay |
|
|
this.marketDataByMonth[key] = {}; |
|
|
] = { |
|
|
|
|
|
date: marketDataItem.date, |
|
|
|
|
|
day: currentDay, |
|
|
|
|
|
marketPrice: marketDataItem.marketPrice |
|
|
|
|
|
}; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Fill up missing months
|
|
|
this.marketDataByMonth[key][ |
|
|
const dates = Object.keys(this.marketDataByMonth).sort(); |
|
|
currentDay < 10 ? `0${currentDay}` : currentDay |
|
|
const startDateString = first(dates); |
|
|
] = { |
|
|
const startDate = min([ |
|
|
date: marketDataItem.date, |
|
|
this.dateOfFirstActivity, |
|
|
day: currentDay, |
|
|
...(startDateString ? [parseISO(startDateString)] : []) |
|
|
marketPrice: marketDataItem.marketPrice |
|
|
]); |
|
|
}; |
|
|
const endDateString = last(dates); |
|
|
} |
|
|
|
|
|
|
|
|
if (endDateString) { |
|
|
// Fill up missing months
|
|
|
const endDate = parseISO(endDateString); |
|
|
const dates = Object.keys(this.marketDataByMonth).sort(); |
|
|
|
|
|
const startDateString = dates[0]; |
|
|
|
|
|
const endDateString = dates.at(-1); |
|
|
|
|
|
|
|
|
let currentDate = startDate; |
|
|
if (endDateString) { |
|
|
|
|
|
const endDate = parseISO(endDateString); |
|
|
|
|
|
|
|
|
while (isBefore(currentDate, endDate)) { |
|
|
let currentDate = min([ |
|
|
const key = format(currentDate, 'yyyy-MM'); |
|
|
startDate, |
|
|
if (!this.marketDataByMonth[key]) { |
|
|
...(startDateString ? [parseISO(startDateString)] : []) |
|
|
this.marketDataByMonth[key] = {}; |
|
|
]); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
currentDate = addMonths(currentDate, 1); |
|
|
while (isBefore(currentDate, endDate)) { |
|
|
|
|
|
const key = format(currentDate, 'yyyy-MM'); |
|
|
|
|
|
if (!this.marketDataByMonth[key]) { |
|
|
|
|
|
this.marketDataByMonth[key] = {}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
currentDate = addMonths(currentDate, 1); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|