protected async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                try
                {
                    Console.WriteLine("Getting Items...");
                    TableQuery <TenantModel> query = new TableQuery <TenantModel>();
                    query.Where(TableQuery.CombineFilters(
                                    TableQuery.GenerateFilterConditionForBool("IsIotHubDeployed", QueryComparisons.Equal, false),
                                    TableOperators.And,
                                    TableQuery.GenerateFilterConditionForDate("Timestamp", QueryComparisons.GreaterThan, DateTime.Now.AddHours(-1))));

                    var items = await this.tableStorageClient.QueryAsync("tenant", query, stoppingToken);

                    foreach (var item in items)
                    {
                        Console.WriteLine($"Processing {item.TenantId}");
                        try
                        {
                            await this.blobStorageClient.CreateBlobContainerIfNotExistsAsync(item.TenantId + "-iot-file-upload");

                            Console.WriteLine("File Upload Container Made");
                            IotHubDescription iothub = await this.azureManagementClient.IotHubManagementClient.RetrieveAsync(item.IotHubName, stoppingToken);

                            if (iothub.Properties.State == "Active")
                            {
                                Console.WriteLine("IoT Hub found");
                                var connectionString = this.azureManagementClient.IotHubManagementClient.GetConnectionString(iothub.Name);
                                await this.appConfigurationClient.SetValueAsync($"tenant:{item.TenantId}:iotHubConnectionString", connectionString);

                                Assembly     assembly = Assembly.GetExecutingAssembly();
                                StreamReader reader   = new StreamReader(assembly.GetManifestResourceStream("dps.json"));
                                string       template = await reader.ReadToEndAsync();

                                template = string.Format(
                                    template,
                                    item.DpsName,
                                    this.config.Global.Location,
                                    connectionString);
                                await this.azureManagementClient.DeployTemplateAsync(template);

                                item.IsIotHubDeployed = true;
                                await this.tableStorageClient.InsertOrReplaceAsync <TenantModel>("tenant", item);
                            }
                        }
                        catch (Microsoft.Azure.Management.IotHub.Models.ErrorDetailsException e)
                        {
                            if (e.Message == "Operation returned an invalid status code 'NotFound'")
                            {
                                Console.WriteLine("This is where we deploy IoT Hub");
                                Assembly     assembly = Assembly.GetExecutingAssembly();
                                StreamReader reader   = new StreamReader(assembly.GetManifestResourceStream("iothub.json"));
                                string       template = await reader.ReadToEndAsync();

                                template = string.Format(
                                    template,
                                    item.IotHubName,
                                    this.config.Global.Location,
                                    this.config.Global.SubscriptionId,
                                    this.config.Global.ResourceGroup,
                                    item.TenantId,
                                    "$twin.properties.desired.batchedTelemetry",
                                    this.config.TenantManagerService.TelemetryEventHubConnectionString,
                                    this.config.TenantManagerService.TwinChangeEventHubConnectionString,
                                    this.config.TenantManagerService.LifecycleEventHubConnectionString,
                                    this.config.Global.StorageAccountConnectionString);
                                await this.azureManagementClient.DeployTemplateAsync(template);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error:");
                    Console.WriteLine(e.Message);
                    Console.WriteLine(e.StackTrace);
                }
                finally
                {
                    await Task.Delay(15000, stoppingToken);
                }
            }
        }
        public virtual async Task <TenantListModel> GetAllActiveTenantAsync()
        {
            try
            {
                // Load the tenant from table storage
                TableQuery <TenantModel> query  = new TableQuery <TenantModel>().Where(TableQuery.GenerateFilterConditionForBool("IsIotHubDeployed", QueryComparisons.Equal, true));
                List <TenantModel>       result = await this.TableStorageClient.QueryAsync <TenantModel>(this.TenantTableId, query);

                return(new TenantListModel(result));
            }
            catch (Exception e)
            {
                throw new Exception("Unable to retrieve the active tenants from table storage", e);
            }
        }
 public Task <IEnumerable <T> > QueryRowsAsync <T>(string column, bool value) where T : new() =>
 QueryRowsAsync <T>(TableQuery.GenerateFilterConditionForBool(column, QueryComparisons.Equal, value));
Ejemplo n.º 4
0
        private static string GetAddressPrefix(CloudTableClient tableClient, TraceWriter log)
        {
            try
            {
                log.Info(string.Format("Retrieving available address prefix"));
                CloudTable table = tableClient.GetTableReference("tblAddressPrefix");

                TableQuery <AddressPrefixEntity> tableQuery = new TableQuery <AddressPrefixEntity>().Where(TableQuery.CombineFilters(
                                                                                                               TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "AddressPrefix"),
                                                                                                               TableOperators.And,
                                                                                                               TableQuery.GenerateFilterConditionForBool("Assigned", QueryComparisons.Equal, false))).Take(1);

                AddressPrefixEntity entity = table.ExecuteQuery(tableQuery).FirstOrDefault();
                if (entity != null && !entity.Assigned)
                {
                    entity.Assigned = true;
                    TableOperation updateOperation = TableOperation.Replace(entity);
                    table.Execute(updateOperation);

                    log.Info(string.Format("Assigned address prefix: {0}", entity.IPAddress));
                    return(entity.IPAddress);
                }
                else
                {
                    log.Error(string.Format("No available address prefixes found"));
                    return(null);
                }
            }
            catch (System.Exception e)
            {
                log.Error(string.Format("Could not retrieve a address prefix. Exception:{0}", e.Message));
                return(null);
            }
        }
Ejemplo n.º 5
0
 public static string PropertyEqual(this string property, bool value)
 {
     return(TableQuery.GenerateFilterConditionForBool(property, QueryComparisons.Equal, value));
 }
