Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

elasticsearch indexing of result #375

Merged
merged 6 commits into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion docs/storing_results.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ Each service that is deployed gets an entry in the `jobResults.jobs` property. I

Use `--sql [connection-string] --table [table-name]` arguments to store in the specified SQL Server database. The connection string must point to an existing SQL Server database. The first time it's called the required table will be created.

The create table has the following schema:
The created table has the following schema:


| Column | Type | Example | Description |
Expand All @@ -125,3 +125,9 @@ The create table has the following schema:
| Scenario | `nvarchard(200)` | hello | name of the scenario that was used |
| Description | `nvarchard(200)` | custom string representing extra information about the job | |
| Document | `nvarchar(max)` | { jobResults: {} } | json document containing the results of the job |

### Elasticsearch

Use `--es [server-url] --index [index-name]` arguments to store in the specified Elasticsearch server. The url must point to an existing Elasticsearch instance. The first time it's called the mapping of the required index will be created.

The created index has the same set of fields as SqlServer
85 changes: 85 additions & 0 deletions src/Microsoft.Crank.Controller/JobSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
using Microsoft.Crank.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.Net.Http;
using System.Text;
using System.Net;

namespace Microsoft.Crank.Controller.Serializers
{
Expand Down Expand Up @@ -130,6 +133,88 @@ INSERT INTO [dbo].[" + tableName + @"]
}
}
}
public static Task WriteJobResultsToEsAsync(
JobResults jobResults,
string elasticSearchUrl,
string indexName,
string session,
string scenario,
string description
)
{
var utcNow = DateTime.UtcNow;
return RetryOnExceptionAsync(5, () =>
WriteResultsToEs(
utcNow,
elasticSearchUrl,
indexName,
session,
scenario,
description,
jobResults
)
, 5000);
}

public static async Task InitializeElasticSearchAsync(string elasticSearchUrl, string indexName)
{
var mappingQuery =
@"{""settings"": {""number_of_shards"": 1},""mappings"": { ""dynamic"": false,
""properties"": {
""DateTimeUtc"": { ""type"": ""date"" },
""Session"": { ""type"": ""keyword"" },
""Scenario"": { ""type"": ""keyword"" },
""Description"": { ""type"": ""keyword"" },
""Document"": { ""type"": ""nested"" }}}}";

await RetryOnExceptionAsync(5, () => InitializeDatabaseInternalAsync(elasticSearchUrl,indexName, mappingQuery), 5000);

static async Task InitializeDatabaseInternalAsync(string elasticSearchUrl,string indexName, string mappingQuery)
{
using (var httpClient = new HttpClient())
{
HttpResponseMessage hrm = await httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Head, $"{elasticSearchUrl}/{indexName.ToLower()}"));
if (hrm.StatusCode == HttpStatusCode.NotFound)
{
hrm = await httpClient.PutAsync($"{elasticSearchUrl}/{indexName.ToLower()}", new StringContent(mappingQuery, Encoding.UTF8, "application/json"));
if (hrm.StatusCode != HttpStatusCode.OK)
{
throw new System.Exception(await hrm.Content.ReadAsStringAsync());
}
}
}
}
}

private static async Task WriteResultsToEs(
DateTime utcNow,
string elasticSearchUrl,
string indexName,
string session,
string scenario,
string description,
JobResults jobResults
)
{
var result = new
{
DateTimeUtc = utcNow,
Session = session,
Scenario = scenario,
Description = description,
Document = jobResults
};

using (var httpClient = new HttpClient())
{
var item = JsonConvert.SerializeObject(result, Formatting.None, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() });
HttpResponseMessage hrm = await httpClient.PostAsync(elasticSearchUrl+"/"+indexName.ToLower() + "/_doc/" + session, new StringContent(item.ToString(), Encoding.UTF8, "application/json"));
if (!hrm.IsSuccessStatusCode)
{
throw new Exception(hrm.RequestMessage?.ToString());
}
}
}

private async static Task RetryOnExceptionAsync(int retries, Func<Task> operation, int milliSecondsDelay = 0)
{
Expand Down
Loading