-
Notifications
You must be signed in to change notification settings - Fork 0
/
Admin.asmx.cs
152 lines (141 loc) · 5.35 KB
/
Admin.asmx.cs
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
//Kellan Nealy, #1331068, info 344 PA3
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Queue;
using Microsoft.WindowsAzure.Storage.Table;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.Services;
namespace WebRole1
{
/// <summary>
/// Summary description for Admin
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class Admin : System.Web.Services.WebService
{
private static CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
ConfigurationManager.AppSettings["StorageConnectionString"]);
private static CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
//queues
private CloudQueue admin = queueClient.GetQueueReference("admin");
private CloudQueue urls = queueClient.GetQueueReference("urls");
//table
private static CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
private static CloudTable table = tableClient.GetTableReference("pages");
private static CloudTable statstable = tableClient.GetTableReference("stats");
//other useful state
private PerformanceCounter cpu = new PerformanceCounter("Processor", "% Processor Time", "_Total");
private PerformanceCounter mem = new PerformanceCounter("Memory", "Available MBytes");
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string StartCrawler()
{
admin.CreateIfNotExists();
urls.CreateIfNotExists();
table.CreateIfNotExists();
statstable.CreateIfNotExists();
CloudQueueMessage adminMessage = new CloudQueueMessage("start");
admin.AddMessage(adminMessage);
return "Crawler Started!";
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string StopCrawler()
{
CloudQueueMessage adminMessage = new CloudQueueMessage("stop");
admin.AddMessage(adminMessage);
return "Crawler Stopped!";
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string ClearIndex()
{
table.DeleteIfExists();
return "index cleared!";
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetPageTitle(string url)
{
try
{
string encoded = HttpUtility.UrlEncode(url);
TableQuery<WebPage> titleQuery = new TableQuery<WebPage>()
.Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, encoded));
foreach (WebPage page in table.ExecuteQuery(titleQuery))
{
return page.title;
}
return "Page not found!";
}
catch
{
return "Query failed!";
}
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetStats()
{
List<string> statsToSend = new List<string>();
try
{
urls.FetchAttributes();
string size = "URL Queue Size: " + urls.ApproximateMessageCount;
statsToSend.Add(size);
statsToSend.Add("Available Memory: " + mem.NextValue().ToString());
statsToSend.Add("CPU Usage %: " + cpu.NextValue().ToString());
TableQuery<Stat> statQuery = new TableQuery<Stat>();
foreach (Stat elem in statstable.ExecuteQuery(statQuery))
{
statsToSend.Add("Index Size: " + elem.indexsize.ToString());
statsToSend.Add("Crawler State: " + elem.state);
statsToSend.Add("Last 10 URLS crawled: " + elem.last10);
statsToSend.Add(elem.errors);
}
}
catch
{
statsToSend.Add("Stats Could Not Be Retrieved!");
}
return new JavaScriptSerializer().Serialize(statsToSend);
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string ClearAll()
{
try
{
admin.Clear();
urls.Clear();
table.Delete();
statstable.Delete();
return "All Cloud Data Cleared!";
}
catch
{
return "Reset Failed! Uh-Oh!";
}
}
}
}
/*
* project greenlight
State of each worker role web crawler (loading, crawling, idle)
Machine counters (CPU Utilization%, RAM available)
#URLs crawled
Last 10 URLs crawled
Size of queue (#urls left in pipeline to be crawled)
Size of index (Table storage with our crawled data)
Any errors and their URLs
*/