-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchartStorage.fs
37 lines (30 loc) · 1.13 KB
/
chartStorage.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
module DividendExplorer.chartStorage
open System
open System.IO
open FSharp.Data
open DividendExplorer
open types
open utils
let cacheDir = "Cache";
type ChartData = JsonProvider<Sample="ChartSample.json">
let chartInterval =
let today = DateTimeOffset.UtcNow
let nextYearStart = DateTimeOffset(today.Year + 1, 1, 1, 0, 0, 0, today.Offset)
let prevYearStart = DateTimeOffset(today.Year - 30, 1, 1, 0, 0, 0, today.Offset)
let periodStart = prevYearStart.ToUnixTimeSeconds()
let periodEnd = nextYearStart.ToUnixTimeSeconds()
(periodStart, periodEnd)
let filePath(symbol: ShareSymbol) =
let periodStart, periodEnd = chartInterval
$"{cacheDir}/{symbol}_{periodStart}_{periodEnd}.json"
let tryLoadChart(symbol: ShareSymbol) =
let cacheFilePath = filePath symbol
match File.Exists cacheFilePath with
| true -> Some(ChartData.Load cacheFilePath)
| _ -> None
let saveChart(symbol: ShareSymbol, contents: string) =
let cacheFilePath = filePath symbol
write $"Save chart data to {cacheFilePath}"
let file = FileInfo(cacheFilePath)
file.Directory.Create()
File.WriteAllText(cacheFilePath, contents)