Skip to content

Commit 1992710

Browse files
committed
Allow upload of tenant and of historical data.
1 parent 4691817 commit 1992710

4 files changed

Lines changed: 51 additions & 30 deletions

File tree

app/app.js

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const mongoose = require('mongoose');
1616
const MongoStore = require('connect-mongo');
1717

1818
const MONGO_DB = process.env.MONGO_URL || 'mongodb://localhost:27017';
19+
const sessionOff = process.env.SESSION_OFF || false;
1920

2021
const connectWithRetry = () => {
2122
mongoose
@@ -32,7 +33,13 @@ const connectWithRetry = () => {
3233
setTimeout(connectWithRetry, 5000);
3334
});
3435
};
35-
connectWithRetry();
36+
37+
if (!sessionOff) {
38+
debug(`Enabling sessions`);
39+
connectWithRetry();
40+
} else {
41+
debug('Sessions are disabled.');
42+
}
3643

3744
// view engine setup
3845
app.set('views', path.join(__dirname, 'views'));
@@ -44,29 +51,31 @@ app.use(express.urlencoded({ extended: false }));
4451
app.use(cookieParser());
4552
app.use(flash());
4653

47-
if (process.env.NODE_ENV === 'production') {
48-
// Use Mongo-DB to store session data.
49-
app.use(
50-
session({
51-
resave: false,
52-
saveUninitialized: true,
53-
secret: SECRET,
54-
store: MongoStore.create({
55-
mongoUrl: MONGO_DB + '/sessions',
56-
mongooseConnection: mongoose.connection,
57-
ttl: 14 * 24 * 60 * 60 // save session for 14 days
54+
if (!sessionOff) {
55+
if (process.env.NODE_ENV === 'production') {
56+
// Use Mongo-DB to store session data.
57+
app.use(
58+
session({
59+
resave: false,
60+
saveUninitialized: true,
61+
secret: SECRET,
62+
store: MongoStore.create({
63+
mongoUrl: MONGO_DB + '/sessions',
64+
mongooseConnection: mongoose.connection,
65+
ttl: 14 * 24 * 60 * 60 // save session for 14 days
66+
})
5867
})
59-
})
60-
);
61-
} else {
62-
// Use Memstore for session data.
63-
app.use(
64-
session({
65-
secret: SECRET,
66-
resave: false,
67-
saveUninitialized: true
68-
})
69-
);
68+
);
69+
} else {
70+
// Use Memstore for session data.
71+
app.use(
72+
session({
73+
secret: SECRET,
74+
resave: false,
75+
saveUninitialized: true
76+
})
77+
);
78+
}
7079
}
7180

7281
app.use(express.static(path.join(__dirname, 'public')));

app/controllers/csv.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,20 @@ function createEntitiesFromRows(rows) {
6262
weight: 'KGM',
6363
batteryLevel: 'C68'
6464
};
65-
const timestamp = new Date().toISOString();
65+
const now = new Date();
66+
const currentTimestamp = now.toISOString();
6667

6768
rows.forEach((row) => {
6869
const entity = {
6970
id: row.id,
7071
type: row.type
7172
};
7273

74+
let timestamp = currentTimestamp;
75+
if (row.offset) {
76+
timestamp = new Date(now.getTime() + Number(row.offset) * 60000).toISOString();
77+
}
78+
7379
Object.keys(row).forEach((key) => {
7480
const value = row[key];
7581
if (value !== '') {
@@ -78,7 +84,7 @@ function createEntitiesFromRows(rows) {
7884
case 'agroVocConcept':
7985
case 'alternateName':
8086
case 'birthdate':
81-
87+
8288
case 'controlledProperty':
8389
case 'dataProvider':
8490
case 'dateIssued':
@@ -206,6 +212,7 @@ function createEntitiesFromRows(rows) {
206212
case 'addressRegion':
207213
case 'postalCode':
208214
case 'providedBy':
215+
case 'offset':
209216
break;
210217

211218
default:
@@ -257,10 +264,10 @@ function createEntitiesFromRows(rows) {
257264
* Create an array of promises to send data to the context broker.
258265
* Each insert represents a series of readings at a given timestamp
259266
*/
260-
function createContextRequests(entities) {
267+
function createContextRequests(entities, tenant) {
261268
const promises = [];
262269
entities.forEach((entitiesAtTimeStamp) => {
263-
promises.push(BatchUpdate.sendAsHTTP(entitiesAtTimeStamp));
270+
promises.push(BatchUpdate.sendAsHTTP(entitiesAtTimeStamp, tenant));
264271
});
265272
return promises;
266273
}
@@ -293,7 +300,7 @@ const upload = (req, res) => {
293300
batchEntities.push(chunk);
294301
}
295302

296-
return createContextRequests(batchEntities);
303+
return createContextRequests(batchEntities, req.get('NGSILD-Tenant'));
297304
})
298305
.then(async (promises) => {
299306
const results = [];

app/controllers/ngsi-ld/animal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ async function getAnimals(req, res) {
3131

3232
function displayMap(req, res) {
3333
debug('displayMap');
34-
return res.render('animalMap', {title: 'Animal Locations'});
34+
return res.render('animalMap', { title: 'Animal Locations' });
3535
}
3636

3737
// This function receives the details of a person from the context

app/lib/batchUpdate.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,17 @@ function is2xxSuccessful(status) {
1818
}
1919

2020
// measures sent over HTTP are POST requests with params
21-
function sendAsHTTP(state) {
21+
function sendAsHTTP(state, tenant) {
2222
const url = CONTEXT_BROKER_URL + '/entityOperations/upsert';
2323
const headers = {
2424
'Content-Type': 'application/json',
2525
Link: '<' + LINKED_DATA + '>; rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json"'
2626
};
27+
28+
if (tenant) {
29+
headers['NGSILD-Tenant'] = tenant;
30+
}
31+
2732
const body = Array.isArray(state) ? state : [state];
2833
try {
2934
return fetch(`${url}?${new URLSearchParams({ options: 'update' })}`, {

0 commit comments

Comments
 (0)