void ClearTable(CloudTable table)
        {
            var deviceIds = _deviceService.GetDeviceIds();

            foreach (var partitionKey in deviceIds)
            {
                TableBatchOperation batchDelete = new TableBatchOperation();

                // gets all the entities in the table for this partition key
                string partitionCondition = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey);
                List<DynamicTableEntity> entities = table.ExecuteQuery(new TableQuery().Where(partitionCondition)).ToList();

                entities.ForEach(e =>
                {
                    batchDelete.Add(TableOperation.Delete(e));

                    // Azure has a limit on batch operations
                    if (batchDelete.Count == 100)
                    {
                        table.ExecuteBatch(batchDelete);
                        batchDelete = new TableBatchOperation();
                    }
                });

                // flush out whatever is left
                if (batchDelete.Count > 0)
                {
                    table.ExecuteBatch(batchDelete);
                }
            }
        }
        public IEnumerable <T> GetRecords <T>()
        {
            IEnumerable <DynamicTableEntity> entities = TestCloudTable.ExecuteQuery(new TableQuery());

            return(entities.Select(e => TableEntity.ConvertBack <T>(e.Properties, new OperationContext())));
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            Console.WriteLine("inicia proceso. Presione cualquier tecla.");
            Console.ReadLine();
            CloudStorageAccount cuentaAlmacenamiento = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("TableConnectionString"));
            CloudTableClient    clienteTablas        = cuentaAlmacenamiento.CreateCloudTableClient();

            CloudTable tabla = clienteTablas.GetTableReference("documentos");

            //tabla.DeleteIfExists();
            //Console.WriteLine("Tabla eliminada");


            tabla.CreateIfNotExists();

            var tablas = clienteTablas.ListTables();

            foreach (CloudTable item in tablas)
            {
                Console.WriteLine(item.Name);
            }


            Profesor profeUno = new Profesor(Guid.NewGuid().ToString(), "Profesores");

            profeUno.NombreProfesor   = "Ricardo Selis";
            profeUno.NombreAsignatura = "Microcontroladores";

            Profesor profeDos = new Profesor(Guid.NewGuid().ToString(), "Profesores");

            profeDos.NombreProfesor   = "Carlos Paredes";
            profeDos.NombreAsignatura = "Diseno Audiovisual";

            Profesor profeTres = new Profesor("007", "Profesores");

            profeTres.NombreProfesor   = "Victoria Moreira";
            profeTres.NombreAsignatura = "Arquitectura";

            TableOperation insertarProfeUno  = TableOperation.Insert(profeUno);
            TableOperation insertarProfeDos  = TableOperation.Insert(profeDos);
            TableOperation insertarProfeTres = TableOperation.Insert(profeTres);

            tabla.Execute(insertarProfeUno);
            tabla.Execute(insertarProfeDos);
            tabla.Execute(insertarProfeTres);

            Console.WriteLine("Se han insertado todos los profesores creados");



            TableOperation operacionModificar = TableOperation.Retrieve <Profesor>("Profesores", "002");
            TableResult    resultadoObtenido  = tabla.Execute(operacionModificar);
            Profesor       entidadModificada  = (Profesor)resultadoObtenido.Result;

            if (entidadModificada != null)
            {
                entidadModificada.NombreProfesor   = "Rocio Pinzon";
                entidadModificada.NombreAsignatura = "Baile";
                TableOperation operacionActualizar = TableOperation.Replace(entidadModificada);
                tabla.Execute(operacionActualizar);
                Console.WriteLine("Entidad Actualizada");
            }
            else
            {
                Console.WriteLine("No se encontro la entidad");
            }



            TableQuery <Profesor> consulta = new TableQuery <Profesor>().Where(TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.GreaterThan, "000"));

            foreach (Profesor profe in tabla.ExecuteQuery(consulta))
            {
                Console.WriteLine("{0}, {1}\t{2}\t{3}", profe.PartitionKey, profe.RowKey, profe.NombreProfesor, profe.NombreAsignatura);
            }



            TableOperation operacionEliminar         = TableOperation.Retrieve <Profesor>("Profesores", "007");
            TableResult    resultadoObtenidoEliminar = tabla.Execute(operacionEliminar);
            Profesor       entidadEliminar           = (Profesor)resultadoObtenidoEliminar.Result;

            if (entidadEliminar != null)
            {
                TableOperation operacionDelete = TableOperation.Delete(entidadEliminar);
                tabla.Execute(operacionDelete);
                Console.WriteLine("Entidad Actualizada");
            }
            else
            {
                Console.WriteLine("No se encontro la entidad");
            }


            Console.ReadLine();
        }
Exemplo n.º 4
0
        public static void DBConnection()
        {
            string ConnectionString = "DefaultEndpointsProtocol=https;AccountName=xttestnorthcentralus;AccountKey=1oJNP/ABfLW2Aqd0s2o7NLrTEtAGtPUu33GEDuOsRH1h23Nylcsq5tOOmYoXB0qtSiHcPjZ94sWaLlll0fcDbw==;EndpointSuffix=core.windows.net";

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConnectionString);
            CloudTableClient    tableClient    = storageAccount.CreateCloudTableClient();

            // Create the CloudTable object.

            string filter = "(PartitionKey eq 'e58c78fd-ae8b-45f1-bc2a-afdaa71e4c14' or PartitionKey eq 'eb2775af-00e5-48cf-ad7b-5b1848ab76e1' or PartitionKey eq 'ab5b0cd3-cd4d-4bcb-aa54-9c1a80fd49fc' or " +
                            "PartitionKey eq '8865aa8b-1878-42cd-953b-da399e68fed9' or PartitionKey eq '7ea66689-dccc-4bd2-b47d-993e5c4914e6' or PartitionKey eq '6466bb17-128a-4e89-b4c9-681293f09953' " +
                            " or PartitionKey eq '3991f177-0e7e-4adc-9ffd-0665a44c16d9') and " +
                            "RowKey gt 'j_' and RowKey lt 'j`'";

            string filter2 = "(PartitionKey eq 'e58c78fd-ae8b-45f1-bc2a-afdaa71e4c14') and " +
                             "RowKey gt 'j_' and RowKey lt 'j`'";


            CloudTable table = tableClient.GetTableReference("XTransport");

            var          query = new TableQuery <JobEntity>().Where(filter2).Take(100);
            StreamWriter file  =
                new StreamWriter(@"C:\Users\arja\source\repos\ConsoleApp1\ConsoleApp1\TextFile1.txt");

            //file.WriteLine(filter);

            file.WriteLine();

            Console.WriteLine("FilterCondition is : " + filter);

            var res = table.ExecuteQuery(query);;


            double totalTime = 0;

            int count = 0;

            for (int i = 0; i < 1000; i++)
            {
                double startTime = (DateTime.Now - DateTime.MinValue).TotalMilliseconds;
                res = table.ExecuteQuery(query);

                double endTime = 0;
                foreach (var job in res)
                {
                    endTime = (DateTime.Now - DateTime.MinValue).TotalMilliseconds;
                    break;
                }

                count++;

                double currentExecutionTime = (endTime - startTime);

                totalTime = totalTime + (endTime - startTime);

                Console.WriteLine("Execution Time at Iteration " + count + " : " + (endTime - startTime));
                file.WriteLine("Execution Time at Iteration " + count + " : " + (endTime - startTime));
                file.WriteLine();
            }

            file.WriteLine();
            file.WriteLine("***************Average Time In Execution***********");
            file.WriteLine();
            file.WriteLine("Avg execution : " + totalTime / (1000 * count) + " seconds");

            Console.WriteLine("Avg execution : " + totalTime / (1000 * count));

            file.Close();
        }