Ejemplo n.º 6
0
        public static async Task <HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = "hello")]
            HttpRequestMessage req,
            TraceWriter log
            )
        {
            log.Info($"<<<<: {await req.Content.ReadAsStringAsync()}");

            HelloRequest command;

            try
            {
                command = await req.Content.ReadAsAsync <HelloRequest>();
            }
            catch (Exception ex)
            {
                return(req.CreateResponse(new Result {
                    ErrorCode = ErrorCode.BadRequest, ErrorMessage = ex.Message
                }));
            }

            if (command?.HostID == null)
            {
                return(req.CreateResponse <Result>(ErrorCode.MissingHostID));
            }

            if (string.IsNullOrEmpty(command.HostID.Hostname))
            {
                return(req.CreateResponse <Result>(ErrorCode.MissingHostname));
            }

            if (string.IsNullOrEmpty(command.HostID.SerialNumber))
            {
                return(req.CreateResponse <Result>(ErrorCode.MissingSerialNumber));
            }

            log.Info($"HostID: {command.HostID.Hostname}, {command.HostID.SerialNumber}");

            var table = await Storage.Table;

            var result = await table.ExecuteAsync(TableOperation.Retrieve <RaspberryPiEntity>("pi", command.HostID.SerialNumber));

            if (result.HttpStatusCode == 404)
            {
                result = await table.ExecuteAsync(
                    TableOperation.Insert(
                        new RaspberryPiEntity(command.HostID.SerialNumber)
                {
                    Id = Guid.NewGuid(),         // once

                    Hostname = command.HostID.Hostname,
                    Version  = command.Version,
                }
                        )
                    );
            }
            else
            {
                var result2 = await table.ExecuteAsync(
                    TableOperation.Merge(
                        new RaspberryPiEntity(command.HostID.SerialNumber)
                {
                    ETag = result.Etag,

                    Hostname = command.HostID.Hostname,
                    Version  = command.Version,
                }
                        )
                    );

                // patch retrieve op
                ((RaspberryPiEntity)result.Result).Hostname = command.HostID.Hostname;
                ((RaspberryPiEntity)result.Result).Version  = command.Version;
            }

            var pi = (RaspberryPiEntity)result.Result;

            log.Info($"TV-ID: {pi.Id}");

            var commandQueue = Storage.GetCommandQueueReference(pi.Id);

            await commandQueue.CreateIfNotExistsAsync();

            if (!string.IsNullOrEmpty(pi.Command))
            {
                try
                {
                    await commandQueue.AddCommandAsync(new PuppeteerCommand { Commands = JArray.Parse(pi.Command) });
                }
                catch (Exception ex)
                {
                    log.Error($"'{pi.Name}' has invalid hello command", ex);
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(pi.GotoUrl))
                {
                    Uri gotoUrl;
                    if (Uri.TryCreate(pi.GotoUrl, UriKind.Absolute, out gotoUrl))
                    {
                        await commandQueue.AddCommandAsync(new GotoCommand { Url = gotoUrl });
                    }
                    else
                    {
                        log.Error($"'{pi.Name}' has invalid goto URL");
                    }
                }
            }

            var jobsSegment = await table.ExecuteQuerySegmentedAsync(new TableQuery <JobEntity>
            {
                FilterString = TableQuery.CombineFilters(
                    TableQuery.GenerateFilterCondition(nameof(JobEntity.PartitionKey), QueryComparisons.Equal, JobEntity.Prefix),
                    TableOperators.And,
                    TableQuery.GenerateFilterConditionForBool(nameof(JobEntity.Enabled), QueryComparisons.Equal, true)
                    )
            }, null);

            var jobs = new List <HelloJobResponse>();

            foreach (var job in jobsSegment.Results)
            {
                JObject jobCommand;

                try
                {
                    jobCommand = JObject.Parse(job.Command);
                }
                catch (Exception ex)
                {
                    log.Error($"job '{job.Name}' has invalid payload. this job will not be run", ex);
                    continue;
                }

                jobs.Add(new HelloJobResponse
                {
                    Name           = job.Name,
                    ETag           = job.ETag,
                    CronExpression = job.CronExpression,
                    Command        = jobCommand,
                    TimeZone       = job.TimeZone
                });
            }

            return(req.CreateResponse <Result <HelloResponse> >(new HelloResponse
            {
                Id = pi.Id,
                Name = pi.Name,
                GetCommandUrl = new Uri(req.RequestUri, $"/api/tv/{pi.Id}/commands/-/get"),
                EventsUrl = new Uri(req.RequestUri, $"/api/tv/{pi.Id}/events"),
                Jobs = jobs
            }));
        }
