I have a postgres table something like this.
+----+-----------+----------------------+--------+
| id | Key | Value | userId |
+----+-----------+----------------------+--------+
| 1 | email | thomas#reggi.com | 1 |
| 2 | firstName | thomas | 1 |
| 3 | lastName | reggi | 1 |
| 4 | email | new.thomas#reggi.com | 1 |
+----+-----------+----------------------+--------+
I'm looking for a way to "reduce" this table down to a json object.
{
"email": "new.thomas#reggi.com",
"firstName": "thomas",
"lastName": "reggi"
}
How close can I get to this with just using postgres?
If the table is called data, try this (jsonb_pretty is just for display purposes):
SELECT jsonb_pretty(
jsonb_object_agg(key, value ORDER BY id)
)
FROM data
WHERE userid = 1;
┌──────────────────────────────────────┐
│ jsonb_pretty │
├──────────────────────────────────────┤
│ { ↵│
│ "email": "new.thomas#reggi.com",↵│
│ "lastName": "reggi", ↵│
│ "firstName": "thomas" ↵│
│ } │
└──────────────────────────────────────┘
(1 row)
This relies on the feature that jsonb does not keep duplicate keys.
It also relies on the fact that jsonb will always retain the last added key/value pair.
If you want to always have the latest value for a key, you could use a CTE and the RANK() window function:
SELECT * FROM p;
┌────┬───────────┬──────────────────────┬────────┬────────────────────────────┐
│ id │ key │ value │ userid │ modification_time │
├────┼───────────┼──────────────────────┼────────┼────────────────────────────┤
│ 1 │ email │ thomas#reggi.com │ 1 │ 2016-10-05 12:53:32.936704 │
│ 2 │ firstName │ thomas │ 1 │ 2016-10-05 12:53:32.936704 │
│ 3 │ lastName │ reggi │ 1 │ 2016-10-05 12:53:32.936704 │
│ 4 │ email │ new.thomas#reggi.com │ 1 │ 2016-11-06 15:53:48.025775 │
└────┴───────────┴──────────────────────┴────────┴────────────────────────────┘
(4 rows)
WITH info_with_rank_for_user AS (
SELECT userId,
modification_time,
value,
key,
RANK() OVER (PARTITION BY userId, key ORDER BY id DESC)
FROM p
)
SELECT userId,
json_object_agg(key, value),
MAX(modification_time) AS last_settings_modification_time
FROM info_with_rank_for_user
WHERE rank = 1
GROUP BY userId
;
┌────────┬────────────────────────────────────────────────────────────────────────────────────┬─────────────────────────────────┐
│ userid │ json_object_agg │ last_settings_modification_time │
├────────┼────────────────────────────────────────────────────────────────────────────────────┼─────────────────────────────────┤
│ 1 │ { "email" : "new.thomas#reggi.com", "firstName" : "thomas", "lastName" : "reggi" } │ 2016-11-06 15:53:48.025775 │
└────────┴────────────────────────────────────────────────────────────────────────────────────┴─────────────────────────────────┘
(1 row)
Related
i want to read file from my src path, but i failed, i can only read file from absolute path, not from my src relatively path,
this is my code , low is tree , my code is in src/ i want to read file in ../jsconfig/*.json, but it not work
│
├─.vscode
│ extensions.json
│ launch.json
│ tasks.json
│
├─images
│ anzhuang.jpg
│
├─jsconfig
│ BHDZ.json
│ BHJG.json
| default.json
│
├─media
│ code.js
│ codicon.ttf
│ index.html
│ jsconfig.json
│ style.css
│
├─pic
│ icon.jfif
│
├─src
│ dbfCustomEditor.js
│ dbfDocument.js
│ dbfEditorProvider.js
│ extension.js
│
└─test
function matchJsonFile(dbfName){
var patten1 = dbfName.slice(0, -4)
var patten2 = dbfName
var res = "../default.json"
for (var i=0;i<jsonFiles.length;i++){
if (jsonFiles[i].search(patten1) >= 0){
res = jsonFiles[i]
break
}
if (jsonFiles[i].search(patten2) >= 0){
res = jsonFiles[i]
break
}
}
return res
}
I'm building a website with subdomains. Each subdomain is mapped to a file-based page using middleware. Below is an example of how I'm mapping subdomains to pages:
app.com maps to /home
app.com/pricing maps to /home/pricing
subdomain1.app.com/dashboard maps to /_subdomains/subdomain1/dashboard
subdomain2.app.com/dashboard/home maps to /_subdomains/subdomain2/dashboard/home
app.com, subdomain1.app.com and subdomain1.app.com/dashboard/ and everything else are working fine, but when I try to access subdomain1.app.com/dashboard/home I'm getting 404 Not Found.
Here's my folder structure:
pages/
├── _subdomains/
│ └── [subdomain]/
│ ├── dashboard/
│ │ ├── home.tsx
│ │ └── index.tsx
│ └── index.tsx
├── home/
│ └── ...
└── _app.tsx
import { NextRequest, NextResponse } from 'next/server';
export const config = {
// I have a feeling this isn't matching /_subdomains/subdomain/dashboard/home
matcher: ['/', '/([^/.]*)', '/auth/([^/.]*)', '/_subdomains/:path*'],
};
export default async function middleware(req: NextRequest) {
const url = req.nextUrl;
const hostname = req.headers.get('host') || process.env.ROOT_DOMAIN;
if (!hostname) {
throw Error('Middleware -> No hostname');
}
const currentHost = hostname.replace(`.${process.env.ROOT_DOMAIN}`, '');
if (currentHost === process.env.ROOT_DOMAIN) {
url.pathname = `/home${url.pathname}`;
} else {
console.log(`/_subdomains/${currentHost}${url.pathname}`)
url.pathname = `/_subdomains/${currentHost}${url.pathname}`;
}
return NextResponse.rewrite(url);
}
I'm fairly certain it's the matcher that isn't working but I don't know why.
Matching /_subdomains/:path* should match /_subdomains/a/b/c according to the docs but it isn't working in this case. Or it could be another issue I'm not sure
Fixed by changing the matcher to
matcher: [
/*
* Match all paths except for:
* 1. /api routes
* 2. /_next (Next.js internals)
* 3. /fonts (inside /public)
* 4. /examples (inside /public)
* 5. all root files inside /public (e.g. /favicon.ico)
*/
"/((?!api|_next|fonts|examples|[\\w-]+\\.\\w+).*)",
],
Thanks to this
I'm trying to move from babel to swc for compiling and bundling a react component library but I have trouble with the configuration.
When I run npm run spack, I get the following error:
thread '<unnamed>' panicked at 'internal error: entered unreachable code: module item found but is_es6 is false: ExportNamed(NamedExport { span: Span { lo: BytePos(954874), hi: BytePos(954914), ctxt: #0 }, specifiers: [Named(ExportNamedSpecifier { span: Span { lo: BytePos(954883), hi: BytePos(954890), ctxt: #0 }, orig: Ident(Ident { span: Span { lo: BytePos(954883), hi: BytePos(954890), ctxt: #4141 }, sym: Atom('default' type=static), optional: false }), exported: Some(Ident(Ident { span: Span { lo: BytePos(954883), hi: BytePos(954890), ctxt: #194 }, sym: Atom('default' type=static), optional: false })), is_type_only: false })], src: Some(Str { span: Span { lo: BytePos(954898), hi: BytePos(954913), ctxt: #0 }, value: Atom('./FormControl' type=dynamic), raw: Some(Atom(''./FormControl'' type=dynamic)) }), type_only: false, asserts: None })', crates/swc_bundler/src/bundler/chunk/cjs.rs:142:29
What I get from this error is that he fails to bundle React components. I can't find the is_es6 configuration mentioned in the error so I'm not sure how to solve this. I tried rereading the doc of swc without any success. The module part of the config doesn't seem to solve my problem.
Here is my working tree:
.
├── jest.config.ts
├── package-lock.json
├── package.json
├── spack.config.js
└── src
├── components
│ ├── FiltersBar
│ │ ├── FiltersBar.test.tsx
│ │ ├── FiltersBar.tsx
│ │ ├── __snapshots__
│ │ │ └── FiltersBar.test.tsx.snap
│ │ └── index.ts
│ └── index.ts
├── index.ts
└── libraries
├── helpers
│ ├── helpers.test.ts
│ ├── helpers.ts
│ └── index.ts
└── index.ts
Here is my .swcrc file:
{
"jsc": {
"target": "es2021",
"parser": {
"syntax": "typescript"
}
},
"module": {
"type": "commonjs"
}
}
I'm pretty new to all this stuff so please bear with me :)
Currently I am posting to Slack when the test fails. but I wanna be able to add the report at the end of the test as well. So when all the feature files are done and we reported all the broken tests. I wanna be able to report this as well
When cypress gets to run in command line npm run cypress run it generates the below test report after all the tests are done. how can I access that report and post it in slack? and also how can I send a message after all the tests are done and confirm the tests has been finished
Spec Tests Passing
Failing Pending Skipped
┌─────────────────────────────────────────────────────────
├────────────────────────────────────────────────────────────────────────────────────────────────┤
│ ✔ 176.feature 00:31 1 1 - - - │
├────────────────────────────────────────────────────────────────────────────────────────────────┤
│ ✔ 185.feature 00:07 1 1 - - - │
├────────────────────────────────────────────────────────────────────────────────────────────────┤
│ ✔ 186.feature 00:11 1 1 - - - │
├────────────────────────────────────────────────────────────────────────────────────────────────┤
│ ✖ 187.feature 06:26 7 6 1 - - │
├────────────────────────────────────────────────────────────────────────────────────────────────┤
│ ✖ 194.feature 03:29 1 - 1 - - │
├────────────────────────────────────────────────────────────────────────────────────────────────┤
│ ✔ 199.feature 00:34 1 1 - - - │
├────────────────────────────────────────────────────────────────────────────────────────────────┤
│ ✔ 200.feature 00:34 1 1 - - - │
├────────────────────────────────────────────────────────────────────────────────────────────────┤
│ ✔ 201.feature 00:30 1 1 - - - │
├────────────────────────────────────────────────────────────────────────────────────────────────┤
│ ✔ 203.feature 01:00 1 1 - - - │
├────────────────────────────────────────────────────────────────────────────────────────────────┤
│ ✔ 205.feature 01:04 1 1 - - - │
├────────────────────────────────────────────────────────────────────────────────────────────────┤
│ ✖ 227.feature 01:47 1 - 1 - - │
──────────────────────────────────────┘ ✖ 3 of 29 failed (10%) 34:22
40 37 3 - -
This is what I have right now. Every test that finishes it sends a message that it has finished the test :checkered_flag: :checkered_flag:. I only want this to show when all the tests and feature files are done. kind of as last message before programs stop. I also need to include the above report in there . I am not sure how I can access that above report .this is my support/index.js file
after(() => {
let reportedTests;
let url = Cypress.env('SLACK_WEBHOOK_URL');
let time = new Date().toLocaleString();
const findTests = (suite) => {
suite.tests.forEach((test) => {
if (test.state === "failed" && test._retries >= 3) {
reportedTests = {
"attachments": [
{
"color": "#FF0000",
"fields": [
{
"title": "The following test has failed ",
"value": test.title,
"short": true,
},
],
},
],
};
cy.request({
method: "POST",
url: url,
followRedirect: false,
headers: {
"Content-Type": "application/json",
},
body: reportedTests
});
}
});
if (suite.suites.length < 1) {
//I need this to only happen at the end when all the tests are done and give me the above report
reportedTests = {
"text": "====== :checkered_flag: :checkered_flag: ==== " + time,
};
cy.request({
method: "POST",
url: url,
followRedirect: false,
headers: {
"Content-Type": "application/json",
},
body: reportedTests
});
return;
}
suite.suites.forEach((nestedSuite) => {
findTests(nestedSuite);
});
};
cy.state("runnable").parent.suites.forEach((suite) => {
findTests(suite);
});
});
To explain my situation. I have a pm2 cron script that I run using:
pm2 start clear-redis-state-cron.js -n clearState --cron '0 0/1 * 1/1 * *'
This runs the js script called clear-redis-state-cron.js just fine.
The role of this script is to stop process p1 and process p2. It then runs a lua redis script that clears some keys from the database. This all works fine but I've put it here for brevity.
var crs = require('./clear-redis-state'),
pm2 = require('pm2');
pm2.connect(function(err) {
pm2.stop('component1');
pm2.stop('component2');
crs.clear();
pm2.restart(__dirname + '/../../node_modules/component1/index.js', { name: 'c1' }, function (err, proc) {
if (err) throw new Error('err');
});
pm2.restart(__dirname + '/../../node_modules/component2/index.js', { name: 'c2' }, function (err, proc) {
if (err) throw new Error('err');
});
});
It runs a clear() js function which is defined here:
var config = require('common/lib/config'),
log = require('common/lib/logger'),
redis = require('common/lib/redis'),
ScriptTo = require('redis-scripto');
exports.clear = function() {
log.init();
if (!config.isSet()) {
// Use local config
var configPath = require('path').join(__dirname, '../../app/config');
config.load(configPath);
}
redis.init(config.get('redis'));
var scriptManager = new ScriptTo(redis.getClient());
scriptManager.loadFromDir(__dirname + '/scripts');
scriptManager.run('clear-state', [], [], function(err, results) {
logError(err);
console.log('results:', results);
});
function logError(err) {
if (err !== null) {
console.log('Error loading lua script merge-keys: ', err);
}
};
}
I have no problems with that. However, it seems to crash on start. Let's say I already have pm2 running two processes component1 and component2 called p1 and p2 respectively. Why would I get the following error when starting the cron when I run it with --no-daemon?
... clear-redis-state-cron.js had too many unstable restarts (15). Stopped. "errored"
My hunch is that either the process is starting up shutting down incorrectly and is in the wrong state as a result so when it tries to close it it's already closed, but because pm2 assumes something went wrong the cron process is stopped.
Any ideas what I might be doing wrong?
Edit: I tried promisifying my shutdown pm2 logic like so:
pm2.connect(function(err) {
Promise.resolve(ops.stop('component1'))
.then(ops.stop('component2'))
.then(ops.clear)
.then(ops.restart(__dirname + '/../../node_modules/component1/index.js', { name: 'component1' }))
.then(ops.restart(__dirname + '/../../node_modules/component2/index.js', { name: 'component2' }))
.catch(logFailureToStop);
});
var logFailureToStop = function (err) {
console.log('Failed to stop ', err);
};
With following result after stopping processes that are running:
$ pm2 list
┌───────────┬────┬──────┬───────┬─────────┬───────────┬────────┬─────────────┬──────────┐
│ App name │ id │ mode │ PID │ status │ restarted │ uptime │ memory │ watching │
├───────────┼────┼──────┼───────┼─────────┼───────────┼────────┼─────────────┼──────────┤
│ component2│ 0 │ fork │ 0 │ stopped │ 17 │ 0 │ 0 B │ disabled │
│ component1│ 1 │ fork │ 18769 │ online │ 31 │ 19s │ 30.539 MB │ disabled │
Managed to resolve this issue in the end.
The issue was caused because I had an on handler event listening into SIGTERM. Apparently these interfere with PM2 so you need to instead use the gracefulStop/gracefulRestart commands.
See: https://github.com/Unitech/PM2/issues/304