Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

Commit 4b57b4e

Browse files
Merge pull request #92 from massand/configuration_sample
Sample for ADM and cleanup changes
2 parents 9100254 + 81e38d5 commit 4b57b4e

File tree

6 files changed

+466
-113
lines changed

6 files changed

+466
-113
lines changed
Lines changed: 149 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,149 @@
1-
// Copyright (c) Microsoft. All rights reserved.
2-
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3-
4-
using System;
5-
using System.Collections.Generic;
6-
using System.Threading;
7-
using System.Threading.Tasks;
8-
9-
namespace Microsoft.Azure.Devices.Samples
10-
{
11-
public class CleanUpDevicesSample
12-
{
13-
private const int QueryBatchSize = 1000;
14-
private const int DeleteBatchSize = 100;
15-
private RegistryManager _rm;
16-
private List<string> _deleteDeviceWithPrefix =
17-
new List<string>{
18-
// C# E2E tests
19-
"E2E_",
20-
21-
// C E2E tests
22-
"e2e_",
23-
"e2e-",
24-
"symmetrickey-registration-id-",
25-
"tpm-registration-id-",
26-
"csdk_",
27-
"someregistrationid-",
28-
};
29-
30-
private List<Device> _devicesToDelete = new List<Device>();
31-
32-
public CleanUpDevicesSample(RegistryManager rm)
33-
{
34-
_rm = rm ?? throw new ArgumentNullException(nameof(rm));
35-
}
36-
37-
public async Task RunSampleAsync()
38-
{
39-
try
40-
{
41-
await PrintDeviceCount();
42-
43-
int devicesDeleted = 0;
44-
Console.WriteLine("Clean up devices:");
45-
string sqlQueryString = "select * from devices";
46-
IQuery query = _rm.CreateQuery(sqlQueryString, QueryBatchSize);
47-
48-
while (query.HasMoreResults)
49-
{
50-
IEnumerable<Shared.Twin> result = await query.GetNextAsTwinAsync();
51-
foreach (var twinResult in result)
52-
{
53-
string deviceId = twinResult.DeviceId;
54-
foreach (string prefix in _deleteDeviceWithPrefix)
55-
{
56-
if (deviceId.StartsWith(prefix))
57-
{
58-
_devicesToDelete.Add(new Device(deviceId));
59-
}
60-
}
61-
}
62-
}
63-
64-
var _bulkDeleteList = new List<Device>(DeleteBatchSize);
65-
while (_devicesToDelete.Count > 0)
66-
{
67-
int i;
68-
for (i = 0; (i < DeleteBatchSize) && (i < _devicesToDelete.Count); i++)
69-
{
70-
_bulkDeleteList.Add(_devicesToDelete[i]);
71-
Console.WriteLine($"\tRemove: {_devicesToDelete[i].Id}");
72-
}
73-
74-
_devicesToDelete.RemoveRange(0, i);
75-
76-
BulkRegistryOperationResult ret = await _rm.RemoveDevices2Async(_bulkDeleteList, true, CancellationToken.None).ConfigureAwait(false);
77-
devicesDeleted += _bulkDeleteList.Count - ret.Errors.Length;
78-
Console.WriteLine($"BATCH DELETE: {devicesDeleted}/{_bulkDeleteList.Count}");
79-
if (!ret.IsSuccessful)
80-
{
81-
foreach (DeviceRegistryOperationError error in ret.Errors)
82-
{
83-
Console.WriteLine($"\tERROR: {error.DeviceId} - {error.ErrorCode}: {error.ErrorStatus}");
84-
}
85-
}
86-
87-
_bulkDeleteList.Clear();
88-
}
89-
90-
Console.WriteLine($"-- Total no of devices deleted: {devicesDeleted}");
91-
}
92-
catch (Exception ex)
93-
{
94-
Console.WriteLine(ex);
95-
}
96-
}
97-
98-
private async Task PrintDeviceCount()
99-
{
100-
string countSqlQuery = "SELECT COUNT() AS numberOfDevices FROM devices";
101-
IQuery countQuery = _rm.CreateQuery(countSqlQuery);
102-
while (countQuery.HasMoreResults)
103-
{
104-
IEnumerable<string> result = await countQuery.GetNextAsJsonAsync().ConfigureAwait(false);
105-
foreach (var item in result)
106-
{
107-
Console.WriteLine($"Total no of devices on the hub: \n{item}");
108-
}
109-
}
110-
}
111-
}
112-
}
113-
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Threading;
8+
using System.Threading.Tasks;
9+
10+
namespace Microsoft.Azure.Devices.Samples
11+
{
12+
public class CleanUpDevicesSample
13+
{
14+
private const int QueryBatchSize = 1000;
15+
private const int DeleteBatchSize = 100;
16+
private RegistryManager _rm;
17+
private List<string> _deleteDeviceWithPrefix =
18+
new List<string>{
19+
// C# E2E tests
20+
"E2E_",
21+
22+
// C E2E tests
23+
"e2e_",
24+
"e2e-",
25+
"symmetrickey-registration-id-",
26+
"tpm-registration-id-",
27+
"csdk_",
28+
"someregistrationid-",
29+
"EdgeDeploymentSample_",
30+
};
31+
32+
private List<string> _deleteConfigurationWithPrefix =
33+
new List<string>{
34+
// C# E2E tests
35+
"edgedeploymentsampleconfiguration-",
36+
};
37+
38+
private List<Device> _devicesToDelete = new List<Device>();
39+
private List<Configuration> _configurationsToDelete = new List<Configuration>();
40+
41+
42+
public CleanUpDevicesSample(RegistryManager rm)
43+
{
44+
_rm = rm ?? throw new ArgumentNullException(nameof(rm));
45+
}
46+
47+
public async Task RunSampleAsync()
48+
{
49+
try
50+
{
51+
await PrintDeviceCount();
52+
53+
int devicesDeleted = 0;
54+
Console.WriteLine("Clean up devices:");
55+
string sqlQueryString = "select * from devices";
56+
IQuery query = _rm.CreateQuery(sqlQueryString, QueryBatchSize);
57+
58+
while (query.HasMoreResults)
59+
{
60+
IEnumerable<Shared.Twin> result = await query.GetNextAsTwinAsync();
61+
foreach (var twinResult in result)
62+
{
63+
string deviceId = twinResult.DeviceId;
64+
foreach (string prefix in _deleteDeviceWithPrefix)
65+
{
66+
if (deviceId.StartsWith(prefix))
67+
{
68+
_devicesToDelete.Add(new Device(deviceId));
69+
}
70+
}
71+
}
72+
}
73+
74+
var _bulkDeleteList = new List<Device>(DeleteBatchSize);
75+
while (_devicesToDelete.Count > 0)
76+
{
77+
int i;
78+
for (i = 0; (i < DeleteBatchSize) && (i < _devicesToDelete.Count); i++)
79+
{
80+
_bulkDeleteList.Add(_devicesToDelete[i]);
81+
Console.WriteLine($"\tRemove: {_devicesToDelete[i].Id}");
82+
}
83+
84+
_devicesToDelete.RemoveRange(0, i);
85+
86+
BulkRegistryOperationResult ret = await _rm.RemoveDevices2Async(_bulkDeleteList, true, CancellationToken.None).ConfigureAwait(false);
87+
devicesDeleted += _bulkDeleteList.Count - ret.Errors.Length;
88+
Console.WriteLine($"BATCH DELETE: {devicesDeleted}/{_bulkDeleteList.Count}");
89+
if (!ret.IsSuccessful)
90+
{
91+
foreach (DeviceRegistryOperationError error in ret.Errors)
92+
{
93+
Console.WriteLine($"\tERROR: {error.DeviceId} - {error.ErrorCode}: {error.ErrorStatus}");
94+
}
95+
}
96+
97+
_bulkDeleteList.Clear();
98+
}
99+
100+
Console.WriteLine($"-- Total no of devices deleted: {devicesDeleted}");
101+
102+
var configurations = await _rm.GetConfigurationsAsync(100, new CancellationToken()).ConfigureAwait(false);
103+
{
104+
foreach (var configuration in configurations)
105+
{
106+
string configurationId = configuration.Id;
107+
foreach (var prefix in _deleteConfigurationWithPrefix)
108+
{
109+
if (configurationId.StartsWith(prefix))
110+
{
111+
_configurationsToDelete.Add(new Configuration(configurationId));
112+
}
113+
}
114+
}
115+
}
116+
117+
var removeConfigTasks = new List<Task>();
118+
_configurationsToDelete.ForEach(configuration =>
119+
{
120+
Console.WriteLine($"Remove: {configuration.Id}");
121+
removeConfigTasks.Add(_rm.RemoveConfigurationAsync(configuration.Id));
122+
});
123+
124+
Task.WaitAll(removeConfigTasks.ToArray());
125+
Console.WriteLine($"-- Total no of configurations deleted: {_configurationsToDelete.Count}");
126+
127+
}
128+
catch (Exception ex)
129+
{
130+
Console.WriteLine(ex);
131+
}
132+
}
133+
134+
private async Task PrintDeviceCount()
135+
{
136+
string countSqlQuery = "SELECT COUNT() AS numberOfDevices FROM devices";
137+
IQuery countQuery = _rm.CreateQuery(countSqlQuery);
138+
while (countQuery.HasMoreResults)
139+
{
140+
IEnumerable<string> result = await countQuery.GetNextAsJsonAsync().ConfigureAwait(false);
141+
foreach (var item in result)
142+
{
143+
Console.WriteLine($"Total no of devices on the hub: \n{item}");
144+
}
145+
}
146+
}
147+
}
148+
}
149+

0 commit comments

Comments
 (0)