async Task ExportTemplates <TEntity>(
            Func <string, Task <TEntity> > findEntity,
            Func <Task <List <TEntity> > > listEntities,
            Func <TEntity, string> getTitle,
            TemplateValueMap templateValueMap)
            where TEntity : Entity
        {
            List <TEntity> entities;

            if (!_include.Any())
            {
                entities = await listEntities();
            }
            else
            {
                var idPrefix = EntityName.FromEntityType(typeof(TEntity)) + "-";
                entities = new List <TEntity>();
                foreach (var id in _include.Where(i => i != null && i.StartsWith(idPrefix)))
                {
                    entities.Add(await findEntity(id !));
                }
            }

            foreach (var entity in entities)
            {
                var filename = OutputFilename <TEntity>(getTitle(entity));
                templateValueMap.AddReferencedTemplate(entity.Id, Path.GetFileName(filename));
                await using var f = File.Create(filename);
                await using var w = new StreamWriter(f, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
                await TemplateWriter.WriteTemplateAsync(w, entity, templateValueMap);

                Log.Information("Exported {EntityId} to {Filename}", entity.Id, filename);
            }
        }
Exemple #2
0
 static async Task WriteValueAsync(JsonWriter jw, object?v, TemplateValueMap templateValueMap)
 {
     await(v switch
     {
         null => jw.WriteNullAsync(),
         string s => jw.WriteValueAsync(s),
         IEnumerable a => WriteArrayAsync(jw, a, templateValueMap),
         var o when o.GetType().IsClass => WriteObjectAsync(jw, o, templateValueMap),
         var e when e.GetType().IsEnum => jw.WriteValueAsync(e.ToString()),
         _ => jw.WriteValueAsync(v)
     });
        public async Task ExportTemplateSet()
        {
            var templateValueMap = new TemplateValueMap();

            templateValueMap.MapNonNullAsArg <DashboardEntity>(nameof(DashboardEntity.OwnerId), "ownerId");
            templateValueMap.MapNonNullAsArg <SignalEntity>(nameof(SignalEntity.OwnerId), "ownerId");
            templateValueMap.MapNonNullAsArg <SqlQueryEntity>(nameof(SqlQueryEntity.OwnerId), "ownerId");
            templateValueMap.MapNonNullAsArg <WorkspaceEntity>(nameof(WorkspaceEntity.OwnerId), "ownerId");
            templateValueMap.MapNonNullAsArg <NotificationChannelPart>(nameof(NotificationChannelPart.NotificationAppInstanceId), "notificationAppInstanceId");
            templateValueMap.MapAsReference <SignalExpressionPart>(nameof(SignalExpressionPart.SignalId));
            templateValueMap.MapAsReferenceList <WorkspaceContentPart>(nameof(WorkspaceContentPart.DashboardIds));
            templateValueMap.MapAsReferenceList <WorkspaceContentPart>(nameof(WorkspaceContentPart.QueryIds));
            templateValueMap.MapAsReferenceList <WorkspaceContentPart>(nameof(WorkspaceContentPart.SignalIds));

            await ExportTemplates <SignalEntity>(
                id => _connection.Signals.FindAsync(id),
                () => _connection.Signals.ListAsync(shared: true),
                signal => signal.Title,
                templateValueMap);

            await ExportTemplates <SqlQueryEntity>(
                id => _connection.SqlQueries.FindAsync(id),
                () => _connection.SqlQueries.ListAsync(shared: true),
                query => query.Title,
                templateValueMap);

            await ExportTemplates <DashboardEntity>(
                id => _connection.Dashboards.FindAsync(id),
                () => _connection.Dashboards.ListAsync(shared: true),
                dashboard => dashboard.Title,
                templateValueMap);

            await ExportTemplates <AlertEntity>(
                id => _connection.Alerts.FindAsync(id),
                () => _connection.Alerts.ListAsync(shared: true),
                alert => alert.Title,
                templateValueMap);

            await ExportTemplates <WorkspaceEntity>(
                id => _connection.Workspaces.FindAsync(id),
                () => _connection.Workspaces.ListAsync(shared: true),
                workspace => workspace.Title,
                templateValueMap);

            await ExportTemplates <RetentionPolicyEntity>(
                id => _connection.RetentionPolicies.FindAsync(id),
                () => _connection.RetentionPolicies.ListAsync(),
                retentionPolicy => retentionPolicy.Id.Replace("retentionpolicy-", ""),
                templateValueMap);
        }
Exemple #4
0
        public static async Task WriteTemplateAsync(TextWriter writer, Entity entity, TemplateValueMap templateValueMap)
        {
            using var jw = new JsonTextWriter(writer)
                  {
                      Formatting          = Formatting.Indented,
                      FloatFormatHandling = FloatFormatHandling.String,
                      DateFormatHandling  = DateFormatHandling.IsoDateFormat,
                      Culture             = CultureInfo.InvariantCulture
                  };

            await WriteObjectAsync(jw, entity, templateValueMap, annotateAsResource : true);
        }