Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new session and adds it to the dictionary. Does not begin streaming data.
        /// </summary>
        /// <param name="args"></param>
        /// <returns>A string container with the SessionKey or a failure message</returns>
        public static Container <string> InitializeSession(SessionInitializer args)
        {
            var validation = args.Validate();

            if (validation.Success)
            {
                // Construct new session from args
                var session = new Session(GetRandomKey().Data[0], args);
                // Create instrument on CHORDS and set session's instrument ID
                var createInstContainer = ChordsBot.CreateInstrument(session.SessionKey);
                if (!createInstContainer.Success)
                {
                    return(new Container <string>("", false, createInstContainer.Message));
                }
                int id = createInstContainer.Data[0];
                session.SetInstrument(id);
                // Map session streams to CHORDS variables
                var confVarsContainer = ChordsBot.ConfigureVariables(session);
                if (!confVarsContainer.Success)
                {
                    return(new Container <string>("", false, confVarsContainer.Message));
                }
                // Add session to dict
                SessionDict.Add(session.SessionKey, session);
                return(new Container <string>(session.SessionKey, true));
            }
            else
            {
                return(new Container <string>("", false, validation.Message));
            }
        }
Exemplo n.º 2
0
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
            // Configure Web API to use only bearer token authentication.

            // Web API calls return JSON by default
            config.Formatters.JsonFormatter.SupportedMediaTypes
            .Add(new MediaTypeHeaderValue("text/html"));

            // Web API routes
            config.MapHttpAttributeRoutes();

            // Create convention-based API routes
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
                );

            ChordsBot.Initialize(Config.ChordsHostUrl);
        }
Exemplo n.º 3
0
        /*Creating Dashboards
         *
         * A Dashboard will have:
         * Rows: A row can contain multiple item to display information (ex: one row can have a table right next to a graph)
         * and a Dashboard Name
         *
         * Each items in a row are called a "panel" which can be multiple things that display information like graphs, tables, etc..
         * To create a panels, we need to initalize them and then send that information to a 'Row' object
         * A dashboard will need to be initalized with its name and a list of rows.
         *
         */
        /// <summary>
        /// Initiates and formats JSON Dashboard object to send data to Grafana
        /// </summary>
        /// <param name="dashName"></param>
        /// <returns>A string containing the HTTP response to the POST</returns>

        public static string CreateDashboard(Session session)

        {
            //URI to contact Grafana's API for interacting with Dashboards
            var uri = "http://ec2-13-57-134-131.us-west-1.compute.amazonaws.com:3000/api/dashboards/db";
            //Creating lists to be use to initialize the Dashboard constructor
            var reqs        = new List <Require>();
            var DashRows    = new List <Row>();
            var panelsList  = new List <Panel>();
            var targetsList = new List <Target>();

            // Specify what the Yaxis shows
            var Yaxes = new List <Yax>();
            var Yaxis = new Yax {
                format = "short", label = null, logBase = 1, show = true
            };
            var Yaxis1 = new Yax {
                format = "short", label = null, logBase = 1, show = true
            };
            var timeRange = new Time {
                from = "now-30d", to = "now"
            };

            Yaxes.Add(Yaxis);
            Yaxes.Add(Yaxis1);

            //Test list of integer, if it works, call GetTarget on this line to get
            List <int> testList = new List <int> {
                340, 341
            };                                               //testList = getTarget(Session.InstrumentID)

            //Initialize target data and call GetTarget here
            List <Tag>    targetTags      = new List <Tag>();
            char          grafanaQueryIDs = 'A';
            List <string> grafanaVarIDs   = ChordsBot.GetTarget(session.InstrumentID);

            for (int index = 0; index < session.StreamIDs.Count; index++)
            {
                //Get a datastream from the session and have Grafana point to it
                var Stream  = DataCenter.GetDataStream(session.NetworkAlias, session.StreamIDs[index]);
                Tag testTag = new Tag {
                    key = "var", @operator = "=", value = grafanaVarIDs[index].ToString()
                };
                targetTags.Add(testTag);
                Target testTarget = new Target
                {
                    alias        = Stream.Data[0].Site.Alias + " , " + Stream.Data[0].Deployment.Name + " , " + Stream.Data[0].DataType.Name + " , " + Stream.Data[0].Property.Name,
                    dsType       = "influxdb",
                    measurement  = "tsdata",
                    policy       = "default",
                    refId        = grafanaQueryIDs.ToString(),
                    resultFormat = "time_series",
                    tags         = targetTags,
                };
                grafanaQueryIDs++;
                targetsList.Add(testTarget);
                targetTags = new List <Tag>();
            }

            //Initializing the Dashboard's "requirement" objects and target objects for Panel
            var panelReqs = new Require {
                type = "panel", id = "graph", name = "graph"
            };
            var dataReqs = new Require {
                type = "datasource", id = "influxdb", name = "InfluxDB", version = "1.0.0"
            };
            //  ChordsBot.GetTarget(sessionKey);
            //Panel initialization

            var panel = new Panel
            {
                title = session.Name,
                //  aliasColors = { },
                description   = session.Description,
                bars          = false,
                datasource    = "NRDC",
                fill          = 1,
                nullPointMode = "null",
                lines         = true,
                // links = { },
                linewidth   = 1,
                pointradius = 5,
                renderer    = "flot",
                //seriesOverrides = { },
                span        = 12,
                targets     = targetsList,
                stack       = false,
                steppedLine = false,
                tooltip     = new Tooltip {
                    shared = true, sort = 0, value_type = "individual"
                },
                type  = "graph",
                xaxis = new Xaxis {
                    mode = "time ", name = null, show = true
                },
                yaxes  = Yaxes,
                legend = new Legend {
                    avg = false, current = false, max = false, min = false, show = true, total = false, values = false
                }
            };

            //formats data and sends JSON request

            reqs.Add(panelReqs);
            reqs.Add(dataReqs);
            panelsList.Add(panel);
            var dashRow = new Row {
                panels = panelsList, title = "Dashboard Row", titleSize = "h6", height = "250px"
            };

            DashRows.Add(dashRow);
            Response testBoard = new Response {
                dashboard = new Dashboard {
                    title = session.Name, rows = DashRows, time = timeRange, version = 3, refresh = "5s"
                }
            };
            var jsonContent   = JsonConvert.SerializeObject(testBoard, Config.DefaultSerializationSettings);
            var stringContent = new StringContent(jsonContent, Encoding.UTF8, "application/json");

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(header, credentials);
            var response = client.PostAsync(uri, stringContent).Result;

            return(response.Content.ReadAsStringAsync().Result);
        }