From e5d340df5d7e7cfef0484a317cc81ea3c1785427 Mon Sep 17 00:00:00 2001 From: mgerb42 Date: Tue, 13 Sep 2016 04:37:56 +0000 Subject: [PATCH] fixed end point - fixed posts in metadata --- client/js/app.js | 2 +- client/js/components/Post.js | 2 +- client/js/components/sensors/SensorInfo.js | 20 ++++++++++++++++ client/js/redux/actions/app.js | 8 +++++-- client/js/redux/actions/sensor.js | 27 ++++++++++++++++++++++ client/js/redux/constants/sensor.js | 5 ++-- client/js/redux/reducers/sensor.js | 18 ++++++++++++++- metadata.js | 11 +++++---- package.json | 3 ++- server/model/daily_sensor/daily_sensor.go | 24 ++++++++++++++----- 10 files changed, 102 insertions(+), 18 deletions(-) diff --git a/client/js/app.js b/client/js/app.js index fe60721..7a19d31 100644 --- a/client/js/app.js +++ b/client/js/app.js @@ -38,7 +38,7 @@ ReactDOM.render(( - + diff --git a/client/js/components/Post.js b/client/js/components/Post.js index 188153b..d340785 100644 --- a/client/js/components/Post.js +++ b/client/js/components/Post.js @@ -21,7 +21,7 @@ export default class Post extends React.Component { componentDidMount() { const params = this.props.params; - this.props.appActions.fetchPost(params.category, params.post); + this.props.appActions.fetchPost(params.post, params.category); } render() { diff --git a/client/js/components/sensors/SensorInfo.js b/client/js/components/sensors/SensorInfo.js index dcd0f3b..c45bd2f 100644 --- a/client/js/components/sensors/SensorInfo.js +++ b/client/js/components/sensors/SensorInfo.js @@ -1,11 +1,31 @@ import React from 'react'; +let location, sensor, actions, uniqueDates; + export default class SensorInfo extends React.Component{ componentDidMount(){ + location = this.props.params.location; + actions = this.props.sensorActions; + sensor = this.props.sensor; + + actions.fetchUniqueDates(location); + + /* this.props.sensorActions.fetchSensorInfoYear('Grand Meadow', '2016'); this.props.sensorActions.fetchSensorInfoMonth('Grand Meadow', '2016', 'May'); + this.props.sensorActions.fetchUniqueDates('Grand Meadow'); + */ } + + componentWillReceiveProps(){ + if(sensor.fetchedUniqueDates){ + uniqueDates = sensor.uniqueDates; + + //!sensor.fetchedInfoMonth ? actions.fetchSensorInfoMonth(location, ) + } + } + render(){ return(
Test123
diff --git a/client/js/redux/actions/app.js b/client/js/redux/actions/app.js index b1c9018..5759223 100644 --- a/client/js/redux/actions/app.js +++ b/client/js/redux/actions/app.js @@ -44,10 +44,14 @@ export function fetchPreview() { } } -export function fetchPost(category, post) { +//adjust url according to parameters +//mainly used to load md files in the parent directory within /posts +export function fetchPost(post, category = null) { + let url; return (dispatch) => { dispatch(fetching()); - return fetch(`/public/posts/${category}/${post}.md`) + url = category !== null || typeof category === 'undefined' ? `/public/posts/${category}/${post}.md` : `/public/posts/${post}.md`; + return fetch(url) .then(response => response.text()) .then(response => { dispatch(loadPost(response)); diff --git a/client/js/redux/actions/sensor.js b/client/js/redux/actions/sensor.js index e356e99..3467e8d 100644 --- a/client/js/redux/actions/sensor.js +++ b/client/js/redux/actions/sensor.js @@ -22,6 +22,13 @@ function loadSensorInfoMonth(sensor_info){ } } +function loadUniqueDates(dates){ + return{ + type: types.LOAD_UNIQUE_DATES, + dates + } +} + function fetchingList(){ return { type: types.FETCHING_LIST @@ -40,6 +47,12 @@ function fetchingInfoMonth(){ } } +function fetchingUniqueDates(){ + return { + type: types.FETCHING_UNIQUE_DATES + } +} + export function fetchSensorList(){ return (dispatch) => { dispatch(fetchingList()); @@ -80,4 +93,18 @@ export function fetchSensorInfoMonth(location, year, month){ console.log(error); }); } +} + +export function fetchUniqueDates(location){ + return (dispatch) => { + dispatch(fetchingUniqueDates()); + return fetch(`/api/uniquedates/${location}`) + .then(response => response.json()) + .then(json => { + dispatch(loadUniqueDates(json)); + }) + .catch(error => { + console.log(error); + }); + } } \ No newline at end of file diff --git a/client/js/redux/constants/sensor.js b/client/js/redux/constants/sensor.js index ae93469..39587d0 100644 --- a/client/js/redux/constants/sensor.js +++ b/client/js/redux/constants/sensor.js @@ -2,9 +2,10 @@ export const LOAD_SENSOR_LIST = 'LOAD_SENSOR_LIST'; export const LOAD_SENSOR_INFO_YEAR = 'LOAD_SENSOR_INFO_YEAR'; export const LOAD_SENSOR_INFO_MONTH = 'LOAD_SENSOR_INFO_MONTH'; - +export const LOAD_UNIQUE_DATES = 'LOAD_UNIQUE_DATES'; //fetching export const FETCHING_LIST = 'FETCHING_LIST'; export const FETCHING_INFO_YEAR = 'FETCHING_INFO_YEAR'; -export const FETCHING_INFO_MONTH = 'FETCHING_INFO_MONTH'; \ No newline at end of file +export const FETCHING_INFO_MONTH = 'FETCHING_INFO_MONTH'; +export const FETCHING_UNIQUE_DATES = 'FETCHING_UNIQUE_DATES'; \ No newline at end of file diff --git a/client/js/redux/reducers/sensor.js b/client/js/redux/reducers/sensor.js index 8d5a4af..3b28d23 100644 --- a/client/js/redux/reducers/sensor.js +++ b/client/js/redux/reducers/sensor.js @@ -6,19 +6,23 @@ const defaultState = { list : [], infoMonth: [], infoYear: [], + uniqueDates: {}, fetchingList: false, fetchingInfoMonth: false, fetchingInfoYear: false, + fetchingUniqueDates: false, fetchedList: false, fetchedInfoMonth: false, - fetchedInfoYear: false + fetchedInfoYear: false, + fetchedUniqueDates: false }; //default reducer export default function app(state = defaultState, action) { switch(action.type){ + //fetching functions - we use a fetching state to display loading images case types.FETCHING_LIST: return Object.assign({}, state, { fetchingList: true, @@ -34,7 +38,13 @@ export default function app(state = defaultState, action) { fetchingInfoYear: true, fetchedInfoYear: false }); + case types:FETCHING_UNIQUE_DATES: + return Object.assign({}, state, { + fetchingUniqueDates: true, + fetchedUniqueDates: false + }); + //other functions case types.LOAD_SENSOR_LIST: return Object.assign({}, state, { list: action.sensor_list, @@ -53,6 +63,12 @@ export default function app(state = defaultState, action) { fetchingInfoYear: false, fetchedInfoYear: true }); + case types.LOAD_UNIQUE_DATES: + return Object.assign({}, state, { + uniqueDates: action.dates, + fetchingUniqueDates: false, + fetchedUniqueDates: true + }); } //return present state if no actions get called return state; diff --git a/metadata.js b/metadata.js index fa3bce8..476a068 100644 --- a/metadata.js +++ b/metadata.js @@ -19,13 +19,15 @@ marked.setOptions({ } }); -const dir = './posts/'; +const rootDirectory = './posts/'; const json = { posts: [] }; //do everything synchronously to keep posts ordered -function parse_dir(dir, folder_name){ +//we are not worried about execution time since this script only runs once when building +//ignores files that are not in a directory +function parse_dir(dir, folder_name = null){ const posts = fs.readdirSync(dir); for(let post of posts){ @@ -33,7 +35,7 @@ function parse_dir(dir, folder_name){ if(stats.isDirectory()){ parse_dir(dir + post + '/', post); - } else { + } else if(folder_name !== null){ const file = fs.readFileSync(dir+post, 'utf8'); const tokens = marked.lexer(file, null); const temp = { @@ -49,7 +51,8 @@ function parse_dir(dir, folder_name){ } //recursively parse posts directory for all markdown files -parse_dir(dir, 'posts'); +//folder defaults to null and immediate child files are not added to json +parse_dir(rootDirectory); //sort posts by date json.posts.sort((a, b) => { diff --git a/package.json b/package.json index 72b6bf9..0788873 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,8 @@ "dev": "webpack-dev-server --content-base public --inline --hot --history-api-fallback", "get_dependencies": "go get ./server && npm install", "prod": "export NODE_ENV=production && webpack -p && babel-node metadata.js && go build ./server/mywebsite.go", - "prod-win": "set NODE_ENV=production && webpack -p && babel-node metadata.js && go build ./server/mywebsite.go" + "prod-win": "set NODE_ENV=production && webpack -p && babel-node metadata.js && go build ./server/mywebsite.go", + "watch": "webpack --watch --colors --progress" }, "repository": { "type": "git", diff --git a/server/model/daily_sensor/daily_sensor.go b/server/model/daily_sensor/daily_sensor.go index 1a5e806..4b45a29 100644 --- a/server/model/daily_sensor/daily_sensor.go +++ b/server/model/daily_sensor/daily_sensor.go @@ -200,13 +200,17 @@ func GetAllSensorInfoByMonth(sensor_location string, year int, monthname string) } } +//I need to find a better implementation of this +//MongoDB $addToSet creates an array of objecta +//this ends up being a different type than the Data struct above +//it would be nice to make all MongoDB queries load directly into a Data struct type UniqueDates struct{ Dates []Data `json:"dates" bson:"dates"` } func GetUniqueSensorDates(sensor_location string) ([]Data, error){ d := []Data{} - temp := UniqueDates{}; + //temp := UniqueDates{}; if db.Mongo.Connected() == true { @@ -214,12 +218,20 @@ func GetUniqueSensorDates(sensor_location string) ([]Data, error){ defer session.Close() c := session.DB(db.Mongo.Info.Database).C(collection) - - err := c.Pipe([]bson.M{{"$match": bson.M{"location": sensor_location}}, - {"$group": bson.M{"_id": "null", "dates": bson.M{"$addToSet": bson.M{"month": "$month", "monthname": "$monthname", "year": "$year"}}}}, - }).One(&temp) - d = temp.Dates; + err := c.Pipe([]bson.M{bson.M{"$match": bson.M{"location": sensor_location}}, + {"$group": bson.M{"_id": "null", "dates": bson.M{"$addToSet": bson.M{"month": "$month", "monthname": "$monthname", "year": "$year"}}}}, + {"$unwind": "$dates"}, + {"$project": bson.M{"_id": 0, "month": "$dates.month", "monthname": "$dates.monthname", "year": "$dates.year"}}, + {"$sort": bson.M{"year": 1, "month": 1}}, + + }).All(&d) + + //d = temp.Dates; + + test12, _ := json.MarshalIndent(d, "", " ") + + log.Println(string(test12)) if err != nil { log.Println(err)