public async Task Run([TimerTrigger("5 * * * * *")] TimerInfo myTimer)
        {
            _logger.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

            // Authenticate to Azure Storage
            try
            {
                CloudBlobClient    cloudBlobClient         = _azureStorageHelpers.ConnectToBlobClient(_config[Settings.STORAGE_ACCOUNT_NAME], _config[Settings.STORAGE_ACCOUNT_KEY]);
                CloudBlobContainer modelCloudBlobContainer = _azureStorageHelpers.GetBlobContainer(cloudBlobClient, _config[Settings.MODEL_CONTAINER_NAME]);

                // Read File From Azure Storage
                string _trainDataPath = Path.Combine(Environment.CurrentDirectory, "Data", "taxi-fare-train.csv");
                string _testDataPath  = Path.Combine(Environment.CurrentDirectory, "Data", "taxi-fare-test.csv");
                string modelPath      = _config[Settings.MODEL_PATH];

                // Add Data to IDataView and Train Model
                await TrainAndSaveModel(_mlContext, _trainDataPath, _testDataPath, modelPath, modelCloudBlobContainer);

                _logger.LogInformation($"The model has been uploaded to {modelCloudBlobContainer.Name}. Saved as {modelPath}");
            }
            catch (Exception ex)
            {
                _logger.LogError($"Exception thrown: {ex.Message}");
                throw;
            }
        }
Exemple #2
0
        public async Task Run([CosmosDBTrigger(
                                   databaseName: "TelementryDB",
                                   collectionName: "DeviceReading",
                                   ConnectionStringSetting = "COSMOS_CONNECTION_STRING",
                                   LeaseCollectionName = "leases",
                                   CreateLeaseCollectionIfNotExists = true)] IReadOnlyList <Document> input)
        {
            try
            {
                List <DeviceReading> backupDocuments = new List <DeviceReading>();
                CloudBlobClient      cloudBlobClient = _azureStorageHelpers.ConnectToBlobClient(
                    _config[Constants.STORAGE_ACCOUNT_NAME],
                    _config[Constants.STORAGE_ACCOUNT_KEY]);
                CloudBlobContainer blobContainer = _azureStorageHelpers.GetBlobContainer(
                    cloudBlobClient,
                    _config[Constants.STORAGE_CONTAINER]);
                string backupFile = Path.Combine($"{DateTime.Now.ToString("dd-MM-yyyy")}-backup.json");

                if (input != null && input.Count > 0)
                {
                    foreach (var document in input)
                    {
                        // Persist to blob storage
                        var deviceReading = JsonConvert.DeserializeObject <DeviceReading>(document.ToString());
                        backupDocuments.Add(deviceReading);
                        _logger.LogInformation($"{document.Id} has been added to list");
                    }
                }

                var jsonData = JsonConvert.SerializeObject(backupDocuments);

                using (StreamWriter file = File.CreateText(backupFile))
                {
                    JsonSerializer serializer = new JsonSerializer();
                    serializer.Serialize(file, jsonData);
                }

                await _azureStorageHelpers.UploadBlobToStorage(blobContainer, backupFile);
            }
            catch (Exception ex)
            {
                _logger.LogWarning($"Something went wrong. Exception thrown: {ex.Message}");
                throw;
            }
        }