Skip to content

Commit

Permalink
feat: pull default orgs on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Feb 16, 2022
1 parent 471766e commit ee82340
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 59 deletions.
3 changes: 2 additions & 1 deletion .env.copy
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ DB_HOST=
DATALAYER_URL=https://localhost:8562
WALLET_URL=https://localhost:9256
USE_SIMULATOR=false
PICKLIST_URL=https://climate-warehouse.s3.us-west-2.amazonaws.com/public/picklists.json
PICKLIST_URL=https://climate-warehouse.s3.us-west-2.amazonaws.com/public/picklists.json
DEFAULT_ORGANIZATIONS=https://climate-warehouse.s3.us-west-2.amazonaws.com/public/cw-organizations.json
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"lodash": "^4.17.21",
"log-update": "^4.0.0",
"mysql2": "^2.3.3",
"node-status-check": "^0.0.1",
"node-xlsx": "^0.21.0",
"random-hash": "^4.0.1",
"regenerator-runtime": "^0.13.9",
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/organization.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const importOrg = async (req, res) => {
'Importing and subscribing organization this can take a few mins.',
});

return Organization.importOrganization(orgUid, ip, port);
return Organization.subscribeToOrganization(orgUid, ip, port);
} catch (error) {
console.trace(error);
res.status(400).json({
Expand Down
2 changes: 1 addition & 1 deletion src/datalayer/persistance.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export const getRoot = async (storeId) => {
try {
const data = JSON.parse(response);
console.log(`Root for ${storeId}`, data);
if (data.status === 2 && !data.hash.includes('0x00000000000')) {
if (data.cinfirmed && !data.hash.includes('0x00000000000')) {
return data;
}

Expand Down
5 changes: 3 additions & 2 deletions src/datalayer/syncService.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export const dataLayerWasUpdated = async () => {

if (org) {
// store has been updated if its confirmed and the hash has changed
return rootHash.status === 2 && org.registryHash != rootHash.hash;
return rootHash.confirmed && org.registryHash != rootHash.hash;
}

return false;
Expand Down Expand Up @@ -168,7 +168,8 @@ export const getSubscribedStoreData = async (
alreadySubscribed = false,
retry = 0,
) => {
if (retry > 30) {
console.log('Subscribing to', storeId, ip, port);
if (retry > 10) {
throw new Error('Max retrys exceeded, Can not subscribe to organization');
}

Expand Down
113 changes: 76 additions & 37 deletions src/models/organizations/organizations.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
import Sequelize from 'sequelize';
const { Model } = Sequelize;
import { sequelize } from '../database';

import {
createDataLayerStore,
syncDataLayer,
subscribeToStoreOnDataLayer,
getSubscribedStoreData,
} from '../../datalayer';

import {
getDefaultOrganizationList,
serverAvailable,
} from '../../utils/data-loaders';

import ModelTypes from './organizations.modeltypes.cjs';

class Organization extends Model {
Expand Down Expand Up @@ -84,47 +90,51 @@ class Organization extends Model {

// eslint-disable-next-line
static importOrganization = async (orgUid, ip, port) => {
const orgData = await getSubscribedStoreData(orgUid, ip, port);
try {
const orgData = await getSubscribedStoreData(orgUid, ip, port);

if (!orgData.registryId) {
throw new Error(
'Currupted organization, no registryId on the datalayer, can not import',
);
}
if (!orgData.registryId) {
throw new Error(
'Currupted organization, no registryId on the datalayer, can not import',
);
}

console.log('IMPORTING REGISTRY: ', orgData.registryId);
console.log('IMPORTING REGISTRY: ', orgData.registryId);

const registryData = await getSubscribedStoreData(
orgData.registryId,
ip,
port,
);
const registryData = await getSubscribedStoreData(
orgData.registryId,
ip,
port,
);

if (!registryData.v1) {
throw new Error('Organization has no registry, can not import');
if (!registryData.v1) {
throw new Error('Organization has no registry, can not import');
}

console.log('IMPORTING REGISTRY V1: ', registryData.v1);

await subscribeToStoreOnDataLayer(registryData.v1, ip, port);

console.log({
orgUid,
name: orgData.name,
icon: orgData.icon,
registryId: registryData.v1,
subscribed: true,
isHome: false,
});

await Organization.upsert({
orgUid,
name: orgData.name,
icon: orgData.icon,
registryId: registryData.v1,
subscribed: true,
isHome: false,
});
} catch (error) {
console.log(error.message);
}

console.log('IMPORTING REGISTRY V1: ', registryData.v1);

await subscribeToStoreOnDataLayer(registryData.v1, ip, port);

console.log({
orgUid,
name: orgData.name,
icon: orgData.icon,
registryId: registryData.v1,
subscribed: true,
isHome: false,
});

await Organization.upsert({
orgUid,
name: orgData.name,
icon: orgData.icon,
registryId: registryData.v1,
subscribed: true,
isHome: false,
});
};

// eslint-disable-next-line
Expand All @@ -133,14 +143,43 @@ class Organization extends Model {
if (exists) {
await Organization.update({ subscribed: true }, { orgUid });
} else {
Organization.importOrganization(orgUid);
throw new Error(
'Can not subscribe, please import this organization first',
);
}
};

// eslint-disable-next-line
static unsubscribeToOrganization = async (orgUid) => {
await Organization.update({ subscribed: false }, { orgUid });
};

static subscribeToDefaultOrganizations = async () => {
try {
const defaultOrgs = await getDefaultOrganizationList();
if (!Array.isArray(defaultOrgs)) {
console.log(
'ERROR: Default Organization List Not found, This instance may be missing data from default orgs',
);
}

await Promise.all(
defaultOrgs.map(async (org) => {
const exists = await Organization.findOne({
where: { orgUid: org.orgUid },
});

if (!exists) {
if (serverAvailable(org.ip, org.port)) {
Organization.importOrganization(org.orgUid, org.ip, org.port);
}
}
}),
);
} catch (error) {
console.log(error);
}
};
}

Organization.init(ModelTypes, {
Expand Down
22 changes: 21 additions & 1 deletion src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { Server } from 'socket.io';
import Debug from 'debug';
import { connection } from './websocket';
import { startDataLayerUpdatePolling } from './datalayer';
import { pullPickListValues } from './utils/picklist-loader';
import { pullPickListValues } from './utils/data-loaders';
import { Organization } from './models';

const debug = Debug('climate-warehouse:server');

Expand Down Expand Up @@ -53,11 +54,30 @@ const syncPickList = () => {
pullPickListValues();
};

const subscribeToOrganizations = () => {
console.log('Subscribing to default organizations');
Organization.subscribeToDefaultOrganizations();
};

function onListening() {
syncPickList();
setInterval(async () => {
syncPickList();
}, 86400000 /* 1 day */);

try {
subscribeToOrganizations();
} catch (error) {
console.log(error);
}
setInterval(async () => {
try {
subscribeToOrganizations();
} catch (error) {
console.log(error);
}
}, 86400000 /* 1 day */);

const addr = server.address();
const bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port;
debug('Listening on ' + bind);
Expand Down
43 changes: 43 additions & 0 deletions src/utils/data-loaders.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import request from 'request-promise';
import dotenv from 'dotenv';
dotenv.config();

let downloadedPickList = {};
export const getPicklistValues = () => downloadedPickList;

export const pullPickListValues = async () => {
const options = {
method: 'GET',
url: process.env.PICKLIST_URL,
};

downloadedPickList = JSON.parse(await request(Object.assign({}, options)));
};

export const getDefaultOrganizationList = async () => {
const options = {
method: 'GET',
url: process.env.DEFAULT_ORGANIZATIONS,
};

return JSON.parse(await request(Object.assign({}, options)));
};

export const serverAvailable = async (server, port) => {
const options = {
method: 'GET',
url: `http://${server}:${port}`,
};

try {
await request(Object.assign({}, options));
return true;
} catch (err) {
if (JSON.stringify(err).includes('Python')) {
console.log('SERVER IS AVAILABLE');
return true;
} else {
return false;
}
}
};
15 changes: 0 additions & 15 deletions src/utils/picklist-loader.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/utils/validation-utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getPicklistValues } from './picklist-loader';
import { getPicklistValues } from './data-loaders';

export const pickListValidation = (field, name) => (value, helper) => {
const pickList = getPicklistValues();
Expand Down

0 comments on commit ee82340

Please sign in to comment.