Exemplo n.º 5
0
        private void ProcessMessage(Message messageToProcess, string restartFlag)
        {
            // Get Mailing List info to get the "From" email address.
            var retrieveOperation = TableOperation.Retrieve <MailingList>(messageToProcess.ListName, "mailinglist");
            var retrievedResult   = mailingListTable.Execute(retrieveOperation);
            var mailingList       = retrievedResult.Result as MailingList;

            if (mailingList == null)
            {
                Trace.TraceError("Mailing list not found: " + messageToProcess.ListName + " for message: " + messageToProcess.MessageRef);
                return;
            }
            // Get email addresses for this Mailing List.
            string filter = TableQuery.CombineFilters(
                TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, messageToProcess.ListName),
                TableOperators.And,
                TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.NotEqual, "mailinglist"));
            var query       = new TableQuery <Subscriber>().Where(filter);
            var subscribers = mailingListTable.ExecuteQuery(query).ToList();

            aiClient.TrackMetric("SubscriberCount", subscribers.Count);
            foreach (Subscriber subscriber in subscribers)
            {
                // Verify that the subscriber email address has been verified.
                if (subscriber.Verified == false)
                {
                    Trace.TraceInformation("Subscriber " + subscriber.EmailAddress + " not Verified, so not queuing ");
                    continue;
                }

                // Create a SendEmail entity for this email.
                var sendEmailRow = new SendEmail
                {
                    PartitionKey     = messageToProcess.PartitionKey,
                    RowKey           = messageToProcess.MessageRef.ToString() + subscriber.EmailAddress,
                    EmailAddress     = subscriber.EmailAddress,
                    EmailSent        = false,
                    MessageRef       = messageToProcess.MessageRef,
                    ScheduledDate    = messageToProcess.ScheduledDate,
                    FromEmailAddress = mailingList.FromEmailAddress,
                    SubjectLine      = messageToProcess.SubjectLine,
                    SubscriberGUID   = subscriber.SubscriberGUID,
                    ListName         = mailingList.ListName
                };

                // When we try to add the entity to the SendEmail table,
                // an exception might happen if this worker role went
                // down after processing some of the email addresses and then restarted.
                // In that case the row might already be present, so we do an Upsert operation.
                try
                {
                    var upsertOperation = TableOperation.InsertOrReplace(sendEmailRow);
                    messageTable.Execute(upsertOperation);
                }
                catch (Exception ex)
                {
                    string err = "Error creating SendEmail row:  " + ex.Message;
                    if (ex.InnerException != null)
                    {
                        err += " Inner Exception: " + ex.InnerException;
                    }
                    Trace.TraceError(err, ex);
                }

                // Create the queue message.
                string queueMessageString =
                    sendEmailRow.PartitionKey + "," +
                    sendEmailRow.RowKey + "," +
                    restartFlag;
                var queueMessage = new CloudQueueMessage(queueMessageString);
                sendEmailQueue.AddMessage(queueMessage);
            }

            Trace.TraceInformation("ProcessMessage end PK: "
                                   + messageToProcess.PartitionKey);
        }
        public ActionResult ShopInfoPage(string building, string floor, string location)
        {
            string tempURL = "";

            switch (building)
            {
            case "Dongsan":
                ViewBag.Building = "동산상가";
                tempURL         += building;
                break;

            case "FifthBuilding":
                ViewBag.Building = "5지구";
                tempURL         += "Fifth";
                break;

            case "SecondBuilding":
                ViewBag.Building = "2지구";
                tempURL         += "Second";
                break;
            }
            ViewBag.Floor = floor;
            switch (floor)
            {
            case "지하1층":
                tempURL += "BaseFirst";
                break;

            case "1층":
                tempURL += "First";
                break;

            case "2층":
                tempURL += "Second";
                break;

            case "3층":
                tempURL += "Third";
                break;

            case "4층":
                tempURL = "Forth";
                break;
            }
            ViewBag.URL      = tempURL;
            ViewBag.Location = location;
            //return View();


            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("MS_AzureStorageAccountConnectionString"));

            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
            CloudTable       table       = tableClient.GetTableReference(building);
            // Construct the query operation for all customer entities where PartitionKey="Smith".

            // Create a retrieve operation that takes a customer entity.
            TableOperation retrieveOperation = TableOperation.Retrieve <ShopInfoEntity>(floor, location);

            // Execute the retrieve operation.
            TableResult retrievedResult = table.Execute(retrieveOperation);

            ShopInfoEntity retrievedEntity = (ShopInfoEntity)retrievedResult.Result;

            if (retrievedEntity == null)
            {
                ViewBag.Location = location;
                return(RedirectToAction("EmptyShop", new { Building = ViewBag.Building, Floor = floor, Shop = location }));
            }

            // Print the phone number of the result.
            CloudTable     tableUserInfo             = tableClient.GetTableReference("UserInformation");
            TableOperation retrieveUserInfoOperation = TableOperation.Retrieve <UserInfoEntity>(retrievedEntity.OwnerID, building + ":" + floor + ":" + location);

            TableResult    retrievedUserInfoResult = tableUserInfo.Execute(retrieveUserInfoOperation);
            UserInfoEntity retrievedUserInfoEntity = (UserInfoEntity)retrievedUserInfoResult.Result;

            //오너 값으로 등록된 사진 가져오기
            string[] tempOwnerId = retrievedEntity.OwnerID.Split('@');

            CloudTable tableOwner = tableClient.GetTableReference(tempOwnerId[0]);

            TableQuery <ContentsEntity> rangeQuery = new TableQuery <ContentsEntity>().Where(
                TableQuery.GenerateFilterCondition("ShopName", QueryComparisons.Equal, retrievedEntity.ShopName));

            //IDictionary<string, string> myActivity = new Dictionary<string, string>
            //{
            //    { "ShopName", retrievedEntity.ShopName },
            //    { "ShopOwner", retrievedEntity.OwnerID.Split('@')[0] },
            //    { "PhoneNumber", retrievedUserInfoEntity.PhoneNumber }
            //};
            ViewBag.ShopName    = retrievedEntity.ShopName;
            ViewBag.PhoneNumber = retrievedUserInfoEntity.PhoneNumber;
            List <IndexToView> myActivity = new List <IndexToView>();

            foreach (ContentsEntity entity in tableOwner.ExecuteQuery(rangeQuery))
            {
                var imageURL = "https://westgateproject.blob.core.windows.net/" + entity.PartitionKey.Split('@')[0] + "/" + entity.RowKey;
                var text     = entity.Context;

                IndexToView temp = new IndexToView(imageURL, text);
                myActivity.Add(temp);
            }
            myActivity.Reverse();
            return(View(myActivity));
            //return View();
        }
        public static void Run([TimerTrigger("0 30 * * * *", RunOnStartup = true)] TimerInfo myTimer, TraceWriter log)
        {
            log.Info($"C# Timer trigger function executed at: {DateTime.Now}");

            int numberOfStates = 12;
            // Retrieve storage account from connection string.
            var storageAccount           = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("AzureWebJobsStorage"));
            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
            CloudTable       table       = tableClient.GetTableReference("snotelmergetracker");

            table.CreateIfNotExists();
#if DEBUG
            int numberOfHoursToCheck = 3 * 7 * 24; //was  1;
#else
            int numberOfHoursToCheck = 7 * 24;     //one week
#endif
            //look back x days and fill in any missing values;
            TableQuery <FileProcessedTracker> dateQuery = new TableQuery <FileProcessedTracker>().Where(
                TableQuery.CombineFilters(
                    TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionName),
                    TableOperators.And,
                    TableQuery.GenerateFilterConditionForDate("ForecastDate", QueryComparisons.GreaterThan, DateTime.UtcNow.AddDays(-1 * numberOfHoursToCheck))
                    )
                );

            var results = table.ExecuteQuery(dateQuery);

            //refactoring the below code to a shared method can cause an .net issue
            //related to binding redirect to arise; leave this here for now.  See AzureUtilities.cs
            //for more info
            log.Info($"Attempting to sign in to ad for datalake upload");
            var adlsAccountName = CloudConfigurationManager.GetSetting("ADLSAccountName");

            //auth secrets
            var domain              = CloudConfigurationManager.GetSetting("Domain");
            var webApp_clientId     = CloudConfigurationManager.GetSetting("WebAppClientId");
            var clientSecret        = CloudConfigurationManager.GetSetting("ClientSecret");
            var clientCredential    = new ClientCredential(webApp_clientId, clientSecret);
            var creds               = ApplicationTokenProvider.LoginSilentAsync(domain, clientCredential).Result;
            var fullAdlsAccountName = CloudConfigurationManager.GetSetting("ADLSFullAccountName");

            // Create client objects and set the subscription ID
            var adlsFileSystemClient = new DataLakeStoreFileSystemManagementClient(creds);
            var adlsClient           = AdlsClient.CreateClient(fullAdlsAccountName, creds);
            var checkDate            = DateTime.UtcNow.AddHours(-1 * numberOfHoursToCheck);
            while (checkDate < DateTime.UtcNow)
            {
                //has it already been marked as complete in the table
                string nameToCheck = SnotelUtilities.CreateSnotelFileDate(checkDate) + ".snotel.csv";
                if (results.Where(r => r.RowKey == nameToCheck).Count() == 0)
                {
                    log.Info($"{nameToCheck} doesn't exist in completed table, need to see if all files exist to concat");
                    var lexStartAndEnd    = SnotelUtilities.CreateSnotelFileDate(checkDate);
                    var hourlyFilesOnAdls = adlsClient.EnumerateDirectory(csvDirectory).Where(f => f.Name.StartsWith(lexStartAndEnd)).Select(f => f.Name).ToList();
                    if (hourlyFilesOnAdls.Count == numberOfStates)
                    {
                        if (ConcatFiles(adlsClient, nameToCheck, hourlyFilesOnAdls))
                        {
                            //mark file as finished in table
                            FileProcessedTracker tracker = new FileProcessedTracker {
                                ForecastDate = checkDate, PartitionKey = partitionName, RowKey = nameToCheck, Url = "unknown"
                            };
                            table.Execute(TableOperation.Insert(tracker));
                        }
                        else
                        {
                            log.Error($"Missing data for {checkDate} need to manually backfill, can't concat");
                        }
                    }
                    else
                    {
                        log.Info($"all state files don't exist for {checkDate}, waiting until next run");
                    }
                }
                else
                {
                    log.Info($"{nameToCheck} marked as already concated");
                }
                checkDate = checkDate.AddHours(1);
            }
        }
Exemplo n.º 8
0
        public List <UserEntity> GetAll()
        {
            TableQuery <UserEntity> query = new TableQuery <UserEntity>();

            return(table.ExecuteQuery(query).ToList());
        }
Exemplo n.º 9
0
        /// <summary>
        /// Returns a list of all visible nodes in all groups
        /// </summary>
        /// <param name="instanceTable"></param>
        /// <returns></returns>
        internal static IEnumerable <VertexTable> GetAll(CloudTable instanceTable)
        {
            TableQuery <VertexTable> query = new TableQuery <VertexTable>();

            return(instanceTable.ExecuteQuery(query));
        }