Ejemplo n.º 7
0
        T ISagaPersister.Get <T>(string property, object value)
        {
            var type      = typeof(T);
            var tableName = type.Name;
            var table     = client.GetTableReference(tableName);

            TableQuery <DictionaryTableEntity> query;

            var propertyInfo = type.GetProperty(property);

            if (propertyInfo.PropertyType == typeof(byte[]))
            {
                query = new TableQuery <DictionaryTableEntity>().Where(TableQuery.GenerateFilterConditionForBinary(property, QueryComparisons.Equal, (byte[])value));
            }
            else if (propertyInfo.PropertyType == typeof(bool))
            {
                query = new TableQuery <DictionaryTableEntity>().Where(TableQuery.GenerateFilterConditionForBool(property, QueryComparisons.Equal, (bool)value));
            }
            else if (propertyInfo.PropertyType == typeof(DateTime))
            {
                query = new TableQuery <DictionaryTableEntity>().Where(TableQuery.GenerateFilterConditionForDate(property, QueryComparisons.Equal, (DateTime)value));
            }
            else if (propertyInfo.PropertyType == typeof(Guid))
            {
                query = new TableQuery <DictionaryTableEntity>().Where(TableQuery.GenerateFilterConditionForGuid(property, QueryComparisons.Equal, (Guid)value));
            }
            else if (propertyInfo.PropertyType == typeof(Int32))
            {
                query = new TableQuery <DictionaryTableEntity>().Where(TableQuery.GenerateFilterConditionForInt(property, QueryComparisons.Equal, (int)value));
            }
            else if (propertyInfo.PropertyType == typeof(Int64))
            {
                query = new TableQuery <DictionaryTableEntity>().Where(TableQuery.GenerateFilterConditionForLong(property, QueryComparisons.Equal, (long)value));
            }
            else if (propertyInfo.PropertyType == typeof(Double))
            {
                query = new TableQuery <DictionaryTableEntity>().Where(TableQuery.GenerateFilterConditionForDouble(property, QueryComparisons.Equal, (double)value));
            }
            else if (propertyInfo.PropertyType == typeof(string))
            {
                query = new TableQuery <DictionaryTableEntity>().Where(TableQuery.GenerateFilterCondition(property, QueryComparisons.Equal, (string)value));
            }
            else
            {
                throw new NotSupportedException(
                          string.Format("The property type '{0}' is not supported in windows azure table storage",
                                        propertyInfo.PropertyType.Name));
            }

            try
            {
                return(ToEntity <T>(table.ExecuteQuery(query).FirstOrDefault()));
            }
            catch (WebException ex)
            {
                // occurs when table has not yet been created, but already looking for absence of instance
                if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null)
                {
                    var response = (HttpWebResponse)ex.Response;
                    if (response.StatusCode == HttpStatusCode.NotFound)
                    {
                        return(default(T));
                    }
                }

                throw;
            }
        }
        protected async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                // Get the list of tenants
                TableQuery <TenantMigrationModel> query   = new TableQuery <TenantMigrationModel>().Where(TableQuery.GenerateFilterConditionForBool("IsIotHubDeployed", QueryComparisons.Equal, true));
                List <TenantMigrationModel>       tenants = await this.tableStorageClient.QueryAsync <TenantMigrationModel>(TenantTable, query);

                // Verify against the list of processed tenants
                TableQuery <TenantMigrationModel> processedTenantsQuery = new TableQuery <TenantMigrationModel>().Where(TableQuery.GenerateFilterConditionForBool(nameof(TenantMigrationModel.IsMigrationCompleted), QueryComparisons.Equal, true));

                List <TenantMigrationModel> processedTenants = await this.tableStorageClient.QueryAsync <TenantMigrationModel>(TenantMigrationStatus, processedTenantsQuery);

                var tenantsToMigrate = tenants.Select(t => t.TenantId).Except(processedTenants.Select(t => t.TenantId));

                if (tenantsToMigrate != null && tenantsToMigrate.Count() > 0)
                {
                    string tenantToMigrate = tenantsToMigrate.First();

                    // Get Deployments for Name
                    Dictionary <string, string> deployments = await this.GetDeployments(tenantToMigrate, "deployments");

                    bool isTenantMigrationCompleted = false;
                    while (!isTenantMigrationCompleted)
                    {
                        // get the data from tenant for deploymentdevices collection
                        IEnumerable <ValueServiceModel> deploymentImpactedDevicesData = await this.GetDataForMigration(tenantToMigrate, DeploymentDevicePropertiesCollection);

                        if (deploymentImpactedDevicesData != null && deploymentImpactedDevicesData.Count() > 0)
                        {
                            // Convert the data into new model to be saved
                            foreach (ValueServiceModel data in deploymentImpactedDevicesData)
                            {
                                DeploymentHistoryModel deviceDeploymentHistory = new DeploymentHistoryModel();
                                deviceDeploymentHistory.DeploymentId           = data.CollectionId.Substring(18);
                                deviceDeploymentHistory.DeviceId               = data.Key;
                                deviceDeploymentHistory.DeploymentName         = deployments.ContainsKey(deviceDeploymentHistory.DeploymentId) ? deployments[deviceDeploymentHistory.DeploymentId] : string.Empty;
                                deviceDeploymentHistory.LastUpdatedDateTimeUtc = data.Timestamp.UtcDateTime;
                                deviceDeploymentHistory.PreviousFirmwareTwin   = null;
                                deviceDeploymentHistory.Twin = JsonConvert.DeserializeObject <TwinServiceModel>(data.Data);

                                // save the record to new collection
                                var isSuccess = await this.SaveDataToStorage(string.Format(DeviceDeploymentHistory, deviceDeploymentHistory.DeviceId), deviceDeploymentHistory.DeploymentId, deviceDeploymentHistory, tenantToMigrate);

                                if (isSuccess)
                                {
                                    // Delete the existing data once saved.
                                    await this.DeleteDataFromStorage(tenantToMigrate, data.CollectionId, data.Key);
                                }
                            }
                        }
                        else
                        {
                            IEnumerable <ValueServiceModel> deploymentImpactedDevicesHistoryData = await this.GetDataForMigration(tenantToMigrate, DeploymentHistoryPropertiesCollection);

                            if (deploymentImpactedDevicesHistoryData != null && deploymentImpactedDevicesHistoryData.Count() > 0)
                            {
                                // Convert the data into new model to be saved
                                foreach (ValueServiceModel data in deploymentImpactedDevicesData)
                                {
                                    DeploymentHistoryModel deviceDeploymentHistory = new DeploymentHistoryModel();
                                    deviceDeploymentHistory.DeploymentId           = data.CollectionId.Substring(18, data.CollectionId.Length - (36 + 1) - 18);
                                    deviceDeploymentHistory.DeviceId               = data.Key;
                                    deviceDeploymentHistory.DeploymentName         = deployments.ContainsKey(deviceDeploymentHistory.DeploymentId) ? deployments[deviceDeploymentHistory.DeploymentId] : string.Empty;
                                    deviceDeploymentHistory.LastUpdatedDateTimeUtc = data.Timestamp.UtcDateTime;
                                    deviceDeploymentHistory.PreviousFirmwareTwin   = null;
                                    deviceDeploymentHistory.Twin = JsonConvert.DeserializeObject <TwinServiceModel>(data.Data);

                                    // save the record to new collection
                                    var isSuccess = await this.SaveDataToStorage(string.Format(DeviceDeploymentHistory, deviceDeploymentHistory.DeviceId), deviceDeploymentHistory.DeploymentId, deviceDeploymentHistory, tenantToMigrate);

                                    if (isSuccess)
                                    {
                                        // Delete the existing data once saved.
                                        await this.DeleteDataFromStorage(tenantToMigrate, data.CollectionId, data.Key);
                                    }
                                }
                            }
                            else
                            {
                                isTenantMigrationCompleted = true;
                                break;
                            }
                        }

                        await Task.Delay(30 * 1000, stoppingToken);
                    }

                    // Update the TenantMigrationStatus to true;
                    await this.InsertTenantMigrationStatus(tenantToMigrate);
                }

                await Task.Delay(300 * 1000, stoppingToken);
            }
        }
