Skip to content

Commit

Permalink
Cached query working for UserChartPart
Browse files Browse the repository at this point in the history
  • Loading branch information
olmobrutall committed Nov 27, 2021
1 parent 1cf9c7d commit b77dcbf
Show file tree
Hide file tree
Showing 39 changed files with 1,011 additions and 523 deletions.
314 changes: 276 additions & 38 deletions Signum.Engine.Extensions/Dashboard/DashboardLogic.cs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Signum.Engine.Extensions/MachineLearning/PredictorLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static void RegisterPublication(PredictorPublicationSymbol publication, P
return Trainings.TryGetC(lite)?.Context;
}

public static void Start(SchemaBuilder sb, Func<IFileTypeAlgorithm> predictorFileAlgorithm)
public static void Start(SchemaBuilder sb, IFileTypeAlgorithm predictorFileAlgorithm)
{
if (sb.NotDefined(MethodInfo.GetCurrentMethod()))
{
Expand Down Expand Up @@ -143,7 +143,7 @@ public static void Start(SchemaBuilder sb, Func<IFileTypeAlgorithm> predictorFil
e.AccuracyValidation,
});

FileTypeLogic.Register(PredictorFileType.PredictorFile, predictorFileAlgorithm());
FileTypeLogic.Register(PredictorFileType.PredictorFile, predictorFileAlgorithm);

SymbolLogic<PredictorAlgorithmSymbol>.Start(sb, () => Algorithms.Keys);
SymbolLogic<PredictorColumnEncodingSymbol>.Start(sb, () => Algorithms.Values.SelectMany(a => a.GetRegisteredEncodingSymbols()).Distinct());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Signum.Analyzer" Version="3.2.0" />
<PackageReference Include="Signum.MSBuildTask" Version="5.0.0" />
<PackageReference Include="Signum.MSBuildTask" Version="6.0.0" />
<PackageReference Include="TensorFlow.Keras" Version="0.6.4" />
</ItemGroup>

Expand Down
1 change: 1 addition & 0 deletions Signum.Engine/Engine/SchemaSynchronizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,7 @@ public static string GetDefaultValue(ITable table, IColumn column, Replacements
column.DbType.IsString() ? "''" :
column.DbType.IsDate() ? "GetDate()" :
column.DbType.IsGuid() ? "NEWID()" :
column.DbType.IsTime() ? "'00:00'" :
"?");

string defaultValue = rep.Interactive ? SafeConsole.AskString($"Default value for '{table.Name.Name}.{column.Name}'? ([Enter] for {typeDefault} or 'force' if there are no {(forNewColumn ? "rows" : "nulls")}) ", stringValidator: str => null) : "";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,73 +1,76 @@
using System.Collections.Immutable;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Signum.Engine.Json;

public static class EntityJsonContext
{
public static JsonSerializerOptions FullJsonSerializerOptions;
static EntityJsonContext()
{
var ejcf = new EntityJsonConverterFactory();

FullJsonSerializerOptions = new JsonSerializerOptions
{
IncludeFields = true,
Converters =
{
ejcf,
new MListJsonConverterFactory(ejcf.AssertCanWrite),
new LiteJsonConverterFactory(),
new JsonStringEnumConverter(),
new TimeSpanConverter(),
new DateOnlyConverter()
}
};
}

static readonly ThreadVariable<ImmutableStack<(PropertyRoute pr, ModifiableEntity? mod, PrimaryKey? rowId)>?> currentPropertyRoute = Statics.ThreadVariable<ImmutableStack<(PropertyRoute pr, ModifiableEntity? mod, PrimaryKey ? rowId) >?>("jsonPropertyRoute");

public static (PropertyRoute pr, ModifiableEntity? mod, PrimaryKey? rowId)? CurrentPropertyRouteAndEntity
{
get { return currentPropertyRoute.Value?.Peek(); }
}

public static IRootEntity? FindCurrentRootEntity()
{
return currentPropertyRoute.Value?.FirstOrDefault(a => a.mod is IRootEntity).mod as IRootEntity;
}

public static PrimaryKey? FindCurrentRowId()
{
return currentPropertyRoute.Value?.Where(a => a.rowId != null).FirstOrDefault().rowId;
}

public static IDisposable SetCurrentPropertyRouteAndEntity((PropertyRoute, ModifiableEntity?, PrimaryKey? rowId) pair)
{
var old = currentPropertyRoute.Value;

currentPropertyRoute.Value = (old ?? ImmutableStack<(PropertyRoute pr, ModifiableEntity? mod, PrimaryKey? rowId)>.Empty).Push(pair);

return new Disposable(() => { currentPropertyRoute.Value = old; });
}

static readonly ThreadVariable<bool> allowDirectMListChangesVariable = Statics.ThreadVariable<bool>("allowDirectMListChanges");

public static bool AllowDirectMListChanges
{
get { return allowDirectMListChangesVariable.Value; }
}

public static IDisposable SetAllowDirectMListChanges(bool allowMListDirectChanges)
{
var old = allowDirectMListChangesVariable.Value;

allowDirectMListChangesVariable.Value = allowMListDirectChanges;

return new Disposable(() => { allowDirectMListChangesVariable.Value = old; });
}



}
using System.Collections.Immutable;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Signum.Engine.Json;

public static class EntityJsonContext
{
public static JsonSerializerOptions FullJsonSerializerOptions;
static EntityJsonContext()
{
var ejcf = new EntityJsonConverterFactory();

FullJsonSerializerOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
IncludeFields = true,
Converters =
{
ejcf,
new LiteJsonConverterFactory(),
new MListJsonConverterFactory(ejcf.AssertCanWrite),
new JsonStringEnumConverter(),
new ResultTableConverter(),
new TimeSpanConverter(),
new DateOnlyConverter(),
new TimeOnlyConverter()
}
};
}

static readonly ThreadVariable<ImmutableStack<(PropertyRoute pr, ModifiableEntity? mod, PrimaryKey? rowId)>?> currentPropertyRoute = Statics.ThreadVariable<ImmutableStack<(PropertyRoute pr, ModifiableEntity? mod, PrimaryKey ? rowId) >?>("jsonPropertyRoute");

public static (PropertyRoute pr, ModifiableEntity? mod, PrimaryKey? rowId)? CurrentPropertyRouteAndEntity
{
get { return currentPropertyRoute.Value?.Peek(); }
}

public static IRootEntity? FindCurrentRootEntity()
{
return currentPropertyRoute.Value?.FirstOrDefault(a => a.mod is IRootEntity).mod as IRootEntity;
}

public static PrimaryKey? FindCurrentRowId()
{
return currentPropertyRoute.Value?.Where(a => a.rowId != null).FirstOrDefault().rowId;
}

public static IDisposable SetCurrentPropertyRouteAndEntity((PropertyRoute, ModifiableEntity?, PrimaryKey? rowId) pair)
{
var old = currentPropertyRoute.Value;

currentPropertyRoute.Value = (old ?? ImmutableStack<(PropertyRoute pr, ModifiableEntity? mod, PrimaryKey? rowId)>.Empty).Push(pair);

return new Disposable(() => { currentPropertyRoute.Value = old; });
}

static readonly ThreadVariable<bool> allowDirectMListChangesVariable = Statics.ThreadVariable<bool>("allowDirectMListChanges");

public static bool AllowDirectMListChanges
{
get { return allowDirectMListChangesVariable.Value; }
}

public static IDisposable SetAllowDirectMListChanges(bool allowMListDirectChanges)
{
var old = allowDirectMListChangesVariable.Value;

allowDirectMListChangesVariable.Value = allowMListDirectChanges;

return new Disposable(() => { allowDirectMListChangesVariable.Value = old; });
}



}
26 changes: 23 additions & 3 deletions Signum.Engine/Json/FilterJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ public override bool CanConvert(Type objectType)