Exemplo n.º 10
0
        private static async Task DealUserToAsset(long minAmount, long maxAmount, string yemUserProductAzureTableName, string writeAssetAzureTable, string writeYemUserProductAzureTable,
                                                  CloudTableClient tableClient, string yemUserPurchase, List <OnSellAssetDto> onSellAssetDtos)
        {
            CloudTable cloudPurchaseOrderTable = tableClient.GetTableReference(yemUserProductAzureTableName);
            TableQuery <YemUserProductDto> queryPurchaseOrder;

            // ReSharper disable once ConvertIfStatementToConditionalTernaryExpression
            if (maxAmount > 0)
            {
                //queryPurchaseOrder = new TableQuery<YemUserProductDto>()
                //    .Where($"PartitionKey eq '{yemUserPurchase}' and  RemainingAmount ge {minAmount} and RemainingAmount lt {maxAmount} and IsLock eq false");
                string startsWithCondition = TableQuery.CombineFilters(
                    TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, yemUserPurchase),
                    TableOperators.And,
                    TableQuery.GenerateFilterConditionForLong("RemainingAmount", QueryComparisons.GreaterThanOrEqual, minAmount));
                string filterCondition = TableQuery.CombineFilters(
                    TableQuery.GenerateFilterConditionForLong("RemainingAmount", QueryComparisons.LessThan, maxAmount),
                    TableOperators.And,
                    startsWithCondition
                    );
                string endCondition = TableQuery.CombineFilters(
                    TableQuery.GenerateFilterConditionForBool("IsLock", QueryComparisons.Equal, false),
                    TableOperators.And,
                    filterCondition
                    );
                queryPurchaseOrder = new TableQuery <YemUserProductDto>().Where(endCondition);
            }
            else
            {
                queryPurchaseOrder = new TableQuery <YemUserProductDto>()
                                     .Where($"PartitionKey eq '{yemUserPurchase}' and RemainingAmount gt 0 and IsLock eq false");
            }
            var yemUserProductDtos = cloudPurchaseOrderTable.ExecuteQuery(queryPurchaseOrder).OrderByDescending(p => p.RemainingAmount).ToList();

            yemUserProductDtos = yemUserProductDtos.Where(p => p.RemainingAmount > 0).ToList();
            Console.WriteLine("要处理的购买订单条数=" + yemUserProductDtos.Count());
            Console.WriteLine("待分配的资产数=" + onSellAssetDtos.Count());
            List <OnSellAssetDto> oldOnSellAssetDtos = onSellAssetDtos;

            if (!oldOnSellAssetDtos.Any())
            {
                Console.WriteLine("没有处理的资产");
                return;
            }
            int index = 0;
            //int syncIndex = 0; //备份索引
            Stopwatch s = new Stopwatch();

            s.Start();
            List <OnSellAssetDto> lastOnSellAssetDtos = new List <OnSellAssetDto>(); //上一次的资产情况

            foreach (var item in yemUserProductDtos)
            {
                if (!lastOnSellAssetDtos.Any()) //第一次
                {
                    lastOnSellAssetDtos.AddRange(oldOnSellAssetDtos.MapToOnSellAssetList());
                }
                index++;
                var addUserAssetRatios = new List <UserAssetRatio>();                   //记录每笔购买产生的比例  用户资产关系
                List <OnSellAssetDto> modifyOnSellAssets = new List <OnSellAssetDto>(); //记录每笔购买使用的资产
                var addAssetUserRatios = new List <UserAssetRatio>();                   //记录每笔购买产生的比例  资产用户关系
                Console.WriteLine("总共处理的条数为:" + yemUserProductDtos.Count + ",当前处理的条数:" + index);
                Console.WriteLine("还需要处理的资产数=" + oldOnSellAssetDtos.Count());
                long sumAssetCount             = oldOnSellAssetDtos.Sum(p => p.RemainderTotal);
                long waitingDealAmount         = item.RemainingAmount; //购买订单待处理的金额
                long currentDealPurchaseAmount = 0;                    //本次处理的金额
                for (int i = oldOnSellAssetDtos.Count - 1; i >= 0; i--)
                {
                    if (currentDealPurchaseAmount == waitingDealAmount)
                    {
                        break;
                    }
                    var  assetItem      = oldOnSellAssetDtos[i];
                    long allocateAmount = AllocateAmountByAsset(waitingDealAmount, assetItem.RemainderTotal, sumAssetCount);
                    if (item.RemainingAmount < allocateAmount)
                    {
                        allocateAmount = item.RemainingAmount;
                    }
                    if (assetItem.RemainderTotal < allocateAmount)
                    {
                        allocateAmount = assetItem.RemainderTotal;
                    }
                    if (allocateAmount <= 0)
                    {
                        continue;
                    }
                    currentDealPurchaseAmount += allocateAmount;                                     //统计该订单分配的总金额
                    assetItem.RemainderTotal  -= allocateAmount;
                    var userAssetRatioTuple = CreateUserAssetRatio(item, assetItem, allocateAmount); //第一个是用户资产关系  第二个是资产用户关系
                    addUserAssetRatios.Add(userAssetRatioTuple.Item1);
                    addAssetUserRatios.Add(userAssetRatioTuple.Item2);
                    var updateOnSellAsset = BuildOnSellAsset(assetItem, allocateAmount); //修改资产的信息
                    modifyOnSellAssets.Add(updateOnSellAsset);
                    if (assetItem.RemainderTotal == 0)
                    {
                        oldOnSellAssetDtos.Remove(assetItem);
                    }
                }
                YemUserProductDto newYemUserProductDto = BuildYemUserProduct(item, currentDealPurchaseAmount); //修改购买订单需要配置的参数
                //保存数据库
                await InsertOrReplaceAzureTable(tableClient, newYemUserProductDto, addUserAssetRatios, addAssetUserRatios,
                                                modifyOnSellAssets, writeAssetAzureTable, writeYemUserProductAzureTable, currentDealPurchaseAmount, lastOnSellAssetDtos);

                lastOnSellAssetDtos = new List <OnSellAssetDto>();
                lastOnSellAssetDtos.AddRange(modifyOnSellAssets.MapToOnSellAssetList()); //记录上一次资产列表

                if (!oldOnSellAssetDtos.Any())
                {
                    Console.WriteLine("没有处理的资产");
                    break;
                }
            }
            s.Stop();
            Console.WriteLine("End:" + s.ElapsedMilliseconds);
        }
        public override async Task BindAsync(BindingContext context)
        {
            string boundPartitionKey = PartitionKey;
            string boundRowKey       = RowKey;
            string boundFilter       = Filter;

            if (context.BindingData != null)
            {
                if (_partitionKeyBindingTemplate != null)
                {
                    boundPartitionKey = _partitionKeyBindingTemplate.Bind(context.BindingData);
                }

                if (_rowKeyBindingTemplate != null)
                {
                    boundRowKey = _rowKeyBindingTemplate.Bind(context.BindingData);
                }

                if (_filterBindingTemplate != null)
                {
                    boundFilter = _filterBindingTemplate.Bind(context.BindingData);
                }
            }

            if (!string.IsNullOrEmpty(boundPartitionKey))
            {
                boundPartitionKey = Resolve(boundPartitionKey);
            }

            if (!string.IsNullOrEmpty(boundRowKey))
            {
                boundRowKey = Resolve(boundRowKey);
            }

            if (!string.IsNullOrEmpty(boundFilter))
            {
                boundFilter = Resolve(boundFilter);
            }

            Attribute[] additionalAttributes = null;
            if (!string.IsNullOrEmpty(Metadata.Connection))
            {
                additionalAttributes = new Attribute[]
                {
                    new StorageAccountAttribute(Metadata.Connection)
                };
            }

            if (Access == FileAccess.Write)
            {
                RuntimeBindingContext runtimeContext           = new RuntimeBindingContext(new TableAttribute(TableName), additionalAttributes);
                IAsyncCollector <DynamicTableEntity> collector = await context.Binder.BindAsync <IAsyncCollector <DynamicTableEntity> >(runtimeContext);

                ICollection entities = ReadAsCollection(context.Value);

                foreach (JObject entity in entities)
                {
                    // Here we're mapping from JObject to DynamicTableEntity because the Table binding doesn't support
                    // a JObject binding. We enable that for the core Table binding in the future, which would allow
                    // this code to go away.
                    DynamicTableEntity tableEntity = CreateTableEntityFromJObject(boundPartitionKey, boundRowKey, entity);
                    await collector.AddAsync(tableEntity);
                }
            }
            else
            {
                string json = null;
                if (!string.IsNullOrEmpty(boundPartitionKey) &&
                    !string.IsNullOrEmpty(boundRowKey))
                {
                    // singleton
                    RuntimeBindingContext runtimeContext = new RuntimeBindingContext(new TableAttribute(TableName, boundPartitionKey, boundRowKey), additionalAttributes);
                    DynamicTableEntity    tableEntity    = await context.Binder.BindAsync <DynamicTableEntity>(runtimeContext);

                    if (tableEntity != null)
                    {
                        json = ConvertEntityToJObject(tableEntity).ToString();
                    }
                }
                else
                {
                    // binding to multiple table entities
                    RuntimeBindingContext runtimeContext = new RuntimeBindingContext(new TableAttribute(TableName), additionalAttributes);
                    CloudTable            table          = await context.Binder.BindAsync <CloudTable>(runtimeContext);

                    string finalQuery = boundFilter;
                    if (!string.IsNullOrEmpty(boundPartitionKey))
                    {
                        var partitionKeyPredicate = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, boundPartitionKey);
                        if (!string.IsNullOrEmpty(boundFilter))
                        {
                            finalQuery = TableQuery.CombineFilters(boundFilter, TableOperators.And, partitionKeyPredicate);
                        }
                        else
                        {
                            finalQuery = partitionKeyPredicate;
                        }
                    }

                    TableQuery tableQuery = new TableQuery
                    {
                        TakeCount    = Take,
                        FilterString = finalQuery
                    };

                    var    entities    = table.ExecuteQuery(tableQuery);
                    JArray entityArray = new JArray();
                    foreach (var entity in entities)
                    {
                        entityArray.Add(ConvertEntityToJObject(entity));
                    }

                    json = entityArray.ToString(Formatting.None);
                }

                if (json != null)
                {
                    if (context.DataType == DataType.Stream)
                    {
                        // We're explicitly NOT disposing the StreamWriter because
                        // we don't want to close the underlying Stream
                        StreamWriter sw = new StreamWriter((Stream)context.Value);
                        await sw.WriteAsync(json);

                        sw.Flush();
                    }
                    else
                    {
                        context.Value = json;
                    }
                }
            }
        }
Exemplo n.º 12
0
        static IEnumerable <GuidEntity> GetValues()
        {
            CloudTable tableReference = GetTableReference();

            return(tableReference.ExecuteQuery(new TableQuery <GuidEntity>()));
        }
        public List <DMAMeterReadingEntity> MeterReadingsFiltered(string finalFilter)
        {
            var query = new TableQuery <DMAMeterReadingEntity>().Where(finalFilter);

            return(meterTable.ExecuteQuery(query).ToList());
        }
Exemplo n.º 14
0
 /// <summary>
 ///     开始计算
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void button1_Click(object sender, EventArgs e)
 {
     try
     {
         this.lbl_showMsg.Text = "正在从赎回表中获取所有符合条件的订单......";
         //正在从赎回订单表中拉数据.......
         CloudTable deptTable    = this.tableClient.GetTableReference("AssetUserRedemptionOrders");
         string     partitionKey = "RedeemOrder";
         TableQuery <RedeemOrderInfo> queryDeptInfo = new TableQuery <RedeemOrderInfo>()
                                                      .Where($"PartitionKey eq '{partitionKey}' and AlreadyDealtAmount eq 0");
         List <RedeemOrderInfo> creditInfos = deptTable.ExecuteQuery(queryDeptInfo).ToList();
         //数据获取完毕,正在开始分析.......
         this.txb_AllNumbers.Text = creditInfos.Count.ToString();
         //ransomorderId
         if (creditInfos.Count == 0)
         {
             MessageBox.Show("获取赎回订单数为0......");
             return;
         }
         this.lbl_showMsg.Text          = "获取赎回订单完毕,开始分析......";
         this.btn_StartComputer.Enabled = false;
         for (int i = 0; i < 6; i++)
         {
             Thread th = new Thread(() =>
             {
                 while (creditInfos.Count > 0)
                 {
                     string userid = string.Empty;
                     int dbNumber  = 0;
                     lock (this.objLock)
                     {
                         if (creditInfos.Count > 0)
                         {
                             //userid = dicUserInfo.Keys.First();
                             //dbNumber = dicUserInfo[userid];
                             //dicUserInfo.Remove(userid);
                         }
                     }
                     if (!string.IsNullOrEmpty(userid))
                     {
                         //执行
                         //bool result = this.CheckPurchaseOrderInfo(userid, dbNumber, yemUserProductDtos, new RedisHelperSpecial(this.loadAppSettings.EndDbNumber));
                         //lock (this.objLock)
                         //{
                         //    this.UpdateTxbText(result ? this.txb_allUserSuccess : this.txb_allUserFail);
                         //}
                     }
                 }
             });
             th.IsBackground = true;
             th.Start();
         }
         Thread thShow = new Thread(() =>
         {
             //while (true)
             //{
             //    if (dicUserInfo.Count > 0)
             //    {
             //        Thread.Sleep(1500);
             //        continue;
             //    }
             //    break;
             //}
             //Thread.Sleep(2000);
             //this.btn_CheckAllUserInfo.Enabled = true;
             //this.lbl_ShowAllUserInfo.Text = "验证完所有用户数据";
             //提示
         });
         thShow.IsBackground = true;
         thShow.Start();
     }
     catch (Exception exception)
     {
         //Console.WriteLine(exception);
         //throw;
     }
 }
