This commit is contained in:
Kevin Darby committed 2019-08-28 18:52:02 -04:00
1 parent bc688ef44d
commit 0a3fc1bca0
4 files changed
+63 -24

No files matched your search

+2 -2
View File
@@ -105,8 +105,8 @@
"program": "${workspaceRoot}/build/Debug/bin/net471/sparkcmd.exe",
"args": [
"historical",
"-p",
"C:\\Users\\darby\\Downloads\\xcbt_md_zn_fut_spd_20190521-r-00057.gz",
"-d",
"mbo/mbo/20190429/20190429-MBO_xcbt_zn_fut_0-eth.gz",
],
"cwd": "${workspaceRoot}",
"stopAtEntry": false,
+20 -2
View File
@@ -26,7 +26,25 @@ namespace mbts.utils
[DllImport("kernel32.dll")]
public static extern bool SetConsoleTitle(string lpConsoleTitle);
[DllImport("libspark")]
public static extern string bts_data_path();
public static string bts_user_path()
{
string path = Environment.GetEnvironmentVariable("APPDATA");
System.Diagnostics.Debug.Assert(!string.IsNullOrEmpty(path));
return path + "\\bts\\";
}
public static string sparkdata_path(string rel)
{
string path = bts_user_path() + rel;
if (path[path.Length - 1] != '\\') path += '\\';
if(!System.IO.Directory.Exists(path))
System.IO.Directory.CreateDirectory(path);
return path;
}
public static string bts_data_path()
{
return sparkdata_path("data\\");
}
}
}
@@ -3,8 +3,10 @@ using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
using Amazon.S3.Transfer;
using bts.api;
namespace bts.history
@@ -17,7 +19,13 @@ namespace bts.history
private static IAmazonS3 _client;
static HistoricalDataDownloader()
{
_client = new AmazonS3Client(_region);
AWSCredentials credentials = new BasicAWSCredentials(string.Empty, string.Empty);
AmazonS3Config config = new AmazonS3Config();
config.ServiceURL = "s3.amazonaws.com";
config.RegionEndpoint = Amazon.RegionEndpoint.GetBySystemName("us-east-1");
_client = new AmazonS3Client(credentials, config);
//_client = new AmazonS3Client(string.Empty, string.Empty, _region);
}
private string _path;
@@ -38,28 +46,20 @@ namespace bts.history
else
return null;
}
async Task<Stream> download(string key)
public async Task<Stream> download(string key)
{
bts.Log.assert( !is_in_cache(key), $"key {key} already exists in cache!");
bts.Log.Gen.info($"downloading {key} from s3");
try
{
GetObjectRequest request = new GetObjectRequest { BucketName = _bucket_name, Key = key };
using (GetObjectResponse response = await _client.GetObjectAsync(request))
{
string title = response.Metadata["x-amz-meta-title"]; // Assume you have "title" as medata added to the object.
string contentType = response.Headers["Content-Type"];
bts.Log.Gen.info($"Object metadata, Title: {title}");
Console.WriteLine($"Content type: {contentType}");
using (Stream response_stream = response.ResponseStream)
{
using (FileStream writer = new FileStream(_path + "/" + key, FileMode.CreateNew))
{
response_stream.CopyTo(writer);
return writer;
}
}
}
TransferUtility trans = new TransferUtility(_client);
string path = _path + key;
path = path.Replace("/", "\\");
var dir = new FileInfo(path).Directory.FullName;
if(!Directory.Exists(dir))
Directory.CreateDirectory(dir);
await trans.DownloadAsync(path, _bucket_name, key);
return File.Open(path, FileMode.Open);
}
catch (AmazonS3Exception e)
{
@@ -91,7 +91,7 @@ namespace bts.history
return string.Empty;
}
}
string make_key(DateTime dt, api.CMEMics mic, string symbol, api.SecurityType st)
public string make_key(DateTime dt, api.CMEMics mic, string symbol, api.SecurityType st)
{
string micstring = map_mic(mic);
string date = dt.ToString("YYYYMMDD");
@@ -16,9 +16,12 @@ namespace bts.history
[Verb("historical", HelpText = "Historical Data Service command")]
public class HistoricalDataOptions
{
[Option('p', "parse", Required = false, HelpText = "Parse a cme gzip file and print to screen")]
[Option('f', "file", Required = false, HelpText = "Parse a cme gzip file and print to screen")]
public string parsefile { get; set; }
[Option('d', "download", Required = false, HelpText = "Download and Parse a cme gzip file from an S3 bucket")]
public string s3key { get; set; }
[Option('s', "server", Required = false, Default = false, HelpText = "Run HistoricalData Server")]
public bool server { get; set; }
}
@@ -28,6 +31,24 @@ namespace bts.history
static ManualResetEvent _quit = new ManualResetEvent(false);
public static int Run(HistoricalDataOptions o)
{
if (!string.IsNullOrEmpty(o.s3key))
{
var dl = new HistoricalDataDownloader();
var str = dl.download(o.s3key).ContinueWith( t=>
{
if(t.IsFaulted)
bts.Log.Gen.error(t.Exception.ToString());
else
{
using (MBOParser parser = new MBOParser(t.Result))
{
foreach (var mbo in parser.next())
Console.WriteLine(mbo.ToString());
}
}
});
}
if(!string.IsNullOrEmpty(o.parsefile))
{
if(!File.Exists(o.parsefile))