public override void Write(Utf8JsonWriter writer, FilterTS value, JsonSerializerOptions options)
{
throw new NotImplementedException();
if (value is FilterConditionTS fc)
JsonSerializer.Serialize(writer, fc, options);
else if (value is FilterGroupTS fg)
JsonSerializer.Serialize(writer, fg, options);
else
throw new UnexpectedValueException(value);
}

public override FilterTS? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
Expand Down Expand Up @@ -121,7 +126,7 @@ public override Filter ToFilter(QueryDescription qd, bool canAggregate, JsonSeri
public class ColumnTS
{
public string token;
public string displayName;
public string? displayName;

public Column ToColumn(QueryDescription qd, bool canAggregate)
{
Expand Down Expand Up @@ -280,7 +285,22 @@ public class QueryRequestTS
public List<OrderTS> orders;
public List<ColumnTS> columns;
public PaginationTS pagination;
public SystemTimeTS/*?*/ systemTime;
public SystemTimeTS? systemTime;

public static QueryRequestTS FromQueryRequest(QueryRequest qr)
{
return new QueryRequestTS
{
queryKey = QueryUtils.GetKey(qr.QueryName),
queryUrl = qr.QueryUrl,
groupResults = qr.GroupResults,
columns = qr.Columns.Select(c => new ColumnTS { token = c.Token.FullKey(), displayName = c.DisplayName }).ToList(),
filters = qr.Filters.Select(f => FilterTS.FromFilter(f)).ToList(),
orders = qr.Orders.Select(o => new OrderTS { orderType = o.OrderType, token = o.Token.FullKey() }).ToList(),
pagination = new PaginationTS(qr.Pagination),
systemTime = qr.SystemTime == null ? null : new SystemTimeTS(qr.SystemTime),
};
}

public QueryRequest ToQueryRequest(JsonSerializerOptions jsonSerializerOptions)
{
Expand Down
17 changes: 15 additions & 2 deletions Signum.Entities.Extensions/Dashboard/CachedQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,29 @@

namespace Signum.Entities.Dashboard;

[EntityKind(EntityKind.System, EntityData.Master), TicksColumn(false)]
[EntityKind(EntityKind.System, EntityData.Master)]
public class CachedQueryEntity : Entity
{
public Lite<DashboardEntity> Dashboard { get; set; }

[PreserveOrder, NoRepeatValidator]
[ImplementedBy(typeof(UserQueryEntity), typeof(UserChartEntity))]
public Lite<IUserAssetEntity> UserAsset { get; set; }
public MList<Lite<IUserAssetEntity>> UserAssets { get; set; } = new MList<Lite<IUserAssetEntity>>();

[DefaultFileType(nameof(CachedQueryFileType.CachedQuery), nameof(CachedQueryFileType))]
public FilePathEmbedded File { get; set; }

public int NumRows { get; set; }

public int NumColumns { get; set; }

public DateTime CreationDate { get; internal set; }

[Unit("ms")]
public long QueryDuration { get; set; }

[Unit("ms")]
public long UploadDuration { get; set; }
}

[AutoInit]
Expand Down
37 changes: 30 additions & 7 deletions Signum.Entities.Extensions/Dashboard/DashboardEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public Lite<TypeEntity>? EntityType

public bool CombineSimilarRows { get; set; } = true;

public CachedQueriesEmbedded? CachedQueries { get; set; }
public CacheQueryConfigurationEmbedded? CacheQueryConfiguration { get; set; }

[NotifyCollectionChanged, NotifyChildProperty]
[NoRepeatValidator]
Expand Down Expand Up @@ -123,13 +123,15 @@ public DashboardEntity Clone()
{
return new DashboardEntity
{
DisplayName = "Clone {0}".FormatWith(this.DisplayName),
DashboardPriority = DashboardPriority,
Parts = Parts.Select(p => p.Clone()).ToMList(),
Owner = Owner,
EntityType = this.EntityType,
EmbeddedInEntity = this.EmbeddedInEntity,
Owner = Owner,
DashboardPriority = DashboardPriority,
AutoRefreshPeriod = this.AutoRefreshPeriod,
DisplayName = "Clone {0}".FormatWith(this.DisplayName),
CombineSimilarRows = this.CombineSimilarRows,
CacheQueryConfiguration = this.CacheQueryConfiguration?.Clone(),
Parts = Parts.Select(p => p.Clone()).ToMList(),
Key = this.Key
};
}
Expand All @@ -144,6 +146,7 @@ public XElement ToXml(IToXmlContext ctx)
DashboardPriority == null ? null! : new XAttribute("DashboardPriority", DashboardPriority.Value.ToString()),
EmbeddedInEntity == null ? null! : new XAttribute("EmbeddedInEntity", EmbeddedInEntity.Value.ToString()),
new XAttribute("CombineSimilarRows", CombineSimilarRows),
CacheQueryConfiguration?.ToXml(ctx),
new XElement("Parts", Parts.Select(p => p.ToXml(ctx))));
}

Expand All @@ -156,6 +159,7 @@ public void FromXml(XElement element, IFromXmlContext ctx)
DashboardPriority = element.Attribute("DashboardPriority")?.Let(a => int.Parse(a.Value));
EmbeddedInEntity = element.Attribute("EmbeddedInEntity")?.Let(a => a.Value.ToEnum<DashboardEmbedededInEntity>());
CombineSimilarRows = element.Attribute("CombineSimilarRows")?.Let(a => bool.Parse(a.Value)) ?? false;
CacheQueryConfiguration = CacheQueryConfiguration.CreateOrAssignEmbedded(element.Element(nameof(CacheQueryConfiguration)), (cqc, elem) => cqc.FromXml(elem));
Parts.Synchronize(element.Element("Parts")!.Elements().ToList(), (pp, x) => pp.FromXml(x, ctx));
}

Expand All @@ -170,7 +174,7 @@ public void FromXml(XElement element, IFromXmlContext ctx)
return ValidationMessage._0IsNotAllowed.NiceToString(pi.NiceName());
}