Exemplo n.º 15
0
        private static IList <Attachment> GetCardsAttachments(string uniqueID, IDictionary <int, string> eventDict, IDictionary <string, List <string> > uniqueSurveyDict)
        {
            List <Attachment> eventCardList = new List <Attachment>();

            var        storageAccount  = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["AzureWebJobsStorage"]);
            var        tableClient     = storageAccount.CreateCloudTableClient();
            CloudTable eventTable      = tableClient.GetTableReference("Event");
            string     searchPartition = uniqueID; //UserID of the user

            TableQuery <EventEntity> query = new TableQuery <EventEntity>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, searchPartition));

            uniqueSurveyDict.Clear();
            eventDict.Clear();

            int count = 1;

            foreach (EventEntity entity in eventTable.ExecuteQuery(query).OrderByDescending(o => o.Timestamp))
            {
                if (!uniqueSurveyDict.ContainsKey(entity.SurveyCode))
                {
                    var heroCard = new HeroCard
                    {
                        Title    = "No." + count + " " + entity.EventName + " (" + entity.SurveyCode + ")",
                        Subtitle = "Date: " + entity.EventStartDate + " to " + entity.EventEndDate,
                        Text     = "Attendance Code 1: " + entity.AttendanceCode1 + "  \n"
                                   + "Attendance Code 2: " + entity.AttendanceCode2 + "  \n"
                                   + "Survey Code: " + entity.SurveyCode + "  \n"
                                   + entity.Description,
                        Buttons = new List <CardAction>
                        {
                            new CardAction()
                            {
                                Title = "View Details",
                                Type  = ActionTypes.PostBack,
                                Value = "ViewDetails" + count
                            },
                            new CardAction()
                            {
                                Title = "View Result",
                                Type  = ActionTypes.PostBack,
                                Value = "ViewResultPrompt" + count
                            },
                            new CardAction()
                            {
                                Title = "Delete Event",
                                Type  = ActionTypes.PostBack,
                                Value = "DeleteEventPrompt" + count
                            }
                        }
                    };

                    eventCardList.Add(heroCard.ToAttachment());
                    eventDict.Add(count, JsonConvert.SerializeObject(entity));
                    uniqueSurveyDict.Add(entity.SurveyCode, new List <string>()
                    {
                        JsonConvert.SerializeObject(entity)
                    });
                    count++;
                }
                else
                {
                    uniqueSurveyDict[entity.SurveyCode].Add(JsonConvert.SerializeObject(entity));
                }
            }
            return(eventCardList);
        }
Exemplo n.º 16
0
        internal bool CheckTableAndContent(string StorageAccountName, string TableName, string FilterString, string WaitChar, bool UseNewTableNames, int TimeoutinMinutes = 15)
        {
            var            tableExists = false;
            StorageAccount account     = null;

            if (!String.IsNullOrEmpty(StorageAccountName))
            {
                account = this.GetStorageAccountFromCache(StorageAccountName);
            }
            if (account != null)
            {
                var        endpoint            = this.GetCoreEndpoint(StorageAccountName);
                var        key                 = this.GetAzureStorageKeyFromCache(StorageAccountName);
                var        credentials         = new StorageCredentials(StorageAccountName, key);
                var        cloudStorageAccount = new CloudStorageAccount(credentials, endpoint, true);
                var        tableClient         = cloudStorageAccount.CreateCloudTableClient();
                var        checkStart          = DateTime.Now;
                var        wait                = true;
                CloudTable table               = null;
                if (UseNewTableNames)
                {
                    try
                    {
                        table = tableClient.ListTables().FirstOrDefault(tab => tab.Name.StartsWith("WADMetricsPT1M"));
                    }
                    catch { } //#table name should be sorted
                }
                else
                {
                    try
                    {
                        table = tableClient.GetTableReference(TableName);
                    }
                    catch { }
                }

                while (wait)
                {
                    if (table != null && table.Exists())
                    {
                        TableQuery query = new TableQuery();
                        query.FilterString = FilterString;
                        var results = table.ExecuteQuery(query);
                        if (results.Count() > 0)
                        {
                            tableExists = true;
                            break;
                        }
                    }

                    WriteHost(WaitChar, newLine: false);
                    TestMockSupport.Delay(5000);
                    if (UseNewTableNames)
                    {
                        try
                        {
                            table = tableClient.ListTables().FirstOrDefault(tab => tab.Name.StartsWith("WADMetricsPT1M"));
                        }
                        catch { } //#table name should be sorted
                    }
                    else
                    {
                        try
                        {
                            table = tableClient.GetTableReference(TableName);
                        }
                        catch { }
                    }

                    wait = ((DateTime.Now) - checkStart).TotalMinutes < TimeoutinMinutes;
                }
            }
            return(tableExists);
        }
Exemplo n.º 17
0
        // GET: Graph
        public async Task <ActionResult> Index(string span = null, string selectedDeviceName = null)
        {
            var availableTimeSpans = new List <TimeSpanOptionsDto>()
            {
                new TimeSpanOptionsDto()
                {
                    ShortCode     = "1d",
                    TimeSpan      = "1 Day",
                    TimeSpanValue = -1
                },
                new TimeSpanOptionsDto()
                {
                    ShortCode     = "2d",
                    TimeSpan      = "2 Days",
                    TimeSpanValue = -2
                },
                new TimeSpanOptionsDto()
                {
                    ShortCode     = "1w",
                    TimeSpan      = "1 Week",
                    TimeSpanValue = -7
                }
            };


            var deviceManager =
                RegistryManager.CreateFromConnectionString(CloudConfigurationManager.GetSetting("IoTHubConnectionString"));
            var devices = await deviceManager.GetDevicesAsync(5);

            var allDevices  = devices as IList <Device> ?? devices.ToList();
            var activeCount = allDevices.Count(x => x.Status == DeviceStatus.Enabled);

            var availableDevicesItems = allDevices.Where(x => x.Status == DeviceStatus.Enabled).Select(t => new DeviceDTO()
            {
                Name   = t.Id,
                Status = t.Status
            });

            var timeSpan = availableTimeSpans.SingleOrDefault(x => x.ShortCode == span)?.TimeSpanValue ?? -1;

            if (string.IsNullOrEmpty(selectedDeviceName))
            {
                if (activeCount == 1)
                {
                    selectedDeviceName = availableDevicesItems.Single().Name;
                }
                else if (activeCount > 1)
                {
                    selectedDeviceName = availableDevicesItems.First().Name;
                }
            }

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));

            // Create the table client.
            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

            // Create the CloudTable object that represents the "people" table.
            CloudTable table        = tableClient.GetTableReference("tempandhumid");
            var        previousTime = Helpers.DateTimeHelpers.GetUnixTimeStamp(DateTimeOffset.Now.AddDays(timeSpan).Date).ToString();


            // Construct the query operation for all customer entities where PartitionKey="Smith".
            TableQuery <TempAndHumid> query = new TableQuery <TempAndHumid>().Where(
                TableQuery.CombineFilters(
                    TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, selectedDeviceName),
                    TableOperators.And,
                    TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.GreaterThanOrEqual, previousTime)));
            var entities = table.ExecuteQuery(query);
            var jsonData = entities.OrderBy(x => x.Timestamp).Select(e => new TempAndHumidDTO()
            {
                Temp      = e.temp,
                Timestamp = e.Timestamp.ToLocalTime().ToString("O"),
                Humidity  = e.humidity
            });
            var json = JsonConvert.SerializeObject(jsonData);

            ViewBag.Data      = json;
            ViewBag.TimeSpans = availableTimeSpans;
            ViewBag.Devices   = availableDevicesItems;
            return(View());
        }
Exemplo n.º 18
0
        public static async Task <IActionResult> ShortenUrl(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            [Table("urls", "1", "KEY", Take = 1)] UrlKey urlKey,
            [Table("urls")] CloudTable inputTable,
            ILogger log)
        {
            log.LogInformation("ShortenUrl function processed a request.");

            // get href from request
            string href = req.Query["href"];

            string  requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data        = JsonConvert.DeserializeObject(requestBody);

            href = href ?? data?.href;

            if (String.IsNullOrWhiteSpace(href))
            {
                return(new BadRequestObjectResult("Href parameter is missing"));
            }

            // create a "key" for generating unique values if it doesn't exist
            if (urlKey == null)
            {
                urlKey = new UrlKey {
                    PartitionKey = "1", RowKey = "KEY", Id = 1024
                };
                var tableInsertUrlKey = TableOperation.Insert(urlKey);
                await inputTable.ExecuteAsync(tableInsertUrlKey);
            }

            // query table for the passed URL
            // and return found short url if it exists
            href = href.ToLower();
            var query = new TableQuery <UrlData>()
                        .Where(TableQuery.GenerateFilterCondition(nameof(UrlData.Url), QueryComparisons.Equal, href));
            var foundUrlData = inputTable.ExecuteQuery <UrlData>(query);

            if (foundUrlData.Count() > 0)
            {
                var responseObj = foundUrlData.First();
                var res         = responseObj.RowKey;
                return(new OkObjectResult(res));
            }

            // generate a new short url
            var idx      = urlKey.Id;
            var s        = string.Empty;
            var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

            while (idx > 0)
            {
                s   += alphabet[idx % alphabet.Length];
                idx /= alphabet.Length;
            }
            s = String.Join(string.Empty, s.Reverse());
            var urlData = new UrlData
            {
                PartitionKey = $"{s[0]}",
                RowKey       = s,
                Url          = href
            };

            urlKey.Id++;
            var operation = TableOperation.Replace(urlKey);
            await inputTable.ExecuteAsync(operation);

            operation = TableOperation.Insert(urlData);
            await inputTable.ExecuteAsync(operation);

            return(new OkObjectResult(urlData.RowKey));
            //return urlData.Url != null
            //    ? (ActionResult)new OkObjectResult($"You have passed URL {urlData.Url}. Shortened URL is {urlData.RowKey}")
            //    : new BadRequestObjectResult("Please pass a href on the query string or in the request body");
        }
