Example #1
0
        public IActionResult Logout()
        {
            HttpServiceUriBuilder contextUri = new HttpServiceUriBuilder().SetServiceName(this.context.ServiceName);

            // Manage session
            if (!HTTPHelper.IsSessionExpired(HttpContext, this))
            {
                HTTPHelper.EndSession(HttpContext, this);
            }

            ViewBag.RedirectURL = contextUri.GetServiceNameSiteHomePath();

            return(Ok(contextUri.GetServiceNameSiteHomePath()));
        }
        public async Task <ActionResult> Devices()
        {
            // Manage session and Context
            HttpServiceUriBuilder contextUri = new HttpServiceUriBuilder().SetServiceName(this.context.ServiceName);

            if (HTTPHelper.IsSessionExpired(HttpContext, this))
            {
                return(Redirect(contextUri.GetServiceNameSiteHomePath()));
            }
            else
            {
                this.ViewData["TargetSite"]  = contextUri.GetServiceNameSite();
                this.ViewData["PageTitle"]   = "Devices";
                this.ViewData["HeaderTitle"] = "Devices Dashboard";

                string reportUniqueId = HashUtil.GetUniqueId();
                string reportName     = "DoverCivaconDemo"; // the dashboard

                EmbedConfig task = await ReportsHandler.GetEmbedReportConfigData(ClientId, GroupId, Username, Password, AuthorityUrl, ResourceUrl, ApiUrl, reportUniqueId, reportName, this.context, ServiceEventSource.Current);

                this.ViewData["EmbedToken"]     = task.EmbedToken.Token;
                this.ViewData["EmbedURL"]       = task.EmbedUrl;
                this.ViewData["EmbedId"]        = task.Id;
                this.ViewData["ReportUniqueId"] = "";

                return(this.View());
            }
        }
Example #3
0
        public IActionResult EmbedStreamReport(string reportUrl)
        {
            // Manage session and Context
            HttpServiceUriBuilder contextUri = new HttpServiceUriBuilder().SetServiceName(this.context.ServiceName);

            ViewBag.RedirectURL = "";

            if (HTTPHelper.IsSessionExpired(HttpContext, this))
            {
                return(Ok(contextUri.GetServiceNameSiteHomePath()));
            }
            else
            {
                this.ViewData["EmbedURL"] = reportUrl;
                return(this.View());
            }
        }
        public async Task <IActionResult> GetQueueLengthAsync()
        {
            // Manage session and Context
            HttpServiceUriBuilder contextUri = new HttpServiceUriBuilder().SetServiceName(this.context.ServiceName);
            string reportsSecretKey          = HTTPHelper.GetQueryParameterValueFor(HttpContext, Names.REPORTS_SECRET_KEY_NAME);
            long   count = 0;

            if ((reportsSecretKey.Length == 0) && HTTPHelper.IsSessionExpired(HttpContext, this))
            {
                return(Ok(contextUri.GetServiceNameSiteHomePath()));
            }
            else
            {
                ServiceUriBuilder uriBuilder = new ServiceUriBuilder(Names.InsightDataServiceName);
                Uri serviceUri = uriBuilder.Build();

                // service may be partitioned.
                // this will aggregate the queue lengths from each partition
                ServicePartitionList partitions = await this.fabricClient.QueryManager.GetPartitionListAsync(serviceUri);

                foreach (Partition partition in partitions)
                {
                    Uri getUrl = new HttpServiceUriBuilder()
                                 .SetServiceName(serviceUri)
                                 .SetPartitionKey(((Int64RangePartitionInformation)partition.PartitionInformation).LowKey)
                                 .SetServicePathAndQuery($"/api/devices/queue/length")
                                 .Build();

                    HttpResponseMessage response = await this.httpClient.GetAsync(getUrl, this.appLifetime.ApplicationStopping);

                    if (response.StatusCode != System.Net.HttpStatusCode.OK)
                    {
                        return(this.StatusCode((int)response.StatusCode));
                    }

                    string result = await response.Content.ReadAsStringAsync();

                    count += Int64.Parse(result);
                }
            }

            return(this.Ok(count));
        }
        public async Task <IActionResult> GetDevicesAsync(string deviceId = null)
        {
            // Manage session and Context
            HttpServiceUriBuilder contextUri       = new HttpServiceUriBuilder().SetServiceName(this.context.ServiceName);
            string reportsSecretKey                = HTTPHelper.GetQueryParameterValueFor(HttpContext, Names.REPORTS_SECRET_KEY_NAME);
            List <DeviceMessage> deviceMessageList = new List <DeviceMessage>();

            if ((reportsSecretKey.Length == 0) && HTTPHelper.IsSessionExpired(HttpContext, this))
            {
                return(Ok(contextUri.GetServiceNameSiteHomePath()));
            }
            else if (reportsSecretKey.Length > 0)
            {
                // simply return some empty answer - no indication of error for security reasons
                if (!reportsSecretKey.Equals(Names.REPORTS_SECRET_KEY_VALUE))
                {
                    return(this.Ok(deviceMessageList));
                }
            }

            deviceMessageList = await GetDevicesDataAsync(deviceId, this.httpClient, this.fabricClient, this.appLifetime);

            return(this.Ok(deviceMessageList));
        }
