-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathFileContext.cs
More file actions
178 lines (157 loc) · 8.3 KB
/
Copy pathFileContext.cs
File metadata and controls
178 lines (157 loc) · 8.3 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
namespace VirtualClient.Contracts
{
using System;
using System.Collections.Generic;
using System.IO.Abstractions;
using System.Linq;
using System.Text.RegularExpressions;
using VirtualClient.Common.Extensions;
/// <summary>
/// Provides context information for a file produced by a Virtual Client
/// component or related toolset.
/// </summary>
public class FileContext
{
private const string FileTimestampFormat = "yyyy-MM-ddTHH-mm-ss-fffffK";
private static readonly Regex PathReservedCharacterExpression = new Regex(@"[""<>:|?*\\/]+", RegexOptions.Compiled);
private static readonly Regex TemplatePlaceholderExpression = new Regex(@"\{(.*?)\}", RegexOptions.Compiled | RegexOptions.IgnoreCase);
/// <summary>
/// Initializes a new instance of the <see cref="FileContext"/> class.
/// </summary>
/// <param name="file">The file for which the context is associated.</param>
/// <param name="contentType">The web content type of the file (e.g. application/json, application/octet-stream, text/plain).</param>
/// <param name="contentEncoding">The web content encoding for the file (e.g. utf-8).</param>
/// <param name="experimentId">The ID of the experiment running that produced the file.</param>
/// <param name="agentId">The ID of the agent running that produced the file.</param>
/// <param name="toolname">The name of the tool/toolset that produced the file (e.g. FioExecutor, FIO).</param>
/// <param name="scenario">The scenario in which the tool/toolset running is related (e.g. fio_randwrite_128g_4k_d32_th4, NTttcp_TCP_4K_Buffer_T1). This is often defined in the profile component parameters.</param>
/// <param name="commandArguments">Arguments supplied to the toolset in the scenario that produced the file.</param>
/// <param name="role">The role for the current instance of the application (e.g. Client, Server).</param>
public FileContext(IFileInfo file, string contentType, string contentEncoding, string experimentId, string agentId = null, string toolname = null, string scenario = null, string commandArguments = null, string role = null)
{
file.ThrowIfNull(nameof(file));
contentType.ThrowIfNullOrWhiteSpace(nameof(contentType));
contentEncoding.ThrowIfNullOrWhiteSpace(nameof(contentEncoding));
experimentId.ThrowIfNullOrWhiteSpace(nameof(experimentId));
this.File = file;
this.ContentType = contentType;
this.ContentEncoding = contentEncoding;
this.ExperimentId = experimentId;
this.AgentId = agentId;
this.ToolName = toolname;
this.Scenario = scenario;
this.CommandArguments = commandArguments;
this.Role = role;
}
/// <summary>
/// The ID of the agent running that produced the file.
/// </summary>
public string AgentId { get; }
/// <summary>
/// Arguments supplied to the toolset in the scenario that produced the file.
/// </summary>
public string CommandArguments { get; }
/// <summary>
/// The web content type of the file (e.g. application/json, application/octet-stream, text/plain).
/// </summary>
public string ContentType { get; }
/// <summary>
/// The web content encoding for the file (e.g. utf-8).
/// </summary>
public string ContentEncoding { get; }
/// <summary>
/// The ID of the experiment running that produced the file.
/// </summary>
public string ExperimentId { get; }
/// <summary>
/// The file for which the context is associated.
/// </summary>
public IFileInfo File { get; }
/// <summary>
/// The role for the current instance of the application (e.g. Client, Server)
/// </summary>
public string Role { get; }
/// <summary>
/// The scenario in which the tool/toolset running is related (e.g. fio_randwrite_128g_4k_d32_th4, NTttcp_TCP_4K_Buffer_T1).
/// This is often defined in the profile component parameters.
/// </summary>
public string Scenario { get; }
/// <summary>
/// The name of the tool/toolset that produced the file (e.g. FioExecutor, FIO).
/// </summary>
public string ToolName { get; }
/// <summary>
/// Returns a file name containing a timestamp as part of the name having removed any
/// characters not allowed in file paths (e.g. 2023-02-01T12-23-30241Z-randomwrite_4k_blocksize.log).
/// </summary>
/// <param name="fileName">The name of the file (e.g. randomwrite_4k_blocksize.log)</param>
/// <param name="timestamp">The timestamp to add to the file name.</param>
public static string GetFileName(string fileName, DateTime timestamp)
{
return PathReservedCharacterExpression.Replace(
$"{timestamp.ToString(FileTimestampFormat)}-{fileName.RemoveWhitespace()}",
string.Empty);
}
/// <summary>
/// Resolves placeholders in the path template provided.
/// </summary>
/// <param name="pathTemplate">A path template containing placeholders to resolve (e.g. {experimentId}-summary.txt).</param>
/// <param name="replacements">Provides the replacement values for the placeholders in the path template.</param>
/// <returns>
/// A path having matching placeholders replaced with actual values
/// (e.g. {experimentId}-summary.txt -> afda108a-4be9-4fe2-a9ef-7b787150896a-summary.txt).
/// </returns>
public static string ResolvePathTemplate(string pathTemplate, IDictionary<string, IConvertible> replacements)
{
string resolvedTemplate = pathTemplate;
MatchCollection matches = FileContext.TemplatePlaceholderExpression.Matches(pathTemplate);
if (matches?.Any() == true)
{
string resolvedValue;
foreach (Match match in matches)
{
string[] effectivePlaceholders = null;
string templatePlaceholder = match.Groups[1].Value;
if (templatePlaceholder.IndexOf('|') < 0)
{
effectivePlaceholders = new string[] { templatePlaceholder };
}
else
{
effectivePlaceholders = templatePlaceholder.Split("|", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
}
bool placeholderMatched = false;
foreach (string placeholder in effectivePlaceholders)
{
// Order of placeholder resolution:
// 1) Metadata known by the VC runtime is applied first because it is definitive.
// 2) Component metadata supplied to the factory.
// 3) Component parameters supplied to the factory.
if (replacements?.Any() == true && FileContext.TryResolvePlaceholder(replacements, placeholder, out resolvedValue))
{
placeholderMatched = true;
resolvedTemplate = resolvedTemplate.Replace(match.Value, resolvedValue);
break;
}
}
if (!placeholderMatched)
{
resolvedTemplate = resolvedTemplate.Replace(match.Value, string.Empty);
}
}
}
return resolvedTemplate;
}
private static bool TryResolvePlaceholder(IDictionary<string, IConvertible> metadata, string propertyName, out string resolvedValue)
{
resolvedValue = null;
if (!string.IsNullOrWhiteSpace(propertyName) && metadata.TryGetValue(propertyName, out IConvertible propertyValue) && propertyValue != null)
{
resolvedValue = propertyValue.ToString();
}
return resolvedValue != null;
}
}
}