Exemplo n.º 19
0
        internal void VerifyUpload(CloudTableClient tableClient)
        {
            foreach (string tableName in TableNames)
            {
                CloudTable table = tableClient.GetTableReference(String.Concat(AzureTableUploaderTest.TableNamePrefix, tableName));

                // Verify partition count
                TableQuery <DynamicTableEntity> statusRecordQuery = new TableQuery <DynamicTableEntity>();
                statusRecordQuery = (new TableQuery <DynamicTableEntity>())
                                    .Where(TableQuery.GenerateFilterCondition(
                                               "RowKey",
                                               QueryComparisons.Equal,
                                               AzureTableQueryableEventUploader.StatusEntityRowKey));
                IEnumerable <DynamicTableEntity> statusRecords = table.ExecuteQuery(statusRecordQuery);
                int partitionCount = 0;
                foreach (DynamicTableEntity statusRecord in statusRecords)
                {
                    // Verify that the object's deletion state is correct
                    bool isDeleted = (bool)statusRecord.Properties["IsDeleted"].BooleanValue;
                    long instance  = (long)statusRecord.Properties["Instance"].Int64Value;
                    if (false == PartitionShouldBeDeleted(partitionCount))
                    {
                        if (isDeleted)
                        {
                            Utility.TraceSource.WriteError(
                                AzureTableUploaderTest.TraceType,
                                "Object represented by partition {0} in table {1} is in a deleted state, but it shouldn't be.",
                                partitionCount,
                                tableName);
                            Verify.Fail("Unexpected deletion state");
                        }
                        long latestInstance = (InstancesForPartition(partitionCount) - 1);
                        if (instance != latestInstance)
                        {
                            Utility.TraceSource.WriteError(
                                AzureTableUploaderTest.TraceType,
                                "Status record for partition {0} in table {1} does not have the expected instance value. Expected value: {2}. Actual value: {3}.",
                                partitionCount,
                                tableName,
                                latestInstance,
                                instance);
                            Verify.Fail("Unexpected instance value in status record");
                        }
                    }
                    else
                    {
                        if (false == isDeleted)
                        {
                            Utility.TraceSource.WriteError(
                                AzureTableUploaderTest.TraceType,
                                "Object represented by partition {0} in table {1} should be in a deleted state, but it isn't.",
                                partitionCount,
                                tableName);
                            Verify.Fail("Unexpected deletion state");
                        }
                        long specialInstanceForDeletedObject;
                        unchecked
                        {
                            specialInstanceForDeletedObject = (long)UInt64.MaxValue;
                        }
                        long latestInstance = (InstancesForPartition(partitionCount) - 1);
                        if ((instance != specialInstanceForDeletedObject) &&
                            (instance != latestInstance))
                        {
                            Utility.TraceSource.WriteError(
                                AzureTableUploaderTest.TraceType,
                                "Status record for partition {0} in table {1} does not have the expected instance value. Acceptable values: {2}, {3}. Actual value: {4}.",
                                partitionCount,
                                tableName,
                                specialInstanceForDeletedObject,
                                latestInstance,
                                instance);
                            Verify.Fail("Unexpected instance value in status record");
                        }
                    }

                    // Verify records within the partition
                    VerifyPartitionEntities(table, partitionCount);

                    partitionCount++;
                }
                if (PartitionCount != partitionCount)
                {
                    Utility.TraceSource.WriteError(
                        AzureTableUploaderTest.TraceType,
                        "Table {0} does not have the expected number of partitions. Expected partition count: {1}. Actual partition count: {2}",
                        tableName,
                        PartitionCount,
                        partitionCount);
                    Verify.Fail("Unexpected partition count in table");
                }
            }
        }
Exemplo n.º 20
0
        public override async Task BindAsync(BindingContext context)
        {
            string boundPartitionKey = PartitionKey;
            string boundRowKey       = RowKey;

            if (context.BindingData != null)
            {
                boundPartitionKey = _partitionKeyBindingTemplate.Bind(context.BindingData);
                if (_rowKeyBindingTemplate != null)
                {
                    boundRowKey = _rowKeyBindingTemplate.Bind(context.BindingData);
                }
            }

            boundPartitionKey = Resolve(boundPartitionKey);
            if (!string.IsNullOrEmpty(boundRowKey))
            {
                boundRowKey = Resolve(boundRowKey);
            }

            if (FileAccess == FileAccess.Write)
            {
                // read the content as a JObject
                JObject jsonObject = null;
                using (StreamReader streamReader = new StreamReader(context.Value))
                {
                    string content = await streamReader.ReadToEndAsync();

                    jsonObject = JObject.Parse(content);
                }

                // TODO: If RowKey has not been specified in the binding, try to
                // derive from the object properties (e.g. "rowKey" or "id" properties);

                IAsyncCollector <DynamicTableEntity> collector = context.Binder.Bind <IAsyncCollector <DynamicTableEntity> >(new TableAttribute(TableName));
                DynamicTableEntity tableEntity = new DynamicTableEntity(boundPartitionKey, boundRowKey);
                foreach (JProperty property in jsonObject.Properties())
                {
                    EntityProperty entityProperty = EntityProperty.CreateEntityPropertyFromObject((object)property.Value);
                    tableEntity.Properties.Add(property.Name, entityProperty);
                }

                await collector.AddAsync(tableEntity);
            }
            else
            {
                if (!string.IsNullOrEmpty(boundPartitionKey) &&
                    !string.IsNullOrEmpty(boundRowKey))
                {
                    // singleton
                    DynamicTableEntity tableEntity = context.Binder.Bind <DynamicTableEntity>(new TableAttribute(TableName, boundPartitionKey, boundRowKey));
                    if (tableEntity != null)
                    {
                        string json = ConvertEntityToJObject(tableEntity).ToString();
                        using (StreamWriter sw = new StreamWriter(context.Value))
                        {
                            await sw.WriteAsync(json);
                        }
                    }
                }
                else
                {
                    // binding to entire table (query multiple table entities)
                    CloudTable table    = context.Binder.Bind <CloudTable>(new TableAttribute(TableName, boundPartitionKey, boundRowKey));
                    var        entities = table.ExecuteQuery(_tableQuery);

                    JArray entityArray = new JArray();
                    foreach (var entity in entities)
                    {
                        entityArray.Add(ConvertEntityToJObject(entity));
                    }

                    string json = entityArray.ToString(Formatting.None);
                    using (StreamWriter sw = new StreamWriter(context.Value))
                    {
                        await sw.WriteAsync(json);
                    }
                }
            }
        }
Exemplo n.º 21
0
        private void VerifyPartitionEntities(CloudTable table, int partIdx)
        {
            // Get records in the partition
            TableQuery <DynamicTableEntity> partitionQuery = new TableQuery <DynamicTableEntity>();

            partitionQuery = (new TableQuery <DynamicTableEntity>())
                             .Where(TableQuery.GenerateFilterCondition(
                                        "PartitionKey",
                                        QueryComparisons.Equal,
                                        partIdx.ToString()));
            IEnumerable <DynamicTableEntity> partitionRecords = table.ExecuteQuery(partitionQuery);

            if (0 == partitionRecords.Count())
            {
                Utility.TraceSource.WriteError(
                    AzureTableUploaderTest.TraceType,
                    "Table {0} partition {1} has no records.",
                    table.Name,
                    partIdx);
                Verify.Fail("No records found in partition");
            }

            bool hasDeletionEventWithIdOnly = (partitionRecords
                                               .Where(r => ((r.Properties.ContainsKey(AzureTableUploaderTest.EventTypeProperty)) &&
                                                            (r.Properties[AzureTableUploaderTest.EventTypeProperty].StringValue.Equals("IdOnlyDeletion"))))
                                               .Count()) > 0;
            int expectedEntityCount = EventsForPartition(partIdx);
            EqualityComparerDataProperty dataComparer = new EqualityComparerDataProperty();

            if (table.Name.Equals(String.Concat(AzureTableUploaderTest.TableNamePrefix, TableNames[1])))
            {
                // Get records without instance information
                IEnumerable <DynamicTableEntity> recordsWithoutInstance = partitionRecords.Where(
                    r => ((false == r.Properties.ContainsKey("dca_instance")) &&
                          (false == r.RowKey.Equals(AzureTableQueryableEventUploader.StatusEntityRowKey))));

                // Verify the records
                VerifyPartitionEntities(
                    table,
                    recordsWithoutInstance,
                    partIdx,
                    hasDeletionEventWithIdOnly ? (expectedEntityCount + 1) : expectedEntityCount,
                    dataComparer,
                    hasDeletionEventWithIdOnly ? 1 : 0,
                    false);
            }

            // Get records with instance information
            IEnumerable <DynamicTableEntity> recordsWithInstance = partitionRecords.Where(
                r => (r.Properties.ContainsKey("dca_instance")));

            // Verify the records
            VerifyPartitionEntities(
                table,
                recordsWithInstance,
                partIdx,
                hasDeletionEventWithIdOnly ? (expectedEntityCount - 1) : expectedEntityCount,
                dataComparer,
                0,
                true);

            // Verify the instance information in the records
            VerifyInstanceInformation(table, recordsWithInstance, partIdx, hasDeletionEventWithIdOnly);
        }
Exemplo n.º 22
0
        public override async Task BindAsync(BindingContext context)
        {
            string boundPartitionKey = PartitionKey;
            string boundRowKey       = RowKey;

            if (context.BindingData != null)
            {
                if (_partitionKeyBindingTemplate != null)
                {
                    boundPartitionKey = _partitionKeyBindingTemplate.Bind(context.BindingData);
                }

                if (_rowKeyBindingTemplate != null)
                {
                    boundRowKey = _rowKeyBindingTemplate.Bind(context.BindingData);
                }
            }

            if (!string.IsNullOrEmpty(boundPartitionKey))
            {
                boundPartitionKey = Resolve(boundPartitionKey);
            }

            if (!string.IsNullOrEmpty(boundRowKey))
            {
                boundRowKey = Resolve(boundRowKey);
            }

            Attribute[] additionalAttributes = null;
            if (!string.IsNullOrEmpty(Metadata.Connection))
            {
                additionalAttributes = new Attribute[]
                {
                    new StorageAccountAttribute(Metadata.Connection)
                };
            }

            if (Access == FileAccess.Write)
            {
                RuntimeBindingContext runtimeContext           = new RuntimeBindingContext(new TableAttribute(TableName), additionalAttributes);
                IAsyncCollector <DynamicTableEntity> collector = await context.Binder.BindAsync <IAsyncCollector <DynamicTableEntity> >(runtimeContext);

                ICollection <JToken> entities = ReadAsCollection(context.Value);

                foreach (JObject entity in entities)
                {
                    // any key values specified on the entity override any values
                    // specified in the binding
                    string keyValue = (string)entity["partitionKey"];
                    if (!string.IsNullOrEmpty(keyValue))
                    {
                        boundPartitionKey = Resolve(keyValue);
                        entity.Remove("partitionKey");
                    }

                    keyValue = (string)entity["rowKey"];
                    if (!string.IsNullOrEmpty(keyValue))
                    {
                        boundRowKey = Resolve(keyValue);
                        entity.Remove("rowKey");
                    }

                    DynamicTableEntity tableEntity = new DynamicTableEntity(boundPartitionKey, boundRowKey);
                    foreach (JProperty property in entity.Properties())
                    {
                        EntityProperty entityProperty = EntityProperty.CreateEntityPropertyFromObject((object)property.Value);
                        tableEntity.Properties.Add(property.Name, entityProperty);
                    }

                    await collector.AddAsync(tableEntity);
                }
            }
            else
            {
                string json = null;

                if (!string.IsNullOrEmpty(boundPartitionKey) &&
                    !string.IsNullOrEmpty(boundRowKey))
                {
                    // singleton
                    RuntimeBindingContext runtimeContext = new RuntimeBindingContext(new TableAttribute(TableName, boundPartitionKey, boundRowKey), additionalAttributes);
                    DynamicTableEntity    tableEntity    = await context.Binder.BindAsync <DynamicTableEntity>(runtimeContext);

                    if (tableEntity != null)
                    {
                        json = ConvertEntityToJObject(tableEntity).ToString();
                    }
                }
                else
                {
                    // binding to entire table (query multiple table entities)
                    RuntimeBindingContext runtimeContext = new RuntimeBindingContext(new TableAttribute(TableName, boundPartitionKey, boundRowKey), additionalAttributes);
                    CloudTable            table          = await context.Binder.BindAsync <CloudTable>(runtimeContext);

                    var entities = table.ExecuteQuery(_tableQuery);

                    JArray entityArray = new JArray();
                    foreach (var entity in entities)
                    {
                        entityArray.Add(ConvertEntityToJObject(entity));
                    }

                    json = entityArray.ToString(Formatting.None);
                }

                if (json != null)
                {
                    // We're explicitly NOT disposing the StreamWriter because
                    // we don't want to close the underlying Stream
                    StreamWriter sw = new StreamWriter(context.Value);
                    await sw.WriteAsync(json);

                    sw.Flush();
                }
            }
        }
