Skip to content

Commit

Permalink
fix: code style and comment issues
Browse files Browse the repository at this point in the history
  • Loading branch information
chejimmy committed Jun 7, 2024
1 parent c4e8eb9 commit 53eba76
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 43 deletions.
8 changes: 4 additions & 4 deletions src/RelativeRangeRequestCache/RelativeRangeCache.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FieldType, LoadingState, dateTime } from '@grafana/data';
import { RelativeRangeCache } from 'RelativeRangeRequestCache/RelativeRangeCache';
import { QueryType, SiteWiseTimeOrder } from 'types';
import { parseSiteWiseRequestCacheId } from './cacheIdUtils';
import { generateSiteWiseRequestCacheId } from './cacheIdUtils';

describe('RelativeRangeCache', () => {
const requestId = 'mock-request-id';
Expand Down Expand Up @@ -146,7 +146,7 @@ describe('RelativeRangeCache', () => {
];

const cacheData = {
[parseSiteWiseRequestCacheId(request)]: {
[generateSiteWiseRequestCacheId(request)]: {
queries: cachedQueryInfo,
range,
},
Expand Down Expand Up @@ -320,7 +320,7 @@ describe('RelativeRangeCache', () => {
];

const cacheData = {
[parseSiteWiseRequestCacheId(requestDescending)]: {
[generateSiteWiseRequestCacheId(requestDescending)]: {
queries: cachedQueryInfoDescending,
range,
},
Expand Down Expand Up @@ -463,7 +463,7 @@ describe('RelativeRangeCache', () => {
];

const cacheData = {
[parseSiteWiseRequestCacheId(request)]: {
[generateSiteWiseRequestCacheId(request)]: {
queries: cachedQueryInfo,
range,
},
Expand Down
6 changes: 3 additions & 3 deletions src/RelativeRangeRequestCache/RelativeRangeCache.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DataFrame, DataQueryRequest, DataQueryResponse, LoadingState, TimeRange } from '@grafana/data';
import { isTimeRangeCoveringStart } from 'timeRangeUtils';
import { SitewiseQuery } from 'types';
import { RequestCacheId, parseSiteWiseRequestCacheId } from './cacheIdUtils';
import { RequestCacheId, generateSiteWiseRequestCacheId } from './cacheIdUtils';
import { CachedQueryInfo, TIME_SERIES_QUERY_TYPES } from './types';
import { trimCachedQueryDataFramesAtStart, trimCachedQueryDataFramesEnding } from './dataFrameUtils';
import { getRefreshRequestRange, isCacheableTimeRange } from './timeRangeUtils';
Expand Down Expand Up @@ -51,7 +51,7 @@ export class RelativeRangeCache {
return;
}

const requestCacheId = parseSiteWiseRequestCacheId(request);
const requestCacheId = generateSiteWiseRequestCacheId(request);

const queryIdMap = new Map(targets.map(q => [q.refId, q]));

Expand Down Expand Up @@ -117,7 +117,7 @@ export class RelativeRangeCache {
* @returns Cached data info if found, undefined otherwise
*/
private lookupCachedData(request: DataQueryRequest<SitewiseQuery>) {
const requestCacheId = parseSiteWiseRequestCacheId(request);
const requestCacheId = generateSiteWiseRequestCacheId(request);
const cachedDataInfo = this.responseDataMap.get(requestCacheId);

return cachedDataInfo;
Expand Down
16 changes: 8 additions & 8 deletions src/RelativeRangeRequestCache/cacheIdUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { QueryType, SiteWiseQuality, SiteWiseResolution, SiteWiseResponseFormat, SiteWiseTimeOrder } from 'types';
import { parseSiteWiseQueriesCacheId, parseSiteWiseRequestCacheId } from './cacheIdUtils';
import { generateSiteWiseQueriesCacheId, generateSiteWiseRequestCacheId } from './cacheIdUtils';
import { dateTime } from '@grafana/data';
import { SitewiseQueriesUnion } from './types';

Expand Down Expand Up @@ -30,9 +30,9 @@ function createSiteWiseQuery(id: number): SitewiseQueriesUnion {
};
}

describe('parseSiteWiseQueriesCacheId()', () => {
describe('generateSiteWiseQueriesCacheId()', () => {
it('parses SiteWise Queries into cache Id', () => {
const actualId = parseSiteWiseQueriesCacheId([createSiteWiseQuery(1), createSiteWiseQuery(2)]);
const actualId = generateSiteWiseQueriesCacheId([createSiteWiseQuery(1), createSiteWiseQuery(2)]);
const expectedId = JSON.stringify([
'["PropertyValueHistory","us-west-2","table","mock-asset-id-1",["mock-asset-id-1"],"mock-property-id-1","mock-property-alias-1","ANY","AUTO",true,true,1000,"grafana-iot-sitewise-datasource","mock-datasource-uid","ASCENDING",true,"mock-hierarchy-1","mock-model-1","ALL"]',
'["PropertyValueHistory","us-west-2","table","mock-asset-id-2",["mock-asset-id-2"],"mock-property-id-2","mock-property-alias-2","ANY","AUTO",true,true,1000,"grafana-iot-sitewise-datasource","mock-datasource-uid","ASCENDING",true,"mock-hierarchy-2","mock-model-2","ALL"]'
Expand Down Expand Up @@ -68,8 +68,8 @@ describe('parseSiteWiseQueriesCacheId()', () => {
queryType: QueryType.PropertyValue,
};

const order1 = parseSiteWiseQueriesCacheId([query2, query1]);
const order2 = parseSiteWiseQueriesCacheId([query1, query2]);
const order1 = generateSiteWiseQueriesCacheId([query2, query1]);
const order2 = generateSiteWiseQueriesCacheId([query1, query2]);

expect(order1).toEqual(order2);
});
Expand All @@ -80,7 +80,7 @@ describe('parseSiteWiseQueriesCacheId()', () => {
refId: 'A-1',
queryType: QueryType.ListAssets,
};
const actualId = parseSiteWiseQueriesCacheId([query]);
const actualId = generateSiteWiseQueriesCacheId([query]);
const expectedId = JSON.stringify([
'["ListAssets",null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null]',
]);
Expand All @@ -89,7 +89,7 @@ describe('parseSiteWiseQueriesCacheId()', () => {
});
});

describe('parseSiteWiseRequestCacheId()', () => {
describe('generateSiteWiseRequestCacheId()', () => {
it('parses SiteWise Queries into cache Id', () => {
const request = {
requestId: 'mock-request-id',
Expand Down Expand Up @@ -117,6 +117,6 @@ describe('parseSiteWiseRequestCacheId()', () => {
])
]);

expect(parseSiteWiseRequestCacheId(request)).toEqual(expectedId);
expect(generateSiteWiseRequestCacheId(request)).toEqual(expectedId);
});
});
10 changes: 5 additions & 5 deletions src/RelativeRangeRequestCache/cacheIdUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ import { SitewiseQueriesUnion } from './types';

export type RequestCacheId = string;

export function parseSiteWiseRequestCacheId(request: DataQueryRequest<SitewiseQueriesUnion>): RequestCacheId {
export function generateSiteWiseRequestCacheId(request: DataQueryRequest<SitewiseQueriesUnion>): RequestCacheId {
const { targets, range: { raw: { from } } } = request;

return JSON.stringify([from, parseSiteWiseQueriesCacheId(targets)]);
return JSON.stringify([from, generateSiteWiseQueriesCacheId(targets)]);
}

type QueryCacheId = string;

export function parseSiteWiseQueriesCacheId(queries: SitewiseQueriesUnion[]): QueryCacheId {
const cacheIds = queries.map(parseSiteWiseQueryCacheId).sort();
export function generateSiteWiseQueriesCacheId(queries: SitewiseQueriesUnion[]): QueryCacheId {
const cacheIds = queries.map(generateSiteWiseQueryCacheId).sort();

return JSON.stringify(cacheIds);
}

/**
* Parse query to cache id.
*/
function parseSiteWiseQueryCacheId(query: SitewiseQueriesUnion): QueryCacheId {
function generateSiteWiseQueryCacheId(query: SitewiseQueriesUnion): QueryCacheId {
const {
queryType,
region,
Expand Down
13 changes: 5 additions & 8 deletions src/RelativeRangeRequestCache/dataFrameUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ import { trimTimeSeriesDataFrame, trimTimeSeriesDataFrameReversedTime } from 'da
* For property value queries, it will return an empty data frame.
* For all other queries, it will return the trimmed data frame.
*
* @param cachedQueryInfos - Cached query infos
* @param queryType - Query type
* @param cachedQueryInfos - Cached query infos
* @param cacheRange - Cache range
* @param cachedQueryInfos - Cached query infos to trim
* @param cacheRange - Cache range to include
* @returns Trimmed data frames
*/
export function trimCachedQueryDataFramesAtStart(cachedQueryInfos: CachedQueryInfo[], cacheRange: AbsoluteTimeRange): DataFrame[] {
Expand Down Expand Up @@ -64,10 +62,9 @@ export function trimCachedQueryDataFramesAtStart(cachedQueryInfos: CachedQueryIn
* For descending ordered data frames, it will return the trimmed data frame.
* For all other queries, it will return an empty data frame.
*
* @param cachedQueryInfos - Cached query infos
* @param cachedQueryInfos
* @param cacheRange
* @returns
* @param cachedQueryInfos - Cached query infos to trim
* @param cacheRange - Cache range to include
* @returns Trimmed data frames
*/
export function trimCachedQueryDataFramesEnding(cachedQueryInfos: CachedQueryInfo[], cacheRange: AbsoluteTimeRange): DataFrame[] {
return cachedQueryInfos
Expand Down
2 changes: 1 addition & 1 deletion src/SiteWiseQueryPaginator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class SitewiseQueryPaginator {
try {
let retrievedData = cachedResponse?.start?.data;
let nextQueries: SitewiseNextQuery[] | undefined;
let errorEncountered = false; // whether there's a error response
const errorEncountered = false; // whether there's a error response
let count = 1;

do {
Expand Down
2 changes: 1 addition & 1 deletion src/components/browser/AssetBrowser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class AssetBrowser extends Component<Props, State> {
}

async componentDidUpdate(oldProps: Props) {
let update: State = { ...this.state };
const update: State = { ...this.state };
let shouldUpdate = false;

if (this.props.region !== oldProps.region) {
Expand Down
20 changes: 10 additions & 10 deletions src/dataFrameUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ interface TrimParams {

/**
* Trim the time series data frame to the specified time range.
* @param param0 - The parameters for trimming the data frame.
* @param param0.dataFrame - The data frame to trim.
* @param param0.timeRange - The time range to trim to.
* @param param0.lastObservation - Whether to include the last observation in the range.
* @param trimParams - The parameters for trimming the data frame.
* @param trimParams.dataFrame - The data frame to trim.
* @param trimParams.timeRange - The time range to trim to.
* @param trimParams.lastObservation - Whether to include the last observation in the range.
* @returns The trimmed data frame.
*/
export function trimTimeSeriesDataFrame({
Expand All @@ -34,7 +34,7 @@ export function trimTimeSeriesDataFrame({
return dataFrame;
}

let timeValues = timeField.values.toArray();
const timeValues = timeField.values.toArray();

let fromIndex = timeValues.findIndex(time => time > from); // from is exclusive
if (fromIndex === -1) {
Expand Down Expand Up @@ -65,10 +65,10 @@ export function trimTimeSeriesDataFrame({

/**
* Trim the time series data frame to the specified time range where the time field is in reversed order.
* @param param0 - The parameters for trimming the data frame.
* @param param0.dataFrame - The data frame to trim.
* @param param0.timeRange - The time range to trim to.
* @param param0.lastObservation - Whether to include the last observation in the range.
* @param trimParams - The parameters for trimming the data frame.
* @param trimParams.dataFrame - The data frame to trim.
* @param trimParams.timeRange - The time range to trim to.
* @param trimParams.lastObservation - Whether to include the last observation in the range.
* @returns The trimmed data frame.
*/
export function trimTimeSeriesDataFrameReversedTime({
Expand All @@ -92,7 +92,7 @@ export function trimTimeSeriesDataFrameReversedTime({
}

// Copy before reverse in place
let timeValues = [...timeField.values.toArray()].reverse();
const timeValues = [...timeField.values.toArray()].reverse();

let fromIndex = timeValues.findIndex(time => time > from); // from is exclusive
if (fromIndex === -1) {
Expand Down
2 changes: 1 addition & 1 deletion src/sitewiseCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ export function frameToAssetInfo(res: DescribeAssetResult): AssetInfo {
}

export function assetSummaryToAssetInfo(res: DataFrameView<AssetSummary>): AssetInfo[] {
let results: AssetInfo[] = [];
const results: AssetInfo[] = [];

for (const info of res.toArray()) {
const hierarchy: AssetPropertyInfo[] = JSON.parse(info.hierarchies); // has Id, Name
Expand Down
4 changes: 2 additions & 2 deletions src/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class SitewiseVariableSupport extends CustomVariableSupport<DataSource, S

query(request: DataQueryRequest<SitewiseQuery>): Observable<DataQueryResponse> {
assign(request.targets, [{ ...request.targets[0], refId: 'A' }]);
let response = this.datasource.query(request);
const response = this.datasource.query(request);
switch (request.targets[0].queryType) {
case QueryType.ListAssetModels:
case QueryType.ListAssets:
Expand All @@ -39,7 +39,7 @@ export class SitewiseVariableSupport extends CustomVariableSupport<DataSource, S
return {data: new DataFrameView<AssetModelSummary>(data)};
}),
map((res) => {
let newData = res.data.map((m)=>{
const newData = res.data.map((m)=>{
return {
value: m.id,
text: m.name,
Expand Down

0 comments on commit 53eba76

Please sign in to comment.