Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/appengine/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
runtime: java11
51 changes: 51 additions & 0 deletions src/main/java/com/google/sps/ServerMain.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
72 changes: 72 additions & 0 deletions src/main/java/com/google/sps/servlets/List.java
Original file line number Diff line number Diff line change
@@ -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<Entity> 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<Entity> results = datastore.run(query);

// Calculate the sum of time spent for each catagory
Map<String, Double> recordSum = new HashMap<String, Double>();
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));
}
}
Empty file.
140 changes: 140 additions & 0 deletions src/main/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.google.sps</groupId>
<artifactId>StudentHelper</artifactId>
<version>1</version>

<properties>
<!-- Java 11 -->
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jetty.version>9.4.41.v20210516</jetty.version>

<!-- Project-specific properties -->
<exec.mainClass>com.google.sps.ServerMain</exec.mainClass>
<googleCloudProjectId>summer22-sps-11</googleCloudProjectId>
</properties>

<dependencies>
<!-- Java Servlets API -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>

<!-- Jetty -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-annotations</artifactId>
<version>${jetty.version}</version>
</dependency>

<!-- Cloud Storage -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>1.113.0</version>
</dependency>

<!-- Cloud Vision -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-vision</artifactId>
<version>1.100.0</version>
</dependency>

<!-- Datastore -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-datastore</artifactId>
<version>1.104.0</version>
</dependency>

<!-- jsoup -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.14.2</version>
</dependency>

</dependencies>

<build>
<plugins>
<!-- Copy static resources like html files into the output jar file. -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>copy-web-resources</id>
<phase>compile</phase>
<goals><goal>copy-resources</goal></goals>
<configuration>
<outputDirectory>
${project.build.directory}/classes/META-INF/resources
</outputDirectory>
<resources>
<resource><directory>./src/main/webapp</directory></resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

<!-- Package everything into a single executable jar file. -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals><goal>shade</goal></goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>${exec.mainClass}</mainClass>
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<!-- Some libraries are signed, so exclude signature files from the output jar. -->
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>

<!-- App Engine plugin for deploying to the live site. -->
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>2.2.0</version>
<configuration>
<projectId>${googleCloudProjectId}</projectId>
<version>1</version>
</configuration>
</plugin>
</plugins>
</build>
</project>
11 changes: 11 additions & 0 deletions src/main/webapp/piechart.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript" src="progress.js"></script>
</head>
<body>
<!--Div that will hold the pie chart-->
<div id="chart_div" onload="drawPieChart()"></div>
</body>
</html>
41 changes: 41 additions & 0 deletions src/main/webapp/progress.js
Original file line number Diff line number Diff line change
@@ -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);
}