Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Clients.Hive.WebJobManager/Hubs/QueryHub.cs @ 13860

Last change on this file since 13860 was 13860, checked in by jlodewyc, 8 years ago

#2582 RC2 migration fixed. OKB query implemented. Preparing for OKB manager

File size: 5.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using HeuristicLab.Clients.Hive.WebJobManager.Services;
23using HeuristicLab.Clients.OKB.Query;
24using Microsoft.AspNetCore.SignalR;
25using Newtonsoft.Json;
26using Newtonsoft.Json.Linq;
27using System;
28using System.Collections.Generic;
29using System.Linq;
30using System.Reflection;
31
32namespace HeuristicLab.Clients.Hive.WebJobManager.Hubs
33{
34    public class QueryHub : Hub
35    {
36        private WebLoginService weblog;
37        private Guid userId;
38        private QueryWebClient queryclient;
39        /// <summary>
40        /// Loads up the services for the client (Instance is only created upon a call from a client)
41        /// </summary>
42        private void loader()
43        {
44            weblog = WebLoginService.Instance;
45            string uid = Context.QueryString["userid"];
46            if (uid == null || uid == "" || Guid.Parse(uid) == Guid.Empty)
47            {
48                userId = Guid.Empty;
49            }
50            else
51            {
52                userId = Guid.Parse(uid);
53                queryclient = weblog.getQueryClient(userId);
54            }
55        }
56        /// <summary>
57        /// Initial client request for all user, group and resource information
58        /// </summary>
59        public void requestInfo()
60        {
61            loader();
62            queryclient.Refresh();
63
64            // var values = JsonConvert.SerializeObject(queryclient.ValueNames);
65            var filters = JArray.FromObject(queryclient.Filters);
66            for (int i = 0; i < queryclient.Filters.Count(); i++)
67            {
68                if (queryclient.Filters.ToList()[i].Label != "AND" && queryclient.Filters.ToList()[i].Label != "OR")
69                    ((JObject)filters[i]).Add("CompareType", queryclient.Filters.ToList()[i].GetType().GetProperty("Comparison").PropertyType.ToString());
70            }
71
72            Clients.Caller.processData(filters.ToString());
73        }
74
75        public void runQuery(string content)
76        {
77            loader();
78            JArray data = JArray.Parse(content);
79            CombinedFilter mainFilt = (CombinedFilter)queryclient.Filters.ToList().Find(x => x.Label == "AND").Clone();
80            queryclient.Refresh();
81            mainFilt.Filters = new List<Filter>();
82            for (var i = 0; i < data.Count(); i++)
83            {
84                mainFilt.Filters.Add(jsonParserFilt((JObject)data[i]));
85            }
86            var idcol = queryclient.GetRunIds(mainFilt).ToList();
87
88            foreach (var id in idcol)
89            {
90                Console.WriteLine(id);
91            }
92
93            var json = JsonConvert.SerializeObject(idcol);
94            Clients.Caller.processResults(json);
95        }
96
97        public void fetchResult(string resultId)
98        {
99            loader();
100            long id = long.Parse(resultId);
101            queryclient.Refresh();
102            var dat = queryclient.GetRuns(new List<long> { id }, true).First();
103            var json = JsonConvert.SerializeObject(dat);
104            Clients.Caller.resultLoad(json);
105        }
106        private Filter jsonParserFilt(JObject current)
107        {
108            if (current["name"] == null)
109            {
110                Filter filter = queryclient.Filters.ToList().Find(x => x.Label == current["Label"].ToString()).Clone();
111                Type filterType = filter.GetType();
112                PropertyInfo compareProp = filterType.GetProperty("Comparison");
113                PropertyInfo valueProp = filterType.GetProperty("Value");
114                if ((current["CompareType"].ToString()  != "HeuristicLab.Clients.OKB.Query.EqualityComparison"))
115                {
116                    if (compareProp != null && valueProp != null)
117                    {
118                        Assembly asm = filterType.Assembly;
119                        Type comtype = asm.GetType(current["CompareType"].ToString());
120                        Object comob = Enum.ToObject(comtype, int.Parse(current["Comparison"].ToString()));
121                        Object valob = Convert.ChangeType(current["Value"].ToString(), valueProp.PropertyType);
122                        compareProp.SetValue(filter, comob);
123                        valueProp.SetValue(filter, valob);
124                    }
125                }
126                return filter;
127            }
128            else
129            {
130                return jsonParserComb(current);
131            }
132        }
133        private Filter jsonParserComb(JObject current)
134        {
135            CombinedFilter filt;
136            if (current["name"].ToString() == "AND")
137                filt = (CombinedFilter)queryclient.Filters.ToList().Find(x => x.Label == "AND").Clone();
138            else
139                filt = (CombinedFilter)queryclient.Filters.ToList().Find(x => x.Label == "OR").Clone();
140            for (var i = 0; i < current["columns"][0].Count(); i++)
141            {
142                filt.Filters.Add(jsonParserFilt((JObject)current["columns"][0][i]));
143            }
144            return filt;
145
146        }
147    }
148}
Note: See TracBrowser for help on using the repository browser.