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

Minor refactor #32

Merged
merged 6 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
17 changes: 3 additions & 14 deletions src/chain_actions/create_action.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::chain_actions::utils::now_date_time;
use crate::chain_actions::utils::add_current_time_path;
use crate::wire_element::WireElement;
use chrono::{DateTime, Datelike, Timelike, Utc};
use hdk::prelude::*;
use holo_hash::{AgentPubKey, EntryHashB64, HeaderHashB64};

Expand All @@ -9,6 +8,7 @@ use ::mockall::automock;

/// an enum passed into create_action to indicate whether the newly created entry is to be
/// linked off a path (like an anchor for entry types) or a supplied entry hash
#[derive(Debug, PartialEq, Clone)]
pub enum PathOrEntryHash {
Path(Path),
EntryHash(EntryHash),
Expand Down Expand Up @@ -65,18 +65,7 @@ impl CreateAction {
None => (),
Some(base_component) => {
// create a time_path
let date: DateTime<Utc> = now_date_time()?;

let time_path = crate::datetime_queries::utils::hour_path_from_date(
base_component,
date.year(),
date.month(),
date.day(),
date.hour(),
);

time_path.ensure()?;
create_link(time_path.hash()?, entry_hash.clone(), ())?;
add_current_time_path(base_component, entry_hash.clone())?;
}
}
let time = sys_time()?; // this won't exactly match the timestamp stored in the element details
Expand Down
16 changes: 2 additions & 14 deletions src/chain_actions/update_action.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::chain_actions::utils::now_date_time;
use crate::chain_actions::utils::add_current_time_path;
use crate::wire_element::WireElement;
use chrono::{DateTime, Datelike, Timelike, Utc};
use hdk::prelude::*;
use holo_hash::{AgentPubKey, EntryHashB64, HeaderHashB64};

Expand Down Expand Up @@ -46,18 +45,7 @@ impl UpdateAction {
None => (),
Some(base_component) => {
// create a time_path
let date: DateTime<Utc> = now_date_time()?;

let time_path = crate::datetime_queries::utils::hour_path_from_date(
base_component,
date.year(),
date.month(),
date.day(),
date.hour(),
);

time_path.ensure()?;
create_link(time_path.hash()?, entry_address.clone(), ())?;
add_current_time_path(base_component, entry_address.clone())?;
}
}
let updated_at = sys_time()?;
Expand Down
22 changes: 19 additions & 3 deletions src/chain_actions/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use chrono::{DateTime, NaiveDateTime, Utc};
use hdk::prelude::ExternResult;
use hdk::time::sys_time;
use chrono::{DateTime, NaiveDateTime, Utc, Datelike, Timelike};
use holo_hash::EntryHash;
use hdk::prelude::*;

/// get the current UTC date time
pub fn now_date_time() -> ExternResult<::chrono::DateTime<::chrono::Utc>> {
Expand All @@ -10,3 +10,19 @@ pub fn now_date_time() -> ExternResult<::chrono::DateTime<::chrono::Utc>> {
DateTime::from_utc(NaiveDateTime::from_timestamp(time.0, time.1), Utc);
Ok(date)
}

pub fn add_current_time_path(base_component: String, entry_address: EntryHash) -> ExternResult<()>{
let date: DateTime<Utc> = now_date_time()?;

let time_path = crate::datetime_queries::utils::hour_path_from_date(
base_component,
date.year(),
date.month(),
date.day(),
date.hour(),
);

time_path.ensure()?;
create_link(time_path.hash()?, entry_address.clone(), ())?;
Ok(())
}
2 changes: 1 addition & 1 deletion src/retrieval/inputs.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use hdk::prelude::*;
use holo_hash::EntryHashB64;

#[derive(Debug, Serialize, Deserialize, SerializedBytes)]
#[derive(Debug, PartialEq, Serialize, Deserialize, SerializedBytes)]
pub enum FetchOptions {
All,
Specific(Vec<EntryHashB64>),
Expand Down