Ejemplo n.º 9
0
        public void TableGenericQueryOnSupportedTypes()
        {
            // 1. Filter on String
            ExecuteQueryAndAssertResults(this.currentTable,
                                         TableQuery.GenerateFilterCondition("String", QueryComparisons.GreaterThanOrEqual, "0050"), Midpoint);

            // 2. Filter on Guid
            ExecuteQueryAndAssertResults(this.currentTable,
                                         TableQuery.GenerateFilterConditionForGuid("Guid", QueryComparisons.Equal, this.middleRef.Guid), 1);

            // 3. Filter on Long
            ExecuteQueryAndAssertResults(this.currentTable,
                                         TableQuery.GenerateFilterConditionForLong("Int64", QueryComparisons.GreaterThanOrEqual,
                                                                                   this.middleRef.LongPrimitive), Midpoint);

            ExecuteQueryAndAssertResults(this.currentTable, TableQuery.GenerateFilterConditionForLong("LongPrimitive",
                                                                                                      QueryComparisons.GreaterThanOrEqual, this.middleRef.LongPrimitive), Midpoint);

            // 4. Filter on Double
            ExecuteQueryAndAssertResults(this.currentTable,
                                         TableQuery.GenerateFilterConditionForDouble("Double", QueryComparisons.GreaterThanOrEqual,
                                                                                     this.middleRef.Double), Midpoint);

            ExecuteQueryAndAssertResults(this.currentTable, TableQuery.GenerateFilterConditionForDouble("DoublePrimitive",
                                                                                                        QueryComparisons.GreaterThanOrEqual, this.middleRef.DoublePrimitive), Midpoint);

            // 5. Filter on Integer
            ExecuteQueryAndAssertResults(this.currentTable,
                                         TableQuery.GenerateFilterConditionForInt("Int32", QueryComparisons.GreaterThanOrEqual,
                                                                                  this.middleRef.Int32), Midpoint);

            ExecuteQueryAndAssertResults(this.currentTable, TableQuery.GenerateFilterConditionForInt("IntegerPrimitive",
                                                                                                     QueryComparisons.GreaterThanOrEqual, middleRef.IntegerPrimitive), Midpoint);

            // 6. Filter on Date
            ExecuteQueryAndAssertResults(this.currentTable,
                                         TableQuery.GenerateFilterConditionForDate("DateTimeOffset", QueryComparisons.GreaterThanOrEqual,
                                                                                   this.middleRef.DateTimeOffset), Midpoint);

            // 7. Filter on Boolean
            ExecuteQueryAndAssertResults(this.currentTable,
                                         TableQuery.GenerateFilterConditionForBool("Bool", QueryComparisons.Equal, this.middleRef.Bool), Midpoint);

            ExecuteQueryAndAssertResults(this.currentTable,
                                         TableQuery.GenerateFilterConditionForBool("BoolPrimitive", QueryComparisons.Equal, this.middleRef.BoolPrimitive),
                                         Midpoint);

            // 8. Filter on Binary
            ExecuteQueryAndAssertResults(this.currentTable,
                                         TableQuery.GenerateFilterConditionForBinary("Binary", QueryComparisons.Equal, this.middleRef.Binary), 1);

            ExecuteQueryAndAssertResults(this.currentTable,
                                         TableQuery.GenerateFilterConditionForBinary("BinaryPrimitive", QueryComparisons.Equal,
                                                                                     middleRef.BinaryPrimitive), 1);

            // 9. Filter on Binary GTE
            ExecuteQueryAndAssertResults(this.currentTable,
                                         TableQuery.GenerateFilterConditionForBinary("Binary", QueryComparisons.GreaterThanOrEqual,
                                                                                     this.middleRef.Binary), Midpoint);

            ExecuteQueryAndAssertResults(this.currentTable, TableQuery.GenerateFilterConditionForBinary("BinaryPrimitive",
                                                                                                        QueryComparisons.GreaterThanOrEqual, this.middleRef.BinaryPrimitive), Midpoint);

            // 10. Complex Filter on Binary GTE
            ExecuteQueryAndAssertResults(this.currentTable, TableQuery.CombineFilters(
                                             TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal,
                                                                                this.middleRef.PartitionKey),
                                             TableOperators.And,
                                             TableQuery.GenerateFilterConditionForBinary("Binary", QueryComparisons.GreaterThanOrEqual,
                                                                                         this.middleRef.Binary)), Midpoint);

            ExecuteQueryAndAssertResults(this.currentTable, TableQuery.GenerateFilterConditionForBinary("BinaryPrimitive",
                                                                                                        QueryComparisons.GreaterThanOrEqual, this.middleRef.BinaryPrimitive), Midpoint);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Method to get a TimeOffMappingEntity by time off request id.
        /// </summary>
        /// <param name="timeOffRequestId">The TimeOffRequestId of the request to retrieve.</param>
        /// <returns>A <see cref="TimeOffMappingEntity"/>.</returns>
        public async Task <TimeOffMappingEntity> GetTimeOffRequestMappingEntityByRequestIdAsync(string timeOffRequestId)
        {
            await this.EnsureInitializedAsync().ConfigureAwait(false);

            var getEntitiesProps = new Dictionary <string, string>()
            {
                { "CurrentCallingAssembly", Assembly.GetCallingAssembly().GetName().Name },
            };

            this.telemetryClient.TrackTrace(MethodBase.GetCurrentMethod().Name, getEntitiesProps);

            // Table query
            TableQuery <TimeOffMappingEntity> query = new TableQuery <TimeOffMappingEntity>();

            query.Where(TableQuery.CombineFilters(TableQuery.GenerateFilterCondition("ShiftsRequestId", QueryComparisons.Equal, timeOffRequestId), TableOperators.And, TableQuery.GenerateFilterConditionForBool("IsActive", QueryComparisons.Equal, true)));

            // Results list
            List <TimeOffMappingEntity> results           = new List <TimeOffMappingEntity>();
            TableContinuationToken      continuationToken = null;

            if (await this.timeoffEntityMappingTable.ExistsAsync().ConfigureAwait(false))
            {
                do
                {
                    TableQuerySegment <TimeOffMappingEntity> queryResults = await this.timeoffEntityMappingTable.ExecuteQuerySegmentedAsync(query, continuationToken).ConfigureAwait(false);

                    continuationToken = queryResults.ContinuationToken;
                    results.AddRange(queryResults.Results);
                }while (continuationToken != null);
            }

            return(results.FirstOrDefault(x => x.ShiftsRequestId == timeOffRequestId));
        }
