Skip to content

Commit

Permalink
feat(updatederivedcfg): Added UpdateDerivedCfg
Browse files Browse the repository at this point in the history
ref: #18962
  • Loading branch information
jacopocinaark committed Dec 19, 2024
1 parent 943bb2d commit 8f56e3e
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 25 deletions.
28 changes: 27 additions & 1 deletion Artesian/Artesian.SDK.Tests/Samples/DerivedTimeSerieTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Artesian.SDK.Tests.Samples
public class DerivedTimeSerieTest
{
private readonly ArtesianServiceConfig _cfg = new ArtesianServiceConfig(new Uri("https://arkive.artesian.cloud/tenantName/"), "APIKey");

[Test]
[Ignore("Run only manually with proper artesian URI and ApiKey set")]
public void CreateDerivedCoalesceTimeSeries()
Expand Down Expand Up @@ -134,6 +134,32 @@ public void CreateDerivedCoalesceTimeSeries()
ClassicAssert.AreEqual(200, item.Value);
}

// Update DerivedCfg
curveIds.Reverse();
var derivedCfgUpdate = new DerivedCfgCoalesce()
{
OrderedReferencedMarketDataIds = curveIds.ToArray(),
};

var marketDataOutput = marketDataService.UpdateDerivedConfigurationAsync(marketData.MarketDataId.Value, derivedCfgUpdate, false).ConfigureAwait(true).GetAwaiter().GetResult();

Thread.Sleep(2000);

ts = qs.CreateActual()
.ForMarketData(new[] { marketData.MarketDataId.Value })
.InGranularity(Granularity.Day)
.InAbsoluteDateRange(new LocalDate(2018, 10, 01), new LocalDate(2018, 10, 10))
.ExecuteAsync().Result;

foreach (var item in ts)
{
if (item.Time.Date <= new DateTime(2018, 10, 3))
ClassicAssert.AreEqual(100, item.Value);

if (item.Time.Date > new DateTime(2018, 10, 3))
ClassicAssert.AreEqual(200, item.Value);
}

/*******************************************************************
* Tidy Up
*******************************************************************/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected override DerivedCfgBase Create(Type objectType, JObject jObject)
}
else if (token.ToObject<DerivedAlgorithm>() == DerivedAlgorithm.Sum)
{
throw new NotImplementedException($@"Not yet impletemented DerivedAlgorithm {DerivedAlgorithm.Sum}");
return new DerivedCfgSum();
}
else if (token.ToObject<DerivedAlgorithm>() == DerivedAlgorithm.Coalesce)
{
Expand All @@ -47,6 +47,7 @@ protected override Type GetType(DerivedAlgorithm discriminatorValue)
{
DerivedAlgorithm.MUV => typeof(DerivedCfgMuv),
DerivedAlgorithm.Coalesce => typeof(DerivedCfgCoalesce),
DerivedAlgorithm.Sum => typeof(DerivedCfgSum),
_ => null,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

namespace Artesian.SDK.Dto.DerivedCfg.Serialize
{
public abstract class JsonPolymorphicConverter<TBase, TDiscriminatorEnum> : JsonConverter<TBase> where TDiscriminatorEnum : struct, Enum
public abstract class JsonPolymorphicConverter<TBase, TDiscriminatorEnum> : JsonConverter<TBase>
where TDiscriminatorEnum : struct, Enum
{
private readonly string _discriminatorPropertyName;

Expand All @@ -22,35 +23,44 @@ public JsonPolymorphicConverter(string discriminatorPropertyName)
throw new JsonException();
}

using JsonDocument jsonDocument = JsonDocument.ParseValue(ref reader);
string propertyName = options.PropertyNamingPolicy?.ConvertName(_discriminatorPropertyName) ?? _discriminatorPropertyName;
if (!jsonDocument.RootElement.TryGetProperty(propertyName, out var value))
using (var jsonDocument = JsonDocument.ParseValue(ref reader))
{
throw new JsonException();
}
var pn = options.PropertyNamingPolicy?.ConvertName(_discriminatorPropertyName) ?? _discriminatorPropertyName;

Type type = null;
TDiscriminatorEnum result;
if (value.ValueKind == JsonValueKind.Number && value.TryGetInt32(out var value2) && Enum.IsDefined(typeof(TDiscriminatorEnum), value2))
{
type = GetType((TDiscriminatorEnum)Enum.ToObject(typeof(TDiscriminatorEnum), value2));
}
else if (value.ValueKind == JsonValueKind.String && Enum.TryParse<TDiscriminatorEnum>(value.GetString(), ignoreCase: true, out result))
{
type = GetType(result);
}
if (!jsonDocument.RootElement.TryGetProperty(pn, out var typeProperty))
{
throw new JsonException();
}

if (type == null)
{
throw new JsonException();
}
Type? type = null;

if (typeProperty.ValueKind == JsonValueKind.Number
&& typeProperty.TryGetInt32(out var enumInt)
&& Enum.IsDefined(typeof(TDiscriminatorEnum), enumInt))
{
type = GetType((TDiscriminatorEnum)Enum.ToObject(typeof(TDiscriminatorEnum), enumInt));
}
else if (typeProperty.ValueKind == JsonValueKind.String
&& Enum.TryParse<TDiscriminatorEnum>(typeProperty.GetString(), true, out var enumVal))
{
type = GetType(enumVal);
}

return (TBase)JsonSerializer.Deserialize(jsonDocument.RootElement.GetRawText(), type, options);
if (type == null)
{
throw new JsonException();
}

var jsonObject = jsonDocument.RootElement.GetRawText();
var result = (TBase?)JsonSerializer.Deserialize(jsonObject, type, options);

return result;
}
}

public override void Write(Utf8JsonWriter writer, TBase value, JsonSerializerOptions options)
{
JsonSerializer.Serialize(writer, (object)value, options);
JsonSerializer.Serialize(writer, (object?)value, options);
}
}
}
}
10 changes: 10 additions & 0 deletions Artesian/Artesian.SDK/Service/MarketData/IMarketDataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// license information.

