diff --git a/WebContent/html/javascript/lib/fuzzy.js b/WebContent/html/javascript/lib/fuzzy.js index d2131cb..46fa244 100644 --- a/WebContent/html/javascript/lib/fuzzy.js +++ b/WebContent/html/javascript/lib/fuzzy.js @@ -1,143 +1,143 @@ -/* - * Fuzzy - * https://github.com/myork/fuzzy - * - * Copyright (c) 2012 Matt York - * Licensed under the MIT license. - */ - -(function() { - -var root = this; - -var fuzzy = {}; - -// Use in node or in browser -if (typeof exports !== 'undefined') { - module.exports = fuzzy; -} else { - root.fuzzy = fuzzy; -} - -// Return all elements of `array` that have a fuzzy -// match against `pattern`. -fuzzy.simpleFilter = function(pattern, array) { - return array.filter(function(str) { - return fuzzy.test(pattern, str); - }); -}; - -// Does `pattern` fuzzy match `str`? -fuzzy.test = function(pattern, str) { - return fuzzy.match(pattern, str) !== null; -}; - -// If `pattern` matches `str`, wrap each matching character -// in `opts.pre` and `opts.post`. If no match, return null -fuzzy.match = function(pattern, str, opts) { - opts = opts || {}; - var patternIdx = 0 - , result = [] - , len = str.length - , totalScore = 0 - , currScore = 0 - // prefix - , pre = opts.pre || '' - // suffix - , post = opts.post || '' - // String to compare against. This might be a lowercase version of the - // raw string - , compareString = opts.caseSensitive && str || str.toLowerCase() - , ch; - - pattern = opts.caseSensitive && pattern || pattern.toLowerCase(); - - // For each character in the string, either add it to the result - // or wrap in template if it's the next string in the pattern - for(var idx = 0; idx < len; idx++) { - ch = str[idx]; - if(compareString[idx] === pattern[patternIdx]) { - ch = pre + ch + post; - patternIdx += 1; - - // consecutive characters should increase the score more than linearly - currScore += 1 + currScore; - } else { - currScore = 0; - } - totalScore += currScore; - result[result.length] = ch; - } - - // return rendered string if we have a match for every char - if(patternIdx === pattern.length) { - // if the string is an exact match with pattern, totalScore should be maxed - totalScore = (compareString === pattern) ? Infinity : totalScore; - return {rendered: result.join(''), score: totalScore}; - } - - return null; -}; - -// The normal entry point. Filters `arr` for matches against `pattern`. -// It returns an array with matching values of the type: -// -// [{ -// string: 'lah' // The rendered string -// , index: 2 // The index of the element in `arr` -// , original: 'blah' // The original element in `arr` -// }] -// -// `opts` is an optional argument bag. Details: -// -// opts = { -// // string to put before a matching character -// pre: '' -// -// // string to put after matching character -// , post: '' -// -// // Optional function. Input is an entry in the given arr`, -// // output should be the string to test `pattern` against. -// // In this example, if `arr = [{crying: 'koala'}]` we would return -// // 'koala'. -// , extract: function(arg) { return arg.crying; } -// } -fuzzy.filter = function(pattern, arr, opts) { - if(!arr || arr.length === 0) { - return []; - } - if (typeof pattern !== 'string') { - return arr; - } - opts = opts || {}; - return arr - .reduce(function(prev, element, idx, arr) { - var str = element; - if(opts.extract) { - str = opts.extract(element); - } - var rendered = fuzzy.match(pattern, str, opts); - if(rendered != null) { - prev[prev.length] = { - string: rendered.rendered - , score: rendered.score - , index: idx - , original: element - }; - } - return prev; - }, []) - - // Sort by score. Browsers are inconsistent wrt stable/unstable - // sorting, so force stable by using the index in the case of tie. - // See http://ofb.net/~sethml/is-sort-stable.html - .sort(function(a,b) { - var compare = b.score - a.score; - if(compare) return compare; - return a.index - b.index; - }); -}; - - +/* + * Fuzzy + * https://github.com/myork/fuzzy + * + * Copyright (c) 2012 Matt York + * Licensed under the MIT license. + */ + +(function() { + +var root = this; + +var fuzzy = {}; + +// Use in node or in browser +if (typeof exports !== 'undefined') { + module.exports = fuzzy; +} else { + root.fuzzy = fuzzy; +} + +// Return all elements of `array` that have a fuzzy +// match against `pattern`. +fuzzy.simpleFilter = function(pattern, array) { + return array.filter(function(str) { + return fuzzy.test(pattern, str); + }); +}; + +// Does `pattern` fuzzy match `str`? +fuzzy.test = function(pattern, str) { + return fuzzy.match(pattern, str) !== null; +}; + +// If `pattern` matches `str`, wrap each matching character +// in `opts.pre` and `opts.post`. If no match, return null +fuzzy.match = function(pattern, str, opts) { + opts = opts || {}; + var patternIdx = 0 + , result = [] + , len = str.length + , totalScore = 0 + , currScore = 0 + // prefix + , pre = opts.pre || '' + // suffix + , post = opts.post || '' + // String to compare against. This might be a lowercase version of the + // raw string + , compareString = opts.caseSensitive && str || str.toLowerCase() + , ch; + + pattern = opts.caseSensitive && pattern || pattern.toLowerCase(); + + // For each character in the string, either add it to the result + // or wrap in template if it's the next string in the pattern + for(var idx = 0; idx < len; idx++) { + ch = str[idx]; + if(compareString[idx] === pattern[patternIdx]) { + ch = pre + ch + post; + patternIdx += 1; + + // consecutive characters should increase the score more than linearly + currScore += 1 + currScore; + } else { + currScore = 0; + } + totalScore += currScore; + result[result.length] = ch; + } + + // return rendered string if we have a match for every char + if(patternIdx === pattern.length) { + // if the string is an exact match with pattern, totalScore should be maxed + totalScore = (compareString === pattern) ? Infinity : totalScore; + return {rendered: result.join(''), score: totalScore}; + } + + return null; +}; + +// The normal entry point. Filters `arr` for matches against `pattern`. +// It returns an array with matching values of the type: +// +// [{ +// string: 'lah' // The rendered string +// , index: 2 // The index of the element in `arr` +// , original: 'blah' // The original element in `arr` +// }] +// +// `opts` is an optional argument bag. Details: +// +// opts = { +// // string to put before a matching character +// pre: '' +// +// // string to put after matching character +// , post: '' +// +// // Optional function. Input is an entry in the given arr`, +// // output should be the string to test `pattern` against. +// // In this example, if `arr = [{crying: 'koala'}]` we would return +// // 'koala'. +// , extract: function(arg) { return arg.crying; } +// } +fuzzy.filter = function(pattern, arr, opts) { + if(!arr || arr.length === 0) { + return []; + } + if (typeof pattern !== 'string') { + return arr; + } + opts = opts || {}; + return arr + .reduce(function(prev, element, idx, arr) { + var str = element; + if(opts.extract) { + str = opts.extract(element); + } + var rendered = fuzzy.match(pattern, str, opts); + if(rendered != null) { + prev[prev.length] = { + string: rendered.rendered + , score: rendered.score + , index: idx + , original: element + }; + } + return prev; + }, []) + + // Sort by score. Browsers are inconsistent wrt stable/unstable + // sorting, so force stable by using the index in the case of tie. + // See http://ofb.net/~sethml/is-sort-stable.html + .sort(function(a,b) { + var compare = b.score - a.score; + if(compare) return compare; + return a.index - b.index; + }); +}; + + }()); \ No newline at end of file diff --git a/WebContent/html/javascript/statistics.jsp b/WebContent/html/javascript/statistics.jsp new file mode 100644 index 0000000..ff97eab --- /dev/null +++ b/WebContent/html/javascript/statistics.jsp @@ -0,0 +1,108 @@ +<%@ page import="database.*,entities.Statistics"%> +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> + + + + +Insert title here + + + <% +Statistics[] genStatistics = StatisticsQueries.getStatistics(); +String statString = Statistics.arrayToString(genStatistics); +%> + + + + \ No newline at end of file diff --git a/WebContent/html/webpages/index.jsp b/WebContent/html/webpages/index.jsp index 8b1e8c1..8ccfa57 100644 --- a/WebContent/html/webpages/index.jsp +++ b/WebContent/html/webpages/index.jsp @@ -67,10 +67,10 @@

Just curious to see what devices we have to offer? You've come to the right place.

- + diff --git a/WebContent/html/webpages/map.jsp b/WebContent/html/webpages/statisticsPage.jsp similarity index 76% rename from WebContent/html/webpages/map.jsp rename to WebContent/html/webpages/statisticsPage.jsp index ac0a458..8444c67 100644 --- a/WebContent/html/webpages/map.jsp +++ b/WebContent/html/webpages/statisticsPage.jsp @@ -30,6 +30,28 @@ text-align: center; padding-bottom: 10px; } + + div.infoContainer{ + border: solid; + border-width: thin; + padding: 15px; + background-color: #E9EAEB; + display: inline-block; + margin-bottom: 30px; + margin-top: 25px; + text-align: left; + } + + div.imgContainer{ + display: inline-block; + text-align: center; + } + + div.statDescription{ + display: inline-block; + width: 200px; + text-align: center; + } @@ -40,7 +62,7 @@ <%@ include file="navbar.jsp"%> -

Our Wonderful Synchrony Financial Map!

+

Distribution of Devices Nationwide

- +

Device Availability Statistics

+
+
+
+
+
+
+
+ <%@ include file="../javascript/statistics.jsp" %> \ No newline at end of file diff --git a/src/database/StatisticsQueries.java b/src/database/StatisticsQueries.java new file mode 100644 index 0000000..289252d --- /dev/null +++ b/src/database/StatisticsQueries.java @@ -0,0 +1,36 @@ +package database; +import java.sql.*; +import entities.Statistics; + +public class StatisticsQueries { + + private static String database = "jdbc:mysql://us-cdbr-iron-east-04.cleardb.net/ad_15a989204c2ff8a?user=b372dfe7409692&password=74f6e317"; + private static String user = "b372dfe7409692"; + private static String password = "74f6e317"; + + public static Statistics[] getStatistics() throws ClassNotFoundException, SQLException { + //database connect + System.getenv("VCAP_SERVICES"); + Class.forName("com.mysql.jdbc.Driver"); + Connection connection = DriverManager.getConnection(database, user, password); + Statement stmt = connection.createStatement(); + ResultSet resultSet = stmt.executeQuery("SELECT Status, count(*) FROM devices GROUP BY Status"); + int counter = 0; + + resultSet.last(); + int rows = resultSet.getRow(); + resultSet.beforeFirst(); + + Statistics[] statusDevice = new Statistics[rows]; + //iterate result set + while(resultSet.next()){ + statusDevice[counter] = new Statistics(resultSet.getString("Status"), resultSet.getInt("count(*)")); + counter++; + } + stmt.close(); + connection.close(); + return statusDevice; + + } + +} diff --git a/src/entities/Statistics.java b/src/entities/Statistics.java new file mode 100644 index 0000000..9be1ef5 --- /dev/null +++ b/src/entities/Statistics.java @@ -0,0 +1,42 @@ +package entities; + +public class Statistics { + private String _status; + private int _count; + public Statistics(String status,int count) { + _status = status; + _count = count; + } + + /** + * Formatting the statistics to fit a JSON object. + * @author - Connor + */ + public String toString(){ + StringBuilder sb = new StringBuilder(); + String comma = ", "; + sb.append("{\"status\": \"").append(_status).append("\"").append(comma); + sb.append("\"count\": ").append(_count); + sb.append("}"); + return sb.toString(); + } + + /** + * This is a static function which will turn a Listed Statistics array into its proper string. (modification) + * @author - Connor + * @param array + * @return + */ + public static String arrayToString(Statistics[] array){ + StringBuilder sb = new StringBuilder(); + sb.append("["); + for(int i = 0; i < array.length; i++){ + sb.append(array[i].toString()); + if(i+1 != array.length){ + sb.append(","); + } + } + sb.append("]"); + return sb.toString(); + } +}