Exemplo n.º 23
0
        private bool VerifyDeletionForPartition(
            CloudTableClient tableClient,
            CloudTable table,
            string tableName,
            int partIdx,
            DateTime deletionCutoffTimeLow,
            DateTime deletionCutoffTimeHigh)
        {
            // Get the entities that are older than the deletion cutoff time
            string cutoffTimeTicks = (DateTime.MaxValue.Ticks - deletionCutoffTimeLow.Ticks).ToString("D20", CultureInfo.InvariantCulture);
            TableQuery <DynamicTableEntity> oldEntityQuery = (new TableQuery <DynamicTableEntity>())
                                                             .Where(TableQuery.CombineFilters(
                                                                        TableQuery.GenerateFilterCondition(
                                                                            "PartitionKey",
                                                                            QueryComparisons.Equal,
                                                                            partIdx.ToString()),
                                                                        TableOperators.And,
                                                                        TableQuery.CombineFilters(
                                                                            TableQuery.GenerateFilterCondition(
                                                                                "RowKey",
                                                                                QueryComparisons.GreaterThan,
                                                                                cutoffTimeTicks),
                                                                            TableOperators.And,
                                                                            TableQuery.GenerateFilterCondition(
                                                                                "RowKey",
                                                                                QueryComparisons.LessThan,
                                                                                AzureTableQueryableEventUploader.StatusEntityRowKey))));
            IEnumerable <DynamicTableEntity> oldEntities = table.ExecuteQuery(oldEntityQuery);

            if (PartitionShouldBeDeleted(partIdx))
            {
                // Since the object representing this partition should've been deleted,
                // we should expect to see any entities that are older than the deletion
                // cutoff time.
                if (oldEntities.Count() > 0)
                {
                    // Don't fail the test yet. Just write a warning and return. We can
                    // only predict the approximate deletion time, so maybe we are checking
                    // too early. The caller will check again later.
                    Utility.TraceSource.WriteWarning(
                        AzureTableUploaderTest.TraceType,
                        "All old entities have not yet been deleted from table {0}, partition {1}. We expect all entities with tick count less than {2} (RowKey greater than {3}) to be deleted.",
                        partIdx,
                        table.Name,
                        deletionCutoffTimeLow.Ticks,
                        cutoffTimeTicks);
                    return(false);
                }
            }
            else
            {
                int oldEntityCount = oldEntities.Count();
                // We allow up to one old entity that is not associated with the current instance to
                // be present. This is because we expect the DCA to not delete the newest old event
                // that is old enough to be deleted. We expect the DCA to retain this event so that
                // the table contains full history for the retention period.
                // However, we cannot enforce a strict check for exactly one old entity to be present
                // because in the test we can only predict the approximate cutoff time for deletion.
                // Therefore, we cannot predict which side of the cutoff time the old entity retained
                // by DCA will fall on. Depending on timing, it could end up on either side, so we
                // relax the check to allow either 0 or 1 old entity that is not associated with the
                // current instance.
                if (oldEntityCount > 1)
                {
                    // Since the object representing this partition should not have been deleted,
                    // we expect that the entities associated with the latest instance of this
                    // object to be present, even if they are older than the deletion cutoff time.
                    if (oldEntityCount < EventsPerInstance)
                    {
                        // Fail the test immediately. Some entities have been unexpectedly deleted.
                        Utility.TraceSource.WriteError(
                            AzureTableUploaderTest.TraceType,
                            "Some entities have been unexpectedly deleted from table {0}. We expected to find at least {1} entities with tick count less than {2} (RowKey greater than {3}), but found only {4}.",
                            table.Name,
                            EventsPerInstance,
                            deletionCutoffTimeLow.Ticks,
                            cutoffTimeTicks,
                            oldEntityCount);
                        Verify.Fail("Entities unexpectedly deleted from table.");
                    }

                    // Make sure that the old entities we found are associated with the latest
                    // instance of the object.

                    // Get the latest instance from the status record
                    TableOperation queryStatusOperation = TableOperation.Retrieve <DynamicTableEntity>(
                        partIdx.ToString(),
                        AzureTableQueryableEventUploader.StatusEntityRowKey);
                    TableResult        queryResult  = table.Execute(queryStatusOperation);
                    DynamicTableEntity statusRecord = queryResult.Result as DynamicTableEntity;
                    long latestInstance             = (long)statusRecord.Properties["Instance"].Int64Value;

                    // Verify that the instance on the old entity matches the latest instance
                    foreach (DynamicTableEntity oldEntity in oldEntities)
                    {
                        long oldEntityInstance = (long)oldEntity.Properties[AzureTableUploaderTest.DcaInstanceProperty].Int64Value;
                        if (oldEntityInstance != latestInstance)
                        {
                            // We found an old entity that is not associated with the
                            // latest instance of the object. However, don't fail
                            // the test yet. Just write a warning and return. We can
                            // only predict the approximate deletion time, so maybe we
                            // are checking too early. The caller will check again later.
                            Utility.TraceSource.WriteWarning(
                                AzureTableUploaderTest.TraceType,
                                "Found an old entity in table {0}, partition {1} that is not associated with the latest instance of the object. Entity instance: {2}, latest instance: {3}.",
                                table.Name,
                                partIdx,
                                oldEntityInstance,
                                latestInstance);
                            return(false);
                        }
                    }
                }
            }

            return(true);
        }
Exemplo n.º 24
0
        public override void Run()
        {
            Trace.TraceInformation("WorkerRoleA entering Run()");
            while (true)
            {
                Stopwatch requestTimer = Stopwatch.StartNew();
                var       request      = RequestTelemetryHelper.StartNewRequest("ProcessMessageWorkflow", DateTimeOffset.UtcNow);
                CallContext.LogicalSetData(CORRELATION_SLOT, request.Id);
                //Thread.SetData(Thread.GetNamedDataSlot(CORRELATION_SLOT), request.Id);
                try
                {
                    var tomorrow = DateTime.Today.AddDays(1.0).ToString("yyyy-MM-dd");
                    // If OnStop has been called, return to do a graceful shutdown.
                    if (onStopCalled == true)
                    {
                        Trace.TraceInformation("onStopCalled WorkerRoleB");
                        returnedFromRunMethod = true;
                        return;
                    }

                    // Retrieve all messages that are scheduled for tomorrow or earlier
                    // and are in Pending or Queuing status.
                    string typeAndDateFilter = TableQuery.CombineFilters(
                        TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.GreaterThan, "message"),
                        TableOperators.And,
                        TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.LessThan, tomorrow));
                    var            query             = (new TableQuery <Message>().Where(typeAndDateFilter));
                    var            messagesToProcess = messageTable.ExecuteQuery(query).ToList();
                    TableOperation replaceOperation;
                    request.Metrics.Add(new KeyValuePair <string, double>("NumberOfMessages", messagesToProcess.Count));
                    // Process each message (queue emails to be sent).
                    foreach (Message messageToProcess in messagesToProcess)
                    {
                        string restartFlag = "0";
                        // If the message is already in Queuing status,
                        // set flag to indicate this is a restart.
                        if (messageToProcess.Status == "Queuing")
                        {
                            restartFlag = "1";
                        }

                        // If the message is in Pending status, change
                        // it to Queuing.
                        if (messageToProcess.Status == "Pending")
                        {
                            messageToProcess.Status = "Queuing";
                            replaceOperation        = TableOperation.Replace(messageToProcess);
                            messageTable.Execute(replaceOperation);
                        }

                        // If the message is in Queuing status,
                        // process it and change it to Processing status;
                        // otherwise it's already in processing status, and
                        // in that case check if processing is complete.
                        if (messageToProcess.Status == "Queuing")
                        {
                            ProcessMessage(messageToProcess, restartFlag);

                            messageToProcess.Status = "Processing";
                            replaceOperation        = TableOperation.Replace(messageToProcess);
                            messageTable.Execute(replaceOperation);
                        }
                        else
                        {
                            CheckAndArchiveIfComplete(messageToProcess);
                        }
                    }
                    RequestTelemetryHelper.DispatchRequest(request, requestTimer.Elapsed, true);
                    // Sleep to minimize query costs.
                    System.Threading.Thread.Sleep(1000 * 30);
                }
                catch (Exception ex)
                {
                    string err = ex.Message;
                    if (ex.InnerException != null)
                    {
                        err += " Inner Exception: " + ex.InnerException.Message;
                    }
                    Trace.TraceError(err, ex);
                    RequestTelemetryHelper.DispatchRequest(request, requestTimer.Elapsed, false);
                    // Don't fill up Trace storage if we have a bug in queue process loop.
                    System.Threading.Thread.Sleep(1000 * 60);
                }
            }
        }
