forked from Azure/azure-webjobs-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogController.cs
104 lines (92 loc) · 3.38 KB
/
LogController.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using DaasEndpoints;
using DataAccess;
using Microsoft.WindowsAzure.StorageClient;
using RunnerInterfaces;
namespace WebFrontEnd.ControllersWebApi
{
public class LogController : ApiController
{
private static Services GetServices()
{
AzureRoleAccountInfo accountInfo = new AzureRoleAccountInfo();
return new Services(accountInfo);
}
[HttpGet]
public HttpResponseMessage InvokeLog(string id)
{
// Parse the ID
Guid funcId;
if(!Guid.TryParse(id, out funcId)) {
return new HttpResponseMessage(HttpStatusCode.BadRequest);
}
// Get the invocation log
var instance = GetServices().GetFunctionInstanceLookup().Lookup(funcId);
if (instance == null)
{
return new HttpResponseMessage(HttpStatusCode.NotFound);
}
// Load the blob
CloudBlob blob;
try
{
blob = new CloudBlob(instance.OutputUrl, GetServices().Account.Credentials);
}
catch (Exception)
{
blob = null;
}
if (blob == null)
{
return new HttpResponseMessage(HttpStatusCode.Unauthorized);
}
// Get a SAS for the next 10 mins
string sas = blob.GetSharedAccessSignature(new SharedAccessPolicy()
{
Permissions = SharedAccessPermissions.Read,
SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(10)
});
// Redirect to it
var resp = new HttpResponseMessage(HttpStatusCode.Found);
resp.Headers.Location = new Uri(blob.Uri.AbsoluteUri + sas);
return resp;
}
[HttpGet]
public HttpResponseMessage GetFunctionLog(int N = 20, string account=null)
{
LogAnalysis l = new LogAnalysis();
IFunctionInstanceQuery query = GetServices().GetFunctionInstanceQuery();
IEnumerable<ChargebackRow> logs = l.GetChargebackLog(N, account, query);
using (var tw = new StringWriter())
{
tw.WriteLine("Name, Id, ParentId, GroupId, FirstParam, Duration");
foreach (var row in logs)
{
// Sanitize the first parameter for CSV usage.
string val = row.FirstParam;
if (val != null)
{
val = val.Replace('\r', ' ').Replace('\n', ' ').Replace(',', ';');
}
tw.WriteLine("{0}, {1}, {2}, {3}, {4}, {5}",
row.Name,
row.Id,
row.ParentId,
row.GroupId,
val,
row.Duration);
}
var content = tw.ToString();
var httpContent = new StringContent(content, System.Text.Encoding.UTF8, @"text/csv");
var resp = new HttpResponseMessage { Content = httpContent };
return resp;
}
}
}
}