2016-01-22 08:22:11 -06:00
|
|
|
import Influx from 'react-influx'
|
|
|
|
import keyMirror from 'keymirror'
|
|
|
|
import Dispatcher from '../dispatchers/Dispatcher'
|
2016-01-22 19:16:53 -06:00
|
|
|
import * as JSON2 from '../../../src/libs/JSON2'
|
2016-04-09 05:09:53 -05:00
|
|
|
import moment from 'moment';
|
|
|
|
import _ from 'underscore';
|
|
|
|
import request from 'superagent'
|
2016-01-22 08:22:11 -06:00
|
|
|
|
2016-04-09 05:09:53 -05:00
|
|
|
const OVERFLOW = 200;
|
2016-01-22 08:22:11 -06:00
|
|
|
const Events = keyMirror({
|
2016-04-09 05:09:53 -05:00
|
|
|
DATABASE_READY: null,
|
|
|
|
SOCKET_READY: null,
|
|
|
|
UPDATED: null,
|
|
|
|
UPDATED_SELECTED: null,
|
|
|
|
UPDATED_TIMESERIES: null
|
|
|
|
});
|
2016-01-22 08:22:11 -06:00
|
|
|
|
|
|
|
class EntryStore extends Influx.Store {
|
|
|
|
constructor(...args) {
|
|
|
|
super(Dispatcher/*, additional dispatchers you have */);
|
2016-04-09 05:09:53 -05:00
|
|
|
this.data = Object.assign(
|
|
|
|
{
|
|
|
|
entries: [], selected: {},
|
|
|
|
options: {sort: {date: -1}, limit: OVERFLOW},
|
|
|
|
period: {
|
|
|
|
$gt: moment().startOf('day').toISOString(),
|
|
|
|
$lt: moment().add(1, 'days').startOf('day').toISOString()
|
|
|
|
}
|
|
|
|
}, this.fromQueryString(window.location.hash.substr(1)));
|
|
|
|
|
|
|
|
this.setMaxListeners(Number.MAX_SAFE_INTEGER);
|
2016-01-22 08:22:11 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
getSearchResults() {
|
2016-04-09 05:09:53 -05:00
|
|
|
return this.data.entries;
|
2016-01-22 08:22:11 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
getDispatcherListeners() {
|
|
|
|
return [
|
|
|
|
[Dispatcher, Dispatcher.Events.REQUEST_INIT_DATABASE, this._onDispatcherRequestInitDatabase],
|
|
|
|
[Dispatcher, Dispatcher.Events.REQUEST_INIT_SOCKET, this._onDispatcherRequestInitSocket],
|
2016-04-09 05:09:53 -05:00
|
|
|
[Dispatcher, Dispatcher.Events.REQUEST_PERIOD, this._onDispatcherRequestPeriod],
|
|
|
|
[Dispatcher, Dispatcher.Events.REQUEST_SELECT_ENTRY, this._onDispatcherRequestSelectEntry],
|
|
|
|
[Dispatcher, Dispatcher.Events.REQUEST_SELECT_ALL_ENTRIES, this._onDispatcherRequestSelectAllEntry],
|
|
|
|
[Dispatcher, Dispatcher.Events.REQUEST_SELECT_CLEAR_ENTRIES, this._onDispatcherRequestClearEntries],
|
|
|
|
[Dispatcher, Dispatcher.Events.REQUEST_ENTRY_SEARCH, this._onDispatcherRequestEntrySearch],
|
|
|
|
[Dispatcher, Dispatcher.Events.REQUEST_GROW_SEARCH, this._onDispatcherRequestGrowSearch]
|
2016-01-22 08:22:11 -06:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
_onDispatcherRequestInitDatabase() {
|
2016-04-09 05:09:53 -05:00
|
|
|
const presearch = Object.keys(this.data.query || {}).length;
|
|
|
|
this.emit(Events.DATABASE_READY, presearch);
|
|
|
|
|
|
|
|
if (presearch) {
|
|
|
|
Dispatcher.emit(Dispatcher.Events.REQUEST_ENTRY_SEARCH, this.data.query, this.data.options, this.data.period);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onDispatcherRequestSelectEntry(id, selected) {
|
|
|
|
if (selected) {
|
|
|
|
this.data.selected[id] = true;
|
|
|
|
} else {
|
|
|
|
delete this.data.selected[id];
|
|
|
|
}
|
|
|
|
|
|
|
|
this.emit(Events.UPDATED_SELECTED);
|
|
|
|
this.emit(Events.UPDATED_SELECTED + id, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
isSelected(id) {
|
|
|
|
return !!this.data.selected[id];
|
|
|
|
}
|
|
|
|
|
|
|
|
getSelected() {
|
|
|
|
return Object.keys(this.data.selected);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onDispatcherRequestSelectAllEntry() {
|
|
|
|
const plucked = _.pluck(this.data.entries, '_id');
|
|
|
|
plucked.forEach(a => this.data.selected[a] = true);
|
|
|
|
|
|
|
|
this.emit(Events.UPDATED_SELECTED);
|
|
|
|
plucked.forEach(id => this.emit(Events.UPDATED_SELECTED + id, true));
|
|
|
|
}
|
|
|
|
|
|
|
|
_onDispatcherRequestClearEntries() {
|
|
|
|
const keys = Object.keys(this.data.selected);
|
|
|
|
|
|
|
|
this.data.selected = {};
|
|
|
|
|
|
|
|
this.emit(Events.UPDATED_SELECTED);
|
|
|
|
keys.forEach(id => this.emit(Events.UPDATED_SELECTED + id, false));
|
2016-01-22 08:22:11 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
_onDispatcherRequestInitSocket() {
|
2016-01-24 18:03:01 -06:00
|
|
|
const url = window.location;
|
|
|
|
(config.socketPorts || []).map(port => {
|
|
|
|
this.socket = io(`${url.protocol}//${url.hostname}:${port}`);
|
2016-01-22 08:22:11 -06:00
|
|
|
this.socket.on('data', data => {
|
2016-04-09 05:09:53 -05:00
|
|
|
data._pushed = true;
|
|
|
|
db.localDb.Entry.upsert(data, () => this.refreshSession());
|
2016-01-22 08:22:11 -06:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
this.emit(Events.SOCKET_READY);
|
|
|
|
}
|
|
|
|
|
2016-04-09 05:09:53 -05:00
|
|
|
_onDispatcherRequestEntrySearch(...args) {
|
|
|
|
this.startSession(...args);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onDispatcherRequestGrowSearch() {
|
|
|
|
this.fetchSession();
|
|
|
|
}
|
|
|
|
|
|
|
|
_onDispatcherRequestPeriod(period) {
|
|
|
|
this.setSessionPeriod(period);
|
|
|
|
}
|
|
|
|
|
|
|
|
getSearchTimeSeries() {
|
|
|
|
return this.data.timeseries;
|
2016-01-22 08:22:11 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
getSearchQuery() {
|
|
|
|
return this.data.query;
|
|
|
|
}
|
|
|
|
|
|
|
|
getSearchOptions() {
|
|
|
|
return this.data.options;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit(...args) {
|
|
|
|
setTimeout(() => super.emit(...args), 0);
|
|
|
|
}
|
|
|
|
|
2016-04-09 05:09:53 -05:00
|
|
|
addEntries(entries, pre) {
|
|
|
|
this.data.entries = pre ? [...entries, ...this.data.entries] : [...this.data.entries, ...entries];
|
|
|
|
}
|
|
|
|
|
|
|
|
fromQueryString(string) {
|
|
|
|
try {
|
|
|
|
return JSON.parse(string);
|
|
|
|
} catch (e) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getQueryString(query = this.data.query, select = this.data.select) {
|
|
|
|
return JSON.stringify({
|
|
|
|
query: query,
|
|
|
|
options: this.data.options,
|
|
|
|
period: this.data.period,
|
|
|
|
text: this.data.text,
|
|
|
|
select: select
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getSessionDBQuery(date = this.data.period) {
|
|
|
|
return Object.assign({}, Object.assign({}, this.data.query, this.data.text ? {
|
|
|
|
serialized: {
|
|
|
|
$regex: `.*${this.data.text}.*`,
|
|
|
|
$options: 'i'
|
|
|
|
}
|
|
|
|
} : {}), {date});
|
|
|
|
}
|
|
|
|
|
|
|
|
getSessionDBOptionsQuery(options = this.data.options) {
|
|
|
|
return Object.assign({}, this.data.options, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
startSession(query = this.data.query, options = this.data.options, period = this.data.period, text = this.data.text) {
|
|
|
|
this.data.entries = [];
|
2016-01-22 08:22:11 -06:00
|
|
|
this.data.options = options;
|
2016-04-09 05:09:53 -05:00
|
|
|
this.data.query = query;
|
|
|
|
this.data.finished = false;
|
|
|
|
this.data.timeseries = [];
|
|
|
|
this.data.period = period;
|
|
|
|
this.data.text = text;
|
|
|
|
this.data.session = Date.now();
|
2016-01-24 18:03:01 -06:00
|
|
|
|
2016-04-09 05:09:53 -05:00
|
|
|
db.Entry.find(this.getSessionDBQuery(), this.getSessionDBOptionsQuery({skip: 0}))
|
|
|
|
.fetch(entries => {
|
|
|
|
this.data.entries = entries;
|
2016-01-22 08:22:11 -06:00
|
|
|
this.emit(Events.UPDATED);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2016-04-09 05:09:53 -05:00
|
|
|
request
|
|
|
|
.post('rest/timeseries')
|
|
|
|
.send(this.getSessionDBQuery())
|
|
|
|
.end((err, res) => {
|
|
|
|
this.data.timeseries = res.body || [];
|
|
|
|
this.emit(Events.UPDATED_TIMESERIES);
|
|
|
|
});
|
|
|
|
|
|
|
|
window.location.hash = this.getQueryString();
|
|
|
|
}
|
|
|
|
|
|
|
|
getSearchPeriod() {
|
|
|
|
return this.data.period;
|
|
|
|
}
|
|
|
|
|
|
|
|
getSessionText() {
|
|
|
|
return this.data.text || '';
|
|
|
|
}
|
|
|
|
|
|
|
|
setSessionText(text) {
|
|
|
|
this.data.text = text;
|
|
|
|
|
|
|
|
this.startSession(); // restart
|
|
|
|
}
|
|
|
|
|
|
|
|
setSessionPeriod(period) {
|
|
|
|
this.data.period = period;
|
|
|
|
|
|
|
|
this.startSession(); // restart
|
|
|
|
}
|
|
|
|
|
|
|
|
getSessionId() {
|
|
|
|
return this.data.session;
|
|
|
|
}
|
|
|
|
|
|
|
|
refreshSession() {
|
|
|
|
db.localDb.Entry
|
|
|
|
.find(this.getSessionDBQuery(), this.getSessionDBOptionsQuery({limit: null, skip: 0}))
|
|
|
|
.fetch(entries => {
|
|
|
|
this.data.entries = entries;
|
|
|
|
this.emit(Events.UPDATED);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchSession(up) {
|
|
|
|
const {options, entries, query} = this.data;
|
|
|
|
const entry = entries[entries.length - 1];
|
|
|
|
|
|
|
|
if (entry) {
|
|
|
|
const periodCpy = JSON.parse(JSON.stringify(this.data.period));
|
|
|
|
if (up) {
|
|
|
|
delete periodCpy.$gte;
|
|
|
|
delete periodCpy.$gt;
|
|
|
|
periodCpy.$gte = entry.date;
|
|
|
|
} else {
|
|
|
|
delete periodCpy.$lte;
|
|
|
|
delete periodCpy.$lt;
|
|
|
|
periodCpy.$lte = entry.date;
|
|
|
|
}
|
|
|
|
|
|
|
|
db
|
|
|
|
.Entry
|
|
|
|
.find(this.getSessionDBQuery(periodCpy), this.getSessionDBOptionsQuery())
|
|
|
|
.fetch(() => {
|
|
|
|
this.refreshSession();
|
|
|
|
});
|
2016-01-22 08:22:11 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default EntryStore.construct(EntryStore, Events)
|