public async Task <HttpResponseMessage> DeleteAllDevices() { return(await GetServiceResponseAsync(async() => { // note that you could also hardcode a filter to delete a subset of devices var filter = new DeviceListFilter() { Skip = 0, Take = 1000, SortColumn = "twin.deviceId", }; DeviceListFilterResult devices = await _deviceLogic.GetDevices(filter); foreach (var d in devices.Results) { if (d.DeviceProperties != null && d.DeviceProperties.DeviceID != null) { string deviceId = d.DeviceProperties.DeviceID; // do this in serial so as not to overload anything Debug.Write("DELETING DEVICE: " + deviceId + "..."); await _deviceLogic.RemoveDeviceAsync(deviceId); Debug.WriteLine(" (Deleted)"); } } return true; })); }
public async Task <HttpResponseMessage> DeleteIcon(string name) { return(await GetServiceResponseAsync <bool>(async() => { var filter = new DeviceListFilter() { Clauses = new List <Clause>(), }; filter.Clauses.Add(new Clause { ColumnName = Constants.DeviceIconFullTagName, ClauseType = ClauseType.EQ, ClauseValue = name, }); var devices = await _deviceManager.QueryDevicesAsync(filter); if (devices.Any()) { throw new Exception("The device icon has been used by devices and can not be deleted."); } return await _deviceIconRepository.DeleteIcon(name); })); }
public async Task <HttpResponseMessage> SaveApplicableDeviceFilter(string filterId, bool isMatched, [FromBody] dynamic method) { return(await GetServiceResponseAsync(async() => { DeviceListFilter methodfilter = new DeviceListFilter(); DeviceListFilter rawfilter = await _filterRepository.GetFilterAsync(filterId); string queryColumnName = GenerateQueryColumnName(method); var conjunctionClause = await this.GetConjunctionClause(filterId); if (isMatched) { methodfilter.AdvancedClause = $"{conjunctionClause.CONDITION} {conjunctionClause.AND} is_defined({queryColumnName})"; } else { methodfilter.AdvancedClause = $"{conjunctionClause.CONDITION} {conjunctionClause.AND} NOT is_defined({queryColumnName})"; } methodfilter.Id = Guid.NewGuid().ToString(); methodfilter.Name = Infrastructure.Constants.UnnamedFilterName; methodfilter.IsAdvanced = true; methodfilter.IsTemporary = true; var savedfilter = await _filterRepository.SaveFilterAsync(methodfilter, false); return new { filterId = savedfilter.Id }; })); }
private async Task <dynamic> GetConjunctionClause(string filterId) { DeviceListFilter rawfilter = await _filterRepository.GetFilterAsync(filterId); var conditionstring = rawfilter.GetSQLCondition(); var whereClause = String.IsNullOrEmpty(conditionstring) ? "" : "WHERE"; var andClause = String.IsNullOrEmpty(conditionstring) ? "" : "AND"; return(new { CONDITION = conditionstring, WHERE = whereClause, AND = andClause }); }
public async Task <DeviceListFilter> SaveFilterAsync(DeviceListFilter filter, bool force = false) { var oldFilter = await GetFilterAsync(filter.Id); if (oldFilter == null) { filter.Id = Guid.NewGuid().ToString(); } else { if (!force) { return(oldFilter); } } if (filter.Name != Constants.UnnamedFilterName) { var query = new TableQuery <DeviceListFilterTableEntity>().Where( TableQuery.CombineFilters( TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.NotEqual, filter.Id), TableOperators.And, TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, filter.Name) ) ); var entities = await _filterTableStorageClient.ExecuteQueryAsync(query); if (entities.Any()) { throw new FilterDuplicatedNameException(filter.Id, filter.Name); } } DeviceListFilterTableEntity newEntity = new DeviceListFilterTableEntity(filter) { ETag = "*" }; var result = await _filterTableStorageClient.DoTableInsertOrReplaceAsync(newEntity, BuildFilterModelFromEntity); if (result.Status == TableStorageResponseStatus.Successful) { // Safely delete old filter after the new renamed filter is saved successfully if (oldFilter != null && !oldFilter.Name.Equals(filter.Name, StringComparison.InvariantCulture)) { var oldEntity = new DeviceListFilterTableEntity(oldFilter) { ETag = "*" }; await _filterTableStorageClient.DoDeleteAsync(oldEntity, e => (object)null); } return(await GetFilterAsync(filter.Id)); } throw new FilterSaveException(filter.Id, filter.Name); }
public async Task <DeviceListFilterResult> GetDevices(DeviceListFilter filter) { await _deviceListFilterRepository.TouchFilterAsync(filter.Id); var task = _deviceListFilterRepository.SaveSuggestClausesAsync(filter.Clauses); var devices = await _deviceRegistryListRepository.GetDeviceList(filter); UpdateNameCache(devices.Results.Select(r => r.Twin)); return(devices); }
private async Task <List <DeviceModel> > GetDevices() { var filter = new DeviceListFilter { Take = 1000 }; var devices = await _deviceLogic.GetDevices(filter); return(devices.Results); }
public async Task <ActionResult> Index() { var model = new DashboardModel(); var clauses = new List <Infrastructure.Models.Clause> { new Clause() { ColumnName = "tags.HubEnabledState", ClauseType = ClauseType.EQ, ClauseValue = "Running" } }; var query = new DeviceListFilter() { Skip = 0, Take = MaxDevicesToDisplayOnDashboard, SortColumn = "twin.deviceId", Clauses = clauses }; DeviceListFilterResult filterResult = await _deviceLogic.GetDevices(query); if ((filterResult != null) && (filterResult.Results != null)) { foreach (DeviceModel devInfo in filterResult.Results) { string deviceId; try { deviceId = devInfo.DeviceProperties.DeviceID; } catch (DeviceRequiredPropertyNotFoundException) { continue; } model.DeviceIdsForDropdown.Add(new StringPair(deviceId, deviceId)); } } // Set key to empty if passed value 0 from arm template string key = _configProvider.GetConfigurationSettingValue("MapApiQueryKey"); model.MapApiQueryKey = key.Equals("0") ? string.Empty : key; AddDefaultCultureIntoCookie(); return(View(model)); }
public async Task <IEnumerable <Twin> > QueryDevicesAsync(DeviceListFilter filter, int maxDevices = 10000) { var sqlQuery = filter.GetSQLQuery(); var deviceQuery = this._deviceManager.CreateQuery(sqlQuery); var twins = new List <Twin>(); while (deviceQuery.HasMoreResults && twins.Count < maxDevices) { twins.AddRange(await deviceQuery.GetNextAsTwinAsync()); } return(twins.Take(maxDevices)); }
public async Task GetDeviceListTest() { var rand = new Random(); var filtedDevices = this._fixture.CreateMany <DeviceModel>(16).ToList(); filtedDevices.ForEach(device => { device.Twin.DeviceId = device.DeviceProperties.DeviceID; device.Twin.Tags["seq"] = rand.Next(); }); var filter = new DeviceListFilter { Skip = 0, Take = int.MaxValue, SortOrder = QuerySortOrder.Ascending }; this._mockDocumentDBClient .Setup(x => x.QueryAsync()) .ReturnsAsync(filtedDevices.AsQueryable()); this._mockIoTHubDeviceManager .Setup(x => x.GetDeviceCountAsync()) .ReturnsAsync(1024); this._mockIoTHubDeviceManager .Setup(x => x.QueryDevicesAsync(It.IsAny <DeviceListFilter>(), It.IsAny <int>())). ReturnsAsync(filtedDevices.Select(d => d.Twin)); var result = await this._deviceRegistryRepository.GetDeviceList(filter); Assert.Equal(result.TotalDeviceCount, 1024); Assert.Equal(result.TotalFilteredCount, filtedDevices.Count()); Assert.True(result.Results.SequenceEqual(filtedDevices)); filter.SortColumn = "twin.tags.seq"; result = await this._deviceRegistryRepository.GetDeviceList(filter); Assert.True(result.Results.SequenceEqual(filtedDevices.OrderBy(d => (int)d.Twin.Tags["seq"]))); filter.SortOrder = QuerySortOrder.Descending; result = await this._deviceRegistryRepository.GetDeviceList(filter); Assert.True(result.Results.SequenceEqual(filtedDevices.OrderByDescending(d => (int)d.Twin.Tags["seq"]))); }
public async Task <HttpResponseMessage> GetDeviceLocationData() { return(await GetServiceResponseAsync <DeviceListLocationsModel>(async() => { var filter = new DeviceListFilter() { Skip = 0, Take = MAX_DEVICES_TO_DISPLAY_ON_DASHBOARD, SortColumn = "twin.deviceId" }; DeviceListFilterResult filterResult = await _deviceLogic.GetDevices(filter); DeviceListLocationsModel dataModel = _deviceLogic.ExtractLocationsData(filterResult.Results); return dataModel; }, false)); }
public async Task <HttpResponseMessage> GetDevicesAsync( [FromUri] string search = null, [FromUri] string sortColumn = null, [FromUri] QuerySortOrder sortOrder = QuerySortOrder.Ascending, [FromUri] int skip = 0, [FromUri] int take = 50, [FromUri] string[] filterColumn = null, [FromUri] ClauseType[] clauseType = null, [FromUri] string[] clauseValue = null) { var clauses = new List <Clause>(); if (filterColumn != null && clauseType != null && clauseValue != null) { // valid filters must send ALL three values--ignore unmatched extras int validFiltersCount = Math.Min(filterColumn.Length, Math.Min(clauseType.Length, clauseValue.Length)); for (int i = 0; i < validFiltersCount; ++i) { var clause = new Clause() { ColumnName = filterColumn[i], ClauseType = clauseType[i], ClauseValue = clauseValue[i] }; clauses.Add(clause); } } var filter = new DeviceListFilter() { SearchQuery = search, SortColumn = sortColumn, SortOrder = sortOrder, Skip = skip, Take = take, Clauses = clauses }; return(await GetServiceResponseAsync(async() => (await _deviceLogic.GetDevices(filter)).Results)); }
private async Task <List <DeviceModel> > LoadAllDevicesAsync() { var filter = new DeviceListFilter() { Skip = 0, Take = MAX_DEVICES_TO_DISPLAY_ON_DASHBOARD, SortColumn = "twin.deviceId" }; string deviceId; var devices = new List <DeviceModel>(); DeviceListFilterResult filterResult = await _deviceLogic.GetDevices(filter); if ((filterResult != null) && (filterResult.Results != null)) { bool? enabledState; DeviceProperties props; foreach (var devInfo in filterResult.Results) { try { deviceId = devInfo.DeviceProperties.DeviceID; props = devInfo.DeviceProperties; enabledState = props.HubEnabledState; } catch (NullReferenceException) { continue; } if (!string.IsNullOrWhiteSpace(deviceId)) { devices.Add(devInfo); } } } return(devices); }
public async void DeleteAllDevices() { var devices = fixture.Create <DeviceListFilterResult>(); DeviceListFilter saveObject = null; deviceLogic.Setup(mock => mock.GetDevices(It.IsAny <DeviceListFilter>())) .Callback <DeviceListFilter>(obj => saveObject = obj) .ReturnsAsync(devices); deviceLogic.Setup(mock => mock.RemoveDeviceAsync(It.IsAny <string>())).Returns(Task.FromResult(true)); var res = await deviceApiController.DeleteAllDevices(); Assert.Equal(saveObject.Skip, 0); Assert.Equal(saveObject.Take, 1000); Assert.Equal(saveObject.SortColumn, "twin.deviceId"); res.AssertOnError(); var data = res.ExtractContentDataAs <bool>(); Assert.True(data); }
public virtual async Task <DeviceListFilterResult> GetDeviceList(DeviceListFilter filter) { List <DeviceModel> deviceList = await this.GetAllDevicesAsync(); IQueryable <DeviceModel> filteredDevices = FilterHelper.FilterDeviceList(deviceList.AsQueryable <DeviceModel>(), filter.Clauses); IQueryable <DeviceModel> filteredAndSearchedDevices = this.SearchDeviceList(filteredDevices, filter.SearchQuery); IQueryable <DeviceModel> sortedDevices = this.SortDeviceList(filteredAndSearchedDevices, filter.SortColumn, filter.SortOrder); List <DeviceModel> pagedDeviceList = sortedDevices.Skip(filter.Skip).Take(filter.Take).ToList(); int filteredCount = filteredAndSearchedDevices.Count(); return(new DeviceListFilterResult() { Results = pagedDeviceList, TotalDeviceCount = deviceList.Count, TotalFilteredCount = filteredCount }); }
public override async Task <DeviceListFilterResult> GetDeviceList(DeviceListFilter filter) { // Kick-off DocDB query initializing var queryTask = this._documentClient.QueryAsync(); // Considering all the device properties was copied to IoT Hub twin as tag or // reported property, we will only query on the IoT Hub twins. The DocumentDB // will not be touched. var filteredDevices = await this._deviceManager.QueryDevicesAsync(filter); var sortedDevices = this.SortDeviceList(filteredDevices.AsQueryable(), filter.SortColumn, filter.SortOrder); var pagedDeviceList = sortedDevices.Skip(filter.Skip).Take(filter.Take).ToList(); // Query on DocDB for traditional device properties, commands and so on var deviceIds = pagedDeviceList.Select(twin => twin.DeviceId); var devicesFromDocDB = (await queryTask).Where(x => deviceIds.Contains(x.DeviceProperties.DeviceID)) .ToDictionary(d => d.DeviceProperties.DeviceID); return(new DeviceListFilterResult { Results = pagedDeviceList.Select(twin => { DeviceModel deviceModel; if (devicesFromDocDB.TryGetValue(twin.DeviceId, out deviceModel)) { deviceModel.Twin = twin; return deviceModel; } else { return null; } }).Where(model => model != null).ToList(), TotalDeviceCount = (int)await this._deviceManager.GetDeviceCountAsync(), TotalFilteredCount = filteredDevices.Count() }); }
public async Task <HttpResponseMessage> GetDevices([FromBody] JObject requestData) { return(await GetServiceResponseAsync <DataTablesResponse <DeviceModel> >(async() => { var dataTableRequest = requestData.ToObject <DataTablesRequest>(); var sortColumnIndex = dataTableRequest.SortColumns[0].ColumnIndex; var listFilter = new DeviceListFilter() { Id = dataTableRequest.Id, Name = dataTableRequest.Name, SortOrder = dataTableRequest.SortColumns[0].SortOrder, SortColumn = dataTableRequest.Columns[sortColumnIndex].Data, SearchQuery = dataTableRequest.Search.Value, Clauses = dataTableRequest.Clauses, AdvancedClause = dataTableRequest.AdvancedClause, IsAdvanced = dataTableRequest.IsAdvanced, Skip = dataTableRequest.Start, Take = dataTableRequest.Length }; var filterResult = await _deviceLogic.GetDevices(listFilter); var dataTablesResponse = new DataTablesResponse <DeviceModel>() { Draw = dataTableRequest.Draw, RecordsTotal = filterResult.TotalDeviceCount, RecordsFiltered = filterResult.TotalFilteredCount, Data = filterResult.Results.ToArray() }; return dataTablesResponse; }, false)); }