Exemplo n.º 25
0
        protected void addDeviceStatus()
        {
            TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");

            CloudTable table = TableClient.GetTableReference("hottub");

            TableQuery query = new TableQuery().Where(TableQuery.GenerateFilterCondition("PartitionKey",
                                                                                         QueryComparisons.Equal, DeviceName));;

            var list = table.ExecuteQuery(query).ToList();

            if (list.Count > 0)
            {
                var lastEntry = list.OrderBy(entry => entry.Timestamp).Last();

                TableRow trName = new TableRow();
                trName.Cells.Add(new TableCell()
                {
                    Text = "Logged in as:"
                });
                trName.Cells.Add(new TableCell()
                {
                    Text = XMSCLIENTFullName
                });
                deviceStatus.Rows.Add(trName);

                trName = new TableRow();
                trName.Cells.Add(new TableCell()
                {
                    Text = "Device Name:"
                });
                trName.Cells.Add(new TableCell()
                {
                    Text = lastEntry.Properties["hostName"].StringValue
                });
                deviceStatus.Rows.Add(trName);

                trName = new TableRow();
                trName.Cells.Add(new TableCell()
                {
                    Text = "Air Temperature:"
                });
                trName.Cells.Add(new TableCell()
                {
                    Text = lastEntry.Properties["airTemperature"].DoubleValue.ToString() + "°F"
                });
                deviceStatus.Rows.Add(trName);

                trName = new TableRow();
                trName.Cells.Add(new TableCell()
                {
                    Text = "Air Humidity:"
                });
                trName.Cells.Add(new TableCell()
                {
                    Text = lastEntry.Properties["humidity"].DoubleValue.ToString() + "%"
                });
                deviceStatus.Rows.Add(trName);

                trName = new TableRow();
                trName.Cells.Add(new TableCell()
                {
                    Text = "Air Heat Index:"
                });
                trName.Cells.Add(new TableCell()
                {
                    Text = lastEntry.Properties["heatIndex"].DoubleValue.ToString() + "°F"
                });
                deviceStatus.Rows.Add(trName);

                trName = new TableRow();
                trName.Cells.Add(new TableCell()
                {
                    Text = "Water Temperature:"
                });
                trName.Cells.Add(new TableCell()
                {
                    Text = lastEntry.Properties["waterTemperature"].DoubleValue.ToString() + "°F"
                });
                deviceStatus.Rows.Add(trName);

                trName    = new TableRow();
                trName.ID = "PowerStatus";
                trName.Cells.Add(new TableCell()
                {
                    Text = "Power State:"
                });
                trName.Cells.Add(new TableCell()
                {
                    Text = lastEntry.Properties["PowerStatus"].StringValue
                });
                deviceStatus.Rows.Add(trName);

                /*trName = new TableRow();
                 * trName.Cells.Add(new TableCell() { Text = "Connected State:" });
                 * trName.Cells.Add(new TableCell() { Text = getDeviceStatus(lastEntry.Properties["hostName"].StringValue).Result });
                 * deviceStatus.Rows.Add(trName);*/

                trName = new TableRow();
                trName.Cells.Add(new TableCell()
                {
                    Text = "Last Changed:"
                });
                DateTime cstTime = TimeZoneInfo.ConvertTimeFromUtc(lastEntry.Timestamp.UtcDateTime, cstZone);

                trName.Cells.Add(new TableCell()
                {
                    Text = cstTime.ToShortDateString() + " " + cstTime.ToShortTimeString() + " CST"
                });
                deviceStatus.Rows.Add(trName);

                if (lastEntry.Properties["PowerStatus"].StringValue == "ON")
                {
                    ChangeState.Text = "Power Off";
                }
                else
                {
                    ChangeState.Text = "Power On";
                }
            }
        }
        private string ModifyLicense(string lic)
        {
            string result = CommandResult.OK;

            // lic: [0]SerialID;[1]Software;[2]MacHash;
            // lic: [3]VariableName=Value;[4]VariableName=Value;[5]VariableName=Value;....

            var lics = lic.Split(';');

            if (lics.Length < 4)
            {
                result = CommandResult.InvalidCommand;
                return(result);
            }

            // Query the table and retrieve a collection of entities.
            var query = new TableQuery <ElasticTableEntity>();
            IEnumerable <ElasticTableEntity> entities = null;
            IEnumerable <ElasticTableEntity> q        = null;

            q        = table.ExecuteQuery(query).Where(e => e.Value("PartitionKey").StartsWith(lics[1])).Select(e => e);
            entities = q.ToList();

            var md5Hash = MD5.Create();
            //string userHash = GetMd5Hash(md5Hash, lics[2]);
            ElasticTableEntity iEntity = null;

            foreach (ElasticTableEntity entity in entities)
            {
                string iHash = GetMd5Hash(md5Hash, entity.RowKey);
                if (iHash == lics[2])
                {
                    iEntity = entity;
                    break;
                }
            }
            if (iEntity == null)
            {
                result = CommandResult.NotFound;
                return(result);
            }
            for (int i = 3; i < lics.Length; ++i)
            {
                var nv = lics[i].Split('=');
                if (nv.Length == 2)
                {
                    if (nv[0].Contains("Date") && iEntity.Properties.ContainsKey(nv[0]))
                    {
                        iEntity[nv[0]] = new DateTime(Int32.Parse(nv[1].Substring(0, 4)), Int32.Parse(nv[1].Substring(4, 2)), Int32.Parse(nv[1].Substring(6, 2)));;
                    }
                }
            }
            table.Execute(TableOperation.Merge(iEntity));

            return(result);

            /*
             * LicenseInfo licInfo = RetrieveEntityUsingPointQuery(table, lics[1], lics[0]);
             *
             * if (licInfo == null)
             * {
             *  result = CommandResult.NotFound;
             *  return result;
             * }
             *
             * for (int i = 4; i < lics.Length; ++i)
             * {
             *  var nv = lics[i].Split('=');
             *  if (nv.Length == 2)
             *  {
             *      switch (nv[0])
             *      {
             *          case nameof(licInfo.Contact):
             *              licInfo.Contact = nv[1];
             *              break;
             *
             *          case nameof(licInfo.Customer):
             *              licInfo.Customer = nv[1];
             *              break;
             *
             *          case nameof(licInfo.Email):
             *              licInfo.Email = nv[1];
             *              break;
             *
             *          case nameof(licInfo.ExpireDate):
             *              licInfo.ExpireDate = new DateTime(Int32.Parse(nv[1].Substring(0, 4)), Int32.Parse(nv[1].Substring(4, 2)), Int32.Parse(nv[1].Substring(6, 2)));
             *              break;
             *
             *          case nameof(licInfo.IssueDate):
             *              licInfo.IssueDate = new DateTime(Int32.Parse(nv[1].Substring(0, 4)), Int32.Parse(nv[1].Substring(4, 2)), Int32.Parse(nv[1].Substring(6, 2)));
             *              break;
             *
             *          case nameof(licInfo.MaintenanceDate):
             *              licInfo.MaintenanceDate = new DateTime(Int32.Parse(nv[1].Substring(0, 4)), Int32.Parse(nv[1].Substring(4, 2)), Int32.Parse(nv[1].Substring(6, 2)));
             *              break;
             *
             *          case nameof(licInfo.Phone):
             *              licInfo.Phone = nv[1];
             *              break;
             *
             *          case nameof(licInfo.SoftVersion):
             *              licInfo.SoftVersion = nv[1];
             *              break;
             *
             *          case nameof(licInfo.Keys):
             *              licInfo.Keys = Int32.Parse(nv[1]);
             *              break;
             *
             *          default:
             *              //result = CommandResult.NotFound;
             *              break;
             *      }
             *
             *  }
             * }
             *
             * try
             * {
             *  // https://azure.microsoft.com/en-us/blog/managing-concurrency-in-microsoft-azure-storage-2/
             *  TableOperation mergeOperation = TableOperation.Merge(licInfo);
             *  table.Execute(mergeOperation);
             * }
             * catch (StorageException)
             * {
             * }
             */
        }
        public static void Run([TimerTrigger("0 0 3/3 1/1 * *", RunOnStartup = true)] TimerInfo myTimer,
                               [Queue("filereadytodownloadqueue", Connection = "AzureWebJobsStorage")] ICollector <FileReadyToDownloadQueueMessage> outputQueueItem,
                               TraceWriter log)
        {
            string partitionName = Constants.NamTrackerPartitionKey;

            log.Info($"DetectNAMGribReadyForDownload Timer trigger function executed at UTC: {DateTime.UtcNow}");

            // Retrieve storage account from connection string.
            var storageAccount           = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("AzureWebJobsStorage"));
            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
            CloudTable       table       = tableClient.GetTableReference(Constants.NamTrackerTable);

            table.CreateIfNotExists();

            //look back eight days and fill in any missing values; I beleive they store files on this server for 7 days
            TableQuery <FileProcessedTracker> dateQuery = new TableQuery <FileProcessedTracker>().Where(
                TableQuery.CombineFilters(
                    TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionName),
                    TableOperators.And,
                    TableQuery.GenerateFilterConditionForDate("ForecastDate", QueryComparisons.GreaterThan, DateTime.UtcNow.AddDays(-8))
                    )
                );

            var results = table.ExecuteQuery(dateQuery);

            //find the list of files available on the server
            HttpWebRequest request            = (HttpWebRequest)HttpWebRequest.Create(@"http://nomads.ncep.noaa.gov/pub/data/nccf/com/nam/prod/");
            string         dateResponseString = "";

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                Stream       dataStream = response.GetResponseStream();
                StreamReader reader     = new StreamReader(dataStream);
                dateResponseString = reader.ReadToEnd();
                reader.Close();
                dataStream.Close();
            }
            //find the dates available in that string
            //>nam.(\d+) matches the date strings
            Regex           regex             = new Regex(@">nam.(\d+)");
            MatchCollection matches           = regex.Matches(dateResponseString);
            List <string>   dateList          = new List <string>();
            string          dateListLogString = "";

            foreach (Match match in matches)
            {
                dateList.Add(match.Groups[1].Value);
                dateListLogString += match.Groups[1].Value;
            }
            log.Info($"Have list of nam directories: {dateListLogString}");
            //for each date list get the file list
            string fileResponseString = "";
            var    fileList           = new List <Tuple <string, string> >();

#if DEBUG == true
            //shorten list for debugging
            dateList = dateList.GetRange(0, 1);
#endif
            foreach (var dateString in dateList)
            {
                HttpWebRequest requestInner = (HttpWebRequest)HttpWebRequest.Create(@"http://nomads.ncep.noaa.gov/pub/data/nccf/com/nam/prod/nam." + dateString);
                using (HttpWebResponse response = (HttpWebResponse)requestInner.GetResponse())
                {
                    Stream       dataStream = response.GetResponseStream();
                    StreamReader reader     = new StreamReader(dataStream);
                    fileResponseString = reader.ReadToEnd();
                    reader.Close();
                    dataStream.Close();
                }
                //find the dates available in that string
                //only looking at the t00 forcast for now, in the future we can expand to the other forecast runs
                Regex           regexFile   = new Regex(@">(nam\.t00z\.awphys[\S]+)\.idx");
                MatchCollection matchesFile = regexFile.Matches(fileResponseString);
                foreach (Match match in matchesFile)
                {
                    fileList.Add(new Tuple <string, string>(match.Groups[1].Value, dateString));
                }
            }

            log.Info($"Have list of {fileList.Count} nam files to compare");
