Beispiel #1
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestMessage req, TraceWriter log)
        {
            var ua      = Parser.GetDefault().Parse(req.Headers.UserAgent.ToString());
            var os      = ua.OS.Family;
            var browser = ua.UserAgent.Family;

            /*
             * var os = req.GetQueryNameValuePairs().FirstOrDefault(q => q.Key == "os").Value;
             * var browser = req.GetQueryNameValuePairs().FirstOrDefault(q => q.Key == "browser").Value;
             * dynamic data = await req.Content.ReadAsAsync<object>();
             * os = os ?? data?.os;
             * browser = browser ?? data?.browser;
             *
             * if (os == null) return req.CreateErrorResponse(HttpStatusCode.BadRequest, "Missing os");
             * if (browser == null) return req.CreateErrorResponse(HttpStatusCode.BadRequest, "Missing browser");*/

            var account = CloudStorageAccount.Parse(Environment.GetEnvironmentVariable("TableConnectionString"));
            var client  = account.CreateCloudTableClient();
            var table   = client.GetTableReference("SignInInfo");
            var newInfo = new SignInInfo(os, browser);
            var insert  = TableOperation.Insert(newInfo);

            table.Execute(insert);

            var query = new TableQuery <SignInInfo>();
            var stats = new SignInStats();

            foreach (var info in table.ExecuteQuery(query))
            {
                stats.totalNumber++;
                if (!stats.byBrowser.ContainsKey(info.Browser))
                {
                    stats.byBrowser[info.Browser] = 0;
                }
                if (!stats.byOS.ContainsKey(info.OS))
                {
                    stats.byOS[info.OS] = 0;
                }
                stats.byBrowser[info.Browser]++;
                stats.byOS[info.OS]++;
            }

            var result = new SignInResult()
            {
                AuthInfo = SignInHub.GetAuthInfo(),
                Stats    = stats
            };

            await SignInHub.UpdateSignInStats(stats);

            return(req.CreateResponse(HttpStatusCode.OK, result, "application/json"));
        }
Beispiel #2
0
 public static async Task UpdateSignInStats(SignInStats stats)
 {
     await hubProxy.Clients.All.SendAsync("updateSignInStats", new object[] { stats });
 }