Ejemplo n.º 11
0
 private string GenerateFilterCondition(DataTable schema, string columnName, string columnValue)
 {
     if (columnName.Equals(AZURE_TABLE_PARTITION_KEY_COL))
     {
         return(TableQuery.GenerateFilterCondition(columnName, QueryComparisons.GreaterThan, columnValue));
     }
     else if (columnName.Equals(AZURE_TABLE_ROW_KEY_COL))
     {
         return(TableQuery.GenerateFilterCondition(columnName, QueryComparisons.GreaterThan, columnValue));
     }
     else if (columnName.Equals(AZURE_TABLE_TIMESTAMP_COL))
     {
         return(TableQuery.GenerateFilterConditionForDate(columnName, QueryComparisons.GreaterThan, DateTimeOffset.Parse(columnValue)));
     }
     else if (!schema.Columns.Contains(columnName))
     {
         return(String.Empty);
     }
     else
     {
         Type type = schema.Columns[columnName].DataType;
         if (type == typeof(Byte[]))
         {
             return(String.Empty);
         }
         else if (type == typeof(Boolean))
         {
             return(TableQuery.GenerateFilterConditionForBool(columnName, QueryComparisons.GreaterThan, Boolean.Parse(columnValue)));
         }
         else if (type == typeof(Int32))
         {
             return(TableQuery.GenerateFilterConditionForInt(columnName, QueryComparisons.GreaterThan, Int32.Parse(columnValue)));
         }
         else if (type == typeof(Int64))
         {
             return(TableQuery.GenerateFilterConditionForLong(columnName, QueryComparisons.GreaterThan, Int64.Parse(columnValue)));
         }
         else if (type == typeof(DateTime))
         {
             return(TableQuery.GenerateFilterConditionForDate(columnName, QueryComparisons.GreaterThan, DateTime.Parse(columnValue)));
         }
         else if (type == typeof(DateTimeOffset))
         {
             return(TableQuery.GenerateFilterConditionForDate(columnName, QueryComparisons.GreaterThan, DateTimeOffset.Parse(columnValue)));
         }
         else if (type == typeof(Double))
         {
             return(TableQuery.GenerateFilterConditionForDouble(columnName, QueryComparisons.GreaterThan, Double.Parse(columnValue)));
         }
         else if (type == typeof(Guid))
         {
             return(TableQuery.GenerateFilterConditionForGuid(columnName, QueryComparisons.GreaterThan, Guid.Parse(columnValue)));
         }
         else if (type == typeof(String))
         {
             return(TableQuery.GenerateFilterCondition(columnName, QueryComparisons.GreaterThan, columnValue));
         }
         else
         {
             return(String.Empty);
         }
     }
 }
