Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:Chia-Network/climate-warehouse i…
Browse files Browse the repository at this point in the history
…nto develop
  • Loading branch information
MichaelTaylor3D committed Aug 10, 2022
2 parents 5e1b7da + 54915bc commit 8b0d183
Show file tree
Hide file tree
Showing 12 changed files with 419 additions and 3 deletions.
34 changes: 34 additions & 0 deletions src/controllers/fileStore.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,40 @@ import _ from 'lodash';
import crypto from 'crypto';
import { FileStore } from '../models';

export const subscribeToFileStore = (req, res) => {
try {
const { orgUid } = req.body;

FileStore.subscribeToFileStore(orgUid);

res.status(200).json({
message: `${orgUid} subscribed to file store.`,
});
} catch (error) {
res.status(400).json({
message: `Can not subscribe to file store.`,
error: error.message,
});
}
};

export const unsubscribeFromFileStore = (req, res) => {
try {
const { orgUid } = req.body;

FileStore.unsubscribeFileStore(orgUid);

res.status(200).json({
message: `Can not unsubscribe the fileStore from ${orgUid}`,
});
} catch (error) {
res.status(400).json({
message: 'Can not retreive file list from filestore',
error: error.message,
});
}
};

export const getFileList = async (req, res) => {
try {
const files = await FileStore.getFileStoreList();
Expand Down
227 changes: 227 additions & 0 deletions src/database/migrations/20220808192709-populate-units-fts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
'use strict';

export default {
async up(queryInterface) {
if (queryInterface.sequelize.getDialect() === 'sqlite') {
await queryInterface.dropTable('units_fts');
await queryInterface.sequelize.query(`
CREATE VIRTUAL TABLE units_fts USING fts5(
warehouseUnitId,
issuanceId,
projectLocationId,
orgUid,
unitOwner,
countryJurisdictionOfOwner,
inCountryJurisdictionOfOwner,
serialNumberBlock,
vintageYear,
unitType,
marketplace,
marketplaceLink,
marketplaceIdentifier,
unitTags,
unitStatus,
unitStatusReason,
unitRegistryLink,
correspondingAdjustmentDeclaration,
correspondingAdjustmentStatus,
unitBlockStart,
unitBlockEnd,
unitCount,
timeStaged
);
`);
await queryInterface.sequelize.query(
`INSERT INTO units_fts SELECT
warehouseUnitId,
issuanceId,
projectLocationId,
orgUid,
unitOwner,
countryJurisdictionOfOwner,
inCountryJurisdictionOfOwner,
serialNumberBlock,
vintageYear,
unitType,
marketplace,
marketplaceLink,
marketplaceIdentifier,
unitTags,
unitStatus,
unitStatusReason,
unitRegistryLink,
correspondingAdjustmentDeclaration,
correspondingAdjustmentStatus,
unitBlockStart,
unitBlockEnd,
unitCount,
timeStaged
FROM units`,
);

await queryInterface.sequelize.query(`
CREATE TRIGGER unit_insert_fts AFTER INSERT ON units BEGIN
INSERT INTO units_fts(
warehouseUnitId,
issuanceId,
projectLocationId,
orgUid,
unitOwner,
countryJurisdictionOfOwner,
inCountryJurisdictionOfOwner,
serialNumberBlock,
vintageYear,
unitType,
marketplace,
marketplaceLink,
marketplaceIdentifier,
unitTags,
unitStatus,
unitStatusReason,
unitRegistryLink,
correspondingAdjustmentDeclaration,
correspondingAdjustmentStatus,
unitBlockStart,
unitBlockEnd,
unitCount
) VALUES (
new.warehouseUnitId,
new.issuanceId,
new.projectLocationId,
new.orgUid,
new.unitOwner,
new.countryJurisdictionOfOwner,
new.inCountryJurisdictionOfOwner,
new.serialNumberBlock,
new.vintageYear,
new.unitType,
new.marketplace,
new.marketplaceLink,
new.marketplaceIdentifier,
new.unitTags,
new.unitStatus,
new.unitStatusReason,
new.unitRegistryLink,
new.correspondingAdjustmentDeclaration,
new.correspondingAdjustmentStatus,
new.unitBlockStart,
new.unitBlockEnd,
new.unitCount
);
END;`);

await queryInterface.sequelize.query(`
CREATE TRIGGER unit_delete_fts AFTER DELETE ON units BEGIN
DELETE FROM units_fts WHERE warehouseUnitId = old.warehouseUnitId;
END;
`);

await queryInterface.sequelize.query(`
CREATE TRIGGER unit_update_fts AFTER UPDATE ON units BEGIN
DELETE FROM units_fts WHERE warehouseUnitId = old.warehouseUnitId;
INSERT INTO units_fts(
warehouseUnitId,
issuanceId,
projectLocationId,
orgUid,
unitOwner,
countryJurisdictionOfOwner,
inCountryJurisdictionOfOwner,
serialNumberBlock,
vintageYear,
unitType,
marketplace,
marketplaceLink,
marketplaceIdentifier,
unitTags,
unitStatus,
unitStatusReason,
unitRegistryLink,
correspondingAdjustmentDeclaration,
correspondingAdjustmentStatus,
unitBlockStart,
unitBlockEnd,
unitCount
) VALUES (
new.warehouseUnitId,
new.issuanceId,
new.projectLocationId,
new.orgUid,
new.unitOwner,
new.countryJurisdictionOfOwner,
new.inCountryJurisdictionOfOwner,
new.serialNumberBlock,
new.vintageYear,
new.unitType,
new.marketplace,
new.marketplaceLink,
new.marketplaceIdentifier,
new.unitTags,
new.unitStatus,
new.unitStatusReason,
new.unitRegistryLink,
new.correspondingAdjustmentDeclaration,
new.correspondingAdjustmentStatus,
new.unitBlockStart,
new.unitBlockEnd,
new.unitCount
);
END;
`);
}
},

async down(queryInterface) {
await queryInterface.dropTable('units_fts');
await queryInterface.sequelize.query(`
CREATE VIRTUAL TABLE units_fts USING fts5(
warehouseProjectId,
orgUid,
currentRegistry,
projectId,
registryOfOrigin,
originProjectId,
program,
projectName,
projectLink,
projectDeveloper,
sector,
coveredByNDC,
projectType,
projectTags,
ndcInformation,
projectStatus,
projectStatusDate,
unitMetric,
methodology,
validationBody,
validationDate,
timeStaged
);
`);
await queryInterface.sequelize.query(
`INSERT INTO units_fts SELECT warehouseUnitId,
orgUid,
currentRegistry,
projectId,
registryOfOrigin,
originProjectId,
program,
projectName,
projectLink,
projectDeveloper,
sector,
coveredByNDC,
projectType,
projectTags,
ndcInformation,
projectStatus,
projectStatusDate,
unitMetric,
methodology,
validationBody,
validationDate,
timeStaged FROM units`,
);
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

export default {
async up(queryInterface, Sequelize) {
await Promise.all(
['organizations'].map((table) => {
queryInterface.addColumn(table, 'fileStoreSubscribed', {
type: Sequelize.STRING,
allowNull: true,
});
}),
);
},

async down(queryInterface) {
await Promise.all(
['organizations'].map((table) => {
queryInterface.removeColumn(table, 'fileStoreSubscribed');
}),
);
},
};
10 changes: 10 additions & 0 deletions src/database/migrations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import RepopulateVirtualTables from './20220515223227-re-populate-virtual-tables
import AddAuthorColumnToAuditTable from './20220708210357-adding-author-column-to-audit-table';
import CreateFileStore from './20220724212553-create-file-store';
import AddOptionalMethodology2FieldToProject from './20220721212845-add-optional-methodology2-field-to-project';
import AddFiltStoreSubscribedColumnToProject from './20220809182156-AddFileStoreSubscribedColumn';
import PopulateUnitsFTS from './20220808192709-populate-units-fts';

export const migrations = [
{
Expand Down Expand Up @@ -139,4 +141,12 @@ export const migrations = [
migration: AddOptionalMethodology2FieldToProject,
name: '20220721212845-add-optional-methodology2-field-to-project',
},
{
migration: AddFiltStoreSubscribedColumnToProject,
name: '20220724161782-add-file-store-subscribed-column-to-project',
},
{
migration: PopulateUnitsFTS,
name: '20220808192709-populate-units-fts',
},
];
29 changes: 29 additions & 0 deletions src/datalayer/persistance.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,35 @@ export const dataLayerAvailable = async () => {
}
};

export const unsubscribeFromDataLayerStore = async (storeId) => {
const options = {
url: `${rpcUrl}/unsubscribe`,
body: JSON.stringify({
id: storeId,
}),
};

logger.info(`RPC Call: ${rpcUrl}/unsubscribe ${storeId}`);

try {
const response = await request(
Object.assign({}, getBaseOptions(), options),
);

const data = JSON.parse(response);

if (Object.keys(data).includes('success') && data.success) {
logger.info(`Successfully UnSubscribed: ${storeId}`);
return data;
}

return false;
} catch (error) {
logger.info(`Error UnSubscribing: ${error}`);
return false;
}
};

export const subscribeToStoreOnDataLayer = async (storeId, ip, port) => {
const options = {
url: `${rpcUrl}/subscribe`,
Expand Down
7 changes: 7 additions & 0 deletions src/datalayer/syncService.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ const dataLayerWasUpdated = async () => {
return updateStoreInfo;
};

const unsubscribeFromDataLayerStore = async (storeId, ip, port) => {
if (!USE_SIMULATOR) {
return dataLayer.unsubscribeFromDataLayerStore(storeId, ip, port);
}
};

const subscribeToStoreOnDataLayer = async (storeId, ip, port) => {
if (USE_SIMULATOR) {
return simulator.subscribeToStoreOnDataLayer(storeId, ip, port);
Expand Down Expand Up @@ -333,4 +339,5 @@ export default {
getStoreIfUpdated,
POLLING_INTERVAL,
getCurrentStoreData,
unsubscribeFromDataLayerStore,
};
Loading

0 comments on commit 8b0d183

Please sign in to comment.