Exemple #1
0
        internal static string SerializeFile(ServiceTagFile file)
        {
            var options = new JsonSerializerOptions()
            {
                PropertyNameCaseInsensitive = true,
                IgnoreNullValues            = true
            };

            return(JsonSerializer.Serialize(file, typeof(ServiceTagFile), options));
        }
Exemple #2
0
        public static async Task  FnCompareFiles(
            [QueueTrigger("nextazureipfile", Connection = "FileStorAcc")] string fileName,
            ILogger log)
        {
            var containerName = Environment.GetEnvironmentVariable("region");
            var storage       = new StorageProvider();
            // in case of multiple runs check so see if there's already a delta - set to run as singleton , so this is belt and brace
            var existingDelta = await storage.FileExistsBlobStorageAsync(containerName, $"{fileName}.delta");

            if (existingDelta)
            {
                log.LogInformation($"Existing Delta found for {fileName}, duplicate run for the same file?");
                return;
            }
            // check for a previous change number
            var currentFileStream = await storage.ReadFileFromBlobToStorageAsync(containerName, fileName);

            var currentFile = DeserializeFile(StreamToString(currentFileStream));

            if (currentFile == null)
            {
                throw new Exception($"No valid file named {fileName} for container {containerName}");
            }
            if (currentFile.Values == null || currentFile.Values.Count == 0)
            {
                // empty file , remove (this can happen?)
                log.LogInformation("empty current file found, removing.");
                await storage.DeleteFileFromBlobStorageAsync(containerName, fileName);

                return;
            }
            var previousFile = await GetPreviousFileAsync(storage, containerName, fileName);

            var delta = CompareFiles(currentFile, previousFile);

            if (null != delta)
            {
                var newFileName = $"{fileName}.delta";
                var newFile     = new ServiceTagFile()
                {
                    Values = delta
                };
                var stream = StringToStream(SerializeFile(newFile));
                // AEG event will be raised for next func
                await storage.WriteStreamAsBlobToStorageAsync(stream, containerName, newFileName);

                // enqueue reference to delta for function to write access restrictions
                log.LogInformation($"C# function processed: {fileName} and wrote delta {newFileName}");
            }

            log.LogInformation($"C# function processed: {fileName}. No delta.");
        }
Exemple #3
0
        public static async Task  FnActionDelta(
            [EventGridTrigger] EventGridEvent eventGridEvent,
            ILogger log)
        {
            // pull the filename from the event
            log.LogInformation($"Event received {eventGridEvent.Id}");

            StorageBlobCreatedEventData data = (eventGridEvent.Data as JObject).ToObject <StorageBlobCreatedEventData>();
            var fileName = string.Empty;
            var uri      = new Uri(data.Url);

            fileName = System.IO.Path.GetFileName(uri.LocalPath);

            log.LogInformation($"filename is {fileName}");


            // check its the file we're interested in
            if (!fileName.EndsWith(".delta"))
            {
                log.LogInformation($"Not interested in this event { fileName}");
                return;
            }
            var storage       = new StorageProvider();
            var newFileName   = $"{fileName}.completed";
            var containerName = Environment.GetEnvironmentVariable("region");

            // set to run as singleton so this is belt and brace
            if (await storage.FileExistsBlobStorageAsync(containerName, newFileName))
            {
                log.LogInformation($"This delta received already and completed. No further action.");
                return;
            }

            log.LogInformation($"delta {fileName} received");

            //
            // action delta
            //

            var newFile = new ServiceTagFile()
            {
                ChangeNumber = fileName
            };
            var stream = StringToStream(SerializeFile(newFile));
            await storage.WriteStreamAsBlobToStorageAsync(stream, containerName, newFileName);

            // enqueue reference to delta for function to write access restrictions
            log.LogInformation($"C# function actioned {fileName} and wrote completed {newFileName}");
        }
Exemple #4
0
        internal static List <ServiceTagResult> CompareFiles(ServiceTagFile currentFile, ServiceTagFile previousFile)
        {
            if (currentFile == null || currentFile.Values == null || currentFile.Values.Count == 0)
            {
                throw new Exception("Invalid current file, no values");
            }
            if (previousFile == null)
            {
                // no prior file , no delta
                return(currentFile.Values);
            }
            if (previousFile.Values == null || previousFile.Values.Count == 0)
            {
                throw new Exception("prior file found, but no values");
            }

            var delta = new List <ServiceTagResult>();

            foreach (var value in currentFile.Values)
            {
                var priorValueById = previousFile.Values.Find(v => v.Id == value.Id);
                if (null == priorValueById)
                {
                    // did not exist before, or must be new this time as current value property is #1
                    delta.Add(value);
                }
                else
                {
                    // compare versions
                    var isNewer = CompareChangeNumbers(value, priorValueById);
                    if (isNewer)
                    {
                        delta.Add(value);
                    }
                }
            }

            if (delta.Count == 0)
            {
                delta = null;
            }
            return(delta);
        }