Ejemplo n.º 12
0
        private string BuildFilerQuery()
        {
            string queryFilters = null;

            foreach (var filter in Filters)
            {
                var fieldName = _entityDefinition.Filters().FirstOrDefault(f => f.Accesor.MemberPath == filter.EntityMember.MemberPath)?.Name;
                if (String.IsNullOrWhiteSpace(fieldName))
                {
                    throw new StorageArgumentOutOfRangeException(nameof(filter), $"Not found filter with signature {filter.EntityMember.MemberPath}");
                }

                string queryFilter = null;

                var toCompare = filter.ConstantMember.Value;
                switch (Type.GetTypeCode(toCompare.GetType()))
                {
                case TypeCode.Boolean:
                    queryFilter = TableQuery.GenerateFilterConditionForBool(fieldName, GetOperator(filter.Operator), (bool)toCompare);
                    break;

                case TypeCode.SByte:
                case TypeCode.Byte:
                case TypeCode.Int16:
                case TypeCode.UInt16:
                case TypeCode.Int32:
                case TypeCode.UInt32:
                    queryFilter = TableQuery.GenerateFilterConditionForInt(fieldName, GetOperator(filter.Operator), (int)toCompare);
                    break;

                case TypeCode.Int64:
                case TypeCode.UInt64:
                    queryFilter = TableQuery.GenerateFilterConditionForLong(fieldName, GetOperator(filter.Operator), (long)toCompare);
                    break;

                case TypeCode.Single:
                case TypeCode.Double:
                case TypeCode.Decimal:
                    queryFilter = TableQuery.GenerateFilterConditionForDouble(fieldName, GetOperator(filter.Operator), (double)toCompare);
                    break;

                case TypeCode.DateTime:
                    queryFilter = TableQuery.GenerateFilterConditionForDate(fieldName, GetOperator(filter.Operator), (DateTime)toCompare);
                    break;

                case TypeCode.Char:
                case TypeCode.String:
                    queryFilter = TableQuery.GenerateFilterCondition(fieldName, GetOperator(filter.Operator), toCompare.ToString());
                    break;

                default:
                    throw new StorageArgumentOutOfRangeException(nameof(filter), $"Can not extract value for type '{toCompare.GetType().FullName}'");
                }

                if (String.IsNullOrWhiteSpace(queryFilters))
                {
                    queryFilters = queryFilter;
                }
                else
                {
                    queryFilters = TableQuery.CombineFilters(queryFilters, TableOperators.And, queryFilter);
                }
            }
            return(queryFilters);
        }
Ejemplo n.º 13
0
        public async static Task <IHttpResponse> GetAllSecureAsync(

            [Api.Meta.Flows.WorkflowParameter(Value = "{{AuthenticationMethod}}")]
            [QueryParameter(Name = "method")]
            IRef <Method> methodRef,

            [OptionalQueryParameter(Name = "successOnly")] bool successOnly,

            [Api.Meta.Flows.WorkflowParameter(Description = "ACP,Dash,TCM,HUDDLE", Value = "")]
            [OptionalQueryParameter(Name = "search")]
            string search,

            [OptionalQueryParameter(Name = "months")] int?monthsMaybe,
            AzureApplication application,
            EastFive.Api.Security security,
            IHttpRequest request,

            [Api.Meta.Flows.WorkflowVariable(AuthorizationPropertyName, "authorization{0}")]
            ContentTypeResponse <RedirectionManager[]> onContent,
            UnauthorizedResponse onUnauthorized,
            ConfigurationFailureResponse onConfigFailure,
            BadRequestResponse onBadRequest)
        {
            // this query is faster than the version commented out below
            var methodMaybe = await Method.ById(methodRef, application, (m) => m, () => default(Method?));

            if (methodMaybe.IsDefault())
            {
                return(onBadRequest().AddReason("Method no longer supported"));
            }
            var method = methodMaybe.Value;

            var months = monthsMaybe.HasValue ? Math.Abs(monthsMaybe.Value) : 1;
            var since  = DateTime.UtcNow.Date.AddMonths(-months);
            var query  = new TableQuery <GenericTableEntity>().Where(
                TableQuery.CombineFilters(
                    TableQuery.GenerateFilterConditionForGuid("method", QueryComparisons.Equal, method.id),
                    TableOperators.And,
                    TableQuery.CombineFilters(
                        TableQuery.GenerateFilterConditionForDate("Timestamp", QueryComparisons.GreaterThanOrEqual, since),
                        TableOperators.And,
                        TableQuery.GenerateFilterConditionForBool("authorized", QueryComparisons.Equal, true)
                        )
                    )
                );
            var table = EastFive.Azure.Persistence.AppSettings.Storage.ConfigurationString(
                (conn) =>
            {
                var account = CloudStorageAccount.Parse(conn);
                var client  = account.CreateCloudTableClient();
                return(client.GetTableReference("authorization"));
            },
                (why) => throw new Exception(why));

            var segment      = default(TableQuerySegment <GenericTableEntity>);
            var token        = default(TableContinuationToken);
            var redirections = new RedirectionManager[] { };

            do
            {
                segment = await table.ExecuteQuerySegmentedAsync(query, token);

                token = segment.ContinuationToken;
                var result = await segment.Results
                             .Select(
                    async (entity) =>
                {
                    var id          = Guid.Parse(entity.RowKey);
                    var when        = entity.Timestamp.DateTime;
                    var props       = entity.WriteEntity(default);
Ejemplo n.º 14
0
        public async Task <PvPGameModel[]> GetOpenGames()
        {
            var table = await this.GetGameTable();

            TableQuery <PvPGameEntity> query = new TableQuery <PvPGameEntity>().Where(TableQuery.GenerateFilterConditionForBool("OpenForJoin", QueryComparisons.Equal, true));

            var result = await table.ExecuteQueryAsync <PvPGameEntity>(query);

            return(result.Select(o => new PvPGameModel()
            {
                GameName = o.GameName, Id = o.RowKey
            }).ToArray());
        }
Ejemplo n.º 15
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>();                   //记录每笔购买产生的比例  资产用户关系
                YemUserProductDto newYemUserProductDto = null;
                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 (item.RemainingAmount == 0)
                    {
                        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;
                    }
                    item.RemainingAmount      -= allocateAmount;
                    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);
                    }
                }
                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 async Task UpdateStateAsync(string serviceName, string functionName)
        {
            var currentState = await _circuitBreakerStateService.GetStateAsync(serviceName, functionName);

            if (currentState == null)
            {
                //We start by default with a closed state
                await _circuitBreakerStateService.CloseCircuitAsync(serviceName, functionName);

                currentState = CircuitBreakerState.Closed;
            }

            if (currentState.IsOpen)
            {
                //We do nothing here, the state should be updated via a timer
                return;
            }

            var tableClient = _storageAccount.CreateCloudTableClient();
            var configurationTableReference = tableClient.GetTableReference(TABLE_CONFIGURATION);
            var configuration = (await configurationTableReference.ExecuteAsync(TableOperation.Retrieve <ConfigurationEntity>(serviceName, functionName))).Result as ConfigurationEntity;

            var operationTableReference = tableClient.GetTableReference(TABLE_OPERATION);

            if (currentState.IsClosed)
            {
                var timestampToCompareWith = DateTime.Now.AddMilliseconds(-configuration.FailureRequestThresholdMilliseconds);

                if (currentState.Timestamp > timestampToCompareWith)
                {
                    timestampToCompareWith = new DateTime(currentState.Timestamp.Ticks);
                }

                //Do we need to open the circuit ?
                var operations = (await operationTableReference.ExecuteQuerySegmentedAsync(
                                      new TableQuery <OperationEntity>().Where(
                                          TableQuery.CombineFilters(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, $"{serviceName}_{functionName}"),
                                                                    TableOperators.And,
                                                                    TableQuery.GenerateFilterConditionForDate("Timestamp", QueryComparisons.GreaterThan, timestampToCompareWith
                                                                                                              ))), null)).Results;

                if (operations.Count(o => !o.IsSuccess) >= configuration.FailureRequestThreshold)
                {
                    //Yes we do
                    await _circuitBreakerStateService.OpenCircuitAsync(serviceName, functionName);
                }
            }
            else
            {
                //Here, the circuit is half open. Do we need to close it ?
                var failures = (await operationTableReference.ExecuteQuerySegmentedAsync(
                                    new TableQuery <OperationEntity>().Where(
                                        TableQuery.CombineFilters(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, $"{serviceName}_{functionName}"),
                                                                  TableOperators.And,
                                                                  TableQuery.GenerateFilterConditionForBool("IsSuccess", QueryComparisons.Equal, false)
                                                                  )), null)).Results;


                //Since the latest failure, did we reach the threshold to close the circuit ?
                var latestFailure = failures.OrderByDescending(o => o.Timestamp).FirstOrDefault();

                var successSinceLatestFailure = (await operationTableReference.ExecuteQuerySegmentedAsync(
                                                     new TableQuery <OperationEntity>().Where(
                                                         TableQuery.CombineFilters(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, $"{serviceName}_{functionName}"),
                                                                                   TableOperators.And,
                                                                                   TableQuery.GenerateFilterConditionForDate("Timestamp", QueryComparisons.GreaterThan, latestFailure.Timestamp)
                                                                                   )), null)).Results.Where(r => r.IsSuccess);

                if (successSinceLatestFailure == null || !successSinceLatestFailure.Any())
                {
                    //If the latest is a failure, the circuit should be reopened
                    await _circuitBreakerStateService.OpenCircuitAsync(serviceName, functionName);
                }
                else if (successSinceLatestFailure.Count() >= configuration.HalfOpenSuccessThreshold)
                {
                    //There we close it
                    await _circuitBreakerStateService.CloseCircuitAsync(serviceName, functionName);
                }
            }
        }
