Skip to content

Commit

Permalink
feat(server): add statistic version
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce committed Nov 10, 2019
1 parent 6b4d287 commit c2dc676
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/server/src/api/statistic-definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ const definitions = {
};

// Keep the export separate from declaration to enable tsc to typecheck the `@type` annotation.
module.exports = definitions;
module.exports = {definitions, VERSION: 1};
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @license Copyright 2019 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';

/* eslint-disable new-cap */

module.exports = {
/**
* @param {import('sequelize').QueryInterface} queryInterface
* @param {typeof import('sequelize')} Sequelize
*/
up: async (queryInterface, Sequelize) => {
await queryInterface.addColumn('statistics', 'version', {type: Sequelize.NUMERIC(8, 2)});
await queryInterface.bulkUpdate('statistics', {version: 1}, {version: null});
},
/**
* @param {import('sequelize').QueryInterface} queryInterface
*/
down: async queryInterface => {
await queryInterface.removeColumn('statistics', 'version');
},
};
2 changes: 2 additions & 0 deletions packages/server/src/api/storage/sql/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ class SqlStorageMethod {
if (!buildModelDefn.attributes.projectId.references) throw new Error('Invalid buildModel');
if (!runModelDefn.attributes.projectId.references) throw new Error('Invalid runModel');
if (!runModelDefn.attributes.buildId.references) throw new Error('Invalid runModel');
if (!statisticModelDefn.attributes.projectId.references) throw new Error('Invalid runModel');
if (!statisticModelDefn.attributes.buildId.references) throw new Error('Invalid runModel');

const sequelize = createSequelize(options);

Expand Down
20 changes: 12 additions & 8 deletions packages/server/src/api/storage/sql/statistic-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@ const Sequelize = require('sequelize');
/** @type {import('sequelize').Model<any, any>} */
const ModelRef = /** @type {any} */ (undefined);

/** @type {LHCI.ServerCommand.TableDefinition<LHCI.ServerCommand.Statistic>} */
const attributes = {
id: {type: Sequelize.UUID(), primaryKey: true},
projectId: {type: Sequelize.UUID(), references: {model: ModelRef, key: 'id'}},
buildId: {type: Sequelize.UUID(), references: {model: ModelRef, key: 'id'}},
version: {type: Sequelize.NUMERIC(8, 2)},
url: {type: Sequelize.STRING({length: 256})},
name: {type: Sequelize.STRING({length: 100})},
value: {type: Sequelize.NUMERIC(12, 4)},
};

module.exports = {
tableName: 'statistics',
attributes: {
id: {type: Sequelize.UUID(), primaryKey: true},
projectId: {type: Sequelize.UUID(), references: {model: ModelRef, key: 'id'}},
buildId: {type: Sequelize.UUID(), references: {model: ModelRef, key: 'id'}},
url: {type: Sequelize.STRING({length: 256})},
name: {type: Sequelize.STRING({length: 100})},
value: {type: Sequelize.NUMERIC(12, 4)},
},
attributes,
indexes: [],
};
6 changes: 5 additions & 1 deletion packages/server/src/api/storage/storage-method.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
const _ = require('@lhci/utils/src/lodash.js');
const PRandom = require('@lhci/utils/src/seed-data/prandom.js');
const {computeRepresentativeRuns} = require('@lhci/utils/src/representative-runs.js');
const statisticDefinitions = require('../statistic-definitions.js');
const {
definitions: statisticDefinitions,
VERSION: STATISTIC_VERSION,
} = require('../statistic-definitions.js');

class StorageMethod {
/**
Expand Down Expand Up @@ -254,6 +257,7 @@ class StorageMethod {
projectId,
buildId,
url,
version: STATISTIC_VERSION,
name,
value,
},
Expand Down
1 change: 1 addition & 0 deletions types/server.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ declare global {
id: string;
projectId: string;
buildId: string;
version: number;
url: string;
name: StatisticName;
value: number;
Expand Down

0 comments on commit c2dc676

Please sign in to comment.