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

Add created, updated, and provisioned timestamps to saved template #551

Merged
merged 11 commits into from
Mar 9, 2024
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.1.0/)
- Added an optional workflow_step param to the get workflow steps API ([#538](https://github.com/opensearch-project/flow-framework/pull/538))

### Enhancements
- Add created, updated, and provisioned timestamps to saved template ([#551](https://github.com/opensearch-project/flow-framework/pull/551))
owaiskazi19 marked this conversation as resolved.
Show resolved Hide resolved

### Bug Fixes
### Infrastructure
### Documentation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ private CommonValue() {}
public static final String CREATE_TIME = "create_time";
/** The template field name for the user who created the workflow **/
public static final String USER_FIELD = "user";
/** The created time field */
public static final String CREATED_TIME = "created_time";
/** The last updated time field */
public static final String LAST_UPDATED_TIME_FIELD = "last_updated_time";
/** The last updated time field */
dbwiddis marked this conversation as resolved.
Show resolved Hide resolved
public static final String LAST_PROVISIONED_TIME_FIELD = "last_provisioned_time";

/*
* Constants associated with Rest or Transport actions
Expand Down Expand Up @@ -156,10 +162,6 @@ private CommonValue() {}
public static final String APP_TYPE_FIELD = "app_type";
/** To include field for an agent response */
public static final String INCLUDE_OUTPUT_IN_AGENT_RESPONSE = "include_output_in_agent_response";
/** The created time field for an agent */
public static final String CREATED_TIME = "created_time";
/** The last updated time field for an agent */
public static final String LAST_UPDATED_TIME_FIELD = "last_updated_time";
/** HttpHost */
public static final String HTTP_HOST_FIELD = "http_host";
/** Http scheme */
Expand Down
143 changes: 120 additions & 23 deletions src/main/java/org/opensearch/flowframework/model/Template.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@
import org.opensearch.flowframework.exception.FlowFrameworkException;

import java.io.IOException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken;
import static org.opensearch.flowframework.common.CommonValue.CREATED_TIME;
import static org.opensearch.flowframework.common.CommonValue.DESCRIPTION_FIELD;
import static org.opensearch.flowframework.common.CommonValue.LAST_PROVISIONED_TIME_FIELD;
import static org.opensearch.flowframework.common.CommonValue.LAST_UPDATED_TIME_FIELD;
import static org.opensearch.flowframework.common.CommonValue.NAME_FIELD;
import static org.opensearch.flowframework.common.CommonValue.UI_METADATA_FIELD;
import static org.opensearch.flowframework.common.CommonValue.USER_FIELD;
Expand All @@ -48,14 +52,17 @@
/** The template field name for template use case */
public static final String USE_CASE_FIELD = "use_case";

private String name;
private String description;
private String useCase; // probably an ENUM actually
private Version templateVersion;
private List<Version> compatibilityVersion;
private Map<String, Workflow> workflows;
private Map<String, Object> uiMetadata;
private User user;
private final String name;
private final String description;
private final String useCase; // probably an ENUM actually
dbwiddis marked this conversation as resolved.
Show resolved Hide resolved
private final Version templateVersion;
private final List<Version> compatibilityVersion;
private final Map<String, Workflow> workflows;
private final Map<String, Object> uiMetadata;
private final User user;
private final long createdTime;
private final long lastUpdatedTime;
private final long lastProvisionedTime;

/**
* Instantiate the object representing a use case template
Expand All @@ -68,6 +75,9 @@
* @param workflows Workflow graph definitions corresponding to the defined operations.
* @param uiMetadata The UI metadata related to the given workflow
* @param user The user extracted from the thread context from the request
* @param createdTime Created time in milliseconds since the epoch
* @param lastUpdatedTime Last Updated time in milliseconds since the epoch
* @param lastProvisionedTime Last Provisioned time in milliseconds since the epoch
*/
public Template(
String name,
Expand All @@ -77,7 +87,10 @@
List<Version> compatibilityVersion,
Map<String, Workflow> workflows,
Map<String, Object> uiMetadata,
User user
User user,
long createdTime,
long lastUpdatedTime,
long lastProvisionedTime
) {
this.name = name;
this.description = description;
Expand All @@ -87,10 +100,11 @@
this.workflows = Map.copyOf(workflows);
this.uiMetadata = uiMetadata;
this.user = user;
this.createdTime = createdTime < 0 ? Instant.now().toEpochMilli() : createdTime;
this.lastUpdatedTime = lastUpdatedTime < this.createdTime ? this.createdTime : lastUpdatedTime;
this.lastProvisionedTime = lastProvisionedTime;
}

private Template() {}

/**
* Class for constructing a Builder for Template
*/
Expand All @@ -103,6 +117,9 @@
private Map<String, Workflow> workflows = new HashMap<>();
private Map<String, Object> uiMetadata = null;
private User user = null;
private long createdTime = -1L;
private long lastUpdatedTime = -1L;
private long lastProvisionedTime = -1L;

/**
* Empty Constructor for the Builder object
Expand Down Expand Up @@ -189,23 +206,55 @@
return this;
}

/**
* Builder method for adding createdTime
* @param createdTime created time in milliseconds since the epoch
* @return the Builder object
*/
public Builder createdTime(long createdTime) {
this.createdTime = createdTime;
return this;
}

/**
* Builder method for adding lastUpdatedTime
* @param lastUpdatedTime last updated time in milliseconds since the epoch
* @return the Builder object
*/
public Builder lastUpdatedTime(long lastUpdatedTime) {
this.lastUpdatedTime = lastUpdatedTime;
return this;
}

/**
* Builder method for adding lastProvisionedTime
* @param lastProvisionedTime last provisioned time in milliseconds since the epoch
* @return the Builder object
*/
public Builder lastProvisionedTime(long lastProvisionedTime) {
this.lastProvisionedTime = lastProvisionedTime;
return this;
}

/**
* Allows building a template
* @return Template Object containing all needed fields
*/
public Template build() {
Template template = new Template();
template.name = this.name;
template.description = this.description;
template.useCase = this.useCase;
template.templateVersion = this.templateVersion;
template.compatibilityVersion = this.compatibilityVersion;
template.workflows = this.workflows;
template.uiMetadata = this.uiMetadata;
template.user = this.user;
return template;
return new Template(
this.name,
this.description,
this.useCase,
this.templateVersion,
this.compatibilityVersion,
this.workflows,
this.uiMetadata,
this.user,
this.createdTime,
this.lastUpdatedTime,
this.lastProvisionedTime
);
}

}

@Override
Expand Down Expand Up @@ -244,6 +293,18 @@
xContentBuilder.field(USER_FIELD, user);
}

if (createdTime > 0) {
xContentBuilder.field(CREATED_TIME, createdTime);
}

if (lastUpdatedTime > 0) {
xContentBuilder.field(LAST_UPDATED_TIME_FIELD, lastUpdatedTime);
}

if (lastProvisionedTime > 0) {
xContentBuilder.field(LAST_PROVISIONED_TIME_FIELD, lastProvisionedTime);

Check warning on line 305 in src/main/java/org/opensearch/flowframework/model/Template.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/model/Template.java#L305

Added line #L305 was not covered by tests
}

return xContentBuilder.endObject();
}

Expand All @@ -263,6 +324,9 @@
Map<String, Workflow> workflows = new HashMap<>();
Map<String, Object> uiMetadata = null;
User user = null;
long createdTime = -1;
long lastUpdatedTime = -1;
long lastProvisionedTime = -1;

ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser);
while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
Expand Down Expand Up @@ -315,6 +379,15 @@
case USER_FIELD:
user = User.parse(parser);
break;
case CREATED_TIME:
createdTime = parser.longValue();
break;
case LAST_UPDATED_TIME_FIELD:
lastUpdatedTime = parser.longValue();
break;
case LAST_PROVISIONED_TIME_FIELD:
lastProvisionedTime = parser.longValue();
break;

Check warning on line 390 in src/main/java/org/opensearch/flowframework/model/Template.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/model/Template.java#L389-L390

Added lines #L389 - L390 were not covered by tests
default:
throw new FlowFrameworkException(
"Unable to parse field [" + fieldName + "] in a template object.",
Expand All @@ -334,6 +407,9 @@
.workflows(workflows)
.uiMetadata(uiMetadata)
.user(user)
.createdTime(createdTime)
.lastUpdatedTime(lastUpdatedTime)
.lastProvisionedTime(lastProvisionedTime)
.build();
}

Expand Down Expand Up @@ -449,6 +525,27 @@
return user;
}

/**
* @return the createdTime
*/
public long createdTime() {
return createdTime;
}

/**
* @return the lastUpdatedTime
*/
public long lastUpdatedTime() {
return lastUpdatedTime;
}

/**
* @return the lastProvisionedTime
*/
public long lastProvisionedTime() {
return lastProvisionedTime;
}

@Override
public String toString() {
return "Template [name="
Expand All @@ -464,7 +561,7 @@
+ ", workflows="
+ workflows
+ ", uiMedata="
+ uiMetadata
+ (uiMetadata == null ? "{}" : uiMetadata)
+ "]";
}
}
Loading
Loading