Skip to content

Instantly share code, notes, and snippets.

@hinst
Created October 5, 2021 10:08
Show Gist options
  • Save hinst/5943f75d002eaa2a7398fe2181c81cff to your computer and use it in GitHub Desktop.
Save hinst/5943f75d002eaa2a7398fe2181c81cff to your computer and use it in GitHub Desktop.
Reproduce bug in InfluxDB: duplicated legacy mappings
import fs from 'fs';
import fetch from 'node-fetch';
class Mapping {
id: string;
}
class AppDoubleMapper {
influxUrl = 'http://192.168.0.4:8086'
accessToken = fs.readFileSync('./influx access token.txt').toString();
organizationName = 'HOME';
organizationId = '89228cc43c64ae59';
bucketName = 'FacilityDataPoint';
bucketId = '3a6cbe694a431712';
async run() {
await this.clearMappings();
await Promise.all([
this.createMapping(),
this.createMapping()
]);
}
private get headers() {
return { Authorization: 'Token ' + this.accessToken };
}
private async readMappings(): Promise<Mapping[]> {
const url = this.influxUrl + '/api/v2/dbrps?orgID=' + encodeURIComponent(this.organizationId);
const response = await fetch(url, { headers: this.headers });
const mappings = (await response.json()).content;
return mappings;
}
private async clearMappings() {
const mappings = await this.readMappings();
if (mappings.length == 0)
console.log('No mappings');
for (const mapping of mappings) {
const url = this.influxUrl + '/api/v2/dbrps' +
'/' + encodeURIComponent(mapping.id) +
'?orgID=' + encodeURIComponent(this.organizationId);
const response = await fetch(url, {
method: 'DELETE',
headers: this.headers
});
console.log('Deleted mapping ' + url + ' ' + response.statusText);
}
}
private async createMapping() {
const url = this.influxUrl + '/api/v2/dbrps';
const bodyObject = {
database: this.organizationName,
retention_policy: this.bucketName,
orgID: this.organizationId,
bucketID: this.bucketId,
default: false,
};
const response = await fetch(url, {
method: 'POST',
headers: this.headers,
body: JSON.stringify(bodyObject)
});
console.log('Create mapping ' + url + ' ' + response.statusText);
if (!response.ok)
console.warn(await response.text());
}
}
new AppDoubleMapper().run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment