/// <summary> /// Converts stub AST query definition into local, fully hydrated AST. /// The returned AST is unvalidated, and has a UniversalId property set if the incoming query has one. /// </summary> /// <returns>The panels async.</returns> /// <param name="query">Stub AST query definition.</param> /// <param name="token">Cancellation token.</param> public async Task <PanelValidationContext> GetPanelsAsync(IQuerySaveDTO query, CancellationToken token) { token.ThrowIfCancellationRequested(); var validationContext = await GetPanelsAsync(query); validationContext.SetUniversalId(query.UniversalId); return(validationContext); }
public async Task <SaveResult> SaveAsync(Guid id, IQuerySaveDTO ast, Func <IQueryDefinition, string> json, CancellationToken cancel) { log.LogInformation("Saving query. Id:{Id} Ast:{Ast}", id, ast); var ctx = await converter.GetPanelsAsync(ast, cancel); log.LogInformation("Save query panel validation context. Context:{@Context}", ctx); if (!ctx.PreflightPassed) { return(new SaveResult { State = SaveState.Preflight, Preflight = ctx.PreflightCheck }); } var query = validator.Validate(ctx); cancel.ThrowIfCancellationRequested(); if (!user.IsInstitutional) { converter.LocalizeDefinition(ast, query); log.LogInformation("Localized federated query. Id:{Id} Ast:{Ast}", id, ast); } var toSave = new QuerySave { QueryId = id, UniversalId = ctx.UniversalId, Name = ast.Name, Category = ast.Category, Definition = json(ast), Resources = query.Panels.GetResources() }; // if ast already has an assive version, use it if (ast.Ver.HasValue) { toSave.Ver = ast.Ver.Value; } try { log.LogInformation("Saving query. Query:{Query} Payload:{@Payload}", id, toSave); var saved = await ImplSaveAsync(toSave); if (saved == null) { log.LogError("Could not save query, not found. Query:{Query}", id); return(new SaveResult { State = SaveState.NotFound, Preflight = ctx.PreflightCheck, Result = null }); } log.LogInformation("Saved query. Query:{@Query}", toSave); return(new SaveResult { State = SaveState.Ok, Preflight = ctx.PreflightCheck, Result = saved }); } catch (DbException de) { log.LogError("Failed to save query. Query:{@Query} Code:{Code} Error:{Error}", toSave, de.ErrorCode, de.Message); de.MapThrow(); throw; } }