Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for dashboard history api #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@
package net.opentsdb.horizon.fs.model;


import javax.persistence.Transient;

public class File extends Folder {

@Transient
private long historyId;

private byte[] content;

public byte[] getContent() {
Expand All @@ -29,4 +34,12 @@ public byte[] getContent() {
public void setContent(byte[] content) {
this.content = content;
}

public long getHistoryId() {
return historyId;
}

public void setHistoryId(long historyId) {
this.historyId = historyId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,55 @@

package net.opentsdb.horizon.fs.model;

import javax.persistence.Transient;
import java.sql.Timestamp;

public class FileHistory {

private long id;
private long fileid;
private byte[] contentid;
private Timestamp createdtime;
private long id;
private long fileid;
private byte[] contentid;
private Timestamp createdtime;

@Transient private String createdBy;

public long getId() {
return id;
}
public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}
public void setId(long id) {
this.id = id;
}

public long getFileid() {
return fileid;
}
public Long getFileid() {
return fileid;
}

public void setFileid(long fileid) {
this.fileid = fileid;
}
public void setFileid(Long fileid) {
this.fileid = fileid;
}

public byte[] getContentid() {
return contentid;
}
public byte[] getContentid() {
return contentid;
}

public void setContentid(byte[] contentid) {
this.contentid = contentid;
}
public void setContentid(byte[] contentid) {
this.contentid = contentid;
}

public Timestamp getCreatedtime() {
return createdtime;
}
public Timestamp getCreatedtime() {
return createdtime;
}

public void setCreatedtime(Timestamp createdtime) {
this.createdtime = createdtime;
}
public void setCreatedtime(Timestamp createdtime) {
this.createdtime = createdtime;
}

public String getCreatedBy() {
return createdBy;
}

public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,34 @@ public Folder getFolderByPathHash(FolderType folderType, byte[] pathHash, Connec
public File getFileAndContentById(FolderType folderType, long id, Connection connection)
throws SQLException {
String sql =
"SELECT f.id, f.name, f.type, f.path, f.pathhash, f.parentpathhash, f.contentid, f.createdtime, f.createdby, f.updatedtime, f.updatedby, c.data "
+ "FROM folder f INNER JOIN content c ON f.contentid = c.sha2 "
"SELECT f.id, f.name, f.type, f.path, f.pathhash, f.parentpathhash, f.contentid, f.createdtime, f.createdby, f.updatedtime, f.updatedby, fh.id as historyid, c.data FROM folder f "
+ "INNER JOIN content c ON f.contentid = c.sha2 "
+ "INNER JOIN folder_history fh ON c.sha2 = fh.contentid "
+ "WHERE f.type = ? AND f.id = ?";
return getFileAndContentById(folderType, id, connection, sql);
}

public File getFileAndContentByHistoryId(
FolderType folderType, long id, long historyId, Connection connection) throws SQLException {
String sql =
"SELECT f.id, f.name, f.type, f.path, f.pathhash, f.parentpathhash, f.contentid, f.createdtime, f.createdby, f.updatedtime, f.updatedby, fh.id as historyid, c.data FROM folder f "
+ "INNER JOIN folder_history fh ON f.id = fh.folderid "
+ "INNER JOIN content c ON fh.contentid = c.sha2 "
+ "WHERE f.type = ? AND f.id = ? AND fh.id = ?";
File file = null;
try (PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setByte(1, folderType.value);
statement.setLong(2, id);
statement.setLong(3, historyId);
try (final ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
file = createFile(rs);
}
}
}
return file;
}

private File getFileAndContentById(
FolderType folderType, long id, Connection connection, String sql) throws SQLException {
File file = null;
Expand All @@ -276,25 +298,31 @@ private File getFileAndContentById(
statement.setLong(2, id);
try (final ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
file = new File();
file.setId(rs.getLong("id"));
file.setName(rs.getString("name"));
file.setType(FolderType.values()[rs.getInt("type")]);
file.setPath(rs.getString("path"));
file.setPathHash(rs.getBytes("pathhash"));
file.setParentPathHash(rs.getBytes("parentpathhash"));
file.setContentid(rs.getBytes("contentid"));
file.setCreatedTime(rs.getTimestamp("createdtime"));
file.setCreatedBy(rs.getString("createdby"));
file.setUpdatedTime(rs.getTimestamp("updatedtime"));
file.setUpdatedBy(rs.getString("updatedby"));
file.setContent(rs.getBytes("data"));
file = createFile(rs);
}
}
}
return file;
}

private File createFile(ResultSet rs) throws SQLException {
File file = new File();
file.setId(rs.getLong("id"));
file.setName(rs.getString("name"));
file.setType(FolderType.values()[rs.getInt("type")]);
file.setPath(rs.getString("path"));
file.setPathHash(rs.getBytes("pathhash"));
file.setParentPathHash(rs.getBytes("parentpathhash"));
file.setContentid(rs.getBytes("contentid"));
file.setCreatedTime(rs.getTimestamp("createdtime"));
file.setCreatedBy(rs.getString("createdby"));
file.setUpdatedTime(rs.getTimestamp("updatedtime"));
file.setUpdatedBy(rs.getString("updatedby"));
file.setHistoryId(rs.getLong("historyid"));
file.setContent(rs.getBytes("data"));
return file;
}

public File getFileById(FolderType folderType, long id, Connection connection)
throws SQLException {
String sql = "SELECT * FROM folder WHERE type = ? AND id = ? AND contentid IS NOT NULL";
Expand Down Expand Up @@ -379,6 +407,85 @@ public File getFileAndContentByPathHash(
return file;
}

public List<FileHistory> getFileHistory(final long id, final int limit, final Connection connection) throws SQLException {

String sql =
"SELECT fh.id, fh.createdtime, c.createdby FROM folder_history fh "
+ "INNER JOIN content c ON fh.contentid = c.sha2 WHERE fh.folderid = ? ORDER BY fh.createdtime DESC LIMIT ?";

List<FileHistory> history = new ArrayList();
try (PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setLong(1, id);
statement.setInt(2, limit);
try (final ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
FileHistory fh = new FileHistory();
fh.setId(rs.getLong("id"));
fh.setCreatedBy(rs.getString("createdby"));
fh.setCreatedtime(rs.getTimestamp("createdtime"));
history.add(fh);
}
}
}
return history;
}

public long getDefaultFileHistoryId(long fileId, Connection connection) throws SQLException {

String sql =
"SELECT fh.id FROM folder f INNER JOIN folder_history fh ON f.id = fh.folderid AND f.contentid = fh.contentid WHERE f.id = ?";
try (PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setLong(1, fileId);
try (final ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
return rs.getLong("id");
}
}
}
return -1;
}

public FileHistory getFileHistory(
final long fileId, final long historyId, final Connection connection) throws SQLException {

String sql = "SELECT contentid, createdtime FROM folder_history WHERE id = ? AND folderid = ?";

FileHistory history = null;
try (PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setLong(1, historyId);
statement.setLong(2, fileId);
try (final ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
history = new FileHistory();
history.setId(historyId);
history.setFileid(fileId);
history.setContentid(rs.getBytes("contentid"));
history.setCreatedtime(rs.getTimestamp("createdtime"));
}
}
}
return history;
}

public Content getContentByHistoryId(long historyId, Connection connection) throws SQLException {
String sql = "SELECT c.sha2, c.data, c.createdtime, c.createdby FROM folder_history fh " +
"INNER JOIN content c ON fh.contentid = c.sha2 WHERE fh.id = ?";
Content content = null;
try (PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setLong(1, historyId);
try (final ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
content = new Content();
content.setSha2(rs.getBytes("sha2"));
content.setData(rs.getBytes("data"));
content.setCreatedby(rs.getString("createdby"));
content.setCreatedtime(rs.getTimestamp("createdtime"));
}
}
}
return content;
}

public Content getContentById(byte[] sha2, Connection connection) throws SQLException {
String sql = "SELECT data FROM content WHERE sha2 = ?";
Content content = null;
Expand Down Expand Up @@ -513,4 +620,5 @@ public boolean isFavorite(String userId, long folderId, Connection connection)
}
return false;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* This file is part of OpenTSDB.
* Copyright (C) 2021 Yahoo.
*
* 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
*
* http://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.
*/

package net.opentsdb.horizon.fs.view;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;

import java.sql.Timestamp;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class ContentDto {

private Object content;
private Timestamp createdTime;
private String createdBy;

public Object getContent() {
return content;
}

public void setContent(Object content) {
this.content = content;
}

public Timestamp getCreatedTime() {
return createdTime;
}

public void setCreatedTime(Timestamp createdTime) {
this.createdTime = createdTime;
}

public String getCreatedBy() {
return createdBy;
}

public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,21 @@ public class FileDto extends FolderDto {

private Object content;

private Long historyId;

public Object getContent() {
return content;
}

public void setContent(Object content) {
this.content = content;
}

public Long getHistoryId() {
return historyId;
}

public void setHistoryId(Long historyId) {
this.historyId = historyId;
}
}
Loading