Ejemplo n.º 17
0
        private string GenerateSearchFilter(string searchText, bool includePrerelease, bool includeSemVer2)
        {
            var result = "";

            if (!string.IsNullOrWhiteSpace(searchText))
            {
                // Filter to rows where the "searchText" prefix matches on the partition key.
                var prefix = searchText?.TrimEnd().Split(separator: null).Last() ?? string.Empty;

                var prefixLower = prefix;
                var prefixUpper = prefix + "~";

                var partitionLowerFilter = TableQuery.GenerateFilterCondition(
                    "PartitionKey",
                    QueryComparisons.GreaterThanOrEqual,
                    prefixLower);

                var partitionUpperFilter = TableQuery.GenerateFilterCondition(
                    "PartitionKey",
                    QueryComparisons.LessThanOrEqual,
                    prefixUpper);

                result = GenerateAnd(partitionLowerFilter, partitionUpperFilter);
            }

            // Filter to rows that are listed.
            result = GenerateAnd(
                result,
                GenerateIsTrue(nameof(PackageEntity.Listed)));

            if (!includePrerelease)
            {
                result = GenerateAnd(
                    result,
                    GenerateIsFalse(nameof(PackageEntity.IsPrerelease)));
            }

            if (!includeSemVer2)
            {
                result = GenerateAnd(
                    result,
                    TableQuery.GenerateFilterConditionForInt(
                        nameof(PackageEntity.SemVerLevel),
                        QueryComparisons.Equal,
                        0));
            }

            return(result);

            string GenerateAnd(string left, string right)
            {
                if (string.IsNullOrEmpty(left))
                {
                    return(right);
                }

                return(TableQuery.CombineFilters(left, TableOperators.And, right));
            }

            string GenerateIsTrue(string propertyName)
            {
                return(TableQuery.GenerateFilterConditionForBool(
                           propertyName,
                           QueryComparisons.Equal,
                           givenValue: true));
            }

            string GenerateIsFalse(string propertyName)
            {
                return(TableQuery.GenerateFilterConditionForBool(
                           propertyName,
                           QueryComparisons.Equal,
                           givenValue: false));
            }
        }
