Skip to content

Commit

Permalink
[#12048] Add UsageStatistics entity and db (#12076)
Browse files Browse the repository at this point in the history
  • Loading branch information
daongochieu2810 authored Feb 11, 2023
1 parent 39d13af commit 74edda6
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/main/java/teammates/storage/sqlapi/UsageStatisticsDb.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package teammates.storage.sqlapi;

import java.time.Instant;
import java.util.List;

import org.hibernate.Session;

import teammates.common.util.HibernateUtil;
import teammates.storage.sqlentity.UsageStatistics;

import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;

/**
* Handles CRUD operations for usage statistics.
*
* @see UsageStatistics
*/
public final class UsageStatisticsDb extends EntitiesDb<UsageStatistics> {

private static final UsageStatisticsDb instance = new UsageStatisticsDb();

private UsageStatisticsDb() {
// prevent initialization
}

public static UsageStatisticsDb inst() {
return instance;
}

/**
* Gets a list of statistics objects between start time and end time.
*/
public List<UsageStatistics> getUsageStatisticsForTimeRange(Instant startTime, Instant endTime) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery<UsageStatistics> cr = cb.createQuery(UsageStatistics.class);
Root<UsageStatistics> root = cr.from(UsageStatistics.class);

cr.select(root).where(cb.and(
cb.greaterThanOrEqualTo(root.get("startTime"), startTime),
cb.lessThan(root.get("startTime"), endTime)));

return session.createQuery(cr).getResultList();
}
}
140 changes: 140 additions & 0 deletions src/main/java/teammates/storage/sqlentity/UsageStatistics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package teammates.storage.sqlentity;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

/**
* Represents a system usage statistics for a specified period of time.
*
* <p>Note that "system usage" here is defined as user-facing usages, such as number of entities created
* and number of actions, as opposed to system resources such as hardware and network.
*/
@Entity
@Table(name = "UsageStatistics")
public class UsageStatistics extends BaseEntity {
@Id
@GeneratedValue
private int id;

@Column(nullable = false)
private Instant startTime;

@Column(nullable = false)
private int timePeriod;
@Column(nullable = false)
private int numResponses;
@Column(nullable = false)
private int numCourses;
@Column(nullable = false)
private int numStudents;
@Column(nullable = false)
private int numInstructors;
@Column(nullable = false)
private int numAccountRequests;
@Column(nullable = false)
private int numEmails;
@Column(nullable = false)
private int numSubmissions;

@CreationTimestamp
@Column(updatable = false)
private Instant createdAt;

@UpdateTimestamp
@Column
private Instant updatedAt;

protected UsageStatistics() {
// required by Hibernate
}

private UsageStatistics(
Instant startTime, int timePeriod, int numResponses, int numCourses,
int numStudents, int numInstructors, int numAccountRequests, int numEmails, int numSubmissions) {
this.startTime = startTime;
this.timePeriod = timePeriod;
this.numResponses = numResponses;
this.numCourses = numCourses;
this.numStudents = numStudents;
this.numInstructors = numInstructors;
this.numAccountRequests = numAccountRequests;
this.numEmails = numEmails;
this.numSubmissions = numSubmissions;
}

public int getId() {
return id;
}

public Instant getStartTime() {
return startTime;
}

public int getTimePeriod() {
return timePeriod;
}

public int getNumResponses() {
return numResponses;
}

public int getNumCourses() {
return numCourses;
}

public int getNumStudents() {
return numStudents;
}

public int getNumInstructors() {
return numInstructors;
}

public int getNumAccountRequests() {
return numAccountRequests;
}

public int getNumEmails() {
return numEmails;
}

public int getNumSubmissions() {
return numSubmissions;
}

public Instant getCreatedAt() {
return createdAt;
}

public void setCreatedAt(Instant createdAt) {
this.createdAt = createdAt;
}

public Instant getUpdatedAt() {
return updatedAt;
}

public void setUpdatedAt(Instant updatedAt) {
this.updatedAt = updatedAt;
}

@Override
public void sanitizeForSaving() {
// required by BaseEntity
}

@Override
public List<String> getInvalidityInfo() {
return new ArrayList<>();
}
}

0 comments on commit 74edda6

Please sign in to comment.