Skip to content

Commit

Permalink
update (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
yesoreyeram authored Aug 12, 2021
1 parent dbac5ef commit 6209fb2
Show file tree
Hide file tree
Showing 12 changed files with 227 additions and 17 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

Change history of the project. All the feature updates, bug fixes, breaking changes will be documented here.

## [ 0.7.6 ]

- UnixTimeStamp variable added (alpha)
- Changed try url to [heroku](https://yesoreyeram-grafana.herokuapp.com/explore)

## [ 0.7.5 ]

- Fixed a bug where data not loading where no column names provided
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Grafana Infinity Datasource

Visualize data from JSON,CSV,XML,GraphQL and HTML endpoints.

[![click here for documentation](https://user-images.githubusercontent.com/153843/122958390-b4ef8900-d37a-11eb-9bb9-d8e32965507f.png)](https://yesoreyeram.github.io/grafana-infinity-datasource)

## Documentation
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "grafana-infinity-datasource",
"version": "0.7.5",
"version": "0.7.6",
"description": "JSON, CSV, XML, GraphQL & HTML datasource for Grafana. Do infinite things with Grafana.",
"main": "dist/module.js",
"scripts": {
Expand Down
25 changes: 25 additions & 0 deletions src/app/parsers/JSONParser.2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,28 @@ describe('Simple Object with number array, string array, object array', () => {
expect(tableResults.columns[1].text).toBe('moo');
});
});
describe('Nested object without array', () => {
const a = new JSONParser(
{
firstName: 'foo',
lastName: 'bar',
values: {
foo: 'foo1',
bar: 'bar1',
baz: 'baz1',
gaz: 'gaz1',
},
},
{ ...defaultTarget, root_selector: 'values' }
);
it('default test', () => {
const tableResults = a.toTable();
expect(tableResults.columns.length).toBe(4);
expect(tableResults.columns[0].text).toBe('foo');
expect(tableResults.columns[1].text).toBe('bar');
expect(tableResults.rows.length).toBe(1);
expect(tableResults.rows[0].length).toBe(4);
expect(tableResults.rows[0][0]).toBe('foo1');
expect(tableResults.rows[0][1]).toBe('bar1');
});
});
67 changes: 67 additions & 0 deletions src/app/variablesQuery/UnixTimeStamp.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { getFormatAndTimeShift, relativeTimeStampParse, shiftTime } from './UnixTimeStamp';

describe('getFormatAndTimeShift', () => {
it('getFormatAndTimeShift', () => {
expect(getFormatAndTimeShift([]).format).toBe('ms');
expect(getFormatAndTimeShift([]).timeShift).toBe('');
expect(getFormatAndTimeShift(['s']).format).toBe('s');
expect(getFormatAndTimeShift([]).timeShift).toBe('');
expect(getFormatAndTimeShift(['ms']).format).toBe('ms');
expect(getFormatAndTimeShift([]).timeShift).toBe('');
expect(getFormatAndTimeShift(['10d', 's']).format).toBe('s');
expect(getFormatAndTimeShift(['10d', 's']).timeShift).toBe('10d');
// expect(getFormatAndTimeShift(['10d']).format).toBe('ms'); // error
expect(getFormatAndTimeShift(['10d']).timeShift).toBe('10d');
});
});

describe('relativeTimeStampParse', () => {
it('relativeTimeStampParse test', () => {
expect(relativeTimeStampParse('30d').value).toBe(30);
expect(relativeTimeStampParse('30d').key).toBe('d');
expect(relativeTimeStampParse('-12M').value).toBe(-12);
expect(relativeTimeStampParse('-12M').key).toBe('M');
expect(relativeTimeStampParse('h').value).toBe(1);
expect(relativeTimeStampParse('h').key).toBe('h');
expect(relativeTimeStampParse('-h').value).toBe(-1);
expect(relativeTimeStampParse('-h').key).toBe('h');
});
});

const currentDate = new Date('2021-07-31T00:00:00.000Z').getTime();
describe('shiftTime', () => {
it('shiftTime', () => {
expect(new Date(shiftTime(currentDate, '')).toDateString()).toBe(new Date(currentDate).toDateString());
expect(new Date(shiftTime(currentDate, '0d')).toDateString()).toBe(new Date(currentDate).toDateString());
expect(new Date(shiftTime(currentDate, '-1d')).toDateString()).toBe(new Date('2021-Jul-30').toDateString());
expect(new Date(shiftTime(currentDate, '1d')).toDateString()).toBe(new Date('2021-Aug-1').toDateString());
expect(new Date(shiftTime(currentDate, 'd')).toDateString()).toBe(new Date('2021-Aug-1').toDateString());
expect(new Date(shiftTime(currentDate, '12d')).toDateString()).toBe(new Date('2021-Aug-12').toDateString());
expect(new Date(shiftTime(currentDate, '40d')).toDateString()).toBe(new Date('2021-Sep-9').toDateString());
expect(new Date(shiftTime(currentDate, '-40d')).toDateString()).toBe(new Date('2021-Jun-21').toDateString());
expect(new Date(shiftTime(currentDate, '-1h')).toISOString()).toBe(
new Date('2021-07-30T23:00:00.000Z').toISOString()
);
expect(new Date(shiftTime(currentDate, '1h')).toISOString()).toBe(
new Date('2021-07-31T01:00:00.000Z').toISOString()
);
expect(new Date(shiftTime(currentDate, '-1m')).toISOString()).toBe(
new Date('2021-07-30T23:59:00.000Z').toISOString()
);
expect(new Date(shiftTime(currentDate, '1m')).toISOString()).toBe(
new Date('2021-07-31T00:01:00.000Z').toISOString()
);
expect(new Date(shiftTime(currentDate, '-1s')).toISOString()).toBe(
new Date('2021-07-30T23:59:59.000Z').toISOString()
);
expect(new Date(shiftTime(currentDate, '1s')).toISOString()).toBe(
new Date('2021-07-31T00:00:01.000Z').toISOString()
);
expect(new Date(shiftTime(currentDate, '-1ms')).toISOString()).toBe(
new Date('2021-07-30T23:59:59.999Z').toISOString()
);
expect(new Date(shiftTime(currentDate, '1ms')).toISOString()).toBe(
new Date('2021-07-31T00:00:00.001Z').toISOString()
);
});
});
84 changes: 84 additions & 0 deletions src/app/variablesQuery/UnixTimeStamp.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { SelectableValue } from '@grafana/data';
import { replaceTokenFromVariable } from './utils';

// UnixTimeStamp(30d) --> (current timestamp +30d) in ms
// UnixTimeStamp(-30d) --> (current timestamp -30d) in ms
// UnixTimeStamp(30h,s) --> (current timestamp +30h) in s
// UnixTimeStamp(-30d,ms) --> (current timestamp -30d) in ms
// UnixTimeStamp(s) --> current timestamp in s
// UnixTimeStamp() --> current timestamp in ms

export const relativeTimeStampParse = (input: string): { value: number; key: string } => {
let numberParts = input.match(/[\-\d]+/g) || ['1'];
let textParts = input.match(/[A-Za-z]+/g) || ['h'];
return {
value: numberParts[0] === '-' ? -1 : +numberParts[0],
key: textParts[0],
};
};

export const shiftTime = (value: number, timeShift: string): number => {
if (timeShift) {
let relativeTimeStamp = relativeTimeStampParse(timeShift);
switch (relativeTimeStamp.key) {
case 'd':
value = value + relativeTimeStamp.value * (HOURS * MINUTES * SECONDS * MILLISECONDS);
break;
case 'h':
value = value + relativeTimeStamp.value * (MINUTES * SECONDS * MILLISECONDS);
break;
case 'm':
value = value + relativeTimeStamp.value * (SECONDS * MILLISECONDS);
break;
case 's':
value = value + relativeTimeStamp.value * MILLISECONDS;
break;
case 'ms':
value = value + relativeTimeStamp.value;
break;
default:
break;
}
}
return value;
};

export const getFormatAndTimeShift = (args: string[]): { format: string; timeShift: string } => {
return {
format: args.length === 1 ? args[0] || 'ms' : args[1] || 'ms',
timeShift: ['s', 'ms', 'seconds', 'milliseconds'].includes(args[0]) ? '' : args[0] || '',
};
};

export const getCurrentTime = () => new Date().getTime();

const HOURS = 24;
const MINUTES = 60;
const SECONDS = 60;
const MILLISECONDS = 1000;

export const UnixTimeStampVariable = (query: string): Array<SelectableValue<string>> => {
let value: number = getCurrentTime();
let args = replaceTokenFromVariable(query, 'UnixTimeStamp').split(',');
let format = getFormatAndTimeShift(args).format;
let timeShift = getFormatAndTimeShift(args).timeShift;
if (args[0] && !['s', 'ms', 'seconds', 'milliseconds'].includes(timeShift)) {
value = shiftTime(value, timeShift);
}
switch (format) {
case 'seconds':
case 's':
value = Math.round(value / 1000);
break;
case 'milliseconds':
case 'ms':
default:
value = value;
}
return [
{
value: query,
text: value,
},
];
};
3 changes: 3 additions & 0 deletions src/app/variablesQuery/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { CollectionVariable } from './Collection';
import { CollectionLookupVariable } from './CollectionLookup';
import { JoinVariable } from './Join';
import { RandomVariable } from './Random';
import { UnixTimeStampVariable } from './UnixTimeStamp';
import { Datasource } from './../../datasource';

const getTemplateVariablesFromResult = (res: any): Array<SelectableValue<string>> => {
Expand Down Expand Up @@ -118,6 +119,8 @@ export class LegacyVariableProvider implements VariableProvider {
resolve(JoinVariable(this.queryString));
} else if (this.queryString.startsWith('Random(') && this.queryString.endsWith(')')) {
resolve(RandomVariable(this.queryString));
} else if (this.queryString.startsWith('UnixTimeStamp(') && this.queryString.endsWith(')')) {
resolve(UnixTimeStampVariable(this.queryString));
} else {
resolve([]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/website/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export const Header = (props: HeaderProps) => {
<ul className="navbar-nav d-flex mb-2 mb-lg-0">
<a
className="nav-links special-menu highlight rounded text-white fw-bolder"
href="https://grafana.sriramajeyam.com"
href="https://yesoreyeram-grafana.herokuapp.com/explore"
target="_blank"
rel="noreferrer"
>
Expand Down
18 changes: 6 additions & 12 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ export default function Home({ data }: HomeProps) {
<Layout showSubMenu={false} title="">
<>
<div
className="position-relative overflow-hidden text-center"
className="position-relative overflow-hidden text-center mainbg"
style={{
width: '100%',
height: `${screenSize.height - 40}px`,
backgroundSize: 'cover',
backgroundColor: '#021E40',
backgroundColor: '#723961',
backgroundPositionX: '10%',
backgroundPositionY: '10%',
backgroundImage: `url(${data.site.siteMetadata.wallpaper})`,
backgroundImage: `radial-gradient(95.47% 139.72% at 49.52% 97.49%,#723961 0%,#AB575C 80%,#0E0A20 100%)`,
}}
>
<div
Expand All @@ -57,15 +57,15 @@ export default function Home({ data }: HomeProps) {
marginTop: `${screenSize.height / 4}px`,
}}
>
<h1 className="display-4 fw-bolder text-white" id="banner-title">
<h1 className="display-4 fw-bolder" id="banner-title">
Grafana Infinity Datasource
</h1>
<br />
<p className="fw-normal text-white">
<span className="fw-bold">Do infinite things with Grafana.</span>
<br />
<br />
<span className="text-secondary">
<span style={{ color: '#ccc' }}>
Visualize data from JSON, CSV, XML, GraphQL, HTML &amp; REST APIs. Also turns any website into grafana
dashboard.
</span>
Expand All @@ -81,7 +81,7 @@ export default function Home({ data }: HomeProps) {
<a
className="btn btn-primary text-black mx-4"
style={{ backgroundImage: 'linear-gradient(#FADE2A,#F05A28)', color: 'black', border: 'none' }}
href="https://grafana.sriramajeyam.com"
href="https://yesoreyeram-grafana.herokuapp.com/explore"
target="_blank"
rel="noreferrer"
>
Expand Down Expand Up @@ -127,12 +127,6 @@ export default function Home({ data }: HomeProps) {
alt="Grafana Infinity datasource is maintained"
/>
</a>
<a
href="https://codeclimate.com/github/yesoreyeram/grafana-infinity-datasource/maintainability"
className="mx-2"
>
<img src="https://api.codeclimate.com/v1/badges/7e2ae1bce7310890065c/maintainability" />
</a>
</p>
</>
</Layout>
Expand Down
25 changes: 23 additions & 2 deletions src/styles/website.scss
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,7 @@ img[src*='#center'] {
}

/* subheader */

#banner-title:hover,
#banner-title,
#topnav .nav-link:hover {
background: linear-gradient(268deg, #fade2a, #f05a28);
background-clip: text;
Expand All @@ -156,6 +155,9 @@ img[src*='#center'] {
-moz-animation: SubHeader 4s ease infinite;
animation: SubHeader 4s ease infinite;
}
#banner-title {
color: transparent;
}
#topnav .special-menu {
padding: 5px 8px;
margin-inline-start: 5px;
Expand Down Expand Up @@ -259,3 +261,22 @@ code {
}
}
}

.mainbg {
opacity: 0;
animation-name: mainbg-animation;
animation-iteration-count: 1;
animation-duration: 0.9s;
animation-delay: 0s;
animation-fill-mode: forwards;
}

@keyframes mainbg-animation {
0% {
opacity: 0;
animation-timing-function: cubic-bezier(0, 0, 0.5, 1);
}
100% {
opacity: 1;
}
}
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ export interface SecureField {
value: string;
configured: boolean;
}
export type VariableTokenLegacy = 'Collection' | 'CollectionLookup' | 'Random' | 'Join';
export type VariableTokenLegacy = 'Collection' | 'CollectionLookup' | 'Random' | 'Join' | 'UnixTimeStamp';
export type InfinityInstanceSettings = DataSourceInstanceSettings<InfinityDataSourceJSONOptions>;

export const DefaultInfinityQuery: InfinityQuery = {
Expand Down
9 changes: 9 additions & 0 deletions src/wiki/template-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,12 @@ Example : `Join($Environment,-hello-,$ServerName)` will produce a new string var
Example : `Random(A,B,C)` will produce one of A/B/C. When creating a variable of this type, set it to refresh "on time range change", so it will produce random element when dashboard refreshes.

More details available in [this github issue](https://github.com/yesoreyeram/grafana-infinity-datasource/issues/4).

## UnixTimeStamp (alpha) - (Legacy variable)

Return relative timestamp in unix timestamp.

| Variable | Output |
| ------------------ | ------------------------------- |
| `UnixTimeStamp()` | Current browser timestamp in ms |
| `UnixTimeStamp(s)` | Current browser timestamp in s |

1 comment on commit 6209fb2

@vercel
Copy link

@vercel vercel bot commented on 6209fb2 Aug 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.