Пример #1
0
        private byte[] GetUploadFileBytes(AppendedResults singleResult, UploadContext uploadContext)
        {
            var jsonBytes = Encoding.UTF8.GetBytes(singleResult.ToJson());

            if (!uploadContext.HasAttachments)
            {
                return(jsonBytes);
            }

            using (var stream = new MemoryStream())
            {
                using (var archive = new ZipArchive(stream, ZipArchiveMode.Update, true))
                {
                    AddArchiveEntry(archive, new Attachment
                    {
                        Content       = jsonBytes,
                        Path          = uploadContext.UploadedFilename,
                        LastWriteTime = DateTimeOffset.UtcNow,
                        ByteSize      = jsonBytes.Length
                    });

                    foreach (var attachment in uploadContext.Attachments)
                    {
                        AddArchiveEntry(archive, attachment);
                    }
                }

                stream.Position = 0;

                var zipBytes = stream.GetBuffer();

                return(zipBytes);
            }
        }
Пример #2
0
        private string SaveLargeVisits(AppendedResults appendedResults, List <FieldVisitInfo> largeVisits, string path)
        {
            var largePath = Path.Combine(
                // ReSharper disable once AssignNullToNotNullAttribute
                Path.GetDirectoryName(path),
                $"{Path.GetFileNameWithoutExtension(path)}.LargeDuration.json");

            var largeResults = new AppendedResults
            {
                PluginAssemblyQualifiedTypeName = appendedResults.PluginAssemblyQualifiedTypeName,
                FrameworkAssemblyQualifiedName  = appendedResults.FrameworkAssemblyQualifiedName,
                AppendedVisits = largeVisits
            };

            Log.Info($"Saving {largeResults.AppendedVisits.Count} visits data to '{largePath}'");

            File.WriteAllText(largePath, largeResults.ToJson().IndentJson());

            return(largePath);
        }
Пример #3
0
        private void UploadVisit(FieldVisitInfo visit,
                                 UploadContext uploadContext,
                                 Action partialAction,
                                 Action failureAction,
                                 Action duplicateAction = null)
        {
            if (ShouldSkipConflictingVisits(visit))
            {
                partialAction();
                return;
            }

            if (Context.DryRun)
            {
                Log.Warn($"Dry-run: Would upload '{visit.FieldVisitIdentifier}'");
                return;
            }

            var singleResult = new AppendedResults
            {
                FrameworkAssemblyQualifiedName  = uploadContext.AppendedResults.FrameworkAssemblyQualifiedName,
                PluginAssemblyQualifiedTypeName = uploadContext.AppendedResults.PluginAssemblyQualifiedTypeName,
                AppendedVisits = new List <FieldVisitInfo> {
                    visit
                }
            };

            try
            {
                using (var stream = new MemoryStream(GetUploadFileBytes(singleResult, uploadContext)))
                {
                    var uploadedFilename = ComposeUploadedFilename(uploadContext, visit);

                    var response = Client.Acquisition.PostFileWithRequest(stream, uploadedFilename,
                                                                          new PostVisitFileToLocation
                    {
                        LocationUniqueId = Guid.Parse(visit.LocationInfo.UniqueId)
                    });

                    Log.Info($"Uploaded '{uploadedFilename}' {visit.FieldVisitIdentifier} to '{visit.LocationInfo.LocationIdentifier}' using {response.HandledByPlugin.Name} plugin");
                }
            }
            catch (WebServiceException exception)
            {
                if (IsDuplicateAttachmentException(exception))
                {
                    Log.Info($"{uploadContext.UploadedFilename}: Skipping already uploaded content.");
                    return;
                }

                if (duplicateAction != null && IsDuplicateFailure(exception))
                {
                    Log.Warn($"{uploadContext.UploadedFilename}: Saving {visit.FieldVisitIdentifier} for later retry: {exception.ErrorCode} {exception.ErrorMessage}");

                    duplicateAction();
                    return;
                }

                Log.Error($"{uploadContext.UploadedFilename}: {visit.FieldVisitIdentifier}: {exception.ErrorCode} {exception.ErrorMessage}");
                failureAction();
            }
        }