diff --git a/src/main/appengine/app.yaml b/src/main/appengine/app.yaml new file mode 100644 index 0000000..3c6c656 --- /dev/null +++ b/src/main/appengine/app.yaml @@ -0,0 +1 @@ +runtime: java11 diff --git a/src/main/java/com/google/sps/ServerMain.java b/src/main/java/com/google/sps/ServerMain.java new file mode 100644 index 0000000..fb722c4 --- /dev/null +++ b/src/main/java/com/google/sps/ServerMain.java @@ -0,0 +1,51 @@ +package com.google.sps; + +import java.net.URL; +import org.eclipse.jetty.annotations.AnnotationConfiguration; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.servlet.DefaultServlet; +import org.eclipse.jetty.servlet.ServletHolder; +import org.eclipse.jetty.webapp.Configuration; +import org.eclipse.jetty.webapp.WebAppContext; +import org.eclipse.jetty.webapp.WebInfConfiguration; + +/** + * Starts up the server, including a DefaultServlet that handles static files, and any servlet + * classes annotated with the @WebServlet annotation. + */ +public class ServerMain { + + public static void main(String[] args) throws Exception { + + // Create a server that listens on port 8080. + Server server = new Server(8080); + WebAppContext webAppContext = new WebAppContext(); + server.setHandler(webAppContext); + + // Load static content from inside the jar file. + URL webAppDir = ServerMain.class.getClassLoader().getResource("META-INF/resources"); + webAppContext.setResourceBase(webAppDir.toURI().toString()); + + // Enable annotations so the server sees classes annotated with @WebServlet. + webAppContext.setConfigurations( + new Configuration[] { + new AnnotationConfiguration(), new WebInfConfiguration(), + }); + + // Look for annotations in the classes directory (dev server) and in the jar file (live server) + webAppContext.setAttribute( + "org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", + ".*/target/classes/|.*\\.jar"); + + // Handle static resources, e.g. html files. + ServletHolder defaultServletHolder = webAppContext.addServlet(DefaultServlet.class, "/"); + defaultServletHolder.setInitParameter("cacheControl", "no-store, max-age=0"); + + // Start the server! 🚀 + server.start(); + System.out.println("Server started!"); + + // Keep the main thread alive while the server is running. + server.join(); + } +} diff --git a/src/main/java/com/google/sps/servlets/List.java b/src/main/java/com/google/sps/servlets/List.java new file mode 100644 index 0000000..9f912d2 --- /dev/null +++ b/src/main/java/com/google/sps/servlets/List.java @@ -0,0 +1,72 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import com.google.cloud.datastore.Datastore; +import com.google.cloud.datastore.DatastoreOptions; +import com.google.cloud.datastore.Entity; +import com.google.cloud.datastore.Query; +import com.google.cloud.datastore.QueryResults; +import com.google.cloud.datastore.StructuredQuery.OrderBy; +import com.google.gson.Gson; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.HashMap; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.time.LocalDate; +import java.time.Month; + +/** Servlet responsible for listing time breakdown. */ +@WebServlet("/time-breakdown") +public class ListRecordsServlet extends HttpServlet { + + @Override + public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { + Datastore datastore = DatastoreOptions.getDefaultInstance().getService(); + + String username = request.getParameter("username"); + // get today's date, month, year as int + LocalDate currentdate = LocalDate.now(); + int currentDay = currentdate.getDayOfMonth(); + int currentMonth = currentdate.getMonth().getValue(); + int currentYear = currentdate.getYear(); + + Query query = Query.newEntityQueryBuilder().setKind("Record") + .setFilter(CompositeFilter.and( + PropertyFilter.eq("username", username), PropertyFilter.eq("year", currentYear), + PropertyFilter.eq("month", currentMonth), PropertyFilter.eq("day", currentDay))) + .build(); + QueryResults results = datastore.run(query); + + // Calculate the sum of time spent for each catagory + Map recordSum = new HashMap(); + while (results.hasNext()) { + Entity entity = results.next(); + String catagory = entity.getString("catagory"); + long time = entity.getDouble("time"); + if (recordSum.containsKey(catagory)) { + recordSum.replace(catagory, recordSum.get(catagory) + time); + } else { + recordSum.put(catagory, time); + } + } + // pass the result as JSON back to client + Gson gson = new Gson(); + response.setContentType("application/json;"); + response.getWriter().println(gson.toJson(recordSum)); + } +} diff --git a/src/main/java/com/servlets/test.java b/src/main/java/com/servlets/test.java deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/pom.xml b/src/main/pom.xml new file mode 100644 index 0000000..e3d84ab --- /dev/null +++ b/src/main/pom.xml @@ -0,0 +1,140 @@ + + 4.0.0 + + com.google.sps + StudentHelper + 1 + + + + 11 + 11 + UTF-8 + 9.4.41.v20210516 + + + com.google.sps.ServerMain + summer22-sps-11 + + + + + + javax.servlet + javax.servlet-api + 4.0.1 + + + + + org.eclipse.jetty + jetty-server + ${jetty.version} + + + org.eclipse.jetty + jetty-annotations + ${jetty.version} + + + + + com.google.cloud + google-cloud-storage + 1.113.0 + + + + + com.google.cloud + google-cloud-vision + 1.100.0 + + + + + com.google.cloud + google-cloud-datastore + 1.104.0 + + + + + org.jsoup + jsoup + 1.14.2 + + + + + + + + + org.apache.maven.plugins + maven-resources-plugin + 2.7 + + + copy-web-resources + compile + copy-resources + + + ${project.build.directory}/classes/META-INF/resources + + + ./src/main/webapp + + + + + + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + + package + shade + + false + + + ${exec.mainClass} + + + + + *:* + + + META-INF/*.SF + META-INF/*.DSA + META-INF/*.RSA + + + + + + + + + + + com.google.cloud.tools + appengine-maven-plugin + 2.2.0 + + ${googleCloudProjectId} + 1 + + + + + diff --git a/src/main/webapp/piechart.html b/src/main/webapp/piechart.html new file mode 100644 index 0000000..5cbc8e1 --- /dev/null +++ b/src/main/webapp/piechart.html @@ -0,0 +1,11 @@ + + + + + + + + +
+ + \ No newline at end of file diff --git a/src/main/webapp/progress.js b/src/main/webapp/progress.js new file mode 100644 index 0000000..6171121 --- /dev/null +++ b/src/main/webapp/progress.js @@ -0,0 +1,41 @@ +// Load the Visualization API and the corechart package. +google.charts.load('current', {'packages':['corechart']}); + +// Set a callback to run when the Google Visualization API is loaded. +google.charts.setOnLoadCallback(drawPieChart); + +// Callback that creates and populates a data table, +// instantiates the pie chart, passes in the data and +// draws it. +function drawPieChart() { + + // Create the data table. + var data = new google.visualization.DataTable(); + data.addColumn('string', 'Catagories'); + data.addColumn('number', 'Time'); + + // fetch data(JSON) from server, iterate through each catagory + // and add it to the DataTable for the chart + let username = sessionStorage.getItem("username"); + $.ajax({ + url: '/time-breakdown', + data: { username: username}, + type: 'GET', + dataType: "json", + success: function (records) { + for (let key in records) { + data.addRows(key, records[key]); + } + } + });​ + + // Set chart options + var options = {'title':'How you spent your time today', + 'is3D':true, + 'width':1000, + 'height':750}; + + // Instantiate and draw our chart, passing in some options. + var chart = new google.visualization.PieChart(document.getElementById('chart_div')); + chart.draw(data, options); +} \ No newline at end of file