Example #1
0
        public async Task <DataSet> GetInfoAsync(string dataSet)
        {
            var token = await GetTokenAsync();

            var info = await $"https://api.domo.com/v1/datasets/{dataSet}"
                       .WithOAuthBearerToken(token.access_token)
                       .GetJsonAsync <DataSet>();

            info.schema.columns = info.schema.columns
                                  .Select(column => {
                column.name = Slugify.ToSlug(column.name);
                return(column);
            })
                                  .Select(column => {
                int i = 0;
                while (info.schema.columns.Count(c => c.name == column.name) > 1)
                {
                    column.name = $"{column.name}-{i}";
                }
                return(column);
            })
                                  .ToList();

            return(info);
        }
        public async Task AddAsync(Report report)
        {
            var collection = await GetCollection();

            report.Slug = Slugify.ToSlug(report.Name);

            var slugNumber = 0;

            while (await GetAsync(report.Slug) != null)
            {
                slugNumber++;
                report.Slug = $"{report.Slug}-{slugNumber}";
            }

            await collection.InsertOneAsync(report);
        }
        public async Task <string> GenerateAsync(Report report, GenerationSettings settings)
        {
            string key = $"{DateTime.Now.ToString("yyyy-MM-dd/yyyy-MM-dd_HH-mm-ss")}_{Guid.NewGuid()}.zip";

            await mongoReportGenerationRepository.AddAsync(new ReportGeneration()
            {
                Slug   = report.Slug,
                Name   = key,
                Status = "Pending"
            });

            try
            {
                var data = await domoService.GetDataAsync(report, settings);

                string url = string.Empty;

                // TODO factor this to another file to make this class testable
                var tempPath = Path.GetTempFileName();
                using (var tempFile = File.Create(tempPath))
                    using (var zipArchive = new ZipArchive(tempFile, ZipArchiveMode.Create))
                    {
                        foreach (var group in data)
                        {
                            var convertedData = ConvertToData(group.Value);
                            var documents     = reportingCloudRepository.GenerateReport(report, convertedData);

                            // count on the order of items in the JArray and in the original group to be the same, to figure out filenames
                            // single sanity check: counts should be the same
                            var renameFiles        = false;
                            var uniqueFilenamesSet = new HashSet <string>();
                            if (documents.Count == convertedData.Count)
                            {
                                renameFiles = true;
                            }

                            for (var i = 0; i < documents.Count; i++)
                            {
                                var document = documents[i];

                                string filename;
                                if (renameFiles)
                                {
                                    var groupedValues = group
                                                        .Value
                                                        .Values
                                                        .ElementAt(i)
                                                        .Item1
                                                        .Values;

                                    filename = string.Join("_", groupedValues
                                                           .Skip(groupedValues.Count - settings.ChildGroups.Count)
                                                           .Select(value => Slugify.ToSlug(value)));

                                    if (uniqueFilenamesSet.Contains(filename))
                                    {
                                        filename = $"{filename}_{Guid.NewGuid().ToString()}";
                                    }
                                    else
                                    {
                                        uniqueFilenamesSet.Add(filename);
                                    }
                                }
                                else
                                {
                                    filename = Guid.NewGuid().ToString();
                                }

                                var zipEntry = zipArchive.CreateEntry($"{group.Key}/{filename}.pdf");
                                using (var zipStream = zipEntry.Open())
                                    using (var stream = new MemoryStream(document))
                                        await stream.CopyToAsync(zipStream);
                            }
                        }
                    }

                url = await s3Service.UploadAsync(tempPath, key);

                await mongoReportGenerationRepository.UpdateAsync(new ReportGeneration()
                {
                    Name   = key,
                    Status = "Success"
                });

                return(url);
            }
            catch (Exception)
            {
                await mongoReportGenerationRepository.UpdateAsync(new ReportGeneration()
                {
                    Name   = key,
                    Status = "Failed"
                });

                throw;
            }
        }