Example #6
0
        public async Task <IActionResult> EmbedReport(string reportName, string reportParm = null, string reportParmStart = null, string reportParmEnd = null, int numberOfObservations = (-1), int minMagnitudeAllowed = 1)
        {
            // Manage session and Context
            HttpServiceUriBuilder contextUri = new HttpServiceUriBuilder().SetServiceName(this.context.ServiceName);

            ViewBag.RedirectURL = "";

            if (HTTPHelper.IsSessionExpired(HttpContext, this))
            {
                return(Ok(contextUri.GetServiceNameSiteHomePath()));
            }
            else
            {
                this.ViewData["TargetSite"]  = contextUri.GetServiceNameSite();
                this.ViewData["PageTitle"]   = "Report";
                this.ViewData["HeaderTitle"] = "Last Posted Events";

                string reportUniqueId = FnvHash.GetUniqueId();

                // Now it is time to refresh the data set
                List <DeviceViewModelList> deviceViewModelList = null;
                int  resampleSetsLimit = 0;
                var  refreshDataresult = false;
                bool publishReportData = true;

                if (reportName.Equals("PSG-VibrationDeviceReport-02"))
                {
                    refreshDataresult = true;
                    publishReportData = false;
                }
                else if (reportName.Equals("PSG-VibrationDeviceReport-01") && reportParm != null)
                {
                    deviceViewModelList = await DevicesController.GetDevicesDataAsync(reportParm, httpClient, fabricClient, appLifetime);
                }
                else
                {
                    resampleSetsLimit = 1;

                    deviceViewModelList = new List <DeviceViewModelList>();
                    ServiceUriBuilder uriBuilder = new ServiceUriBuilder(Names.InsightDataServiceName);
                    Uri serviceUri = uriBuilder.Build();

                    // service may be partitioned.
                    // this will aggregate device IDs from all partitions
                    ServicePartitionList partitions = await fabricClient.QueryManager.GetPartitionListAsync(serviceUri);

                    foreach (Partition partition in partitions)
                    {
                        string pathAndQuery      = null;
                        int    index             = 0;
                        float  indexInterval     = 1F;
                        bool   keepLooping       = true;
                        int    observationsCount = 0;
                        int    batchIndex        = 0;
                        int    batchSize         = 10000;


                        while (keepLooping)
                        {
                            if (reportParmEnd == null)
                            {
                                pathAndQuery = $"/api/devices/history/byKey/{reportParmStart}";
                                keepLooping  = false;
                            }
                            else if (numberOfObservations != (-1))
                            {
                                pathAndQuery = $"/api/devices/history/byKeyRange/{reportParmStart}/{reportParmEnd}/{batchIndex}/{batchSize}";

                                if (index == 0)
                                {
                                    string getCountPathAndQuery = $"/api/devices/history/count/interval/{reportParmStart}/{reportParmEnd}";
                                    Uri    getCountUrl          = new HttpServiceUriBuilder()
                                                                  .SetServiceName(serviceUri)
                                                                  .SetPartitionKey(((Int64RangePartitionInformation)partition.PartitionInformation).LowKey)
                                                                  .SetServicePathAndQuery(getCountPathAndQuery)
                                                                  .Build();

                                    HttpResponseMessage localResponse = await httpClient.GetAsync(getCountUrl, appLifetime.ApplicationStopping);

                                    if (localResponse.StatusCode == System.Net.HttpStatusCode.OK)
                                    {
                                        string localResult = await localResponse.Content.ReadAsStringAsync();

                                        long count = Int64.Parse(localResult);

                                        indexInterval = count / numberOfObservations;

                                        if (indexInterval < 1)
                                        {
                                            indexInterval = 1;
                                        }
                                    }
                                }
                            }
                            else if (reportParmEnd != null)
                            {
                                pathAndQuery = $"/api/devices/history/byKeyRange/{reportParmStart}/{reportParmEnd}";
                                keepLooping  = false;
                            }

                            Uri getUrl = new HttpServiceUriBuilder()
                                         .SetServiceName(serviceUri)
                                         .SetPartitionKey(((Int64RangePartitionInformation)partition.PartitionInformation).LowKey)
                                         .SetServicePathAndQuery(pathAndQuery)
                                         .Build();

                            HttpResponseMessage response = await httpClient.GetAsync(getUrl, appLifetime.ApplicationStopping);

                            if (response.StatusCode == System.Net.HttpStatusCode.OK)
                            {
                                JsonSerializer serializer = new JsonSerializer();
                                using (StreamReader streamReader = new StreamReader(await response.Content.ReadAsStreamAsync()))
                                {
                                    using (JsonTextReader jsonReader = new JsonTextReader(streamReader))
                                    {
                                        List <DeviceViewModelList> localResult = serializer.Deserialize <List <DeviceViewModelList> >(jsonReader);

                                        if (localResult != null)
                                        {
                                            if (localResult.Count != 0)
                                            {
                                                foreach (DeviceViewModelList device in localResult)
                                                {
                                                    if (index >= (observationsCount * indexInterval))
                                                    {
                                                        deviceViewModelList.Add(device);
                                                        observationsCount++;
                                                    }
                                                    index++;

                                                    if (numberOfObservations != (-1))
                                                    {
                                                        if (observationsCount == numberOfObservations)
                                                        {
                                                            keepLooping = false;
                                                            break;
                                                        }
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                keepLooping = false;
                                            }
                                        }
                                        else
                                        {
                                            keepLooping = false;
                                        }
                                    }
                                }
                            }
                            batchIndex += batchSize;
                        }
                    }
                }

                if (publishReportData)
                {
                    refreshDataresult = await ReportsHandler.PublishReportDataFor(reportUniqueId, DevicesDataStream01URL, deviceViewModelList, context, httpClient, appLifetime.ApplicationStopping, ServiceEventSource.Current, resampleSetsLimit, minMagnitudeAllowed);
                }

                if (reportName.Equals("PSG-VibrationDeviceReport-02"))
                {
                    reportUniqueId = "";
                }

                EmbedConfig task = await ReportsHandler.GetEmbedReportConfigData(ClientId, GroupId, Username, Password, AuthorityUrl, ResourceUrl, ApiUrl, reportUniqueId, reportName, this.context, ServiceEventSource.Current);

                this.ViewData["EmbedToken"]     = task.EmbedToken.Token;
                this.ViewData["EmbedURL"]       = task.EmbedUrl;
                this.ViewData["EmbedId"]        = task.Id;
                this.ViewData["ReportUniqueId"] = reportUniqueId;

                return(this.View());
            }
        }
Example #7
0
        public ActionResult Login(UserProfile objUser)
        {
            // Manage session and Context
            HttpServiceUriBuilder contextUri = new HttpServiceUriBuilder().SetServiceName(this.context.ServiceName);

            if (ModelState.IsValid)
            {
                ViewBag.Message     = "";
                ViewBag.RedirectURL = "";
                bool newUserRegistration = false;
                bool userAllowedToLogin  = false;

                if ((objUser.Password != null && objUser.Password.Length > 0))
                {
                    // First let deal to see if this a user registration
                    if (objUser.FirstName != null)
                    {
                        newUserRegistration = true;
                        Task <bool> result = RESTHandler.ExecuteFabricPOSTForEntity(typeof(UserProfile),
                                                                                    Names.InsightDataServiceName,
                                                                                    "api/entities/user/withIdentity/" + objUser.UserName,
                                                                                    "user",
                                                                                    objUser,
                                                                                    this.context,
                                                                                    this.httpClient,
                                                                                    this.appLifetime.ApplicationStopping,
                                                                                    ServiceEventSource.Current);
                        if (result.Result)
                        {
                            userAllowedToLogin = true;
                        }
                        else
                        {
                            ViewBag.RedirectURL = contextUri.GetServiceNameSiteHomePath();
                            ViewBag.Message     = "Error during new user registration - User already exist in the database";
                        }
                    }

                    if (!userAllowedToLogin && !newUserRegistration)
                    {
                        Task <object> userObject = RESTHandler.ExecuteFabricGETForEntity(typeof(UserProfile),
                                                                                         Names.InsightDataServiceName,
                                                                                         "api/entities/user/byIdentity/" + objUser.UserName,
                                                                                         "user",
                                                                                         this.context,
                                                                                         this.httpClient,
                                                                                         this.appLifetime.ApplicationStopping,
                                                                                         ServiceEventSource.Current);
                        if (userObject != null)
                        {
                            UserProfile userProfile = (UserProfile)userObject.Result;

                            if (objUser.Password.Equals(userProfile.Password))
                            {
                                userAllowedToLogin = true;
                            }
                            else
                            {
                                ViewBag.RedirectURL = contextUri.GetServiceNameSiteHomePath();
                                ViewBag.Message     = "Invalid Username and/or Password";
                            }
                        }
                        else
                        {
                            ViewBag.RedirectURL = contextUri.GetServiceNameSiteHomePath();
                            ViewBag.Message     = "Error checking user credentials";
                        }
                    }

                    if (userAllowedToLogin)
                    {
                        try
                        {
                            string redirectTo = HTTPHelper.StartSession(HttpContext, this, objUser, "User", "/api/devices", contextUri.GetServiceNameSiteHomePath());

                            //TODO : make the redirection configurable as part of insight application
                            return(Redirect(redirectTo));
                        }
                        catch (System.Exception ex)
                        {
                            ViewBag.RedirectURL = contextUri.GetServiceNameSiteHomePath();
                            ViewBag.Message     = "Internal Error During User Login- Report to the System Administrator";
                            Console.WriteLine("On Login Session exception msg=[" + ex.Message + "]");
                        }
                    }
                }
                else
                {
                    ViewBag.RedirectURL = contextUri.GetServiceNameSiteHomePath();
                    ViewBag.Message     = "Either username and/or password not provided";
                }
            }

            if (!HTTPHelper.IsSessionExpired(HttpContext, this))
            {
                HTTPHelper.EndSession(HttpContext, this);
            }

            return(View("Index", objUser));
        }