using Artesian.SDK.Dto;
using Artesian.SDK.Dto.DerivedCfg;

using NodaTime;
using System.Collections.Generic;
Expand Down Expand Up @@ -64,6 +65,15 @@ public interface IMarketDataService
/// <param name="ctk">CancellationToken</param>
/// <returns></returns>
Task DeleteMarketDataAsync(int id, CancellationToken ctk = default);
/// <summary>
/// Update Derived Configuration for marketData with id supplied in <paramref name="marketDataId"/> and Rebuild
/// </summary>
/// <param name="marketDataId">Id of the marketData</param>
/// <param name="derivedCfg">The Derived Configuration to be updated</param>
/// <param name="force">Force the update of configuration also if another rebuild process is running (Defualt=false)</param>
/// <param name="ctk">Cancellation Token</param>
/// <returns>MarketData Entity Output</returns>
Task<MarketDataEntity.Output> UpdateDerivedConfigurationAsync(int marketDataId, DerivedCfgBase derivedCfg, bool force, CancellationToken ctk = default);
#endregion

#region SearchFacet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System;
using Flurl;
using System.Globalization;
using Artesian.SDK.Dto.DerivedCfg;

namespace Artesian.SDK.Service
{
Expand Down Expand Up @@ -107,5 +108,21 @@ public Task DeleteMarketDataAsync(int id, CancellationToken ctk = default)

return _client.Exec(HttpMethod.Delete, url, ctk: ctk);
}
/// <summary>
/// Update Derived Configuration for marketData with id supplied in <paramref name="marketDataId"/> and Rebuild
/// </summary>
/// <param name="marketDataId">Id of the marketData</param>
/// <param name="derivedCfg">The Derived Configuration to be updated</param>
/// <param name="force">Force the update of configuration also if another rebuild process is running (Defualt=false)</param>
/// <param name="ctk">Cancellation Token</param>
/// <returns>MarketData Entity Output</returns>
public Task<MarketDataEntity.Output> UpdateDerivedConfigurationAsync(int marketDataId, DerivedCfgBase derivedCfg, bool force, CancellationToken ctk = default)
{
var url = "/marketdata/entity/".AppendPathSegment(marketDataId.ToString(CultureInfo.InvariantCulture)).AppendPathSegment("updateDerivedConfiguration")
.SetQueryParam("force", force);


return _client.Exec<MarketDataEntity.Output, DerivedCfgBase>(HttpMethod.Post, url, derivedCfg, ctk: ctk);
}
}
}

0 comments on commit 8f56e3e

Please sign in to comment.