#if DEBUG == true
            //shorten list for debugging
            fileList = fileList.GetRange(0, 1);
#endif
            //compare fileList to existing files
            foreach (var file in fileList)
            {
                int countOfFilesInTable = results.Where(f => f.RowKey == file.Item2 + "." + file.Item1 && f.PartitionKey == partitionName).Count();
                if (countOfFilesInTable == 1)
                {
                    //file exists already
                    log.Info($"Already have {file.Item1} with date {file.Item2} in table as downloaded.");
                    continue;
                }
                else if (countOfFilesInTable > 1)
                {
                    log.Error($"Have multiple files with name {file.Item1} in partition {partitionName}");
                }
                else
                {
                    //template is the for grib filter tool available here: http://nomads.ncep.noaa.gov/txt_descriptions/grib_filter_doc.shtml
                    //TODO: should make levels and variables configurable
                    //TODO: should make region configurable
                    string downloadTemplate = @"http://nomads.ncep.noaa.gov/cgi-bin/filter_nam.pl?file=%FILENAME%&lev_10_m_above_ground=on&lev_80_m_above_ground&lev_2_m_above_ground=on&lev_surface=on&lev_tropopause=on&var_APCP=on&var_CRAIN=on&var_CSNOW=on&var_RH=on&var_TMP=on&var_UGRD=on&var_VGRD=on&subregion=&leftlon=-125&rightlon=-104&toplat=49&bottomlat=32&dir=%2Fnam.%DATE%";
                    downloadTemplate = downloadTemplate.Replace("%FILENAME%", file.Item1);
                    downloadTemplate = downloadTemplate.Replace("%DATE%", file.Item2);
                    log.Info($"Adding file {file.Item1} with date {file.Item2} to download queue.");
                    //enter a new queue item for every file missing
                    outputQueueItem.Add(new FileReadyToDownloadQueueMessage {
                        FileName = file.Item1, FileDate = file.Item2, Url = downloadTemplate, Filetype = partitionName
                    });
                }
            }
        }
Exemplo n.º 28
0
 public IEnumerable <DiagnosticsSource> GetSources()
 {
     return(_table.ExecuteQuery(new TableQuery <DynamicTableEntity>()).Select(x => new DiagnosticsSource(x)));
 }
Exemplo n.º 29
0
        static void Main(string[] args)
        {
            // Parse the connection string and return a reference to the storage account.
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                ConfigurationManager.AppSettings["StorageConnectionString"]);

            // Create the table client.
            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

            // Retrieve a reference to the table.
            CloudTable table = tableClient.GetTableReference("DurableFunctionsHubHistory");

            while (true)
            {
                ConcurrentDictionary <string, DateTime?> partitionKeys = new ConcurrentDictionary <string, DateTime?>();
                Parallel.ForEach(table.ExecuteQuery(new TableQuery()), entity =>
                {
                    partitionKeys.TryAdd(entity.PartitionKey, entity.Properties["_Timestamp"].DateTime);
                });

                var stats = new Dictionary <string, TimeSpan>();

                foreach (var partitionKey in partitionKeys.OrderBy(x => x.Value))
                {
                    table = tableClient.GetTableReference("DurableFunctionsHubHistory");

                    // Construct the query operation for all customer entities where PartitionKey="Smith".
                    var query = new TableQuery().Where(
                        TableQuery.CombineFilters(
                            TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey.Key),
                            TableOperators.And,
                            TableQuery.GenerateFilterCondition("Name", QueryComparisons.Equal, "OPARuleOrchestrator")
                            )
                        );

                    var jobStartedRows = table.ExecuteQuery(query).ToList();

                    var startTime = jobStartedRows[0].Properties["_Timestamp"].DateTime;

                    var secondQuery = new TableQuery().Where(
                        TableQuery.CombineFilters(
                            TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey.Key),
                            TableOperators.And,
                            TableQuery.GenerateFilterCondition("OrchestrationStatus", QueryComparisons.Equal, "Completed")
                            )
                        );

                    var jobEndedRows = table.ExecuteQuery(secondQuery).ToList();
                    if (jobEndedRows.Count == 0)
                    {
                        continue;
                    }

                    var endTime = jobEndedRows[0].Properties["_Timestamp"].DateTime;

                    var differenceInTimespan = endTime - startTime;

                    stats.Add(partitionKey.Key, differenceInTimespan.Value);
                }
                Console.WriteLine("**************************************");
                Console.WriteLine("Job executiion partition key,     Time(in seconds),   Time(in milliseconds)");

                foreach (var item in stats)
                {
                    Console.WriteLine(item.Key + ",       " + item.Value.Seconds + ",             " + item.Value.Milliseconds);
                }

                Console.WriteLine("**************************************");
                Console.ReadLine();
                //// Print the fields for each customer.
                //foreach (CustomerEntity entity in table.ExecuteQuery(query))
                //{
                //    Console.WriteLine("{0}, {1}\t{2}\t{3}", entity.PartitionKey, entity.RowKey,
                //        entity.Email, entity.PhoneNumber);
                //}
            }
        }
Exemplo n.º 30
0
        static void Main(string[] args)
        {
            // 01 - Connect to your azure storage account
            string connectionString         = ConfigurationManager.AppSettings["StorageConnectionString"];
            CloudStorageAccount account     = CloudStorageAccount.Parse(connectionString);
            CloudTableClient    tableClient = account.CreateCloudTableClient();

            // 02 - Create a table called "customers"
            CloudTable table = tableClient.GetTableReference("customers");

            table.CreateIfNotExists();

            // 03 - Insert single entity (instance of the CustomerEntity class) into the table
            CustomerEntity customer = new CustomerEntity("Bulgaria", "Ivan");

            customer.BirthDate = DateTime.Today;
            TableOperation insert = TableOperation.Insert(customer);

            table.Execute(insert);

            // 04 - Insert two additional CustomerEntity objects using batching (use PartitionKey "Netherlands")
            CustomerEntity customer1 = new CustomerEntity("Netherlands", "Holan");

            customer1.BirthDate = DateTime.Today;
            CustomerEntity customer2 = new CustomerEntity("Netherlands", "Lohan");

            customer2.BirthDate = DateTime.Today;

            // Create batch operation
            TableBatchOperation operation = new TableBatchOperation();

            operation.Insert(customer1);
            operation.Insert(customer2);
            table.ExecuteBatch(operation);

            // 05 - Retrieve one of the entities using TableOperation.Retrieve and print its PartitionKey using Console.WriteLine()
            TableOperation tops = TableOperation.Retrieve <CustomerEntity>("Netherlands", "Lohan");
            TableResult    tRes = table.Execute(tops);
            var            res  = tRes.Result as CustomerEntity;

            Console.WriteLine(res.PartitionKey);

            // 06 - Retrieve all entities with PartitionKey "Netherlands" using TableQuery and print their RowKey using Console.WriteLine()
            TableQuery <CustomerEntity> query = new TableQuery <CustomerEntity>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "Netherlands"));

            foreach (CustomerEntity element in table.ExecuteQuery(query))
            {
                Console.WriteLine("Name:" + element.RowKey);
            }

            // 07 - Delete one of the entities from the table
            tops = TableOperation.Retrieve <CustomerEntity>("Netherlands", "Lohan");
            tRes = table.Execute(tops);
            CustomerEntity deleteEntity = (CustomerEntity)tRes.Result;
            // Delete entity
            TableOperation delete = TableOperation.Delete(deleteEntity);

            table.Execute(delete);

            // 08 - Insert a new entity into "customers" using DynamicTableEntity instead of CustomerEntity
            DynamicTableEntity dynamicEntity = new DynamicTableEntity("BG", "Misho");

            dynamicEntity.Properties.Add("Standing", new EntityProperty("sophomore"));
            //Insert
            TableOperation tOperation = TableOperation.Insert(dynamicEntity);

            table.Execute(tOperation);
            //Get
            tops = TableOperation.Retrieve("BG", "Misho");
            var rezultat = (DynamicTableEntity)table.Execute(tops).Result;

            Console.WriteLine("Dynamic:" + rezultat.PartitionKey);
        }
Exemplo n.º 31
0
        private void deleteEvent(int selectedEvent)
        {
            try
            {
                if (eventDict.ContainsKey(selectedEvent))
                {
                    EventEntity oneEventEntity = JsonConvert.DeserializeObject <EventEntity>(eventDict[selectedEvent]);

                    //Delete from event Table
                    var        storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["AzureWebJobsStorage"]);
                    var        tableClient    = storageAccount.CreateCloudTableClient();
                    CloudTable eventTable     = tableClient.GetTableReference("Event");
                    eventTable.CreateIfNotExists();

                    //Delete from Feedback Table
                    CloudTable feedbackTable = tableClient.GetTableReference("Feedback");
                    feedbackTable.CreateIfNotExists();

                    //Delete from Attendance
                    CloudTable attendanceTable = tableClient.GetTableReference("Attendance");
                    attendanceTable.CreateIfNotExists();

                    //Delete from Question Table
                    CloudTable questionTable = tableClient.GetTableReference("Question");
                    questionTable.CreateIfNotExists();

                    if (uniqueSurveyDict.ContainsKey(oneEventEntity.SurveyCode))
                    {
                        List <string> deleteEventList = uniqueSurveyDict[oneEventEntity.SurveyCode];

                        foreach (string e in deleteEventList)
                        {
                            EventEntity events = JsonConvert.DeserializeObject <EventEntity>(e);

                            TableQuery <FeedbackEntity> feedbackQuery = new TableQuery <FeedbackEntity>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, events.SurveyCode));
                            foreach (FeedbackEntity entity in feedbackTable.ExecuteQuery(feedbackQuery))
                            {
                                feedbackTable.Execute(TableOperation.Delete(entity));
                            }

                            TableQuery <FeedbackEntity> attendanceQuery = new TableQuery <FeedbackEntity>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, events.RowKey));
                            foreach (FeedbackEntity entity in attendanceTable.ExecuteQuery(attendanceQuery))
                            {
                                attendanceTable.Execute(TableOperation.Delete(entity));
                            }

                            TableQuery <FeedbackEntity> questionQuery = new TableQuery <FeedbackEntity>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, events.SurveyCode));
                            foreach (FeedbackEntity entity in questionTable.ExecuteQuery(questionQuery))
                            {
                                questionTable.Execute(TableOperation.Delete(entity));
                            }

                            eventTable.Execute(TableOperation.Delete(events));
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
        }