Ejemplo n.º 18
0
        public async void Execute()
        {
            while (true)
            {
                IEnumerable <StillInterestedEntity> messages = null;
                do
                {
                    var day = DateTimeOffset.UtcNow.AddDays(-configurationProvider.GetConfiguration <int>("stillInterestedTimeoutDays"));

                    TableQuery <StillInterestedEntity> tableQuery = new TableQuery <StillInterestedEntity>().Where(TableQuery.CombineFilters(
                                                                                                                       TableQuery.GenerateFilterConditionForDate("Timestamp", QueryComparisons.LessThanOrEqual, day),
                                                                                                                       TableOperators.And, TableQuery.GenerateFilterConditionForBool("ReceivedAnswer", QueryComparisons.Equal, false)));

                    messages = await tableStorageProvider.RetrieveFromTableAsync("stillInterested", tableQuery);

                    if (messages.Any(x => !x.ReceivedAnswer))
                    {
                        foreach (var message in messages.Where(x => !x.ReceivedAnswer))
                        {
                            var conversationReference = JsonConvert.DeserializeObject <ConversationReference>(message.ConversationReference).GetPostToBotMessage();

                            var client = new ConnectorClient(new Uri(conversationReference.ServiceUrl));

                            using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, conversationReference))
                            {
                                var botData = scope.Resolve <IBotData>();
                                await botData.LoadAsync(CancellationToken.None);

                                var task = scope.Resolve <IDialogTask>();

                                //interrupt the stack
                                var dialog = new StillInterestedDialog(message);
                                task.Call(dialog.Void <object, IMessageActivity>(), null);

                                await task.PollAsync(CancellationToken.None);

                                //flush dialog stack
                                await botData.FlushAsync(CancellationToken.None);
                            }
                        }
                        await tableStorageProvider.DeleteFromTableAsync("stillInterested", messages);
                    }
                } while (messages.Any());
                Thread.Sleep(TimeSpan.FromHours(configurationProvider.GetConfiguration <int>("stillInterestedPollIntervalHours")));
            }
        }
Ejemplo n.º 19
0
        public static string FilterString(string propertyName, string queryComparison, object value)
        {
            var filterString = "";

            var val = "";

            if (value != null)
            {
                val = value.GetType().ToString().Replace("System.", "");
            }

            switch (val)
            {
            //case "":
            //    filterString = TableQuery.GenerateFilterCondition(property, queryComparison, "");
            //    break;

            case "Byte[]":
                filterString = TableQuery.GenerateFilterConditionForBinary(propertyName, queryComparison, (byte[])value);
                break;

            case "Boolean":
                filterString = TableQuery.GenerateFilterConditionForBool(propertyName, queryComparison, (bool)value);
                break;

            case "DateTime":
                filterString = TableQuery.GenerateFilterConditionForDate(propertyName, queryComparison, (DateTime)value);
                break;

            case "Double":
                filterString = TableQuery.GenerateFilterConditionForDouble(propertyName, queryComparison, (double)value);
                break;

            case "Guid":
                filterString = TableQuery.GenerateFilterConditionForGuid(propertyName, queryComparison, (Guid)value);
                break;

            case "Int32":
                filterString = TableQuery.GenerateFilterConditionForInt(propertyName, queryComparison, (int)value);
                break;

            case "Int64":
                filterString = TableQuery.GenerateFilterConditionForLong(propertyName, queryComparison, (long)value);
                break;

            default:
                //if (Utils.IsRowKey(property))
                //    filterString = TableQuery.GenerateFilterCondition("RowKey", queryComparison, (String)value);
                //else

                //if (Utils.IsPartitionKey(property))
                //    filterString = TableQuery.GenerateFilterCondition("PartitionKey", queryComparison, (String)value);
                //else
                filterString = TableQuery.GenerateFilterCondition(propertyName, queryComparison, (String)value);
                break;
            }

            //Event("Type Not Handled error on QueryOn");

            return(filterString);
        }
Ejemplo n.º 20
0
        public override string ToString()
        {
            var filterOperation = QueryComparisons.Equal;

            switch (Operator)
            {
            case QueryFilterOperator.Equal:
                filterOperation = QueryComparisons.Equal;
                break;

            case QueryFilterOperator.NotEqual:
                filterOperation = QueryComparisons.NotEqual;
                break;

            case QueryFilterOperator.Lower:
                filterOperation = QueryComparisons.LessThan;
                break;

            case QueryFilterOperator.Greater:
                filterOperation = QueryComparisons.GreaterThan;
                break;

            case QueryFilterOperator.LowerEqual:
                filterOperation = QueryComparisons.LessThanOrEqual;
                break;

            case QueryFilterOperator.GreaterEqual:
                filterOperation = QueryComparisons.GreaterThanOrEqual;
                break;
            }

            if (Value is string)
            {
                return(TableQuery.GenerateFilterCondition(Property, filterOperation, (string)Value));
            }

            if (Value is bool)
            {
                return(TableQuery.GenerateFilterConditionForBool(Property, filterOperation, (bool)Value));
            }

            if (Value is byte[])
            {
                return(TableQuery.GenerateFilterConditionForBinary(Property, filterOperation, (byte[])Value));
            }

            if (Value is DateTimeOffset)
            {
                return(TableQuery.GenerateFilterConditionForDate(Property, filterOperation, (DateTimeOffset)Value));
            }

            if (Value is double)
            {
                return(TableQuery.GenerateFilterConditionForDouble(Property, filterOperation, (double)Value));
            }

            if (Value is Guid)
            {
                return(TableQuery.GenerateFilterConditionForGuid(Property, filterOperation, (Guid)Value));
            }

            if (Value is int)
            {
                return(TableQuery.GenerateFilterConditionForInt(Property, filterOperation, (int)Value));
            }

            if (Value is long)
            {
                return(TableQuery.GenerateFilterConditionForLong(Property, filterOperation, (long)Value));
            }

            throw new NotSupportedException($"QueryFilter of Type \"{Value?.GetType().FullName}\" is not supported.");
        }