This repository was archived by the owner on Mar 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAC_SystemResouceListViewController.m
More file actions
358 lines (304 loc) · 12.1 KB
/
Copy pathAC_SystemResouceListViewController.m
File metadata and controls
358 lines (304 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
* Copyright 2009-2011 AppFirst, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import "AC_SystemResouceListViewController.h"
#import "AppComm.h"
#import "AM_SystemData.h"
#import "AM_ProcessData.h"
#import "AM_ResourceCell.h"
#import "AV_GenericTableCell.h"
#import "AFCpuDetailView.h"
#import "config.h"
#import "JSON.h"
#import "AC_GraphViewController.h"
#import "AppDelegate_Shared.h"
@implementation AC_SystemResouceListViewController
@synthesize resourceUrl, responseData, server, dataArray;
- (void) setResources:(NSMutableArray *)list {
[list retain];
[resources release];
resources = list;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[resources release];
[resourceUrl release];
[responseData release];
[server release];
[dataArray release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
//[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
dataArray = [[NSMutableArray alloc] init];
[self getData];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [resources count];
}
- (int) calculateHeight: (int) row {
int height = IPHONE_TABLE_ROW_HEIGHT;
AM_ResourceCell* resource = [resources objectAtIndex:row];
if ([resource renderOption] == AF_GraphCell) {
height = IPHONE_TABLE_ROW_HEIGHT * 2;
} else if ([resource renderOption] == AF_ExtendedGraphCell) {
if ([resource graphOption] == AF_Bar) {
height = IPHONE_TABLE_ROW_HEIGHT * ([[[resources objectAtIndex:row] extra] count] + 2);
} else {
height = IPHONE_TABLE_ROW_HEIGHT * ([[[resources objectAtIndex:row] extra] count] * 3) +
IPHONE_TABLE_ROW_HEIGHT * 2;
}
}
return height;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
AM_ResourceCell* resource = [resources objectAtIndex:indexPath.row];
UITableViewCell* cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AC_SystemResourceListViewControllerCell"] autorelease];
AV_GenericTableCell* cellView = [[AV_GenericTableCell alloc] initWithFrame:CGRectMake(0,
0,
tableView.frame.size.width,
[self calculateHeight:indexPath.row])];
[cellView setData:resource];
[cell.contentView addSubview: cellView];
[cellView release];
return cell;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
UIAlertView *errorView = [[UIAlertView alloc] initWithTitle: @"Can't get resource data. "
message: [error localizedDescription]
delegate: self
cancelButtonTitle: @"Ok"
otherButtonTitles: nil];
[errorView show];
[errorView release];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString *jsonString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
@try {
NSMutableArray* newDataArray = (NSMutableArray*)[jsonString JSONValue];
for (int cnt = 0; cnt < [newDataArray count]; cnt ++) {
AM_SystemData* systemData = [[AM_SystemData alloc] initWithJSONObject:[newDataArray objectAtIndex:cnt]];
[dataArray addObject:systemData];
[systemData release];
}
}
@catch (NSException * e) {
NSLog(@"main: Caught %@: %@", [e name], [e reason]);
return;
}
[jsonString release];
[self.tableView reloadData];
//[self finishLoading:[AppHelper formatDateString:[NSDate date]]];
}
// this function is responsible for getting data of last hour.
- (void) getData
{
responseData = [[NSMutableData data] retain];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setHTTPMethod:@"GET"];
[request setURL:[NSURL URLWithString:resourceUrl]];
[request setTimeoutInterval:20];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:[AppComm authString] forHTTPHeaderField:@"Authorization"];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [self calculateHeight:indexPath.row];
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
- (NSMutableArray*) getResourceLabelsByIndex: (int) index dataObject: (AM_SystemData*) dataObject resourceName: (NSString*) resourceName{
NSMutableArray* labels = [[[NSMutableArray alloc] init] autorelease];
switch (index) {
case 2:
for (id key in [[dataObject disk_percent_part] allKeys]) {
[labels addObject:[NSString stringWithFormat:@"%@ (%@)", resourceName, key]];
}
break;
case 3:
for (id key in [[dataObject disk_busy] allKeys]) {
[labels addObject:[NSString stringWithFormat:@"%@ (%@)", resourceName, key]];
}
break;
default:
[labels addObject:resourceName];
break;
}
return labels;
}
- (NSMutableArray*) getResourceValueByIndex: (int) index dataObject: (AM_SystemData*) dataObject {
NSMutableArray* newArray = [[[NSMutableArray alloc] init] autorelease];
switch (index) {
case 0:
[newArray addObject: [NSNumber numberWithDouble: [dataObject cpu]]];
break;
case 1:
[newArray addObject: [NSNumber numberWithDouble: [dataObject memory]]];
break;
case 2:
for (id key in [[dataObject disk_percent_part] allKeys]) {
[newArray addObject: [NSNumber numberWithDouble: [[[dataObject disk_percent_part] objectForKey:key] doubleValue]]];
}
break;
case 3:
for (id key in [[dataObject disk_busy] allKeys]) {
[newArray addObject: [NSNumber numberWithDouble: [[[dataObject disk_busy] objectForKey:key] doubleValue]]];
}
case 4:
[newArray addObject: [NSNumber numberWithInteger: [dataObject page_faults]]];
break;
case 5:
[newArray addObject: [NSNumber numberWithInteger: [dataObject process_num]]];
break;
case 6:
[newArray addObject: [NSNumber numberWithInteger: [dataObject thread_num]]];
break;
default:
break;
}
return newArray;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([dataArray count] == 0) {
UIAlertView *errorView = [[UIAlertView alloc] initWithTitle: @"Please wait... "
message: @"Historical data is still loading."
delegate: self
cancelButtonTitle: @"Ok"
otherButtonTitles: nil];
[errorView show];
[errorView release];
return;
}
AC_GraphViewController* aGraphController = [[AC_GraphViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:aGraphController];
NSMutableArray* newXvalues = [[NSMutableArray alloc] init];
NSMutableArray* newYvalues = [[NSMutableArray alloc] init];
AM_ResourceCell* resource = [resources objectAtIndex:indexPath.row];
for (int cnt = 0; cnt < [dataArray count]; cnt ++) {
[newXvalues addObject:[NSNumber numberWithLong:[(AM_SystemData*)[dataArray objectAtIndex:cnt] time]]];
[newYvalues addObject:[self getResourceValueByIndex:indexPath.row
dataObject:[dataArray objectAtIndex:cnt]]];
}
[aGraphController setupGraph:newXvalues newYvalues:newYvalues newLabels:[self getResourceLabelsByIndex:indexPath.row dataObject:[dataArray objectAtIndex:0] resourceName:[resource resourceName]]];
// show the navigation controller modally
AppDelegate_Shared* appDelegate = (AppDelegate_Shared *)[[UIApplication sharedApplication] delegate];
[[appDelegate navigationController] presentModalViewController:navController animated:YES];
// Clean up resources
[navController release];
[aGraphController release];
[newXvalues release];
[newYvalues release];
}
@end