-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.cs
More file actions
601 lines (546 loc) · 23.9 KB
/
Copy pathprocess.cs
File metadata and controls
601 lines (546 loc) · 23.9 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
using System;
using System.IO;
using System.Data;
using System.Data.Common;
using Snowflake.Data.Client;
using System.Runtime.CompilerServices;
///<summary>
/// Wrap boilerplate of executing sql on snowflake into a single class/method
///</summary>
class SnowflakeWrapper
{
string authString = "";
///<param name="authString"> is connection string used for initiating snowflake connection</param>
public SnowflakeWrapper(string authString)
{
this.authString = authString;
}
/// <summary>
/// Convert a C# native type to corresponding DbType used by snowflake connector
/// </summary>
/// <param name="T"> Input type </param>
/// <returns> DbType equivalent of Input `T`</returns>
public static DbType? toDbType(Type T)
{
switch (Type.GetTypeCode(T))
{
case TypeCode.Int32:
return DbType.Int32;
case TypeCode.Int64:
return DbType.Int64;
case TypeCode.Int16:
return DbType.Int16;
case TypeCode.String:
return DbType.String;
case TypeCode.Boolean:
return DbType.Boolean;
default:
return null;
}
}
/// <summary>
/// Create a command requiring 0 parameters, and only a raw string literal.
///
/// Note: It is responsibility of caller to call Dispose on generated command/returned value
/// </summary>
/// <param name="conn"> connection object to database</param>
/// <param name="query"> query string </param>
/// <returns></returns>
public static IDbCommand PrepareCommand(IDbConnection conn, string query)
{
IDbCommand cmd = conn.CreateCommand();
cmd.CommandText = query;
return cmd;
}
/// <summary>
/// Create a command that consists of 1 or more parameters.
/// Single parameters are passed as the value
/// Two or more parameters are passed as tuples.
///
/// For example: If a query (qry) requires parameters string "hi" and 5, the command would be created as follows
/// > IDbCommand cmd = PrepareCommand<(string, int)>(conn, qry, ("hi", 5));
/// For example: For a single parameter, 7
/// > IDbCommand cmd = PrepareCommand<int>(conn, qry, 7);
/// </summary>
/// <typeparam name="T"> a type, or a tuple of types, that can be mapped via toDbType</typeparam>
/// <param name="conn"> Connection object that allows for command creation</param>
/// <param name="query"> Query template string, with ? substituted for parameter values (named parameters not supported)</param>
/// <param name="parameterTuple"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public static IDbCommand PrepareCommand<T>(IDbConnection conn, string query, T parameterTuple)
{
IDbCommand cmd = conn.CreateCommand();
cmd.CommandText = query;
ITuple genericTuple;
if (parameterTuple is not ITuple)
{
genericTuple = ValueTuple.Create<T>(parameterTuple);
}
else {
genericTuple = (ITuple)parameterTuple;
}
Type[] types = genericTuple.GetType().GetGenericArguments();
for (int i = 0; i < genericTuple.Length; i++)
{
var param = cmd.CreateParameter();
param.ParameterName = 'p' + (i).ToString();
if (toDbType(types[i]) is null)
{
throw new ArgumentException($"{types[i]} does not have a mapping to corresponding DbType for parameters");
}
param.DbType = toDbType(types[i]).Value;
param.Value = genericTuple[i];
cmd.Parameters.Add(param);
}
return cmd;
}
/// <summary>
/// More abstract version of ExecuteCommandArray, which allows arbitrary IDbCommands
/// to be specified for execution. See ICommandList for details.
///
/// Generated commands are explicitly disposed (IDbCommand implements IDispose, and corresponding Dispose is called).
/// </summary>
/// <param name="commands"></param>
/// <returns></returns>
public bool ExecuteCommandList(ICommandList commands)
{
bool sqlSuccess = true;
string executingCommand = "";
try
{
using (IDbConnection conn = new SnowflakeDbConnection())
{
conn.ConnectionString = this.authString;
conn.Open();
commands.SetConnection(conn);
foreach (IDbCommand cmd in commands.GetCommands())
{
try
{
executingCommand = cmd.CommandText;
cmd.ExecuteNonQuery();
}
catch (SnowflakeDbException e)
{
foreach (IDbCommand recoveryCmd in commands.GetRecoveryCommands())
{
recoveryCmd.ExecuteNonQuery();
recoveryCmd.Dispose();
}
Console.Error.WriteLine($"failed to execute: {e.Message}");
Console.Error.WriteLine($"failed command: {executingCommand}");
sqlSuccess = false;
}
finally {
cmd.Dispose();
}
}
}
}
catch (SnowflakeDbException e)
{
Console.Error.WriteLine($"failed to execute: {e.Message}");
Console.Error.WriteLine($"failed command: {executingCommand}");
sqlSuccess = false;
}
catch (Exception exc)
{
Console.Error.WriteLine("Encountered Generic Exception: ");
Console.Error.WriteLine(exc.Message);
sqlSuccess= false;
}
return sqlSuccess;
}
/// <summary>
/// Execute array of snowflake sql statements using connection string of provided in constructor
/// </summary>
/// <param name="commands"> List of sql commands </param>
/// <returns> If error was encountered during command execution </returns>
public bool ExecuteCommandArray(string[] commands, string[] failureRecoveryCommands)
{
bool sqlSuccess = true;
string executingCommand = "";
try {
using (IDbConnection conn = new SnowflakeDbConnection())
{
conn.ConnectionString = this.authString;
conn.Open();
using (IDbCommand cmd = conn.CreateCommand())
{
//string[] commands = [ezpaarseFormat, deleteExisting1, deleteExisting2, stageFile, insertContent];
try {
foreach (string cmdStr in commands)
{
cmd.CommandText = cmdStr;
executingCommand = cmdStr;
cmd.ExecuteNonQuery();
}
} catch (SnowflakeDbException e)
{
foreach (string recoveryCommand in failureRecoveryCommands)
{
cmd.CommandText = recoveryCommand;
cmd.ExecuteNonQuery();
}
Console.Error.WriteLine($"failed to execute: {e.Message}");
Console.Error.WriteLine($"failed command: {executingCommand}");
sqlSuccess = false;
} finally {
conn.Close();
}
}
}
}
catch (SnowflakeDbException e)
{
Console.Error.WriteLine($"failed to execute: {e.Message}");
Console.Error.WriteLine($"failed command: {executingCommand}");
sqlSuccess = false;
}
return sqlSuccess;
}
}
/// <summary>
/// Put file operations around more readable function names, with
/// a dependent element of baseDir
/// </summary>
class FileHandler {
string baseDir = Directory.GetCurrentDirectory();
public FileHandler(string baseDirPath)
{
this.baseDir = baseDirPath;
}
public FileHandler() {}
/// <summary>
/// List files to be processed i.e. ls baseDir/pending/
/// </summary>
/// <returns> List of files or exit with error message</returns>
public string[] GetPendingFilesOrFail()
{
string pendingDir = Path.Join(baseDir, "pending");
try
{
string[] pendingFiles = Directory.GetFiles(pendingDir);
if (pendingFiles.Length == 0)
{
Console.WriteLine($"No files match in {pendingDir}");
Environment.Exit(0); // fail gracefully if no files found
}
return pendingFiles;
}
catch
{
Console.Error.WriteLine("pending Dir access failed");
Environment.Exit(1);
return []; // imagine your type system can't detect unreachable statements
}
}
/// <summary>
/// safety checks a path must pass to go through to next processing steps
/// </summary>
/// <param name="pendingPath">base path of path to be processed in /pending/</param>
/// <returns>path doesn't exist in /working/ or /done/</returns>
public bool CheckPendingShouldBeProcessed(string pendingPath)
{
string workingPath = Path.Join(baseDir, "working", pendingPath);
string donePath = Path.Join(baseDir, "done", pendingPath);
return !Path.Exists(donePath) && !Path.Exists(workingPath);
}
/// <summary>
/// move file from /pending/file to /working/file
/// </summary>
/// <param name="name">base path of file</param>
/// <returns>full path of destination path i.e. /working/file for convenience</returns>
public string MoveToWorking(string name)
{
string pendingPath = Path.Join(baseDir, "pending", name);
string workingPath = Path.Join(baseDir, "working", name);
File.Move(pendingPath, workingPath);
return workingPath;
}
/// <summary>
/// move file from working to done to indicate that processing has been done
/// </summary>
/// <param name="name">base path of file in /working/ to be moved</param>
public void MoveToDone(string name)
{
string donePath = Path.Join(baseDir, "done", name);
string workingPath = Path.Join(baseDir, "working", name);
File.Move(workingPath, donePath);
}
public void MoveToPending(string name)
{
string pendingPath = Path.Join(baseDir, "pending", name);
string workingPath = Path.Join(baseDir, "working", name);
File.Move(workingPath, pendingPath);
}
/// <summary>
/// remove files created during processing associated with the file being processed
/// to be done after file has been processed
/// </summary>
/// <param name="associatedPath">base path of path that has been processed</param>
public void RemoveTempFiles(string associatedPath)
{
string workingPath = Path.Join(baseDir, "working");
string[] tempFiles = Directory.GetFiles(workingPath, $"{associatedPath}.*");
foreach (string tempFile in tempFiles)
{
File.Delete(tempFile);
}
}
/// <summary>
/// Translation of following sed statement
/// <c>sed 1d < $EZPFILESDIR/working/$fname | sed "s/^/$fname;/" >> $EZPFILESDIR/working/$fname.data </c>
/// i.e. Truncate first line (for eg. csv header) and insert $fname to start of each line
/// </summary>
/// <param name="originalPath"> input file </param>
/// <param name="outputPath"> output file </param>
/// <param name="firstColumn"> new first line </param>
public static void TruncateFirstLineAndInsertColumn(string originalPath, string outputPath, string firstColumn)
{
using (StreamReader reader = new StreamReader(originalPath)) {
reader.ReadLine(); // discard first line
using StreamWriter writer = new StreamWriter(outputPath);
//writer.WriteLine(firstLine);
while (reader.ReadLine() is { } line) {
writer.WriteLine(firstColumn + line);
}
}
}
}
/// <summary>
/// An abstract object (interface was cleaner than inheriting from an abstract class)
/// that requires ability to set a connection object, and provides iterators for a
/// sequence of commands and recovery commands.
///
/// Using iterable allows connection object to be bound late, while still being able to instantiate
/// the prerequisite dependent variables to be bound to the implementing object. In simpler words, the
/// user of the IDbCommand doesn't need to care about how the IDbCommand was produced
/// </summary>
interface ICommandList
{
/// <summary>
/// SQL queries to be ran in the happy path.
/// Using SnowflakeWrapper.PrepareCommand simplifies the creation of IDbCommand object from a
/// query and parameters
/// </summary>
/// <returns> Iterable of IDbCommand objects that are ready to be execute (ExeNonQuery())</returns>
public IEnumerable<IDbCommand> GetCommands();
/// <summary>
/// SQL queries to be ran if an SQL error occurs. Primarily used to make sure that database isn't left in a
/// transitionary state (for example, csv uploaded to server, but failed to be processed)
/// Using SnowflakeWrapper.PrepareCommand simplifies the creation of IDbCommand object from a
/// query and parameters
/// </summary>
/// <returns>Iterable of IDbCommand objects that are ready to be execute (ExeNonQuery())</returns>
public IEnumerable<IDbCommand> GetRecoveryCommands();
/// <summary>
/// Getter method for a connection object. Can be used to check if connection has been initialized
/// </summary>
/// <returns>null if connection not initialized, otherwise connection object</returns>
public IDbConnection? GetConnection();
/// <summary>
/// Setter method for a connection object. Used for late binding the connection to the
/// associated queries
/// </summary>
/// <param name="conn"> Initialized connection object that can produce commands</param>
public void SetConnection(IDbConnection conn);
}
/// <summary>
/// implementation of ICommandList. Contains the actual queries to be executed
/// on the server.
/// </summary>
class CommandList : ICommandList
{
/// <summary>
/// ezpYYYYMMDD.log i.e. the basename of the csv that is being used for ingestion. Used as loadid.
/// </summary>
string basename;
/// <summary>
/// full path of the file to be uploaded to the server for ingestion,
/// </summary>
string filename;
/// <summary>
/// connection object used to create IDbCommands for command iterable
/// </summary>
IDbConnection? conn = null;
// connection getter
public IDbConnection? GetConnection()
{
return this.conn;
}
// connection setter
public void SetConnection(IDbConnection conn)
{
this.conn = conn;
}
// constructor for values that queries depend on.
public CommandList(string basename, string filename)
{
this.basename = basename;
this.filename = filename;
}
/// <summary>
/// Using yield return, create an iterator for IDbCommands using hard coded query strings,
/// and query parameters provided via instance fields
/// </summary>
/// <returns> iterator of commands to be executed</returns>
/// <exception cref="ArgumentException"> safety check for this.conn. Should never be encountered in prod, since all code paths
/// should initialize this.conn before trying to iterate through commands.</exception>
public IEnumerable<IDbCommand> GetCommands()
{
if (this.conn == null)
{
throw new ArgumentException("Connection not initialized");
}
string deleteExisting1 = $"DELETE FROM EZPAARSE_RESULT_DEPTS WHERE recordid IN (SELECT recordid FROM EZPAARSE_RESULTS WHERE loadid = :p0)";
IDbCommand cmd = SnowflakeWrapper.PrepareCommand<string>(this.conn, deleteExisting1, this.basename);
yield return cmd;
string deleteExisting2 = $"DELETE FROM EZPAARSE_RESULTS WHERE loadid = :p0;";
cmd = SnowflakeWrapper.PrepareCommand<string>(this.conn, deleteExisting2, this.basename);
yield return cmd;
string ezpaarseFormat = """
CREATE OR REPLACE TEMP FILE FORMAT ezpaarse_csv
TYPE = CSV
FIELD_DELIMITER = ';'
FIELD_OPTIONALLY_ENCLOSED_BY = '"'
TIMESTAMP_FORMAT = 'YYYY-MM-DD"T"HH24:MI:SS+TZH:TZM'
ESCAPE=NONE
ESCAPE_UNENCLOSED_FIELD=NONE
DATE_FORMAT = 'YYYY-MM-DD'
""";
cmd = SnowflakeWrapper.PrepareCommand(this.conn, ezpaarseFormat);
yield return cmd;
// PUT does not support substitution
string stageFile = $"PUT file://{this.filename} @%EZPAARSE_RESULTS OVERWRITE=TRUE"; // upload file to snowflake server home of user account
cmd = SnowflakeWrapper.PrepareCommand(this.conn, stageFile);
yield return cmd;
// record id is an autoincrement field in oracle, so using uuid as a number.
// explicitly rewriting column names such that recordid can be implicitly added and user_hash set to null
string insertContent = """
COPY INTO EZPAARSE_RESULTS (
loadid, datetime, date, login, platform, platform_name,
publisher_name, rtype, mime, print_identifier, online_identifier,
title_id, doi, publication_title, publication_date, unitid, domain,
on_campus, log_id, ezpaarse_version, ezpaarse_date,
middlewares_version, middlewares_date, platforms_version,
platforms_date, middlewares, title, type, subject, geoip_country,
geoip_latitude, geoip_longitude, host, ezproxy_session, url,
status, size
) FROM
(SELECT
$1::CHAR(128) AS loadid,
$2::TIMESTAMP_TZ AS datetime,
$3::DATE AS date,
RTRIM(REGEXP_REPLACE($4, '@pitt\\.edu', ''))::CHAR(17) AS login,
$5::CHAR(64) AS platform,
$6::CHAR(128) AS platform_name,
$7::CHAR(128) AS publisher_name,
$8::CHAR(24) AS rtype,
$9::CHAR(16) AS mime,
$10::CHAR(32) AS print_identifier,
$11::CHAR(32) AS online_identifier,
$12::CHAR(256) AS title_id,
$13::CHAR(256) AS doi,
SUBSTR($14, 1, 256)::CHAR(8192) AS publication_title,
$15::CHAR(10) AS publication_date,
SUBSTR($16, 1, 1024)::CHAR(8192) AS unitid,
$17::CHAR(128) AS domain,
TO_BOOLEAN($18::CHAR(1)) AS on_campus,
$19::CHAR(64) AS log_id,
$20::CHAR(64) AS ezpaarse_version,
$21::DATE AS ezpaarse_date,
$22::CHAR(64) AS middlewares_version,
$23::DATE AS middlewares_date,
$24::CHAR(64) AS platforms_version,
$25::DATE AS platforms_date,
$26::CHAR(256) AS middlewares,
SUBSTR($27, 1, 1024)::CHAR(1024) AS title,
$28::CHAR(32) AS type,
$29::CHAR(512) AS subject,
$30::CHAR(2) AS geoip_country,
$31::NUMBER(7,4) AS geoip_latitude,
$32::NUMBER(7,4) AS geoip_longitude,
$33::CHAR(15) AS host,
$34::CHAR(15) AS ezproxy_session,
SUBSTR($35, 1, 1024) AS url,
$36::NUMBER(3) AS status,
$37::NUMBER(10) AS size
FROM @%EZPAARSE_RESULTS (FILE_FORMAT => 'ezpaarse_csv'))
""";
cmd = SnowflakeWrapper.PrepareCommand(this.conn, insertContent);
yield return cmd;
// Remove does not support parameters
string remove_uploaded = $"REMOVE @%EZPAARSE_RESULTS PATTERN={this.basename}.data.gz";
cmd = SnowflakeWrapper.PrepareCommand(this.conn, remove_uploaded);
yield return cmd;
yield break;
}
/// <summary>
/// same as GetCommands, but for recovery commands (see interface docs for details)
/// </summary>
/// <returns> iterator of commands to be executed if happy path fails</returns>
/// <exception cref="ArgumentException"> safety check for this.conn. Should never be encountered in prod, since all code paths
/// should initialize this.conn before trying to iterate through commands.</exception>
public IEnumerable<IDbCommand> GetRecoveryCommands()
{
if (this.conn == null)
{
throw new ArgumentException("Connection not initialized");
}
string removeUploaded = $"REMOVE @%EZPAARSE_RESULTS PATTERN={basename}.data.gz";
IDbCommand cmd = SnowflakeWrapper.PrepareCommand(this.conn, removeUploaded);
yield return cmd;
yield break;
}
}
/// <summary>
/// Entry point of file
/// </summary>
class ProcessEzpaarse
{
/// <summary>
/// Process each file in /pending/ if possible i.e.
/// upload content of each /pending/ file as a csv into the snowflake
/// table EZPAARSE_RESULTS
/// </summary>
public static void Main()
{
string? basePath = Environment.GetEnvironmentVariable("BASE_DIR");
if (basePath == null) {
Console.WriteLine("BASE_DIR environment variable not provided, using current directory");
basePath = Directory.GetCurrentDirectory();
}
FileHandler fileHandler = new FileHandler(basePath);
string[] pendingFiles = fileHandler.GetPendingFilesOrFail();
foreach (string pendingFullPath in pendingFiles)
{
string pendingFileName = Path.GetFileName(pendingFullPath);
string loadid = Path.GetFileName(pendingFullPath);
if (!fileHandler.CheckPendingShouldBeProcessed(pendingFileName))
{
Console.WriteLine($"{pendingFileName} already exists in pending or done, skipping...");
continue;
}
string workingPath = fileHandler.MoveToWorking(pendingFileName);
// preprocess csv
FileHandler.TruncateFirstLineAndInsertColumn(workingPath, workingPath + ".data", loadid + ";");
ICommandList commands = new CommandList(loadid, workingPath + ".data");
SnowflakeWrapper wrapper = new SnowflakeWrapper(Environment.GetEnvironmentVariable("SNOWFLAKE_AUTH_STRING") ?? "");
bool success = wrapper.ExecuteCommandList(commands);
if (success)
{
fileHandler.MoveToDone(pendingFileName);
fileHandler.RemoveTempFiles(pendingFileName);
}
else
{
//fileHandler.MoveToPending(pendingFileName);
fileHandler.RemoveTempFiles(pendingFileName);
Console.WriteLine($"failed to execute sql for {pendingFileName}");
Environment.Exit(1);
}
}
}
}