if(pi.Name == nameof(CachedQueries) && CachedQueries != null && EntityType != null)
if(pi.Name == nameof(CacheQueryConfiguration) && CacheQueryConfiguration != null && EntityType != null)
{
return ValidationMessage._0ShouldBeNullWhen1IsSet.NiceToString(pi.NiceName(), NicePropertyName(() => EntityType));
}
Expand All @@ -179,10 +183,29 @@ public void FromXml(XElement element, IFromXmlContext ctx)
}
}

public class CachedQueriesEmbedded : EmbeddedEntity
public class CacheQueryConfigurationEmbedded : EmbeddedEntity
{
[Unit("s")]
public int TimeoutForQueries { get; set; } = 5 * 60;

public int MaxRows { get; set; } = 1000 * 1000;

internal CacheQueryConfigurationEmbedded Clone() => new CacheQueryConfigurationEmbedded
{
TimeoutForQueries = TimeoutForQueries,
MaxRows = MaxRows,
};

internal XElement ToXml(IToXmlContext ctx) => new XElement("CacheQueryConfiguration",
new XAttribute(nameof(TimeoutForQueries), TimeoutForQueries),
new XAttribute(nameof(MaxRows), MaxRows)
);

internal void FromXml(XElement elem)
{
TimeoutForQueries = elem.Attribute(nameof(TimeoutForQueries))?.Value.ToInt() ?? 5 * 60;
MaxRows = elem.Attribute(nameof(MaxRows))?.Value.ToInt() ?? 1000 * 1000;
}
}

[AutoInit]
Expand Down
Loading

0 comments on commit b77dcbf

Please sign in to comment.